wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 38  →  ?path2? @ 39
/Collections/Generic/SerializableDictionary.cs
@@ -72,41 +72,12 @@
}
}
 
/// <summary>
/// Deep-clones the serializable dictionary.
/// </summary>
/// <returns>a deep clone of the original dictionary</returns>
public SerializableDictionary<TKey, TValue> Clone()
{
SerializableDictionary<TKey, TValue> clone;
try
{
using (var memoryStream = new MemoryStream())
{
new XmlSerializer(
typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
.Serialize(memoryStream, this);
memoryStream.Position = 0;
clone = (SerializableDictionary<TKey, TValue>)
new XmlSerializer(
typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName))
.Deserialize(memoryStream);
}
}
/* cloning failed so return an empty dictionary */
catch (Exception)
{
clone = new SerializableDictionary<TKey, TValue>();
}
return clone;
}
 
#region Constants
 
public string DictionaryNodeName { get; set; } = "Dictionary";
public string ItemNodeName { get; set; } = "Item";
public string KeyNodeName { get; set; } = "Key";
public string ValueNodeName { get; set; } = "Value";
public readonly string DictionaryNodeName = @"Dictionary";
public readonly string ItemNodeName = @"Item";
public readonly string KeyNodeName = @"Key";
public readonly string ValueNodeName = @"Value";
 
#endregion Constants
 
File deleted
/Collections/Specialized/ConcurrentLazyList.cs
/Collections/Specialized/MultiKeyDictionary.cs
@@ -5,61 +5,94 @@
///////////////////////////////////////////////////////////////////////////
// Based on the work of Herman Schoenfeld
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
 
namespace wasSharp.Collections.Specialized
{
public class MultiKeyDictionary<K1, K2, V> : Dictionary<K1, Dictionary<K2, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2]
{
get
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
throw new ArgumentOutOfRangeException();
return base[key1][key2];
{
SyncRoot.ExitReadLock();
return default(V);
}
var v = base[key1][key2];
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
 
this[key1][key2] = value;
SyncRoot.ExitWriteLock();
}
}
 
public new IEnumerable<V> Values =>
base.Values.SelectMany(baseDict => baseDict.Keys, (baseDict, baseKey) => baseDict[baseKey]);
public new IEnumerable<V> Values
{
get
{
SyncRoot.EnterReadLock();
var v = base.Values.SelectMany(baseDict => baseDict.Keys, (baseDict, baseKey) => baseDict[baseKey]);
SyncRoot.ExitReadLock();
return v;
}
}
 
public void Add(K1 key1, K2 key2, V value)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
 
this[key1][key2] = value;
SyncRoot.ExitWriteLock();
}
 
public void Remove(K1 key1, K2 key2)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
{
SyncRoot.ExitWriteLock();
return;
}
this[key1].Remove(key2);
Remove(key1);
SyncRoot.ExitWriteLock();
}
 
public bool ContainsKey(K1 key1, K2 key2)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2);
SyncRoot.ExitReadLock();
return c;
}
 
public bool TryGetValue(K1 key1, K2 key2, out V value)
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
{
SyncRoot.ExitReadLock();
value = default(V);
return false;
}
value = base[key1][key2];
SyncRoot.ExitReadLock();
return true;
}
}
@@ -66,46 +99,69 @@
 
public class MultiKeyDictionary<K1, K2, K3, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3]
{
get { return ContainsKey(key1) ? this[key1][key2, key3] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
SyncRoot.ExitReadLock();
return c;
}
 
public void Add(K1 key1, K2 key2, K3 key3, V value)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
SyncRoot.ExitWriteLock();
}
 
public void Remove(K1 key1, K2 key2, K3 key3)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2, key3))
{
SyncRoot.ExitWriteLock();
return;
}
this[key1][key2].Remove(key3);
this[key1].Remove(key2);
Remove(key1);
SyncRoot.ExitWriteLock();
}
 
public bool TryGetValue(K1 key1, K2 key2, K3 key3, out V value)
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2, key3))
{
SyncRoot.ExitReadLock();
value = default(V);
return false;
}
value = base[key1][key2, key3];
SyncRoot.ExitReadLock();
return true;
}
}
@@ -112,39 +168,65 @@
 
public class MultiKeyDictionary<K1, K2, K3, K4, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, V>();
this[key1][key2, key3, key4] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
SyncRoot.ExitReadLock();
return c;
}
}
 
