wasSharp – Diff between revs 27 and 39
?pathlinks?
Rev 27 | Rev 39 | |||
---|---|---|---|---|
Line 3... | Line 3... | |||
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 | /////////////////////////////////////////////////////////////////////////// |
|
Line 6... | Line 6... | |||
6 | |
6 | |
|
- | 7 | using System; |
||
7 | using System; |
8 | using System.Threading; |
|
8 | using System.Collections; |
9 | using System.Collections; |
|
9 | using System.Collections.Generic; |
10 | using System.Collections.Generic; |
|
10 | using System.Collections.Specialized; |
11 | using System.Collections.Specialized; |
|
Line 19... | Line 20... | |||
19 | /// An implementation of an observable HashSet. |
20 | /// An implementation of an observable HashSet. |
|
20 | /// </summary> |
21 | /// </summary> |
|
21 | /// <typeparam name="T">the object type</typeparam> |
22 | /// <typeparam name="T">the object type</typeparam> |
|
22 | public class ObservableHashSet<T> : ICollection<T>, INotifyCollectionChanged, IEnumerable<T> |
23 | public class ObservableHashSet<T> : ICollection<T>, INotifyCollectionChanged, IEnumerable<T> |
|
23 | { |
24 | { |
|
- | 25 | private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
||
24 | private readonly HashSet<T> store = new HashSet<T>(); |
26 | private readonly HashSet<T> store = new HashSet<T>(); |
|
Line 25... | Line 27... | |||
25 | |
27 | |
|
26 | public ObservableHashSet(HashSet<T> set) |
28 | public ObservableHashSet(HashSet<T> set) |
|
- | 29 | { |
||
27 | { |
30 | SyncRoot.EnterWriteLock(); |
|
- | 31 | UnionWith(set); |
||
28 | UnionWith(set); |
32 | SyncRoot.ExitWriteLock(); |
|
Line 29... | Line 33... | |||
29 | } |
33 | } |
|
30 | |
34 | |
|
31 | public ObservableHashSet() |
35 | public ObservableHashSet() |
|
Line 32... | Line 36... | |||
32 | { |
36 | { |
|
33 | } |
37 | } |
|
- | 38 | |
||
34 | |
39 | public ObservableHashSet(T item) |
|
- | 40 | { |
||
35 | public ObservableHashSet(T item) |
41 | SyncRoot.EnterWriteLock(); |
|
Line 36... | Line 42... | |||
36 | { |
42 | Add(item); |
|
37 | Add(item); |
43 | SyncRoot.ExitWriteLock(); |
|
- | 44 | } |
||
38 | } |
45 | |
|
- | 46 | public ObservableHashSet(ObservableHashSet<T> other) |
||
39 | |
47 | { |
|
Line 40... | Line 48... | |||
40 | public ObservableHashSet(ObservableHashSet<T> other) |
48 | SyncRoot.EnterWriteLock(); |
|
41 | { |
49 | UnionWith(other); |
|
- | 50 | SyncRoot.ExitWriteLock(); |
||
42 | UnionWith(other); |
51 | } |
|
- | 52 | |
||
43 | } |
53 | public ObservableHashSet(IEnumerable<T> list) |
|
Line 44... | Line 54... | |||
44 | |
54 | { |
|
Line 45... | Line 55... | |||
45 | public ObservableHashSet(IEnumerable<T> list) |
55 | SyncRoot.EnterWriteLock(); |
|
46 | { |
56 | UnionWith(list); |
|
- | 57 | SyncRoot.ExitWriteLock(); |
||
47 | UnionWith(list); |
58 | } |
|
- | 59 | |
||
- | 60 | public bool IsVirgin { get; private set; } = true; |
||
48 | } |
61 | |
|
Line 49... | Line 62... | |||
49 | |
62 | public IEnumerator<T> GetEnumerator() |
|
50 | public bool IsVirgin { get; private set; } = true; |
63 | { |
|
- | 64 | SyncRoot.EnterReadLock(); |
||
- | 65 | var enumerator = store.GetEnumerator(); |
||
- | 66 | SyncRoot.ExitReadLock(); |
||
51 | |
67 | return enumerator; |
|
52 | public IEnumerator<T> GetEnumerator() |
68 | } |
|
Line 53... | Line 69... | |||
53 | { |
69 | |
|
54 | return store.GetEnumerator(); |
70 | IEnumerator IEnumerable.GetEnumerator() |
|
- | 71 | { |
||
55 | } |
72 | SyncRoot.EnterReadLock(); |
|
- | 73 | var enumerator = GetEnumerator(); |
||
56 | |
74 | SyncRoot.ExitReadLock(); |
|
57 | IEnumerator IEnumerable.GetEnumerator() |
75 | return enumerator; |
|
58 | { |
76 | } |
|
Line 59... | Line 77... | |||
59 | return GetEnumerator(); |
77 | |
|
60 | } |
78 | public void Add(T item) |
|
- | 79 | { |
||
61 | |
80 | SyncRoot.EnterWriteLock(); |
|
- | 81 | store.Add(item); |
||
62 | public void Add(T item) |
82 | SyncRoot.ExitWriteLock(); |
|
63 | { |
83 | IsVirgin = false; |
|
64 | store.Add(item); |
84 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); |
|
65 | IsVirgin = false; |
85 | } |
|
Line 66... | Line 86... | |||
66 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); |
86 | |
|
67 | } |
87 | public void Clear() |
|
- | 88 | { |
||
68 | |
89 | SyncRoot.EnterWriteLock(); |
|
- | 90 | store.Clear(); |
||
- | 91 | SyncRoot.ExitWriteLock(); |
||
69 | public void Clear() |
92 | if (!IsVirgin) |
|
Line 70... | Line 93... | |||
70 | { |
93 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
|
71 | store.Clear(); |
94 | IsVirgin = false; |
|
- | 95 | } |
||
72 | if (!IsVirgin) |
96 | |
|
- | 97 | public bool Contains(T item) |
||
73 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
98 | { |
|
Line 74... | Line 99... | |||
74 | IsVirgin = false; |
99 | SyncRoot.EnterReadLock(); |
|
75 | } |
100 | var contains = store.Contains(item); |
|
- | 101 | SyncRoot.ExitReadLock(); |
||
76 | |
102 | return contains; |
|
- | 103 | } |
||
77 | public bool Contains(T item) |
104 | |
|
78 | { |
105 | public void CopyTo(T[] array, int arrayIndex) |
|
79 | return store.Contains(item); |
106 | { |
|
80 | } |
107 | SyncRoot.EnterReadLock(); |
|
81 | |
108 | store.CopyTo(array, arrayIndex); |
|
Line 100... | Line 127... | |||
100 | public event NotifyCollectionChangedEventHandler CollectionChanged; |
127 | public event NotifyCollectionChangedEventHandler CollectionChanged; |
|
Line 101... | Line 128... | |||
101 | |
128 | |
|
102 | public void UnionWith(IEnumerable<T> list) |
129 | public void UnionWith(IEnumerable<T> list) |
|
103 | { |
130 | { |
|
- | 131 | var added = new List<T>(list.Except(store)); |
||
104 | var added = new List<T>(list.Except(store)); |
132 | SyncRoot.EnterWriteLock(); |
|
- | 133 | store.UnionWith(added); |
||
105 | store.UnionWith(added); |
134 | SyncRoot.ExitWriteLock(); |
|
106 | if (!IsVirgin && added.Any()) |
135 | if (!IsVirgin && added.Any()) |
|
107 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added)); |
136 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added)); |
|
108 | IsVirgin = false; |
137 | IsVirgin = false; |
|
Line 114... | Line 143... | |||
114 | } |
143 | } |
|
Line 115... | Line 144... | |||
115 | |
144 | |
|
116 | public void ExceptWith(IEnumerable<T> list) |
145 | public void ExceptWith(IEnumerable<T> list) |
|
117 | { |
146 | { |
|
- | 147 | var removed = new List<T>(list.Intersect(store)); |
||
118 | var removed = new List<T>(list.Intersect(store)); |
148 | SyncRoot.EnterWriteLock(); |
|
- | 149 | store.ExceptWith(removed); |
||
119 | store.ExceptWith(removed); |
150 | SyncRoot.ExitWriteLock(); |
|
120 | if (!IsVirgin && removed.Any()) |
151 | if (!IsVirgin && removed.Any()) |
|
121 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
152 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
|
122 | removed)); |
153 | removed)); |
|
123 | IsVirgin = false; |
154 | IsVirgin = false; |
|
Line 124... | Line 155... | |||
124 | } |
155 | } |
|
125 | |
156 | |
|
126 | public void RemoveWhere(Func<T, bool> func) |
157 | public void RemoveWhere(Func<T, bool> func) |
|
- | 158 | { |
||
127 | { |
159 | var removed = new List<T>(store.Where(func)); |
|
- | 160 | SyncRoot.EnterWriteLock(); |
||
128 | var removed = new List<T>(store.Where(func)); |
161 | store.ExceptWith(removed); |
|
129 | store.ExceptWith(removed); |
162 | SyncRoot.ExitWriteLock(); |
|
130 | if (!IsVirgin && removed.Any()) |
163 | if (!IsVirgin && removed.Any()) |
|
131 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
164 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
|
132 | removed)); |
165 | removed)); |
|
Line 133... | Line 166... | |||
133 | IsVirgin = false; |
166 | IsVirgin = false; |
|
134 | } |
167 | } |
|
- | 168 | |
||
135 | |
169 | public IEnumerable<T> AsEnumerable() |
|
- | 170 | { |
||
- | 171 | SyncRoot.EnterWriteLock(); |
||
136 | public IEnumerable<T> AsEnumerable() |
172 | var enumerable = store.AsEnumerable(); |
|
137 | { |
173 | SyncRoot.ExitWriteLock(); |
|
138 | return store.AsEnumerable(); |
174 | return enumerable; |