wasSharp – Diff between revs 27 and 39

Subversion Repositories:
Rev:
Show entire fileRegard whitespace
Rev 27 Rev 39
Line 5... Line 5...
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
Line 6... Line 6...
6   6  
7 using System.Collections; 7 using System.Collections;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
-   9 using System.Collections.Specialized;
Line 9... Line 10...
9 using System.Collections.Specialized; 10 using System.Threading;
10   11  
11 namespace wasSharp.Collections.Specialized 12 namespace wasSharp.Collections.Specialized
12 { 13 {
Line 18... Line 19...
18 /// </summary> 19 /// </summary>
19 /// <typeparam name="K">the key type</typeparam> 20 /// <typeparam name="K">the key type</typeparam>
20 /// <typeparam name="V">the value type</typeparam> 21 /// <typeparam name="V">the value type</typeparam>
21 public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged 22 public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged
22 { 23 {
-   24 private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
23 private readonly Dictionary<K, V> store = new Dictionary<K, V>(); 25 private readonly Dictionary<K, V> store = new Dictionary<K, V>();
Line 24... Line 26...
24   26  
Line 25... Line 27...
25 public bool IsVirgin { get; private set; } = true; 27 public bool IsVirgin { get; private set; } = true;
26   28  
-   29 public V this[K key]
-   30 {
-   31 get
27 public V this[K key] 32 {
-   33 SyncRoot.EnterReadLock();
-   34 var v = store[key];
-   35 SyncRoot.ExitReadLock();
Line -... Line 36...
-   36 return v;
-   37 }
-   38  
28 { 39 set
-   40 {
-   41 SyncRoot.EnterWriteLock();
29 get { return store[key]; } 42 store[key] = value;
Line 30... Line 43...
30   43 SyncRoot.ExitWriteLock();
Line 31... Line 44...
31 set { store[key] = value; } 44 }
Line 32... Line 45...
32 } 45 }
-   46  
-   47 public int Count => store.Count;
-   48  
-   49 public bool IsReadOnly => false;
-   50  
-   51 public ICollection<K> Keys
-   52 {
-   53 get
-   54 {
Line 33... Line 55...
33   55 SyncRoot.EnterReadLock();
-   56 var v = store.Keys;
-   57 SyncRoot.ExitReadLock();
-   58 return v;
-   59 }
-   60 }
-   61  
-   62 public ICollection<V> Values
-   63 {
-   64 get
Line 34... Line 65...
34 public int Count => store.Count; 65 {
35   66 SyncRoot.EnterReadLock();
-   67 var v = store.Values;
36 public bool IsReadOnly => false; 68 SyncRoot.ExitReadLock();
-   69 return v;
37   70 }
38 public ICollection<K> Keys => store.Keys; 71 }
39   72  
Line 40... Line 73...
40 public ICollection<V> Values => store.Values; 73 public void Add(KeyValuePair<K, V> item)
41   74 {
-   75 SyncRoot.EnterWriteLock();
42 public void Add(KeyValuePair<K, V> item) 76 ((IDictionary<K, V>)store).Add(item);
-   77 SyncRoot.ExitWriteLock();
43 { 78 IsVirgin = false;
44 ((IDictionary<K, V>)store).Add(item); 79 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
45 IsVirgin = false; 80 }
46 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); 81  
Line 47... Line 82...
47 } 82 public void Add(K key, V value)
48   83 {
-   84 SyncRoot.EnterWriteLock();
49 public void Add(K key, V value) 85 store.Add(key, value);
-   86 SyncRoot.ExitWriteLock();
50 { 87 IsVirgin = false;
51 store.Add(key, value); 88 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
52 IsVirgin = false; 89 new KeyValuePair<K, V>(key, value)));
53 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 90 }
Line 54... Line 91...
54 new KeyValuePair<K, V>(key, value))); 91  
55 } 92 public void Clear()
-   93 {
56   94 SyncRoot.EnterWriteLock();
-   95 store.Clear();
-   96 SyncRoot.ExitWriteLock();
57 public void Clear() 97 if (!IsVirgin)
Line 58... Line 98...
58 { 98 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
59 store.Clear(); 99 IsVirgin = false;
-   100 }
60 if (!IsVirgin) 101  
-   102 public bool Contains(KeyValuePair<K, V> item)
-   103 {
61 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 104 SyncRoot.EnterReadLock();
Line 62... Line 105...
62 IsVirgin = false; 105 var c = ((IDictionary<K, V>)store).Contains(item);
63 } 106 SyncRoot.ExitReadLock();
-   107 return c;
64   108 }
-   109  
65 public bool Contains(KeyValuePair<K, V> item) 110 public bool ContainsKey(K key)
Line 66... Line 111...
66 { 111 {
67 return ((IDictionary<K, V>)store).Contains(item); 112 SyncRoot.EnterReadLock();
-   113 var c = store.ContainsKey(key);
68 } 114 SyncRoot.ExitReadLock();
-   115 return c;
-   116 }
69   117  
Line 70... Line 118...
70 public bool ContainsKey(K key) 118 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
71 { 119 {
-   120 SyncRoot.EnterReadLock();
72 return store.ContainsKey(key); 121 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex);
-   122 SyncRoot.ExitReadLock();
73 } 123 }
74   124  
75 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) 125 public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
76 { 126 {
77 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex); 127 SyncRoot.EnterReadLock();
Line 78... Line 128...
78 } 128 var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
79   129 SyncRoot.ExitReadLock();
80 public IEnumerator<KeyValuePair<K, V>> GetEnumerator() 130 return enumerator;
-   131 }
81 { 132  
82 return ((IDictionary<K, V>)store).GetEnumerator(); 133 public bool Remove(KeyValuePair<K, V> item)
Line 83... Line 134...
83 } 134 {
-   135 SyncRoot.EnterWriteLock();
84   136 var removed = ((IDictionary<K, V>)store).Remove(item);
85 public bool Remove(KeyValuePair<K, V> item) 137 SyncRoot.ExitWriteLock();
86 { 138 IsVirgin = false;
87 var removed = ((IDictionary<K, V>)store).Remove(item); 139 if (removed)
88 IsVirgin = false; 140 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
Line 89... Line 141...
89 if (removed) 141 return removed;
90 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 142 }
-   143  
91 return removed; 144 public bool Remove(K key)
-   145 {
-   146 KeyValuePair<K, V> item;
92 } 147 SyncRoot.EnterWriteLock();
Line 93... Line 148...
93   148 if (store.ContainsKey(key))
94 public bool Remove(K key) 149 item = new KeyValuePair<K, V>(key, store[key]);
-   150  
95 { 151 var removed = store.Remove(key);
-   152 SyncRoot.ExitWriteLock();
-   153 IsVirgin = false;
96 KeyValuePair<K, V> item; 154 if (removed)
Line 97... Line 155...
97 if (store.ContainsKey(key)) 155 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
Line 98... Line 156...
98 item = new KeyValuePair<K, V>(key, store[key]); 156 return removed;