wasSharp – Blame information for rev 31

Subversion Repositories:
Rev:
Rev Author Line No. Line
31 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6 // Based on work by Ben Mosher
7  
8 using System;
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Threading;
12  
13 namespace wasSharp.Collections.Specialized
14 {
15 public class ConcurrentHashSet<T> : IDisposable, ICollection<T>, IEnumerable<T>, ISet<T>
16 {
17 private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
18 private readonly HashSet<T> _hashSet = new HashSet<T>();
19  
20 #region Implementation of ICollection<T>
21  
22 public bool Add(T item)
23 {
24 _lock.EnterWriteLock();
25 try
26 {
27 return _hashSet.Add(item);
28 }
29 finally
30 {
31 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
32 }
33 }
34  
35 public void Clear()
36 {
37 _lock.EnterWriteLock();
38 try
39 {
40 _hashSet.Clear();
41 }
42 finally
43 {
44 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
45 }
46 }
47  
48 public bool Contains(T item)
49 {
50 _lock.EnterReadLock();
51 try
52 {
53 return _hashSet.Contains(item);
54 }
55 finally
56 {
57 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
58 }
59 }
60  
61 public bool Remove(T item)
62 {
63 _lock.EnterWriteLock();
64 try
65 {
66 return _hashSet.Remove(item);
67 }
68 finally
69 {
70 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
71 }
72 }
73  
74 public int Count
75 {
76 get
77 {
78 _lock.EnterReadLock();
79 try
80 {
81 return _hashSet.Count;
82 }
83 finally
84 {
85 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
86 }
87 }
88 }
89  
90 void ICollection<T>.Add(T item)
91 {
92 _lock.EnterWriteLock();
93 try
94 {
95 _hashSet.Add(item);
96 }
97 finally
98 {
99 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
100 }
101 }
102  
103 public void CopyTo(T[] array, int arrayIndex)
104 {
105 _lock.EnterReadLock();
106 try
107 {
108 _hashSet.CopyTo(array, arrayIndex);
109 }
110 finally
111 {
112 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
113 }
114 }
115  
116 public IEnumerator<T> GetEnumerator()
117 {
118 _lock.EnterReadLock();
119 try
120 {
121 return _hashSet.GetEnumerator();
122 }
123 finally
124 {
125 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
126 }
127 }
128  
129 IEnumerator IEnumerable.GetEnumerator()
130 {
131 _lock.EnterReadLock();
132 try
133 {
134 return _hashSet.GetEnumerator();
135 }
136 finally
137 {
138 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
139 }
140 }
141  
142 public bool IsReadOnly
143 {
144 get
145 {
146 return ((ICollection<T>)_hashSet).IsReadOnly;
147 }
148 }
149  
150 #endregion Implementation of ICollection<T>
151  
152 #region Dispose
153  
154 public void Dispose()
155 {
156 Dispose(true);
157 GC.SuppressFinalize(this);
158 }
159  
160 protected virtual void Dispose(bool disposing)
161 {
162 if (disposing)
163 {
164 if (_lock != null)
165 _lock.Dispose();
166 _hashSet.Clear();
167 }
168 }
169  
170 ~ConcurrentHashSet()
171 {
172 Dispose(false);
173 }
174  
175 #endregion Dispose
176  
177 #region Implementation of ISet<T>
178  
179 public void ExceptWith(IEnumerable<T> other)
180 {
181 _lock.EnterWriteLock();
182 try
183 {
184 _hashSet.ExceptWith(other);
185 }
186 finally
187 {
188 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
189 }
190 }
191  
192 public void IntersectWith(IEnumerable<T> other)
193 {
194 _lock.EnterWriteLock();
195 try
196 {
197 _hashSet.IntersectWith(other);
198 }
199 finally
200 {
201 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
202 }
203 }
204  
205 public bool IsProperSubsetOf(IEnumerable<T> other)
206 {
207 _lock.EnterReadLock();
208 try
209 {
210 return _hashSet.IsProperSubsetOf(other);
211 }
212 finally
213 {
214 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
215 }
216 }
217  
218 public bool IsProperSupersetOf(IEnumerable<T> other)
219 {
220 _lock.EnterReadLock();
221 try
222 {
223 return _hashSet.IsProperSupersetOf(other);
224 }
225 finally
226 {
227 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
228 }
229 }
230  
231 public bool IsSubsetOf(IEnumerable<T> other)
232 {
233 _lock.EnterReadLock();
234 try
235 {
236 return _hashSet.IsSubsetOf(other);
237 }
238 finally
239 {
240 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
241 }
242 }
243  
244 public bool IsSupersetOf(IEnumerable<T> other)
245 {
246 _lock.EnterReadLock();
247 try
248 {
249 return _hashSet.IsSupersetOf(other);
250 }
251 finally
252 {
253 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
254 }
255 }
256  
257 public bool Overlaps(IEnumerable<T> other)
258 {
259 _lock.EnterReadLock();
260 try
261 {
262 return _hashSet.Overlaps(other);
263 }
264 finally
265 {
266 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
267 }
268 }
269  
270 public bool SetEquals(IEnumerable<T> other)
271 {
272 _lock.EnterReadLock();
273 try
274 {
275 return _hashSet.SetEquals(other);
276 }
277 finally
278 {
279 if (_lock.IsReadLockHeld) _lock.ExitReadLock();
280 }
281 }
282  
283 public void SymmetricExceptWith(IEnumerable<T> other)
284 {
285 _lock.EnterWriteLock();
286 try
287 {
288 _hashSet.SymmetricExceptWith(other);
289 }
290 finally
291 {
292 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
293 }
294 }
295  
296 public void UnionWith(IEnumerable<T> other)
297 {
298 _lock.EnterWriteLock();
299 try
300 {
301 _hashSet.UnionWith(other);
302 }
303 finally
304 {
305 if (_lock.IsWriteLockHeld) _lock.ExitWriteLock();
306 }
307 }
308  
309 #endregion Implementation of ISet<T>
310 }
311 }