我正在尝试本着 ConcurrentDictionary 的精神实现 ConcurrentHashSet,
采取的方法是使用内部支持 ConcurrentDictionary 并编写小型委托方法,这就是我所取得的进展,但是我坚持使用集合理论方法,尤其是。我不确定是否可以使用 foreach 并且仍然不违反并发性
public class ConcurrentHashSet<TElement> : ISet<TElement>
{
private readonly ConcurrentDictionary<TElement, object> _internal;
public ConcurrentHashSet(IEnumerable<TElement> elements = null)
{
_internal = new ConcurrentDictionary<TElement, object>();
if (elements != null)
UnionWith(elements);
}
public void UnionWith(IEnumerable<TElement> other)
{
if (other == null) throw new ArgumentNullException("other");
foreach (var otherElement in other)
Add(otherElement);
}
public void IntersectWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void ExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public void SymmetricExceptWith(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSupersetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool IsProperSubsetOf(IEnumerable<TElement> other)
{
throw new NotImplementedException();
}
public bool Overlaps(IEnumerable<TElement> other)
{
return other.Any(otherElement => _internal.ContainsKey(otherElement));
}
public bool SetEquals(IEnumerable<TElement> other)
{
int otherCount = 0;
int thisCount = Count;
foreach (var otherElement in other)
{
otherCount++;
if (!_internal.ContainsKey(otherElement))
return false;
}
return otherCount == thisCount;
}
public bool Add(TElement item)
{
return _internal.TryAdd(item, null);
}
public void Clear()
{
_internal.Clear();
}
// I am not sure here if that fullfills contract correctly
void ICollection<TElement>.Add(TElement item)
{
Add(item);
}
public bool Contains(TElement item)
{
return _internal.ContainsKey(item);
}
public void CopyTo(TElement[] array, int arrayIndex)
{
_internal.Keys.CopyTo(array, arrayIndex);
}
public bool Remove(TElement item)
{
object ignore;
return _internal.TryRemove(item, out ignore);
}
public int Count
{
get { return _internal.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerator<TElement> GetEnumerator()
{
return _internal.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
我刚刚遇到了类似的场景(“我对快速添加、包含和删除感兴趣”)并实现了这个傻瓜:
using System.Collections.Generic;
using System.Threading;
namespace BlahBlah.Utilities
{
public class ConcurrentHashSet<T> : IDisposable
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly HashSet<T> _hashSet = new HashSet<T>();
#region Implementation of ICollection<T> ...ish
public bool Add(T item)
{
try
{
_lock.EnterWriteLock();
return _hashSet.Add(item);
}
finally
{
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
}
}
public void Clear()
{
try
{
_lock.EnterWriteLock();
_hashSet.Clear();
}
finally
{
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
}
}
public bool Contains(T item)
{
try
{
_lock.EnterReadLock();
return _hashSet.Contains(item);
}
finally
{
if (_lock.IsReadLockHeld) _lock.ExitReadLock();
}
}
public bool Remove(T item)
{
try
{
_lock.EnterWriteLock();
return _hashSet.Remove(item);
}
finally
{
if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
}
}
public int Count
{
get
{
try
{
_lock.EnterReadLock();
return _hashSet.Count;
}
finally
{
if (_lock.IsReadLockHeld) _lock.ExitReadLock();
}
}
}
#endregion
#region Dispose
public void Dispose()
{
if (_lock != null) _lock.Dispose();
}
#endregion
}
}
还没有真正测试过它(性能或可靠性方面)。 YMMV。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)