ConcurrentDictionary.cs 9.9 KB

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