Common
A library of common classes.
Set.cs
1using System.Collections;
2using System.Collections.Generic;
3using System.Runtime.Serialization;
4
6{
10 [DataContract(Name = "Set", Namespace = Constants.DataContractNamespace)]
11 public class Set<T> : ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, ISet<T>
12 {
16 public HashSet<T> HashSet { get; private set; } = new HashSet<T>();
17
21 [DataMember(Order = 1, Name = "Values")]
22 public T[] Array { get => GetArray(); set => HashSet = new HashSet<T>(value); }
23
27 public Set()
28 {
29
30 }
31
36 public Set(IEnumerable<T> collection)
37 {
38 HashSet = new HashSet<T>(collection);
39 }
40
45 public static implicit operator Set<T>(HashSet<T> set)
46 {
47 return set == null ? null : new Set<T> { HashSet = set };
48 }
49
53 private T[] GetArray()
54 {
55 if (HashSet.Count == 0)
56 return System.Array.Empty<T>();
57
58 var array = new T[HashSet.Count];
59 CopyTo(array, 0);
60 return array;
61 }
62
64 public int Count => HashSet.Count;
65
67 public bool IsReadOnly => ((ICollection<T>)HashSet).IsReadOnly;
68
70 void ICollection<T>.Add(T item)
71 {
72 ((ICollection<T>)HashSet).Add(item);
73 }
74
76 public void Clear()
77 {
78 HashSet.Clear();
79 }
80
82 public bool Contains(T item)
83 {
84 return HashSet.Contains(item);
85 }
86
88 public void CopyTo(T[] array, int arrayIndex)
89 {
90 HashSet.CopyTo(array, arrayIndex);
91 }
92
94 public void ExceptWith(IEnumerable<T> other)
95 {
96 HashSet.ExceptWith(other);
97 }
98
100 public HashSet<T>.Enumerator GetEnumerator()
101 {
102 return HashSet.GetEnumerator();
103 }
104
106 IEnumerator<T> IEnumerable<T>.GetEnumerator()
107 {
108 return HashSet.GetEnumerator();
109 }
110
112 public void IntersectWith(IEnumerable<T> other)
113 {
114 HashSet.IntersectWith(other);
115 }
116
118 public bool IsProperSubsetOf(IEnumerable<T> other)
119 {
120 return HashSet.IsProperSubsetOf(other);
121 }
122
124 public bool IsProperSupersetOf(IEnumerable<T> other)
125 {
126 return HashSet.IsProperSupersetOf(other);
127 }
128
130 public bool IsSubsetOf(IEnumerable<T> other)
131 {
132 return HashSet.IsSubsetOf(other);
133 }
134
136 public bool IsSupersetOf(IEnumerable<T> other)
137 {
138 return HashSet.IsSupersetOf(other);
139 }
140
142 public bool Overlaps(IEnumerable<T> other)
143 {
144 return HashSet.Overlaps(other);
145 }
146
148 public bool Remove(T item)
149 {
150 return HashSet.Remove(item);
151 }
152
154 public bool SetEquals(IEnumerable<T> other)
155 {
156 return HashSet.SetEquals(other);
157 }
158
160 public void SymmetricExceptWith(IEnumerable<T> other)
161 {
162 HashSet.SymmetricExceptWith(other);
163 }
164
166 public void UnionWith(IEnumerable<T> other)
167 {
168 HashSet.UnionWith(other);
169 }
170
172 public bool Add(T item)
173 {
174 return HashSet.Add(item);
175 }
176
178 IEnumerator IEnumerable.GetEnumerator()
179 {
180 return ((IEnumerable)HashSet).GetEnumerator();
181 }
182 }
183}
A hash set that is data contract serializable.
Definition: Set.cs:12
bool SetEquals(IEnumerable< T > other)
Definition: Set.cs:154
void UnionWith(IEnumerable< T > other)
Definition: Set.cs:166
void IntersectWith(IEnumerable< T > other)
Definition: Set.cs:112
bool IsSupersetOf(IEnumerable< T > other)
Definition: Set.cs:136
Set(IEnumerable< T > collection)
Initializes a new hash set from a collection.
Definition: Set.cs:36
bool IsProperSubsetOf(IEnumerable< T > other)
Definition: Set.cs:118
bool IsSubsetOf(IEnumerable< T > other)
Definition: Set.cs:130
T[] GetArray()
Returns a new array of hash set entries.
Definition: Set.cs:53
HashSet< T > HashSet
The underlying hash set.
Definition: Set.cs:16
HashSet< T >.Enumerator GetEnumerator()
Definition: Set.cs:100
bool Overlaps(IEnumerable< T > other)
Definition: Set.cs:142
T[] Array
An array of hash set entries.
Definition: Set.cs:22
bool Contains(T item)
Definition: Set.cs:82
void CopyTo(T[] array, int arrayIndex)
Definition: Set.cs:88
void SymmetricExceptWith(IEnumerable< T > other)
Definition: Set.cs:160
Set()
Initializes a new hash set.
Definition: Set.cs:27
bool IsProperSupersetOf(IEnumerable< T > other)
Definition: Set.cs:124
void ExceptWith(IEnumerable< T > other)
Definition: Set.cs:94