Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ConcurrentDictionary.cs
1 // ConcurrentDictionary.cs
2 //
3 // Copyright (c) 2009 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 || MOBILE
26
27 using System;
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.Diagnostics;
33
34 namespace System.Collections.Concurrent
35 {
36         [DebuggerDisplay ("Count={Count}")]
37         [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
38         public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>,
39           ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>,
40           IDictionary, ICollection, IEnumerable
41         {
42                 IEqualityComparer<TKey> comparer;
43
44                 SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> internalDictionary;
45
46                 public ConcurrentDictionary () : this (EqualityComparer<TKey>.Default)
47                 {
48                 }
49
50                 public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection)
51                         : this (collection, EqualityComparer<TKey>.Default)
52                 {
53                 }
54
55                 public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
56                 {
57                         this.comparer = comparer;
58                         this.internalDictionary = new SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> (comparer);
59                 }
60
61                 public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
62                         : this (comparer)
63                 {
64                         foreach (KeyValuePair<TKey, TValue> pair in collection)
65                                 Add (pair.Key, pair.Value);
66                 }
67
68                 // Parameters unused
69                 public ConcurrentDictionary (int concurrencyLevel, int capacity)
70                         : this (EqualityComparer<TKey>.Default)
71                 {
72
73                 }
74
75                 public ConcurrentDictionary (int concurrencyLevel,
76                                              IEnumerable<KeyValuePair<TKey, TValue>> collection,
77                                              IEqualityComparer<TKey> comparer)
78                         : this (collection, comparer)
79                 {
80
81                 }
82
83                 // Parameters unused
84                 public ConcurrentDictionary (int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer)
85                         : this (comparer)
86                 {
87
88                 }
89
90                 void Add (TKey key, TValue value)
91                 {
92                         while (!TryAdd (key, value));
93                 }
94
95                 void IDictionary<TKey, TValue>.Add (TKey key, TValue value)
96                 {
97                         Add (key, value);
98                 }
99
100                 public bool TryAdd (TKey key, TValue value)
101                 {
102                         return internalDictionary.Insert (Hash (key), key, Make (key, value));
103                 }
104
105                 void ICollection<KeyValuePair<TKey,TValue>>.Add (KeyValuePair<TKey, TValue> pair)
106                 {
107                         Add (pair.Key, pair.Value);
108                 }
109
110                 public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
111                 {
112                         return internalDictionary.InsertOrUpdate (Hash (key),
113                                                                   key,
114                                                                   () => Make (key, addValueFactory (key)),
115                                                                   (e) => Make (key, updateValueFactory (key, e.Value))).Value;
116                 }
117
118                 public TValue AddOrUpdate (TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
119                 {
120                         return AddOrUpdate (key, (_) => addValue, updateValueFactory);
121                 }
122
123                 TValue AddOrUpdate (TKey key, TValue addValue, TValue updateValue)
124                 {
125                         return internalDictionary.InsertOrUpdate (Hash (key),
126                                                                   key,
127                                                                   Make (key, addValue),
128                                                                   Make (key, updateValue)).Value;
129                 }
130
131                 TValue GetValue (TKey key)
132                 {
133                         TValue temp;
134                         if (!TryGetValue (key, out temp))
135                                 throw new KeyNotFoundException (key.ToString ());
136                         return temp;
137                 }
138
139                 public bool TryGetValue (TKey key, out TValue value)
140                 {
141                         KeyValuePair<TKey, TValue> pair;
142                         bool result = internalDictionary.Find (Hash (key), key, out pair);
143                         value = pair.Value;
144
145                         return result;
146                 }
147
148                 public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue)
149                 {
150                         return internalDictionary.CompareExchange (Hash (key), key, Make (key, newValue), (e) => e.Value.Equals (comparisonValue));
151                 }
152
153                 public TValue this[TKey key] {
154                         get {
155                                 return GetValue (key);
156                         }
157                         set {
158                                 AddOrUpdate (key, value, value);
159                         }
160                 }
161
162                 public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
163                 {
164                         return internalDictionary.InsertOrGet (Hash (key), key, Make (key, default(TValue)), () => Make (key, valueFactory (key))).Value;
165                 }
166
167                 public TValue GetOrAdd (TKey key, TValue value)
168                 {
169                         return internalDictionary.InsertOrGet (Hash (key), key, Make (key, value), null).Value;
170                 }
171
172                 public bool TryRemove (TKey key, out TValue value)
173                 {
174                         KeyValuePair<TKey, TValue> data;
175                         bool result = internalDictionary.Delete (Hash (key), key, out data);
176                         value = data.Value;
177                         return result;
178                 }
179
180                 bool Remove (TKey key)
181                 {
182                         TValue dummy;
183
184                         return TryRemove (key, out dummy);
185                 }
186
187                 bool IDictionary<TKey, TValue>.Remove (TKey key)
188                 {
189                         return Remove (key);
190                 }
191
192                 bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> pair)
193                 {
194                         return Remove (pair.Key);
195                 }
196
197                 public bool ContainsKey (TKey key)
198                 {
199                         KeyValuePair<TKey, TValue> dummy;
200                         return internalDictionary.Find (Hash (key), key, out dummy);
201                 }
202
203                 bool IDictionary.Contains (object key)
204                 {
205                         if (!(key is TKey))
206                                 return false;
207
208                         return ContainsKey ((TKey)key);
209                 }
210
211                 void IDictionary.Remove (object key)
212                 {
213                         if (!(key is TKey))
214                                 return;
215
216                         Remove ((TKey)key);
217                 }
218
219                 object IDictionary.this [object key]
220                 {
221                         get {
222                                 if (!(key is TKey))
223                                         throw new ArgumentException ("key isn't of correct type", "key");
224
225                                 return this[(TKey)key];
226                         }
227                         set {
228                                 if (!(key is TKey) || !(value is TValue))
229                                         throw new ArgumentException ("key or value aren't of correct type");
230
231                                 this[(TKey)key] = (TValue)value;
232                         }
233                 }
234
235                 void IDictionary.Add (object key, object value)
236                 {
237                         if (!(key is TKey) || !(value is TValue))
238                                 throw new ArgumentException ("key or value aren't of correct type");
239
240                         Add ((TKey)key, (TValue)value);
241                 }
242
243                 bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
244                 {
245                         return ContainsKey (pair.Key);
246                 }
247
248                 public KeyValuePair<TKey,TValue>[] ToArray ()
249                 {
250                         // This is most certainly not optimum but there is
251                         // not a lot of possibilities
252
253                         return new List<KeyValuePair<TKey,TValue>> (this).ToArray ();
254                 }
255
256                 public void Clear()
257                 {
258                         // Pronk
259                         internalDictionary = new SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> (comparer);
260                 }
261
262                 public int Count {
263                         get {
264                                 return internalDictionary.Count;
265                         }
266                 }
267
268                 public bool IsEmpty {
269                         get {
270                                 return Count == 0;
271                         }
272                 }
273
274                 bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
275                         get {
276                                 return false;
277                         }
278                 }
279
280                 bool IDictionary.IsReadOnly {
281                         get {
282                                 return false;
283                         }
284                 }
285
286                 public ICollection<TKey> Keys {
287                         get {
288                                 return GetPart<TKey> ((kvp) => kvp.Key);
289                         }
290                 }
291
292                 public ICollection<TValue> Values {
293                         get {
294                                 return GetPart<TValue> ((kvp) => kvp.Value);
295                         }
296                 }
297
298                 ICollection IDictionary.Keys {
299                         get {
300                                 return (ICollection)Keys;
301                         }
302                 }
303
304                 ICollection IDictionary.Values {
305                         get {
306                                 return (ICollection)Values;
307                         }
308                 }
309
310                 ICollection<T> GetPart<T> (Func<KeyValuePair<TKey, TValue>, T> extractor)
311                 {
312                         List<T> temp = new List<T> ();
313
314                         foreach (KeyValuePair<TKey, TValue> kvp in this)
315                                 temp.Add (extractor (kvp));
316
317                         return temp.AsReadOnly ();
318                 }
319
320                 void ICollection.CopyTo (Array array, int startIndex)
321                 {
322                         KeyValuePair<TKey, TValue>[] arr = array as KeyValuePair<TKey, TValue>[];
323                         if (arr == null)
324                                 return;
325
326                         CopyTo (arr, startIndex, Count);
327                 }
328
329                 void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
330                 {
331                         CopyTo (array, startIndex, Count);
332                 }
333
334                 void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
335                 {
336                         CopyTo (array, startIndex);
337                 }
338
339                 void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex, int num)
340                 {
341                         foreach (var kvp in this) {
342                                 array [startIndex++] = kvp;
343
344                                 if (--num <= 0)
345                                         return;
346                         }
347                 }
348
349                 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
350                 {
351                         return GetEnumeratorInternal ();
352                 }
353
354                 IEnumerator IEnumerable.GetEnumerator ()
355                 {
356                         return (IEnumerator)GetEnumeratorInternal ();
357                 }
358
359                 IEnumerator<KeyValuePair<TKey, TValue>> GetEnumeratorInternal ()
360                 {
361                         return internalDictionary.GetEnumerator ();
362                 }
363
364                 IDictionaryEnumerator IDictionary.GetEnumerator ()
365                 {
366                         return new ConcurrentDictionaryEnumerator (GetEnumeratorInternal ());
367                 }
368
369                 class ConcurrentDictionaryEnumerator : IDictionaryEnumerator
370                 {
371                         IEnumerator<KeyValuePair<TKey, TValue>> internalEnum;
372
373                         public ConcurrentDictionaryEnumerator (IEnumerator<KeyValuePair<TKey, TValue>> internalEnum)
374                         {
375                                 this.internalEnum = internalEnum;
376                         }
377
378                         public bool MoveNext ()
379                         {
380                                 return internalEnum.MoveNext ();
381                         }
382
383                         public void Reset ()
384                         {
385                                 internalEnum.Reset ();
386                         }
387
388                         public object Current {
389                                 get {
390                                         return Entry;
391                                 }
392                         }
393
394                         public DictionaryEntry Entry {
395                                 get {
396                                         KeyValuePair<TKey, TValue> current = internalEnum.Current;
397                                         return new DictionaryEntry (current.Key, current.Value);
398                                 }
399                         }
400
401                         public object Key {
402                                 get {
403                                         return internalEnum.Current.Key;
404                                 }
405                         }
406
407                         public object Value {
408                                 get {
409                                         return internalEnum.Current.Value;
410                                 }
411                         }
412                 }
413
414                 object ICollection.SyncRoot {
415                         get {
416                                 return this;
417                         }
418                 }
419
420                 bool IDictionary.IsFixedSize {
421                         get {
422                                 return false;
423                         }
424                 }
425
426                 bool ICollection.IsSynchronized {
427                         get { return true; }
428                 }
429
430                 static KeyValuePair<U, V> Make<U, V> (U key, V value)
431                 {
432                         return new KeyValuePair<U, V> (key, value);
433                 }
434
435                 uint Hash (TKey key)
436                 {
437                         return (uint)comparer.GetHashCode (key);
438                 }
439         }
440 }
441 #endif