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