ConcurrentOrderedList.cs
Go to the documentation of this file.
1 // ConcurrentOrderedList.cs
2 //
3 // Copyright (c) 2010 Jérémie "Garuma" Laval
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24 
25 #if NET_4_0
26 
27 using System;
28 using System.Threading;
29 using System.Collections;
31 using System.Runtime.Serialization;
32 
33 #if INSIDE_MONO_PARALLEL
35 
36 namespace Mono.Collections.Concurrent
37 #else
38 namespace System.Collections.Concurrent
39 #endif
40 {
41 #if INSIDE_MONO_PARALLEL
42  public
43 #endif
44  class ConcurrentOrderedList<T>: ICollection<T>, IEnumerable<T>
45  {
46  class Node
47  {
48  public T Data;
49  public int Key;
50  public Node Next;
51  public bool Marked;
52 
53  public Node ()
54  {
55 
56  }
57 
58  public Node (Node wrapped)
59  {
60  Marked = true;
61  Next = wrapped;
62  }
63  }
64 
65  Node head;
66  Node tail;
67 
68  IEqualityComparer<T> comparer;
69 
70  int count;
71 
72  public ConcurrentOrderedList () : this (EqualityComparer<T>.Default)
73  {
74 
75  }
76 
77  public ConcurrentOrderedList (IEqualityComparer<T> comparer)
78  {
79  if (comparer == null)
80  throw new ArgumentNullException ("comparer");
81 
82  this.comparer = comparer;
83 
84  head = new Node ();
85  tail = new Node ();
86  head.Next = tail;
87  }
88 
89  public bool TryAdd (T data)
90  {
91  Node node = new Node ();
92  node.Data = data;
93  node.Key = comparer.GetHashCode (data);
94 
95  if (ListInsert (node)) {
96  Interlocked.Increment (ref count);
97  return true;
98  }
99 
100  return false;
101  }
102 
103  public bool TryRemove (T data)
104  {
105  T dummy;
106  return TryRemoveHash (comparer.GetHashCode (data), out dummy);
107  }
108 
109  public bool TryRemoveHash (int key, out T data)
110  {
111  if (ListDelete (key, out data)) {
112  Interlocked.Decrement (ref count);
113  return true;
114  }
115 
116  return false;
117  }
118 
119  public bool TryPop (out T data)
120  {
121  return ListPop (out data);
122  }
123 
124  public bool Contains (T data)
125  {
126  return ContainsHash (comparer.GetHashCode (data));
127  }
128 
129  public bool ContainsHash (int key)
130  {
131  Node node;
132 
133  if (!ListFind (key, out node))
134  return false;
135 
136  return true;
137 
138  }
139 
140  public bool TryGetFromHash (int key, out T data)
141  {
142  data = default (T);
143  Node node;
144 
145  if (!ListFind (key, out node))
146  return false;
147 
148  data = node.Data;
149  return true;
150  }
151 
152  public void Clear ()
153  {
154  head.Next = tail;
155  }
156 
157  public void CopyTo (T[] array, int startIndex)
158  {
159  if (array == null)
160  throw new ArgumentNullException ("array");
161  if (startIndex < 0)
162  throw new ArgumentOutOfRangeException ("startIndex");
163  if (count > array.Length - startIndex)
164  throw new ArgumentException ("array", "The number of elements is greater than the available space from startIndex to the end of the destination array.");
165 
166  foreach (T item in this) {
167  if (startIndex >= array.Length)
168  break;
169 
170  array[startIndex++] = item;
171  }
172  }
173 
174  public IEqualityComparer<T> Comparer {
175  get {
176  return comparer;
177  }
178  }
179 
180  public int Count {
181  get {
182  return count;
183  }
184  }
185 
186  Node ListSearch (int key, ref Node left)
187  {
188  Node leftNodeNext = null, rightNode = null;
189 
190  do {
191  Node t = head;
192  Node tNext = t.Next;
193  do {
194  if (!tNext.Marked) {
195  left = t;
196  leftNodeNext = tNext;
197  }
198  t = tNext.Marked ? tNext.Next : tNext;
199  if (t == tail)
200  break;
201 
202  tNext = t.Next;
203  } while (tNext.Marked || t.Key < key);
204 
205  rightNode = t;
206 
207  if (leftNodeNext == rightNode) {
208  if (rightNode != tail && rightNode.Next.Marked)
209  continue;
210  else
211  return rightNode;
212  }
213 
214  if (AotInterlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
215  if (rightNode != tail && rightNode.Next.Marked)
216  continue;
217  else
218  return rightNode;
219  }
220  } while (true);
221  }
222 
223  bool ListDelete (int key, out T data)
224  {
225  Node rightNode = null, rightNodeNext = null, leftNode = null;
226  data = default (T);
227 
228  do {
229  rightNode = ListSearch (key, ref leftNode);
230  if (rightNode == tail || rightNode.Key != key)
231  return false;
232 
233  data = rightNode.Data;
234 
235  rightNodeNext = rightNode.Next;
236  if (!rightNodeNext.Marked)
237  if (AotInterlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
238  break;
239  } while (true);
240 
241  if (AotInterlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
242  ListSearch (rightNode.Key, ref leftNode);
243 
244  return true;
245  }
246 
247  bool ListPop (out T data)
248  {
249  Node rightNode = null, rightNodeNext = null, leftNode = head;
250  data = default (T);
251 
252  do {
253  rightNode = head.Next;
254  if (rightNode == tail)
255  return false;
256 
257  data = rightNode.Data;
258 
259  rightNodeNext = rightNode.Next;
260  if (!rightNodeNext.Marked)
261  if (AotInterlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
262  break;
263  } while (true);
264 
265  if (AotInterlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
266  ListSearch (rightNode.Key, ref leftNode);
267 
268  return true;
269  }
270 
271  bool ListInsert (Node newNode)
272  {
273  int key = newNode.Key;
274  Node rightNode = null, leftNode = null;
275 
276  do {
277  rightNode = ListSearch (key, ref leftNode);
278  if (rightNode != tail && rightNode.Key == key)
279  return false;
280 
281  newNode.Next = rightNode;
282  if (AotInterlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
283  return true;
284  } while (true);
285  }
286 
287  bool ListFind (int key, out Node data)
288  {
289  Node rightNode = null, leftNode = null;
290  data = null;
291 
292  data = rightNode = ListSearch (key, ref leftNode);
293 
294  return rightNode != tail && rightNode.Key == key;
295  }
296 
297  IEnumerator<T> IEnumerable<T>.GetEnumerator ()
298  {
299  return GetEnumeratorInternal ();
300  }
301 
302  IEnumerator IEnumerable.GetEnumerator ()
303  {
304  return GetEnumeratorInternal ();
305  }
306 
307  IEnumerator<T> GetEnumeratorInternal ()
308  {
309  Node node = head.Next;
310 
311  while (node != tail) {
312  while (node.Marked) {
313  node = node.Next;
314  if (node == tail)
315  yield break;
316  }
317  yield return node.Data;
318  node = node.Next;
319  }
320  }
321 
322  bool ICollection<T>.IsReadOnly {
323  get {
324  return false;
325  }
326  }
327 
328  void ICollection<T>.Add (T item)
329  {
330  TryAdd (item);
331  }
332 
333  bool ICollection<T>.Remove (T item)
334  {
335  return TryRemove (item);
336  }
337  }
338 }
339 
340 #endif
Interlocked reference exchanges do not work with the older Mono AOT compiler so this type fudges arou...