ConcurrentDictionary.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #if NET_4_0
  2. // ConcurrentSkipList.cs
  3. //
  4. // Copyright (c) 2009 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  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. class Pair
  37. {
  38. public readonly TKey Key;
  39. public TValue Value;
  40. public Pair (TKey key, TValue value)
  41. {
  42. Key = key;
  43. Value = value;
  44. }
  45. public override bool Equals (object obj)
  46. {
  47. Pair rhs = obj as Pair;
  48. return rhs == null ? false : Key.Equals (rhs.Key) && Value.Equals (rhs.Value);
  49. }
  50. public override int GetHashCode ()
  51. {
  52. return Key.GetHashCode ();
  53. }
  54. }
  55. class Basket: List<Pair>
  56. {
  57. }
  58. // Assumption: a List<T> is never empty
  59. ConcurrentSkipList<Basket> container
  60. = new ConcurrentSkipList<Basket> ((value) => value[0].GetHashCode ());
  61. int count;
  62. IEqualityComparer<TKey> comparer;
  63. public ConcurrentDictionary () : this (EqualityComparer<TKey>.Default)
  64. {
  65. }
  66. public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> values)
  67. : this (values, EqualityComparer<TKey>.Default)
  68. {
  69. foreach (KeyValuePair<TKey, TValue> pair in values)
  70. Add (pair.Key, pair.Value);
  71. }
  72. public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
  73. {
  74. this.comparer = comparer;
  75. }
  76. public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> values, IEqualityComparer<TKey> comparer)
  77. : this (comparer)
  78. {
  79. foreach (KeyValuePair<TKey, TValue> pair in values)
  80. Add (pair.Key, pair.Value);
  81. }
  82. // Parameters unused
  83. public ConcurrentDictionary (int concurrencyLevel, int capacity)
  84. : this (EqualityComparer<TKey>.Default)
  85. {
  86. }
  87. public ConcurrentDictionary (int concurrencyLevel,
  88. IEnumerable<KeyValuePair<TKey, TValue>> values,
  89. IEqualityComparer<TKey> comparer)
  90. : this (values, comparer)
  91. {
  92. }
  93. // Parameters unused
  94. public ConcurrentDictionary (int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer)
  95. : this (comparer)
  96. {
  97. }
  98. void Add (TKey key, TValue value)
  99. {
  100. while (!TryAdd (key, value));
  101. }
  102. void IDictionary<TKey, TValue>.Add (TKey key, TValue value)
  103. {
  104. Add (key, value);
  105. }
  106. public bool TryAdd (TKey key, TValue value)
  107. {
  108. Basket basket;
  109. // Add a value to an existing basket
  110. if (TryGetBasket (key, out basket)) {
  111. // Find a maybe more sexy locking scheme later
  112. lock (basket) {
  113. foreach (var p in basket) {
  114. if (comparer.Equals (p.Key, key))
  115. throw new ArgumentException ("An element with the same key already exists");
  116. }
  117. basket.Add (new Pair (key, value));
  118. }
  119. } else {
  120. // Add a new basket
  121. basket = new Basket ();
  122. basket.Add (new Pair (key, value));
  123. if (container.TryAdd (basket)) {
  124. Interlocked.Increment (ref count);
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. Interlocked.Increment (ref count);
  131. return true;
  132. }
  133. void ICollection<KeyValuePair<TKey,TValue>>.Add (KeyValuePair<TKey, TValue> pair)
  134. {
  135. Add (pair.Key, pair.Value);
  136. }
  137. public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  138. {
  139. Basket basket;
  140. TValue temp;
  141. if (!TryGetBasket (key, out basket)) {
  142. Add (key, (temp = addValueFactory (key)));
  143. } else {
  144. lock (basket) {
  145. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  146. if (pair == null)
  147. throw new InvalidOperationException ("pair is null, shouldn't be");
  148. pair.Value = (temp = updateValueFactory (key, pair.Value));
  149. }
  150. }
  151. return temp;
  152. }
  153. public TValue AddOrUpdate (TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  154. {
  155. return AddOrUpdate (key, (_) => addValue, updateValueFactory);
  156. }
  157. TValue GetValue (TKey key)
  158. {
  159. TValue temp;
  160. if (!TryGetValue (key, out temp))
  161. // TODO: find a correct Exception
  162. throw new ArgumentException ("Not a valid key for this dictionary", "key");
  163. return temp;
  164. }
  165. public bool TryGetValue (TKey key, out TValue value)
  166. {
  167. Basket basket;
  168. value = default (TValue);
  169. if (!TryGetBasket (key, out basket))
  170. return false;
  171. lock (basket) {
  172. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  173. if (pair == null)
  174. return false;
  175. value = pair.Value;
  176. }
  177. return true;
  178. }
  179. public bool TryUpdate (TKey key, TValue newValue, TValue comparand)
  180. {
  181. Basket basket;
  182. if (!TryGetBasket (key, out basket))
  183. return false;
  184. lock (basket) {
  185. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  186. if (pair.Value.Equals (comparand)) {
  187. pair.Value = newValue;
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. public TValue this[TKey key] {
  194. get {
  195. return GetValue (key);
  196. }
  197. set {
  198. Basket basket;
  199. if (!TryGetBasket (key, out basket)) {
  200. Add (key, value);
  201. return;
  202. }
  203. lock (basket) {
  204. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  205. if (pair == null)
  206. throw new InvalidOperationException ("pair is null, shouldn't be");
  207. pair.Value = value;
  208. }
  209. }
  210. }
  211. public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
  212. {
  213. Basket basket;
  214. TValue temp = default (TValue);
  215. if (TryGetBasket (key, out basket)) {
  216. Pair pair = null;
  217. lock (basket) {
  218. pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  219. if (pair != null)
  220. temp = pair.Value;
  221. }
  222. if (pair == null)
  223. Add (key, (temp = valueFactory (key)));
  224. } else {
  225. Add (key, (temp = valueFactory (key)));
  226. }
  227. return temp;
  228. }
  229. public TValue GetOrAdd (TKey key, TValue value)
  230. {
  231. return GetOrAdd (key, (_) => value);
  232. }
  233. public bool TryRemove(TKey key, out TValue value)
  234. {
  235. value = default (TValue);
  236. Basket b;
  237. if (!TryGetBasket (key, out b))
  238. return false;
  239. lock (b) {
  240. TValue temp = default (TValue);
  241. // Should always be == 1 but who know
  242. bool result = b.RemoveAll ((p) => {
  243. bool r = comparer.Equals (p.Key, key);
  244. if (r) temp = p.Value;
  245. return r;
  246. }) >= 1;
  247. value = temp;
  248. if (result)
  249. Interlocked.Decrement (ref count);
  250. return result;
  251. }
  252. }
  253. bool Remove (TKey key)
  254. {
  255. TValue dummy;
  256. return TryRemove (key, out dummy);
  257. }
  258. bool IDictionary<TKey, TValue>.Remove (TKey key)
  259. {
  260. return Remove (key);
  261. }
  262. bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> pair)
  263. {
  264. return Remove (pair.Key);
  265. }
  266. public bool ContainsKey (TKey key)
  267. {
  268. return container.ContainsFromHash (key.GetHashCode ());
  269. }
  270. bool IDictionary.Contains (object key)
  271. {
  272. if (!(key is TKey))
  273. return false;
  274. return ContainsKey ((TKey)key);
  275. }
  276. void IDictionary.Remove (object key)
  277. {
  278. if (!(key is TKey))
  279. return;
  280. Remove ((TKey)key);
  281. }
  282. object IDictionary.this [object key]
  283. {
  284. get {
  285. if (!(key is TKey))
  286. throw new ArgumentException ("key isn't of correct type", "key");
  287. return this[(TKey)key];
  288. }
  289. set {
  290. if (!(key is TKey) || !(value is TValue))
  291. throw new ArgumentException ("key or value aren't of correct type");
  292. this[(TKey)key] = (TValue)value;
  293. }
  294. }
  295. void IDictionary.Add (object key, object value)
  296. {
  297. if (!(key is TKey) || !(value is TValue))
  298. throw new ArgumentException ("key or value aren't of correct type");
  299. Add ((TKey)key, (TValue)value);
  300. }
  301. bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
  302. {
  303. return ContainsKey (pair.Key);
  304. }
  305. public KeyValuePair<TKey,TValue>[] ToArray ()
  306. {
  307. // This is most certainly not optimum but there is
  308. // not a lot of possibilities
  309. return new List<KeyValuePair<TKey,TValue>> (this).ToArray ();
  310. }
  311. public void Clear()
  312. {
  313. // Pronk
  314. container = new ConcurrentSkipList<Basket> ((value) => value [0].GetHashCode ());
  315. }
  316. public int Count {
  317. get {
  318. return count;
  319. }
  320. }
  321. public bool IsEmpty {
  322. get {
  323. return count == 0;
  324. }
  325. }
  326. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
  327. get {
  328. return false;
  329. }
  330. }
  331. bool IDictionary.IsReadOnly {
  332. get {
  333. return false;
  334. }
  335. }
  336. public ICollection<TKey> Keys {
  337. get {
  338. return GetPart<TKey> ((kvp) => kvp.Key);
  339. }
  340. }
  341. public ICollection<TValue> Values {
  342. get {
  343. return GetPart<TValue> ((kvp) => kvp.Value);
  344. }
  345. }
  346. ICollection IDictionary.Keys {
  347. get {
  348. return (ICollection)Keys;
  349. }
  350. }
  351. ICollection IDictionary.Values {
  352. get {
  353. return (ICollection)Values;
  354. }
  355. }
  356. ICollection<T> GetPart<T> (Func<KeyValuePair<TKey, TValue>, T> extractor)
  357. {
  358. List<T> temp = new List<T> ();
  359. foreach (KeyValuePair<TKey, TValue> kvp in this)
  360. temp.Add (extractor (kvp));
  361. return temp.AsReadOnly ();
  362. }
  363. void ICollection.CopyTo (Array array, int startIndex)
  364. {
  365. KeyValuePair<TKey, TValue>[] arr = array as KeyValuePair<TKey, TValue>[];
  366. if (arr == null)
  367. return;
  368. CopyTo (arr, startIndex, count);
  369. }
  370. void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
  371. {
  372. CopyTo (array, startIndex, count);
  373. }
  374. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
  375. {
  376. CopyTo (array, startIndex);
  377. }
  378. void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex, int num)
  379. {
  380. // TODO: This is quite unsafe as the count value will likely change during
  381. // the copying. Watchout for IndexOutOfRange thingies
  382. if (array.Length <= count + startIndex)
  383. throw new InvalidOperationException ("The array isn't big enough");
  384. int i = startIndex;
  385. foreach (Basket b in container) {
  386. lock (b) {
  387. foreach (Pair p in b) {
  388. if (i >= num)
  389. break;
  390. array[i++] = new KeyValuePair<TKey, TValue> (p.Key, p.Value);
  391. }
  392. }
  393. }
  394. }
  395. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
  396. {
  397. return GetEnumeratorInternal ();
  398. }
  399. IEnumerator IEnumerable.GetEnumerator ()
  400. {
  401. return (IEnumerator)GetEnumeratorInternal ();
  402. }
  403. IEnumerator<KeyValuePair<TKey, TValue>> GetEnumeratorInternal ()
  404. {
  405. foreach (Basket b in container) {
  406. lock (b) {
  407. foreach (Pair p in b)
  408. yield return new KeyValuePair<TKey, TValue> (p.Key, p.Value);
  409. }
  410. }
  411. }
  412. IDictionaryEnumerator IDictionary.GetEnumerator ()
  413. {
  414. return new ConcurrentDictionaryEnumerator (GetEnumeratorInternal ());
  415. }
  416. class ConcurrentDictionaryEnumerator : IDictionaryEnumerator
  417. {
  418. IEnumerator<KeyValuePair<TKey, TValue>> internalEnum;
  419. public ConcurrentDictionaryEnumerator (IEnumerator<KeyValuePair<TKey, TValue>> internalEnum)
  420. {
  421. this.internalEnum = internalEnum;
  422. }
  423. public bool MoveNext ()
  424. {
  425. return internalEnum.MoveNext ();
  426. }
  427. public void Reset ()
  428. {
  429. internalEnum.Reset ();
  430. }
  431. public object Current {
  432. get {
  433. return Entry;
  434. }
  435. }
  436. public DictionaryEntry Entry {
  437. get {
  438. KeyValuePair<TKey, TValue> current = internalEnum.Current;
  439. return new DictionaryEntry (current.Key, current.Value);
  440. }
  441. }
  442. public object Key {
  443. get {
  444. return internalEnum.Current.Key;
  445. }
  446. }
  447. public object Value {
  448. get {
  449. return internalEnum.Current.Value;
  450. }
  451. }
  452. }
  453. object ICollection.SyncRoot {
  454. get {
  455. return this;
  456. }
  457. }
  458. bool IDictionary.IsFixedSize {
  459. get {
  460. return false;
  461. }
  462. }
  463. bool ICollection.IsSynchronized {
  464. get { return true; }
  465. }
  466. bool TryGetBasket (TKey key, out Basket basket)
  467. {
  468. basket = null;
  469. if (!container.GetFromHash (key.GetHashCode (), out basket))
  470. return false;
  471. return true;
  472. }
  473. }
  474. }
  475. #endif