-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReadOnlySet.cs
199 lines (164 loc) · 4.85 KB
/
ReadOnlySet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
#endregion
/// <summary>
/// Provides a generic read-only set.
/// </summary>
/// <typeparam name="T">
/// The type of items in the set.
/// </typeparam>
/// <remarks>
/// An instance of the ReadOnlySet generic class is always read-only.
/// A set that is read-only is simply a set with a wrapper that prevents
/// modifying the set; therefore, if changes are made to the underlying
/// set, the read-only dictionary reflects those changes. See
/// <see cref="HashSet{T}"/> or <see cref="SortedSet{T}"/>
/// for a modifiable version of this class.
/// </remarks>
[Serializable]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(ReadOnlySetDebugView<>))]
[SuppressMessage(
"Microsoft.Naming",
"CA1710:IdentifiersShouldHaveCorrectSuffix",
Justification = "This must end with 'Set' instead of 'Collection' because it implements the standard ISet<T> interface.")]
internal sealed class ReadOnlySet<T> : ISet<T>
{
#region Private Data Members
private readonly ISet<T> source;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance that wraps the supplied <paramref name="source"/>.
/// </summary>
/// <param name="source">The set to wrap.</param>
public ReadOnlySet(ISet<T> source)
{
Conditions.RequireReference(source, nameof(source));
this.source = source;
}
#endregion
#region Public Properties
/// <summary>
/// Gets the number of items in the set.
/// </summary>
public int Count => this.source.Count;
/// <summary>
/// Gets whether this set is read-only, which will always return true.
/// </summary>
public bool IsReadOnly => true;
#endregion
#region ISet<T> Members
bool ISet<T>.Add(T item)
{
throw NewNotSupportedException();
}
void ISet<T>.ExceptWith(IEnumerable<T> other)
{
throw NewNotSupportedException();
}
void ISet<T>.IntersectWith(IEnumerable<T> other)
{
throw NewNotSupportedException();
}
/// <summary>
/// Determines whether the current set is a proper (strict) subset of a specified collection.
/// </summary>
public bool IsProperSubsetOf(IEnumerable<T> other)
{
bool result = this.source.IsProperSubsetOf(other);
return result;
}
/// <summary>
/// Determines whether the current set is a proper (strict) superset of a specified collection.
/// </summary>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
bool result = this.source.IsProperSupersetOf(other);
return result;
}
/// <summary>
/// Determines whether a set is a subset of a specified collection.
/// </summary>
public bool IsSubsetOf(IEnumerable<T> other)
{
bool result = this.source.IsSubsetOf(other);
return result;
}
/// <summary>
/// Determines whether the current set is a superset of a specified collection.
/// </summary>
public bool IsSupersetOf(IEnumerable<T> other)
{
bool result = this.source.IsSupersetOf(other);
return result;
}
/// <summary>
/// Determines whether the current set overlaps with the specified collection.
/// </summary>
public bool Overlaps(IEnumerable<T> other)
{
bool result = this.source.Overlaps(other);
return result;
}
/// <summary>
/// Determines whether the current set and the specified collection contain the same elements.
/// </summary>
public bool SetEquals(IEnumerable<T> other)
{
bool result = this.source.SetEquals(other);
return result;
}
void ISet<T>.SymmetricExceptWith(IEnumerable<T> other)
{
throw NewNotSupportedException();
}
void ISet<T>.UnionWith(IEnumerable<T> other)
{
throw NewNotSupportedException();
}
void ICollection<T>.Add(T item)
{
throw NewNotSupportedException();
}
void ICollection<T>.Clear()
{
throw NewNotSupportedException();
}
/// <summary>
/// Determines whether the set contains a specific value.
/// </summary>
public bool Contains(T item)
{
bool result = this.source.Contains(item);
return result;
}
/// <summary>
/// Copies the elements of the set to an array, starting at a particular array index.
/// </summary>
public void CopyTo(T[] array, int arrayIndex)
{
this.source.CopyTo(array, arrayIndex);
}
bool ICollection<T>.Remove(T item)
{
throw NewNotSupportedException();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
public IEnumerator<T> GetEnumerator() => this.source.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => this.GetEnumerator();
#endregion
#region Private Methods
private static NotSupportedException NewNotSupportedException() => Exceptions.Log(new NotSupportedException("The set is read-only."));
#endregion
}
}