wasSharp
/Collections/Specialized/ConcurrentHashSet.cs |
@@ -9,14 +9,67 @@ |
using System.Collections; |
using System.Collections.Generic; |
using System.Threading; |
using System.Linq; |
|
namespace wasSharp.Collections.Specialized |
{ |
public static class ConcurrentHashSetExtensions |
{ |
public static ConcurrentHashSet<T> ToConcurrentHashSet<T>(this IEnumerable<T> enumerable) |
{ |
return new ConcurrentHashSet<T>(enumerable); |
} |
} |
|
public class ConcurrentHashSet<T> : IDisposable, ICollection<T>, IEnumerable<T>, ISet<T> |
{ |
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
private readonly HashSet<T> _hashSet = new HashSet<T>(); |
private readonly HashSet<T> _hashSet = null; |
private IEnumerable<string> enumerable; |
|
public ConcurrentHashSet() |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_hashSet = new HashSet<T>(); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public ConcurrentHashSet(IEqualityComparer<T> stringComparer) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_hashSet = new HashSet<T>(stringComparer); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public ConcurrentHashSet(IEnumerable<T> enumerable) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_hashSet = new HashSet<T>(); |
foreach (var i in enumerable) |
{ |
_hashSet.Add(i); |
} |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
#region Implementation of ICollection<T> |
|
public bool Add(T item) |
@@ -71,6 +124,19 @@ |
} |
} |
|
public int RemoveWhere(Predicate<T> match) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
return _hashSet.RemoveWhere(match); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public int Count |
{ |
get |