1 // ConcurrentOrderedList.cs
3 // Copyright (c) 2010 Jérémie "Garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
27 using System.Threading;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Runtime.Serialization;
32 #if INSIDE_MONO_PARALLEL
33 using System.Collections.Concurrent;
35 namespace Mono.Collections.Concurrent
37 namespace System.Collections.Concurrent
40 #if INSIDE_MONO_PARALLEL
43 class ConcurrentOrderedList<T>: ICollection<T>, IEnumerable<T>
57 public Node (Node wrapped)
67 IEqualityComparer<T> comparer;
71 public ConcurrentOrderedList () : this (EqualityComparer<T>.Default)
76 public ConcurrentOrderedList (IEqualityComparer<T> comparer)
79 throw new ArgumentNullException ("comparer");
81 this.comparer = comparer;
88 public bool TryAdd (T data)
90 Node node = new Node ();
92 node.Key = comparer.GetHashCode (data);
94 if (ListInsert (node)) {
95 Interlocked.Increment (ref count);
102 public bool TryRemove (T data)
105 return TryRemoveHash (comparer.GetHashCode (data), out dummy);
108 public bool TryRemoveHash (int key, out T data)
110 if (ListDelete (key, out data)) {
111 Interlocked.Decrement (ref count);
118 public bool TryPop (out T data)
120 return ListPop (out data);
123 public bool Contains (T data)
125 return ContainsHash (comparer.GetHashCode (data));
128 public bool ContainsHash (int key)
132 if (!ListFind (key, out node))
139 public bool TryGetFromHash (int key, out T data)
144 if (!ListFind (key, out node))
156 public void CopyTo (T[] array, int startIndex)
159 throw new ArgumentNullException ("array");
161 throw new ArgumentOutOfRangeException ("startIndex");
162 if (count > array.Length - startIndex)
163 throw new ArgumentException ("array", "The number of elements is greater than the available space from startIndex to the end of the destination array.");
165 foreach (T item in this) {
166 if (startIndex >= array.Length)
169 array[startIndex++] = item;
173 public IEqualityComparer<T> Comparer {
185 Node ListSearch (int key, ref Node left)
187 Node leftNodeNext = null, rightNode = null;
195 leftNodeNext = tNext;
197 t = tNext.Marked ? tNext.Next : tNext;
202 } while (tNext.Marked || t.Key < key);
206 if (leftNodeNext == rightNode) {
207 if (rightNode != tail && rightNode.Next.Marked)
213 if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
214 if (rightNode != tail && rightNode.Next.Marked)
222 bool ListDelete (int key, out T data)
224 Node rightNode = null, rightNodeNext = null, leftNode = null;
228 rightNode = ListSearch (key, ref leftNode);
229 if (rightNode == tail || rightNode.Key != key)
232 data = rightNode.Data;
234 rightNodeNext = rightNode.Next;
235 if (!rightNodeNext.Marked)
236 if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
240 if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
241 ListSearch (rightNode.Key, ref leftNode);
246 bool ListPop (out T data)
248 Node rightNode = null, rightNodeNext = null, leftNode = head;
252 rightNode = head.Next;
253 if (rightNode == tail)
256 data = rightNode.Data;
258 rightNodeNext = rightNode.Next;
259 if (!rightNodeNext.Marked)
260 if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
264 if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
265 ListSearch (rightNode.Key, ref leftNode);
270 bool ListInsert (Node newNode)
272 int key = newNode.Key;
273 Node rightNode = null, leftNode = null;
276 rightNode = ListSearch (key, ref leftNode);
277 if (rightNode != tail && rightNode.Key == key)
280 newNode.Next = rightNode;
281 if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
286 bool ListFind (int key, out Node data)
288 Node rightNode = null, leftNode = null;
291 data = rightNode = ListSearch (key, ref leftNode);
293 return rightNode != tail && rightNode.Key == key;
296 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
298 return GetEnumeratorInternal ();
301 IEnumerator IEnumerable.GetEnumerator ()
303 return GetEnumeratorInternal ();
306 IEnumerator<T> GetEnumeratorInternal ()
308 Node node = head.Next;
310 while (node != tail) {
311 while (node.Marked) {
316 yield return node.Data;
321 bool ICollection<T>.IsReadOnly {
327 void ICollection<T>.Add (T item)
332 bool ICollection<T>.Remove (T item)
334 return TryRemove (item);