wasSharp – Blame information for rev 40
?pathlinks?
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; |
||
40 | office | 12 | using System.Linq; |
31 | office | 13 | |
14 | namespace wasSharp.Collections.Specialized |
||
15 | { |
||
40 | office | 16 | public static class ConcurrentHashSetExtensions |
17 | { |
||
18 | public static ConcurrentHashSet<T> ToConcurrentHashSet<T>(this IEnumerable<T> enumerable) |
||
19 | { |
||
20 | return new ConcurrentHashSet<T>(enumerable); |
||
21 | } |
||
22 | } |
||
23 | |||
31 | office | 24 | public class ConcurrentHashSet<T> : IDisposable, ICollection<T>, IEnumerable<T>, ISet<T> |
25 | { |
||
26 | private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
||
40 | office | 27 | private readonly HashSet<T> _hashSet = null; |
28 | private IEnumerable<string> enumerable; |
||
31 | office | 29 | |
40 | office | 30 | public ConcurrentHashSet() |
31 | { |
||
32 | _lock.EnterWriteLock(); |
||
33 | try |
||
34 | { |
||
35 | _hashSet = new HashSet<T>(); |
||
36 | } |
||
37 | finally |
||
38 | { |
||
39 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public ConcurrentHashSet(IEqualityComparer<T> stringComparer) |
||
44 | { |
||
45 | _lock.EnterWriteLock(); |
||
46 | try |
||
47 | { |
||
48 | _hashSet = new HashSet<T>(stringComparer); |
||
49 | } |
||
50 | finally |
||
51 | { |
||
52 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public ConcurrentHashSet(IEnumerable<T> enumerable) |
||
57 | { |
||
58 | _lock.EnterWriteLock(); |
||
59 | try |
||
60 | { |
||
61 | _hashSet = new HashSet<T>(); |
||
62 | foreach (var i in enumerable) |
||
63 | { |
||
64 | _hashSet.Add(i); |
||
65 | } |
||
66 | } |
||
67 | finally |
||
68 | { |
||
69 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
70 | } |
||
71 | } |
||
72 | |||
31 | office | 73 | #region Implementation of ICollection<T> |
74 | |||
75 | public bool Add(T item) |
||
76 | { |
||
77 | _lock.EnterWriteLock(); |
||
78 | try |
||
79 | { |
||
80 | return _hashSet.Add(item); |
||
81 | } |
||
82 | finally |
||
83 | { |
||
84 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public void Clear() |
||
89 | { |
||
90 | _lock.EnterWriteLock(); |
||
91 | try |
||
92 | { |
||
93 | _hashSet.Clear(); |
||
94 | } |
||
95 | finally |
||
96 | { |
||
97 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public bool Contains(T item) |
||
102 | { |
||
103 | _lock.EnterReadLock(); |
||
104 | try |
||
105 | { |
||
106 | return _hashSet.Contains(item); |
||
107 | } |
||
108 | finally |
||
109 | { |
||
110 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | public bool Remove(T item) |
||
115 | { |
||
116 | _lock.EnterWriteLock(); |
||
117 | try |
||
118 | { |
||
119 | return _hashSet.Remove(item); |
||
120 | } |
||
121 | finally |
||
122 | { |
||
123 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
124 | } |
||
125 | } |
||
126 | |||
40 | office | 127 | public int RemoveWhere(Predicate<T> match) |
128 | { |
||
129 | _lock.EnterWriteLock(); |
||
130 | try |
||
131 | { |
||
132 | return _hashSet.RemoveWhere(match); |
||
133 | } |
||
134 | finally |
||
135 | { |
||
136 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
137 | } |
||
138 | } |
||
139 | |||
31 | office | 140 | public int Count |
141 | { |
||
142 | get |
||
143 | { |
||
144 | _lock.EnterReadLock(); |
||
145 | try |
||
146 | { |
||
147 | return _hashSet.Count; |
||
148 | } |
||
149 | finally |
||
150 | { |
||
151 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | void ICollection<T>.Add(T item) |
||
157 | { |
||
158 | _lock.EnterWriteLock(); |
||
159 | try |
||
160 | { |
||
161 | _hashSet.Add(item); |
||
162 | } |
||
163 | finally |
||
164 | { |
||
165 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public void CopyTo(T[] array, int arrayIndex) |
||
170 | { |
||
171 | _lock.EnterReadLock(); |
||
172 | try |
||
173 | { |
||
174 | _hashSet.CopyTo(array, arrayIndex); |
||
175 | } |
||
176 | finally |
||
177 | { |
||
178 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public IEnumerator<T> GetEnumerator() |
||
183 | { |
||
184 | _lock.EnterReadLock(); |
||
185 | try |
||
186 | { |
||
187 | return _hashSet.GetEnumerator(); |
||
188 | } |
||
189 | finally |
||
190 | { |
||
191 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | IEnumerator IEnumerable.GetEnumerator() |
||
196 | { |
||
197 | _lock.EnterReadLock(); |
||
198 | try |
||
199 | { |
||
200 | return _hashSet.GetEnumerator(); |
||
201 | } |
||
202 | finally |
||
203 | { |
||
204 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public bool IsReadOnly |
||
209 | { |
||
210 | get |
||
211 | { |
||
212 | return ((ICollection<T>)_hashSet).IsReadOnly; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | #endregion Implementation of ICollection<T> |
||
217 | |||
218 | #region Dispose |
||
219 | |||
220 | public void Dispose() |
||
221 | { |
||
222 | Dispose(true); |
||
223 | GC.SuppressFinalize(this); |
||
224 | } |
||
225 | |||
226 | protected virtual void Dispose(bool disposing) |
||
227 | { |
||
228 | if (disposing) |
||
229 | { |
||
230 | if (_lock != null) |
||
231 | _lock.Dispose(); |
||
232 | _hashSet.Clear(); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | ~ConcurrentHashSet() |
||
237 | { |
||
238 | Dispose(false); |
||
239 | } |
||
240 | |||
241 | #endregion Dispose |
||
242 | |||
243 | #region Implementation of ISet<T> |
||
244 | |||
245 | public void ExceptWith(IEnumerable<T> other) |
||
246 | { |
||
247 | _lock.EnterWriteLock(); |
||
248 | try |
||
249 | { |
||
250 | _hashSet.ExceptWith(other); |
||
251 | } |
||
252 | finally |
||
253 | { |
||
254 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | public void IntersectWith(IEnumerable<T> other) |
||
259 | { |
||
260 | _lock.EnterWriteLock(); |
||
261 | try |
||
262 | { |
||
263 | _hashSet.IntersectWith(other); |
||
264 | } |
||
265 | finally |
||
266 | { |
||
267 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | public bool IsProperSubsetOf(IEnumerable<T> other) |
||
272 | { |
||
273 | _lock.EnterReadLock(); |
||
274 | try |
||
275 | { |
||
276 | return _hashSet.IsProperSubsetOf(other); |
||
277 | } |
||
278 | finally |
||
279 | { |
||
280 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | public bool IsProperSupersetOf(IEnumerable<T> other) |
||
285 | { |
||
286 | _lock.EnterReadLock(); |
||
287 | try |
||
288 | { |
||
289 | return _hashSet.IsProperSupersetOf(other); |
||
290 | } |
||
291 | finally |
||
292 | { |
||
293 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | public bool IsSubsetOf(IEnumerable<T> other) |
||
298 | { |
||
299 | _lock.EnterReadLock(); |
||
300 | try |
||
301 | { |
||
302 | return _hashSet.IsSubsetOf(other); |
||
303 | } |
||
304 | finally |
||
305 | { |
||
306 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | public bool IsSupersetOf(IEnumerable<T> other) |
||
311 | { |
||
312 | _lock.EnterReadLock(); |
||
313 | try |
||
314 | { |
||
315 | return _hashSet.IsSupersetOf(other); |
||
316 | } |
||
317 | finally |
||
318 | { |
||
319 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | public bool Overlaps(IEnumerable<T> other) |
||
324 | { |
||
325 | _lock.EnterReadLock(); |
||
326 | try |
||
327 | { |
||
328 | return _hashSet.Overlaps(other); |
||
329 | } |
||
330 | finally |
||
331 | { |
||
332 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | public bool SetEquals(IEnumerable<T> other) |
||
337 | { |
||
338 | _lock.EnterReadLock(); |
||
339 | try |
||
340 | { |
||
341 | return _hashSet.SetEquals(other); |
||
342 | } |
||
343 | finally |
||
344 | { |
||
345 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | public void SymmetricExceptWith(IEnumerable<T> other) |
||
350 | { |
||
351 | _lock.EnterWriteLock(); |
||
352 | try |
||
353 | { |
||
354 | _hashSet.SymmetricExceptWith(other); |
||
355 | } |
||
356 | finally |
||
357 | { |
||
358 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | public void UnionWith(IEnumerable<T> other) |
||
363 | { |
||
364 | _lock.EnterWriteLock(); |
||
365 | try |
||
366 | { |
||
367 | _hashSet.UnionWith(other); |
||
368 | } |
||
369 | finally |
||
370 | { |
||
371 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
372 | } |
||
373 | } |
||
374 | |||
375 | #endregion Implementation of ISet<T> |
||
376 | } |
||
377 | } |