wasSharp – Diff between revs 39 and 44

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 39 Rev 44
Line 19... Line 19...
19 /// </summary> 19 /// </summary>
20 /// <typeparam name="K">the key type</typeparam> 20 /// <typeparam name="K">the key type</typeparam>
21 /// <typeparam name="V">the value type</typeparam> 21 /// <typeparam name="V">the value type</typeparam>
22 public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged 22 public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged
23 { 23 {
24 private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); 24 private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
25 private readonly Dictionary<K, V> store = new Dictionary<K, V>(); 25 private readonly Dictionary<K, V> store = new Dictionary<K, V>();
Line 26... Line 26...
26   26  
Line 27... Line 27...
27 public bool IsVirgin { get; private set; } = true; 27 public bool IsVirgin { get; private set; } = true;
28   28  
29 public V this[K key] 29 public V this[K key]
30 { 30 {
31 get 31 get
-   32 {
-   33 _lock.EnterReadLock();
32 { 34 try
-   35 {
-   36 return store[key];
-   37 }
33 SyncRoot.EnterReadLock(); 38 finally
34 var v = store[key]; 39 {
35 SyncRoot.ExitReadLock(); 40 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
Line 36... Line 41...
36 return v; 41 }
37 } 42 }
38   43  
-   44 set
-   45 {
39 set 46 _lock.EnterWriteLock();
-   47 try
-   48 {
-   49 store[key] = value;
40 { 50 }
-   51 finally
41 SyncRoot.EnterWriteLock(); 52 {
42 store[key] = value; 53 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
Line 43... Line 54...
43 SyncRoot.ExitWriteLock(); 54 }
Line 50... Line 61...
50   61  
51 public ICollection<K> Keys 62 public ICollection<K> Keys
52 { 63 {
53 get 64 get
54 { 65 {
-   66 _lock.EnterReadLock();
-   67 try
55 SyncRoot.EnterReadLock(); 68 {
-   69 return store.Keys;
-   70 }
-   71 finally
56 var v = store.Keys; 72 {
57 SyncRoot.ExitReadLock(); 73 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
58 return v; 74 }
59 } 75 }
Line 60... Line 76...
60 } 76 }
61   77  
62 public ICollection<V> Values 78 public ICollection<V> Values
63 { 79 {
64 get 80 get
-   81 {
-   82 _lock.EnterReadLock();
65 { 83 try
-   84 {
-   85 return store.Values;
-   86 }
66 SyncRoot.EnterReadLock(); 87 finally
67 var v = store.Values; 88 {
68 SyncRoot.ExitReadLock(); 89 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
69 return v; 90 }
Line 70... Line 91...
70 } 91 }
71 } 92 }
72   93  
-   94 public void Add(KeyValuePair<K, V> item)
-   95 {
73 public void Add(KeyValuePair<K, V> item) 96 _lock.EnterWriteLock();
-   97 try
-   98 {
-   99 ((IDictionary<K, V>) store).Add(item);
74 { 100 }
-   101 finally
75 SyncRoot.EnterWriteLock(); 102 {
76 ((IDictionary<K, V>)store).Add(item); 103 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
77 SyncRoot.ExitWriteLock(); 104 }
Line 78... Line 105...
78 IsVirgin = false; 105 IsVirgin = false;
79 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); 106 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
80 } 107 }
-   108  
-   109 public void Add(K key, V value)
81   110 {
-   111 _lock.EnterWriteLock();
-   112 try
-   113 {
82 public void Add(K key, V value) 114 store.Add(key, value);
-   115 }
83 { 116 finally
84 SyncRoot.EnterWriteLock(); 117 {
85 store.Add(key, value); 118 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
86 SyncRoot.ExitWriteLock(); 119 }
Line 87... Line 120...
87 IsVirgin = false; 120 IsVirgin = false;
88 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 121 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
89 new KeyValuePair<K, V>(key, value))); 122 new KeyValuePair<K, V>(key, value)));
-   123 }
-   124  
90 } 125 public void Clear()
-   126 {
-   127 _lock.EnterWriteLock();
-   128 try
91   129 {
-   130 store.Clear();
92 public void Clear() 131 }
93 { 132 finally
94 SyncRoot.EnterWriteLock(); 133 {
95 store.Clear(); 134 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
Line 96... Line 135...
96 SyncRoot.ExitWriteLock(); 135 }
97 if (!IsVirgin) 136 if (!IsVirgin)
98 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 137 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
-   138 IsVirgin = false;
-   139 }
99 IsVirgin = false; 140  
-   141 public bool Contains(KeyValuePair<K, V> item)
-   142 {
-   143 _lock.EnterReadLock();
100 } 144 try
101   145 {
102 public bool Contains(KeyValuePair<K, V> item) 146 return ((IDictionary<K, V>) store).Contains(item);
Line 103... Line 147...
103 { 147 }
104 SyncRoot.EnterReadLock(); 148 finally
105 var c = ((IDictionary<K, V>)store).Contains(item); 149 {
-   150 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
-   151 }
106 SyncRoot.ExitReadLock(); 152 }
-   153  
-   154 public bool ContainsKey(K key)
-   155 {
107 return c; 156 _lock.EnterReadLock();
108 } 157 try
109   158 {
Line 110... Line 159...
110 public bool ContainsKey(K key) 159 return store.ContainsKey(key);
111 { 160 }
112 SyncRoot.EnterReadLock(); 161 finally
-   162 {
-   163 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
113 var c = store.ContainsKey(key); 164 }
-   165 }
-   166  
-   167 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
114 SyncRoot.ExitReadLock(); 168 {
-   169 _lock.EnterReadLock();
115 return c; 170 try
Line 116... Line 171...
116 } 171 {
117   172 ((IDictionary<K, V>) store).CopyTo(array, arrayIndex);
118 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) 173 }
-   174 finally
-   175 {
119 { 176 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
-   177 }
120 SyncRoot.EnterReadLock(); 178 }
121 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex); 179  
-   180 public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
-   181 {
-   182 _lock.EnterReadLock();
-   183 try
-   184 {
-   185 using (var enumerator = ((IDictionary<K, V>)store).GetEnumerator())
122 SyncRoot.ExitReadLock(); 186 {
Line 123... Line 187...
123 } 187 while (enumerator.MoveNext())
124   188 yield return enumerator.Current;
125 public IEnumerator<KeyValuePair<K, V>> GetEnumerator() 189 }
-   190 }
-   191 finally
-   192 {
126 { 193 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
-   194 }
-   195 }
-   196  
127 SyncRoot.EnterReadLock(); 197 public bool Remove(KeyValuePair<K, V> item)
-   198 {
128 var enumerator = ((IDictionary<K, V>)store).GetEnumerator(); 199 _lock.EnterWriteLock();
129 SyncRoot.ExitReadLock(); 200 bool removed;
130 return enumerator; 201 try
131 } 202 {
132   203 removed = ((IDictionary<K, V>) store).Remove(item);
Line 133... Line 204...
133 public bool Remove(KeyValuePair<K, V> item) 204 }
134 { 205 finally
135 SyncRoot.EnterWriteLock(); 206 {
136 var removed = ((IDictionary<K, V>)store).Remove(item); 207 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
-   208 }
-   209 IsVirgin = false;
-   210 if (removed)
137 SyncRoot.ExitWriteLock(); 211 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
138 IsVirgin = false; 212 return removed;
Line 139... Line 213...
139 if (removed) 213 }
-   214  
-   215 public bool Remove(K key)
-   216 {
140 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 217 KeyValuePair<K, V> item;
-   218 _lock.EnterWriteLock();
141 return removed; 219 bool removed;
142 } 220 try
143   221 {
144 public bool Remove(K key) 222 if (store.ContainsKey(key))
145 { 223 item = new KeyValuePair<K, V>(key, store[key]);
Line 146... Line 224...
146 KeyValuePair<K, V> item; 224  
147 SyncRoot.EnterWriteLock(); 225 removed = store.Remove(key);
148 if (store.ContainsKey(key)) 226 }
-   227 finally
-   228 {
149 item = new KeyValuePair<K, V>(key, store[key]); 229 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
-   230 }
-   231 IsVirgin = false;
-   232 if (removed)
150   233 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
151 var removed = store.Remove(key); 234 return removed;
152 SyncRoot.ExitWriteLock(); 235 }
Line 153... Line 236...
153 IsVirgin = false; 236  
154 if (removed) 237 public bool TryGetValue(K key, out V value)
155 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 238 {
-   239 _lock.EnterReadLock();
-   240 try
156 return removed; 241 {
-   242 return store.TryGetValue(key, out value);
157 } 243 }
158   244 finally
-   245 {
-   246 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
-   247 }
-   248 }
-   249  
-   250 IEnumerator IEnumerable.GetEnumerator()
159 public bool TryGetValue(K key, out V value) 251 {
Line 160... Line 252...
160 { 252 _lock.EnterReadLock();
Line 161... Line 253...
161 SyncRoot.EnterReadLock(); 253 try