ConcurrentDictionary.cs 11 KB

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