|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Runtime.Serialization; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +namespace NumSharp.Utilities |
| 9 | +{ |
| 10 | + [DebuggerDisplay("Count = {Count}")] |
| 11 | + [Serializable] |
| 12 | + public class ConcurrentHashSet<T> : ICollection<T>, ISet<T>, ISerializable, IDeserializationCallback |
| 13 | + { |
| 14 | + private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
| 15 | + |
| 16 | + private readonly Hashset<T> hashset = new Hashset<T>(); |
| 17 | + |
| 18 | + public ConcurrentHashSet() |
| 19 | + { } |
| 20 | + |
| 21 | + public ConcurrentHashSet(IEqualityComparer<T> comparer) |
| 22 | + { |
| 23 | + hashset = new Hashset<T>(comparer); |
| 24 | + } |
| 25 | + |
| 26 | + public ConcurrentHashSet(IEnumerable<T> collection) |
| 27 | + { |
| 28 | + hashset = new Hashset<T>(collection); |
| 29 | + } |
| 30 | + |
| 31 | + public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer) |
| 32 | + { |
| 33 | + hashset = new Hashset<T>(collection, comparer); |
| 34 | + } |
| 35 | + |
| 36 | + protected ConcurrentHashSet(SerializationInfo info, StreamingContext context) |
| 37 | + { |
| 38 | + hashset = new Hashset<T>(); |
| 39 | + |
| 40 | + // not sure about this one really... |
| 41 | + var iSerializable = hashset as ISerializable; |
| 42 | + iSerializable.GetObjectData(info, context); |
| 43 | + } |
| 44 | + |
| 45 | + #region Dispose |
| 46 | + |
| 47 | + public void Dispose() |
| 48 | + { |
| 49 | + Dispose(true); |
| 50 | + GC.SuppressFinalize(this); |
| 51 | + } |
| 52 | + |
| 53 | + protected virtual void Dispose(bool disposing) |
| 54 | + { |
| 55 | + if (disposing) |
| 56 | + if (_lock != null) |
| 57 | + _lock.Dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + public IEnumerator<T> GetEnumerator() |
| 61 | + { |
| 62 | + return hashset.GetEnumerator(); |
| 63 | + } |
| 64 | + |
| 65 | + ~ConcurrentHashSet() |
| 66 | + { |
| 67 | + Dispose(false); |
| 68 | + } |
| 69 | + |
| 70 | + public void OnDeserialization(object sender) |
| 71 | + { |
| 72 | + hashset.OnDeserialization(sender); |
| 73 | + } |
| 74 | + |
| 75 | + public void GetObjectData(SerializationInfo info, StreamingContext context) |
| 76 | + { |
| 77 | + hashset.GetObjectData(info, context); |
| 78 | + } |
| 79 | + |
| 80 | + IEnumerator IEnumerable.GetEnumerator() |
| 81 | + { |
| 82 | + return GetEnumerator(); |
| 83 | + } |
| 84 | + |
| 85 | + #endregion |
| 86 | + |
| 87 | + public bool Add(T item) |
| 88 | + { |
| 89 | + _lock.EnterWriteLock(); |
| 90 | + try |
| 91 | + { |
| 92 | + return hashset.Add(item); |
| 93 | + } |
| 94 | + finally |
| 95 | + { |
| 96 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + void ICollection<T>.Add(T item) |
| 101 | + { |
| 102 | + _lock.EnterWriteLock(); |
| 103 | + try |
| 104 | + { |
| 105 | + hashset.Add(item); |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void UnionWith(IEnumerable<T> other) |
| 114 | + { |
| 115 | + _lock.EnterWriteLock(); |
| 116 | + _lock.EnterReadLock(); |
| 117 | + try |
| 118 | + { |
| 119 | + hashset.UnionWith(other); |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 124 | + if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void IntersectWith(IEnumerable<T> other) |
| 129 | + { |
| 130 | + _lock.EnterWriteLock(); |
| 131 | + _lock.EnterReadLock(); |
| 132 | + try |
| 133 | + { |
| 134 | + hashset.IntersectWith(other); |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 139 | + if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void ExceptWith(IEnumerable<T> other) |
| 144 | + { |
| 145 | + _lock.EnterWriteLock(); |
| 146 | + _lock.EnterReadLock(); |
| 147 | + try |
| 148 | + { |
| 149 | + hashset.ExceptWith(other); |
| 150 | + } |
| 151 | + finally |
| 152 | + { |
| 153 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 154 | + if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public void SymmetricExceptWith(IEnumerable<T> other) |
| 159 | + { |
| 160 | + _lock.EnterWriteLock(); |
| 161 | + try |
| 162 | + { |
| 163 | + hashset.SymmetricExceptWith(other); |
| 164 | + } |
| 165 | + finally |
| 166 | + { |
| 167 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public bool IsSubsetOf(IEnumerable<T> other) |
| 172 | + { |
| 173 | + _lock.EnterWriteLock(); |
| 174 | + try |
| 175 | + { |
| 176 | + return hashset.IsSubsetOf(other); |
| 177 | + } |
| 178 | + finally |
| 179 | + { |
| 180 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public bool IsSupersetOf(IEnumerable<T> other) |
| 185 | + { |
| 186 | + _lock.EnterWriteLock(); |
| 187 | + try |
| 188 | + { |
| 189 | + return hashset.IsSupersetOf(other); |
| 190 | + } |
| 191 | + finally |
| 192 | + { |
| 193 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public bool IsProperSupersetOf(IEnumerable<T> other) |
| 198 | + { |
| 199 | + _lock.EnterWriteLock(); |
| 200 | + try |
| 201 | + { |
| 202 | + return hashset.IsProperSupersetOf(other); |
| 203 | + } |
| 204 | + finally |
| 205 | + { |
| 206 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public bool IsProperSubsetOf(IEnumerable<T> other) |
| 211 | + { |
| 212 | + _lock.EnterWriteLock(); |
| 213 | + try |
| 214 | + { |
| 215 | + return hashset.IsProperSubsetOf(other); |
| 216 | + } |
| 217 | + finally |
| 218 | + { |
| 219 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + public bool Overlaps(IEnumerable<T> other) |
| 224 | + { |
| 225 | + _lock.EnterWriteLock(); |
| 226 | + try |
| 227 | + { |
| 228 | + return hashset.Overlaps(other); |
| 229 | + } |
| 230 | + finally |
| 231 | + { |
| 232 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + public bool SetEquals(IEnumerable<T> other) |
| 237 | + { |
| 238 | + _lock.EnterWriteLock(); |
| 239 | + try |
| 240 | + { |
| 241 | + return hashset.SetEquals(other); |
| 242 | + } |
| 243 | + finally |
| 244 | + { |
| 245 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + bool ISet<T>.Add(T item) |
| 250 | + { |
| 251 | + _lock.EnterWriteLock(); |
| 252 | + try |
| 253 | + { |
| 254 | + return hashset.Add(item); |
| 255 | + } |
| 256 | + finally |
| 257 | + { |
| 258 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + public void Clear() |
| 263 | + { |
| 264 | + _lock.EnterWriteLock(); |
| 265 | + try |
| 266 | + { |
| 267 | + hashset.Clear(); |
| 268 | + } |
| 269 | + finally |
| 270 | + { |
| 271 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + public bool Contains(T item) |
| 276 | + { |
| 277 | + _lock.EnterWriteLock(); |
| 278 | + try |
| 279 | + { |
| 280 | + return hashset.Contains(item); |
| 281 | + } |
| 282 | + finally |
| 283 | + { |
| 284 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + public void CopyTo(T[] array, int arrayIndex) |
| 289 | + { |
| 290 | + _lock.EnterWriteLock(); |
| 291 | + try |
| 292 | + { |
| 293 | + hashset.CopyTo(array, arrayIndex); |
| 294 | + } |
| 295 | + finally |
| 296 | + { |
| 297 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + public bool Remove(T item) |
| 302 | + { |
| 303 | + _lock.EnterWriteLock(); |
| 304 | + try |
| 305 | + { |
| 306 | + return hashset.Remove(item); |
| 307 | + } |
| 308 | + finally |
| 309 | + { |
| 310 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + public int Count |
| 315 | + { |
| 316 | + get |
| 317 | + { |
| 318 | + _lock.EnterWriteLock(); |
| 319 | + try |
| 320 | + { |
| 321 | + return hashset.Count; |
| 322 | + } |
| 323 | + finally |
| 324 | + { |
| 325 | + if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + public bool IsReadOnly |
| 331 | + { |
| 332 | + get { return false; } |
| 333 | + } |
| 334 | + } |
| 335 | +} |
0 commit comments