/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 |
/Collections/Specialized/ConcurrentList.cs |
@@ -0,0 +1,242 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.Collections; |
using System.Collections.Generic; |
using System.Threading; |
|
namespace wasSharp.Collections.Specialized |
{ |
public static class ConcurrentListExtensions |
{ |
public static ConcurrentList<T> ToConcurrentList<T>(this IEnumerable<T> enumerable) |
{ |
return new ConcurrentList<T>(enumerable); |
} |
} |
|
public class ConcurrentList<T> : IList<T> |
{ |
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
private readonly List<T> _list = null; |
|
public ConcurrentList(IEnumerable<T> v) |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list = new List<T>(); |
foreach (var i in v) |
{ |
_list.Add(i); |
} |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public ConcurrentList() |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list = new List<T>(); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public int Count |
{ |
get |
{ |
_lock.EnterReadLock(); |
var c = _list.Count; |
_lock.ExitReadLock(); |
return c; |
} |
} |
|
public bool IsReadOnly |
{ |
get |
{ |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).IsReadOnly; |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
} |
|
public T this[int index] |
{ |
get |
{ |
try |
{ |
_lock.EnterReadLock(); |
return _list[index]; |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
|
set |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list[index] = value; |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
} |
|
public int IndexOf(T item) |
{ |
try |
{ |
_lock.EnterReadLock(); |
return _list.IndexOf(item); |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
|
public void Insert(int index, T item) |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list.Insert(index, item); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public void RemoveAt(int index) |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list.RemoveAt(index); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public void Add(T item) |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list.Add(item); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public void Clear() |
{ |
try |
{ |
_lock.EnterWriteLock(); |
_list.Clear(); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public bool Contains(T item) |
{ |
try |
{ |
_lock.EnterReadLock(); |
return _list.Contains(item); |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
|
public void CopyTo(T[] array, int arrayIndex) |
{ |
try |
{ |
_lock.EnterReadLock(); |
_list.CopyTo(array, arrayIndex); |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
|
public bool Remove(T item) |
{ |
try |
{ |
_lock.EnterWriteLock(); |
return _list.Remove(item); |
} |
finally |
{ |
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
} |
} |
|
public IEnumerator<T> GetEnumerator() |
{ |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).GetEnumerator(); |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
|
IEnumerator IEnumerable.GetEnumerator() |
{ |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).GetEnumerator(); |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
} |
} |
} |