wasSharp – Diff between revs 27 and 39

Subversion Repositories:
Rev:
Show entire fileIgnore 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();
28 { 36 return v;
-   37 }
-   38  
-   39 set
29 get { return store[key]; } 40 {
-   41 SyncRoot.EnterWriteLock();
-   42 store[key] = value;
30   43 SyncRoot.ExitWriteLock();
Line 31... Line 44...
31 set { store[key] = value; } 44 }
Line 32... Line 45...
32 } 45 }
Line 33... Line 46...
33   46  
-   47 public int Count => store.Count;
-   48  
-   49 public bool IsReadOnly => false;
-   50  
-   51 public ICollection<K> Keys
-   52 {
-   53 get
-   54 {
-   55 SyncRoot.EnterReadLock();
34 public int Count => store.Count; 56 var v = store.Keys;
35   57 SyncRoot.ExitReadLock();
-   58 return v;
-   59 }
-   60 }
-   61  
-   62 public ICollection<V> Values
-   63 {
-   64 get
-   65 {
-   66 SyncRoot.EnterReadLock();
Line 36... Line 67...
36 public bool IsReadOnly => false; 67 var v = store.Values;
37   68 SyncRoot.ExitReadLock();
-   69 return v;
38 public ICollection<K> Keys => store.Keys; 70 }
-   71 }
39   72  
40 public ICollection<V> Values => store.Values; 73 public void Add(KeyValuePair<K, V> item)
41   74 {
Line 42... Line 75...
42 public void Add(KeyValuePair<K, V> item) 75 SyncRoot.EnterWriteLock();
43 { 76 ((IDictionary<K, V>)store).Add(item);
-   77 SyncRoot.ExitWriteLock();
44 ((IDictionary<K, V>)store).Add(item); 78 IsVirgin = false;
-   79 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
45 IsVirgin = false; 80 }
46 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); 81  
47 } 82 public void Add(K key, V value)
48   83 {
Line 49... Line 84...
49 public void Add(K key, V value) 84 SyncRoot.EnterWriteLock();
50 { 85 store.Add(key, value);
-   86 SyncRoot.ExitWriteLock();
51 store.Add(key, value); 87 IsVirgin = false;
-   88 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
52 IsVirgin = false; 89 new KeyValuePair<K, V>(key, value)));
53 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 90 }
54 new KeyValuePair<K, V>(key, value))); 91  
55 } 92 public void Clear()
Line 56... Line 93...
56   93 {
57 public void Clear() 94 SyncRoot.EnterWriteLock();
-   95 store.Clear();
58 { 96 SyncRoot.ExitWriteLock();
-   97 if (!IsVirgin)
-   98 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
59 store.Clear(); 99 IsVirgin = false;
Line 60... Line 100...
60 if (!IsVirgin) 100 }
61 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 101  
-   102 public bool Contains(KeyValuePair<K, V> item)
62 IsVirgin = false; 103 {
-   104 SyncRoot.EnterReadLock();
-   105 var c = ((IDictionary<K, V>)store).Contains(item);
63 } 106 SyncRoot.ExitReadLock();
Line 64... Line 107...
64   107 return c;
65 public bool Contains(KeyValuePair<K, V> item) 108 }
-   109  
66 { 110 public bool ContainsKey(K key)
-   111 {
67 return ((IDictionary<K, V>)store).Contains(item); 112 SyncRoot.EnterReadLock();
Line 68... Line 113...
68 } 113 var c = store.ContainsKey(key);
69   114 SyncRoot.ExitReadLock();
-   115 return c;
70 public bool ContainsKey(K key) 116 }
-   117  
-   118 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
71 { 119 {
Line 72... Line 120...
72 return store.ContainsKey(key); 120 SyncRoot.EnterReadLock();
73 } 121 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex);
-   122 SyncRoot.ExitReadLock();
74   123 }
-   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();
78 } 128 var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
79   129 SyncRoot.ExitReadLock();
Line 80... Line 130...
80 public IEnumerator<KeyValuePair<K, V>> GetEnumerator() 130 return enumerator;
81 { 131 }
82 return ((IDictionary<K, V>)store).GetEnumerator(); 132  
-   133 public bool Remove(KeyValuePair<K, V> item)
83 } 134 {
84   135 SyncRoot.EnterWriteLock();
Line 85... Line 136...
85 public bool Remove(KeyValuePair<K, V> item) 136 var removed = ((IDictionary<K, V>)store).Remove(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));
89 if (removed) 141 return removed;
90 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 142 }
Line 91... Line 143...
91 return removed; 143  
92 } 144 public bool Remove(K key)
-   145 {
93   146 KeyValuePair<K, V> item;
-   147 SyncRoot.EnterWriteLock();
-   148 if (store.ContainsKey(key))
94 public bool Remove(K key) 149 item = new KeyValuePair<K, V>(key, store[key]);
Line 95... Line 150...
95 { 150  
96 KeyValuePair<K, V> item; 151 var removed = store.Remove(key);
-   152 SyncRoot.ExitWriteLock();
97 if (store.ContainsKey(key)) 153 IsVirgin = false;
-   154 if (removed)
-   155 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
98 item = new KeyValuePair<K, V>(key, store[key]); 156 return removed;
Line 99... Line 157...
99   157 }
Line 100... Line 158...
100 var removed = store.Remove(key); 158