public class MultiKeyDictionary<K1, K2, K3, K4, K5, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, V>();
this[key1][key2, key3, key4, key5] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -151,20 +233,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, V>();
this[key1][key2, key3, key4, key5, key6] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -171,20 +266,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>();
this[key1][key2, key3, key4, key5, key6, key7] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -191,20 +299,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -211,20 +332,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -231,17 +365,24 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10]
{
get
{
return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V);
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] = value;
SyncRoot.ExitWriteLock();
}
}
 
@@ -248,8 +389,11 @@
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9,
K10 key10)
{
return ContainsKey(key1) &&
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -256,20 +400,27 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[
K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10, K11 key11]
{
get
{
return ContainsKey(key1)
SyncRoot.EnterReadLock();
var v = ContainsKey(key1)
? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11]
: default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11] = value;
SyncRoot.ExitWriteLock();
}
}
 
@@ -276,8 +427,11 @@
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9,
K10 key10, K11 key11)
{
return ContainsKey(key1) &&
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10, key11);
SyncRoot.ExitReadLock();
return c;
}
}
}
/Collections/Specialized/ObservableDictionary.cs
@@ -7,6 +7,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
 
namespace wasSharp.Collections.Specialized
{
@@ -20,6 +21,7 @@
/// <typeparam name="V">the value type</typeparam>
public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly Dictionary<K, V> store = new Dictionary<K, V>();
 
public bool IsVirgin { get; private set; } = true;
@@ -26,9 +28,20 @@
 
public V this[K key]
{
get { return store[key]; }
get
{
SyncRoot.EnterReadLock();
var v = store[key];
SyncRoot.ExitReadLock();
return v;
}
 
set { store[key] = value; }
set
{
SyncRoot.EnterWriteLock();
store[key] = value;
SyncRoot.ExitWriteLock();
}
}
 
public int Count => store.Count;
@@ -35,13 +48,33 @@
 
public bool IsReadOnly => false;
 
public ICollection<K> Keys => store.Keys;
public ICollection<K> Keys
{
get
{
SyncRoot.EnterReadLock();
var v = store.Keys;
SyncRoot.ExitReadLock();
return v;
}
}
 
public ICollection<V> Values => store.Values;
public ICollection<V> Values
{
get
{
SyncRoot.EnterReadLock();
var v = store.Values;
SyncRoot.ExitReadLock();
return v;
}
}
 
public void Add(KeyValuePair<K, V> item)
{
SyncRoot.EnterWriteLock();
((IDictionary<K, V>)store).Add(item);
SyncRoot.ExitWriteLock();
IsVirgin = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
@@ -48,7 +81,9 @@
 
public void Add(K key, V value)
{
SyncRoot.EnterWriteLock();
store.Add(key, value);
SyncRoot.ExitWriteLock();
IsVirgin = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
new KeyValuePair<K, V>(key, value)));
@@ -56,7 +91,9 @@
 
public void Clear()
{
SyncRoot.EnterWriteLock();
store.Clear();
SyncRoot.ExitWriteLock();
if (!IsVirgin)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
IsVirgin = false;
@@ -64,27 +101,40 @@
 
public bool Contains(KeyValuePair<K, V> item)
{
return ((IDictionary<K, V>)store).Contains(item);
SyncRoot.EnterReadLock();
var c = ((IDictionary<K, V>)store).Contains(item);
SyncRoot.ExitReadLock();
return c;
}
 
public bool ContainsKey(K key)
{
return store.ContainsKey(key);
SyncRoot.EnterReadLock();
var c = store.ContainsKey(key);
SyncRoot.ExitReadLock();
return c;
}
 
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
{
SyncRoot.EnterReadLock();
((IDictionary<K, V>)store).CopyTo(array, arrayIndex);
SyncRoot.ExitReadLock();
}
 
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return ((IDictionary<K, V>)store).GetEnumerator();
SyncRoot.EnterReadLock();
var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
SyncRoot.ExitReadLock();
return enumerator;
}
 
public bool Remove(KeyValuePair<K, V> item)
{
SyncRoot.EnterWriteLock();
var removed = ((IDictionary<K, V>)store).Remove(item);
SyncRoot.ExitWriteLock();
IsVirgin = false;
if (removed)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
@@ -94,10 +144,12 @@
public bool Remove(K key)
{
KeyValuePair<K, V> item;
SyncRoot.EnterWriteLock();
if (store.ContainsKey(key))
item = new KeyValuePair<K, V>(key, store[key]);
 
var removed = store.Remove(key);
SyncRoot.ExitWriteLock();
IsVirgin = false;
if (removed)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
@@ -106,12 +158,18 @@
 
public bool TryGetValue(K key, out V value)
{
return store.TryGetValue(key, out value);
SyncRoot.EnterReadLock();
var c = store.TryGetValue(key, out value);
SyncRoot.ExitReadLock();
return c;
}
 
IEnumerator IEnumerable.GetEnumerator()
{
return ((IDictionary<K, V>)store).GetEnumerator();
SyncRoot.EnterReadLock();
var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
SyncRoot.ExitReadLock();
return enumerator;
}
 
public event NotifyCollectionChangedEventHandler CollectionChanged;
/Collections/Specialized/ObservableHashSet.cs
@@ -5,6 +5,7 @@
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
@@ -21,11 +22,14 @@
/// <typeparam name="T">the object type</typeparam>
public class ObservableHashSet<T> : ICollection<T>, INotifyCollectionChanged, IEnumerable<T>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly HashSet<T> store = new HashSet<T>();
 
public ObservableHashSet(HashSet<T> set)
{
SyncRoot.EnterWriteLock();
UnionWith(set);
SyncRoot.ExitWriteLock();
}
 
public ObservableHashSet()
@@ -34,17 +38,23 @@
 
public ObservableHashSet(T item)
{
SyncRoot.EnterWriteLock();
Add(item);
SyncRoot.ExitWriteLock();
}
 
public ObservableHashSet(ObservableHashSet<T> other)
{
SyncRoot.EnterWriteLock();
UnionWith(other);
SyncRoot.ExitWriteLock();
}
 
public ObservableHashSet(IEnumerable<T> list)
{
SyncRoot.EnterWriteLock();
UnionWith(list);
SyncRoot.ExitWriteLock();
}
 
public bool IsVirgin { get; private set; } = true;
@@ -51,17 +61,25 @@
 
public IEnumerator<T> GetEnumerator()
{
return store.GetEnumerator();
SyncRoot.EnterReadLock();
var enumerator = store.GetEnumerator();
SyncRoot.ExitReadLock();
return enumerator;
}
 
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
SyncRoot.EnterReadLock();
var enumerator = GetEnumerator();
SyncRoot.ExitReadLock();
return enumerator;
}
 
public void Add(T item)
{
SyncRoot.EnterWriteLock();
store.Add(item);
SyncRoot.ExitWriteLock();
IsVirgin = false;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
@@ -68,7 +86,9 @@
 
public void Clear()
{
SyncRoot.EnterWriteLock();
store.Clear();
SyncRoot.ExitWriteLock();
if (!IsVirgin)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
IsVirgin = false;
@@ -76,17 +96,24 @@
 
public bool Contains(T item)
{
return store.Contains(item);
SyncRoot.EnterReadLock();
var contains = store.Contains(item);
SyncRoot.ExitReadLock();
return contains;
}
 
public void CopyTo(T[] array, int arrayIndex)
{
SyncRoot.EnterReadLock();
store.CopyTo(array, arrayIndex);
SyncRoot.ExitReadLock();
}
 
public bool Remove(T item)
{
SyncRoot.EnterWriteLock();
var removed = store.Remove(item);
SyncRoot.ExitWriteLock();
IsVirgin = false;
if (removed)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
@@ -102,7 +129,9 @@
public void UnionWith(IEnumerable<T> list)
{
var added = new List<T>(list.Except(store));
SyncRoot.EnterWriteLock();
store.UnionWith(added);
SyncRoot.ExitWriteLock();
if (!IsVirgin && added.Any())
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added));
IsVirgin = false;
@@ -116,7 +145,9 @@
public void ExceptWith(IEnumerable<T> list)
{
var removed = new List<T>(list.Intersect(store));
SyncRoot.EnterWriteLock();
store.ExceptWith(removed);
SyncRoot.ExitWriteLock();
if (!IsVirgin && removed.Any())
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
removed));
@@ -126,7 +157,9 @@
public void RemoveWhere(Func<T, bool> func)
{
var removed = new List<T>(store.Where(func));
SyncRoot.EnterWriteLock();
store.ExceptWith(removed);
SyncRoot.ExitWriteLock();
if (!IsVirgin && removed.Any())
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
removed));
@@ -135,7 +168,10 @@
 
public IEnumerable<T> AsEnumerable()
{
return store.AsEnumerable();
SyncRoot.EnterWriteLock();
var enumerable = store.AsEnumerable();
SyncRoot.ExitWriteLock();
return enumerable;
}
}
}
/Collections/Utilities/CollectionExtensions.cs
@@ -98,10 +98,5 @@
}
return dict;
}
 
public static IList<T> ToLazyList<T>(this IEnumerable<T> list)
{
return new ConcurrentLazyList<T>(list);
}
}
}