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
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
33 #if INSIDE_MONO_PARALLEL
34 using System.Collections.Concurrent;
36 namespace Mono.Collections.Concurrent
38 namespace System.Collections.Concurrent
41 #if INSIDE_MONO_PARALLEL
44 class ConcurrentOrderedList<T>: ICollection<T>, IEnumerable<T>
58 public Node (Node wrapped)
68 IEqualityComparer<T> comparer;
72 public ConcurrentOrderedList () : this (EqualityComparer<T>.Default)
77 public ConcurrentOrderedList (IEqualityComparer<T> comparer)
80 throw new ArgumentNullException ("comparer");
82 this.comparer = comparer;
89 public bool TryAdd (T data)
91 Node node = new Node ();
93 node.Key = comparer.GetHashCode (data);
95 if (ListInsert (node)) {
96 Interlocked.Increment (ref count);
103 public bool TryRemove (T data)
106 return TryRemoveHash (comparer.GetHashCode (data), out dummy);
109 public bool TryRemoveHash (int key, out T data)
111 if (ListDelete (key, out data)) {
112 Interlocked.Decrement (ref count);
119 public bool Contains (T data)
121 return ContainsHash (comparer.GetHashCode (data));
124 public bool ContainsHash (int key)
128 if (!ListFind (key, out node))
135 public bool TryGetFromHash (int key, out T data)
140 if (!ListFind (key, out node))
152 public void CopyTo (T[] array, int startIndex)
155 throw new ArgumentNullException ("array");
157 throw new ArgumentOutOfRangeException ("startIndex");
158 if (count > array.Length - startIndex)
159 throw new ArgumentException ("array", "The number of elements is greater than the available space from startIndex to the end of the destination array.");
161 foreach (T item in this) {
162 if (startIndex >= array.Length)
165 array[startIndex++] = item;
169 public IEqualityComparer<T> Comparer {
181 Node ListSearch (int key, ref Node left)
183 Node leftNodeNext = null, rightNode = null;
191 leftNodeNext = tNext;
193 t = tNext.Marked ? tNext.Next : tNext;
198 } while (tNext.Marked || t.Key < key);
202 if (leftNodeNext == rightNode) {
203 if (rightNode != tail && rightNode.Next.Marked)
209 if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
210 if (rightNode != tail && rightNode.Next.Marked)
218 bool ListDelete (int key, out T data)
220 Node rightNode = null, rightNodeNext = null, leftNode = null;
224 rightNode = ListSearch (key, ref leftNode);
225 if (rightNode == tail || rightNode.Key != key)
228 data = rightNode.Data;
230 rightNodeNext = rightNode.Next;
231 if (!rightNodeNext.Marked)
232 if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
236 if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
237 ListSearch (rightNode.Key, ref leftNode);
242 bool ListInsert (Node newNode)
244 int key = newNode.Key;
245 Node rightNode = null, leftNode = null;
248 rightNode = ListSearch (key, ref leftNode);
249 if (rightNode != tail && rightNode.Key == key)
252 newNode.Next = rightNode;
253 if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
258 bool ListFind (int key, out Node data)
260 Node rightNode = null, leftNode = null;
263 data = rightNode = ListSearch (key, ref leftNode);
265 return rightNode != tail && rightNode.Key == key;
268 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
270 return GetEnumeratorInternal ();
273 IEnumerator IEnumerable.GetEnumerator ()
275 return GetEnumeratorInternal ();
278 IEnumerator<T> GetEnumeratorInternal ()
280 Node node = head.Next;
282 while (node != tail) {
283 while (node.Marked) {
288 yield return node.Data;
293 bool ICollection<T>.IsReadOnly {
299 void ICollection<T>.Add (T item)
304 bool ICollection<T>.Remove (T item)
306 return TryRemove (item);