wasSharp – Diff between revs 27 and 39

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 27 Rev 39
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
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; 9 using System.Collections.Specialized;
-   10 using System.Threading;
10   11  
11 namespace wasSharp.Collections.Specialized 12 namespace wasSharp.Collections.Specialized
12 { 13 {
13 /////////////////////////////////////////////////////////////////////////// 14 ///////////////////////////////////////////////////////////////////////////
14 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 15 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
15 /////////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////////
16 /// <summary> 17 /// <summary>
17 /// An implementation of an observable Dictionary. 18 /// An implementation of an observable Dictionary.
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>();
24   26  
25 public bool IsVirgin { get; private set; } = true; 27 public bool IsVirgin { get; private set; } = true;
26   28  
27 public V this[K key] 29 public V this[K key]
28 { 30 {
-   31 get
-   32 {
-   33 SyncRoot.EnterReadLock();
29 get { return store[key]; } 34 var v = store[key];
-   35 SyncRoot.ExitReadLock();
-   36 return v;
-   37 }
30   38  
-   39 set
-   40 {
-   41 SyncRoot.EnterWriteLock();
31 set { store[key] = value; } 42 store[key] = value;
-   43 SyncRoot.ExitWriteLock();
-   44 }
32 } 45 }
33   46  
34 public int Count => store.Count; 47 public int Count => store.Count;
35   48  
36 public bool IsReadOnly => false; 49 public bool IsReadOnly => false;
37   50  
-   51 public ICollection<K> Keys
-   52 {
-   53 get
-   54 {
-   55 SyncRoot.EnterReadLock();
-   56 var v = store.Keys;
-   57 SyncRoot.ExitReadLock();
-   58 return v;
-   59 }
38 public ICollection<K> Keys => store.Keys; 60 }
39   61  
-   62 public ICollection<V> Values
-   63 {
-   64 get
-   65 {
-   66 SyncRoot.EnterReadLock();
-   67 var v = store.Values;
-   68 SyncRoot.ExitReadLock();
-   69 return v;
-   70 }
40 public ICollection<V> Values => store.Values; 71 }
41   72  
42 public void Add(KeyValuePair<K, V> item) 73 public void Add(KeyValuePair<K, V> item)
43 { 74 {
-   75 SyncRoot.EnterWriteLock();
44 ((IDictionary<K, V>)store).Add(item); 76 ((IDictionary<K, V>)store).Add(item);
-   77 SyncRoot.ExitWriteLock();
45 IsVirgin = false; 78 IsVirgin = false;
46 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); 79 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
47 } 80 }
48   81  
49 public void Add(K key, V value) 82 public void Add(K key, V value)
50 { 83 {
-   84 SyncRoot.EnterWriteLock();
51 store.Add(key, value); 85 store.Add(key, value);
-   86 SyncRoot.ExitWriteLock();
52 IsVirgin = false; 87 IsVirgin = false;
53 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 88 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
54 new KeyValuePair<K, V>(key, value))); 89 new KeyValuePair<K, V>(key, value)));
55 } 90 }
56   91  
57 public void Clear() 92 public void Clear()
58 { 93 {
-   94 SyncRoot.EnterWriteLock();
59 store.Clear(); 95 store.Clear();
-   96 SyncRoot.ExitWriteLock();
60 if (!IsVirgin) 97 if (!IsVirgin)
61 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 98 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
62 IsVirgin = false; 99 IsVirgin = false;
63 } 100 }
64   101  
65 public bool Contains(KeyValuePair<K, V> item) 102 public bool Contains(KeyValuePair<K, V> item)
66 { 103 {
-   104 SyncRoot.EnterReadLock();
67 return ((IDictionary<K, V>)store).Contains(item); 105 var c = ((IDictionary<K, V>)store).Contains(item);
-   106 SyncRoot.ExitReadLock();
-   107 return c;
68 } 108 }
69   109  
70 public bool ContainsKey(K key) 110 public bool ContainsKey(K key)
71 { 111 {
-   112 SyncRoot.EnterReadLock();
72 return store.ContainsKey(key); 113 var c = store.ContainsKey(key);
-   114 SyncRoot.ExitReadLock();
-   115 return c;
73 } 116 }
74   117  
75 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) 118 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
76 { 119 {
-   120 SyncRoot.EnterReadLock();
77 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex); 121 ((IDictionary<K, V>)store).CopyTo(array, arrayIndex);
-   122 SyncRoot.ExitReadLock();
78 } 123 }
79   124  
80 public IEnumerator<KeyValuePair<K, V>> GetEnumerator() 125 public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
81 { 126 {
-   127 SyncRoot.EnterReadLock();
82 return ((IDictionary<K, V>)store).GetEnumerator(); 128 var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
-   129 SyncRoot.ExitReadLock();
-   130 return enumerator;
83 } 131 }
84   132  
85 public bool Remove(KeyValuePair<K, V> item) 133 public bool Remove(KeyValuePair<K, V> item)
86 { 134 {
-   135 SyncRoot.EnterWriteLock();
87 var removed = ((IDictionary<K, V>)store).Remove(item); 136 var removed = ((IDictionary<K, V>)store).Remove(item);
-   137 SyncRoot.ExitWriteLock();
88 IsVirgin = false; 138 IsVirgin = false;
89 if (removed) 139 if (removed)
90 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 140 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
91 return removed; 141 return removed;
92 } 142 }
93   143  
94 public bool Remove(K key) 144 public bool Remove(K key)
95 { 145 {
96 KeyValuePair<K, V> item; 146 KeyValuePair<K, V> item;
-   147 SyncRoot.EnterWriteLock();
97 if (store.ContainsKey(key)) 148 if (store.ContainsKey(key))
98 item = new KeyValuePair<K, V>(key, store[key]); 149 item = new KeyValuePair<K, V>(key, store[key]);
99   150  
100 var removed = store.Remove(key); 151 var removed = store.Remove(key);
-   152 SyncRoot.ExitWriteLock();
101 IsVirgin = false; 153 IsVirgin = false;
102 if (removed) 154 if (removed)
103 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); 155 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
104 return removed; 156 return removed;
105 } 157 }
106   158  
107 public bool TryGetValue(K key, out V value) 159 public bool TryGetValue(K key, out V value)
108 { 160 {
-   161 SyncRoot.EnterReadLock();
109 return store.TryGetValue(key, out value); 162 var c = store.TryGetValue(key, out value);
-   163 SyncRoot.ExitReadLock();
-   164 return c;
110 } 165 }
111   166  
112 IEnumerator IEnumerable.GetEnumerator() 167 IEnumerator IEnumerable.GetEnumerator()
113 { 168 {
-   169 SyncRoot.EnterReadLock();
114 return ((IDictionary<K, V>)store).GetEnumerator(); 170 var enumerator = ((IDictionary<K, V>)store).GetEnumerator();
-   171 SyncRoot.ExitReadLock();
-   172 return enumerator;
115 } 173 }
116   174  
117 public event NotifyCollectionChangedEventHandler CollectionChanged; 175 public event NotifyCollectionChangedEventHandler CollectionChanged;
118   176  
119 private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) 177 private void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
120 { 178 {
121 CollectionChanged?.Invoke(this, args); 179 CollectionChanged?.Invoke(this, args);
122 } 180 }
123 } 181 }
124 } 182 }
125   183