Dictionary.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. //
  2. // System.Collections.Generic.Dictionary
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. // Marek Safar ([email protected]) (stubs)
  7. // Ankit Jain ([email protected])
  8. // David Waite ([email protected])
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. // Copyright (C) 2005 David Waite
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. #if NET_2_0
  34. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using System.Runtime.Serialization;
  38. using System.Security.Permissions;
  39. namespace System.Collections.Generic {
  40. [Serializable]
  41. public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>,
  42. IDictionary,
  43. ICollection,
  44. ICollection<KeyValuePair<TKey, TValue>>,
  45. IEnumerable<KeyValuePair<TKey, TValue>>,
  46. ISerializable,
  47. IDeserializationCallback
  48. {
  49. const int INITIAL_SIZE = 10;
  50. const float DEFAULT_LOAD_FACTOR = (90f / 100);
  51. private class Slot {
  52. public KeyValuePair<TKey, TValue> Data;
  53. public Slot Next;
  54. public Slot (TKey Key, TValue Value, Slot Next)
  55. {
  56. this.Data = new KeyValuePair<TKey,TValue> (Key, Value);
  57. this.Next = Next;
  58. }
  59. }
  60. Slot [] table;
  61. int used_slots;
  62. private int threshold;
  63. IEqualityComparer<TKey> hcp;
  64. SerializationInfo serialization_info;
  65. private uint generation;
  66. public int Count {
  67. get { return used_slots; }
  68. /* FIXME: this should be 'private' not 'internal'. */
  69. internal set {
  70. used_slots = value;
  71. ++generation;
  72. }
  73. }
  74. public TValue this [TKey key] {
  75. get {
  76. int index;
  77. Slot slot = GetSlot (key, out index);
  78. if (slot == null)
  79. throw new KeyNotFoundException ();
  80. return slot.Data.Value;
  81. }
  82. set {
  83. int index;
  84. Slot slot = GetSlot (key, out index);
  85. if (slot == null) {
  86. DoAdd (index, key, value);
  87. } else {
  88. ++generation;
  89. slot.Data = new KeyValuePair<TKey, TValue> (key, value);
  90. }
  91. }
  92. }
  93. public Dictionary ()
  94. {
  95. Init (INITIAL_SIZE, null);
  96. }
  97. public Dictionary (IEqualityComparer<TKey> comparer)
  98. {
  99. Init (INITIAL_SIZE, comparer);
  100. }
  101. public Dictionary (IDictionary<TKey, TValue> dictionary)
  102. : this (dictionary, null)
  103. {
  104. }
  105. public Dictionary (int capacity)
  106. {
  107. Init (capacity, null);
  108. }
  109. public Dictionary (IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
  110. {
  111. if (dictionary == null)
  112. throw new ArgumentNullException ("dictionary");
  113. int capacity = dictionary.Count;
  114. Init (capacity, comparer);
  115. foreach (KeyValuePair<TKey, TValue> entry in dictionary)
  116. this.Add (entry.Key, entry.Value);
  117. }
  118. public Dictionary (int capacity, IEqualityComparer<TKey> comparer)
  119. {
  120. Init (capacity, comparer);
  121. }
  122. protected Dictionary (SerializationInfo info, StreamingContext context)
  123. {
  124. serialization_info = info;
  125. }
  126. void SetThreshold ()
  127. {
  128. threshold = (int)(table.Length * DEFAULT_LOAD_FACTOR);
  129. if (threshold == 0 && table.Length > 0)
  130. threshold = 1;
  131. }
  132. private void Init (int capacity, IEqualityComparer<TKey> hcp)
  133. {
  134. if (capacity < 0)
  135. throw new ArgumentOutOfRangeException ("capacity");
  136. this.hcp = (hcp != null) ? hcp : EqualityComparer<TKey>.Default;
  137. if (capacity == 0)
  138. capacity = INITIAL_SIZE;
  139. table = new Slot [capacity];
  140. SetThreshold ();
  141. generation = 0;
  142. }
  143. void CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
  144. {
  145. if (array == null)
  146. throw new ArgumentNullException ("array");
  147. if (index < 0)
  148. throw new ArgumentOutOfRangeException ("index");
  149. if (index >= array.Length)
  150. throw new ArgumentException ("index larger than largest valid index of array");
  151. if (array.Length - index < Count)
  152. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  153. for (int i = 0; i < table.Length; ++i) {
  154. for (Slot slot = table [i]; slot != null; slot = slot.Next)
  155. array [index++] = slot.Data;
  156. }
  157. }
  158. private void Resize ()
  159. {
  160. // From the SDK docs:
  161. // Hashtable is automatically increased
  162. // to the smallest prime number that is larger
  163. // than twice the current number of Hashtable buckets
  164. uint newSize = (uint) Hashtable.ToPrime ((table.Length << 1) | 1);
  165. Slot nextslot = null;
  166. Slot [] oldTable = table;
  167. table = new Slot [newSize];
  168. SetThreshold ();
  169. int index;
  170. for (int i = 0; i < oldTable.Length; i++) {
  171. for (Slot slot = oldTable [i]; slot != null; slot = nextslot) {
  172. nextslot = slot.Next;
  173. index = DoHash (slot.Data.Key);
  174. slot.Next = table [index];
  175. table [index] = slot;
  176. }
  177. }
  178. }
  179. public void Add (TKey key, TValue value)
  180. {
  181. int index;
  182. Slot slot = GetSlot (key, out index);
  183. if (slot != null)
  184. throw new ArgumentException ("An element with the same key already exists in the dictionary.");
  185. DoAdd (index, key, value);
  186. }
  187. void DoAdd (int index, TKey key, TValue value)
  188. {
  189. if (Count++ >= threshold) {
  190. Resize ();
  191. index = DoHash (key);
  192. }
  193. table [index] = new Slot (key, value, table [index]);
  194. }
  195. private int DoHash (TKey key)
  196. {
  197. int size = this.table.Length;
  198. int h = hcp.GetHashCode (key) & Int32.MaxValue;
  199. int spot = (int) ((uint) h % size);
  200. return spot;
  201. }
  202. public IEqualityComparer<TKey> Comparer {
  203. get { return hcp; }
  204. }
  205. public void Clear ()
  206. {
  207. Count = 0;
  208. for (int i = 0; i < table.Length; i++)
  209. table [i] = null;
  210. }
  211. public bool ContainsKey (TKey key)
  212. {
  213. int index;
  214. return GetSlot (key, out index) != null;
  215. }
  216. public bool ContainsValue (TValue value)
  217. {
  218. IEqualityComparer<TValue> cmp = EqualityComparer<TValue>.Default;
  219. for (int i = 0; i < table.Length; ++i) {
  220. for (Slot slot = table [i]; slot != null; slot = slot.Next) {
  221. if (cmp.Equals (value, slot.Data.Value))
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
  228. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  229. {
  230. if (info == null)
  231. throw new ArgumentNullException ("info");
  232. info.AddValue ("hcp", hcp);
  233. KeyValuePair<TKey, TValue> [] data = null;
  234. if (Count > 0) {
  235. data = new KeyValuePair<TKey,TValue> [Count];
  236. CopyTo (data, 0);
  237. }
  238. info.AddValue ("data", data);
  239. info.AddValue ("buckets_hint", table.Length);
  240. }
  241. public virtual void OnDeserialization (object sender)
  242. {
  243. if (serialization_info == null)
  244. return;
  245. hcp = (IEqualityComparer<TKey>) serialization_info.GetValue ("hcp", typeof (IEqualityComparer<TKey>));
  246. KeyValuePair<TKey, TValue> [] data =
  247. (KeyValuePair<TKey, TValue> [])
  248. serialization_info.GetValue ("data", typeof (KeyValuePair<TKey, TValue> []));
  249. int buckets = serialization_info.GetInt32 ("buckets_hint");
  250. if (buckets < INITIAL_SIZE)
  251. buckets = INITIAL_SIZE;
  252. table = new Slot [buckets];
  253. SetThreshold ();
  254. Count = 0;
  255. if (data != null) {
  256. for (int i = 0; i < data.Length; ++i)
  257. Add (data [i].Key, data [i].Value);
  258. }
  259. serialization_info = null;
  260. }
  261. public bool Remove (TKey key)
  262. {
  263. int index;
  264. Slot slot = GetSlot (key, out index);
  265. if (slot == null)
  266. return false;
  267. --Count;
  268. if (slot == table [index]) {
  269. table [index] = table [index].Next;
  270. } else {
  271. Slot prev = table [index];
  272. while (prev.Next != slot)
  273. prev = prev.Next;
  274. prev.Next = slot.Next;
  275. }
  276. return true;
  277. }
  278. //
  279. // Return the slot containing key, and set 'index' to the chain the key was found in.
  280. // If the key is not found, return null and set 'index' to the chain that would've contained the key.
  281. //
  282. private Slot GetSlot (TKey key, out int index)
  283. {
  284. if (key == null)
  285. throw new ArgumentNullException ("key");
  286. index = DoHash (key);
  287. Slot slot = table [index];
  288. while (slot != null && !hcp.Equals (key, slot.Data.Key))
  289. slot = slot.Next;
  290. return slot;
  291. }
  292. public bool TryGetValue (TKey key, out TValue value)
  293. {
  294. int index;
  295. Slot slot = GetSlot (key, out index);
  296. bool found = slot != null;
  297. value = found ? slot.Data.Value : default (TValue);
  298. return found;
  299. }
  300. ICollection<TKey> IDictionary<TKey, TValue>.Keys {
  301. get { return Keys; }
  302. }
  303. ICollection<TValue> IDictionary<TKey, TValue>.Values {
  304. get { return Values; }
  305. }
  306. public KeyCollection Keys {
  307. get { return new KeyCollection (this); }
  308. }
  309. public ValueCollection Values {
  310. get { return new ValueCollection (this); }
  311. }
  312. ICollection IDictionary.Keys {
  313. get { return Keys; }
  314. }
  315. ICollection IDictionary.Values {
  316. get { return Values; }
  317. }
  318. bool IDictionary.IsFixedSize {
  319. get { return false; }
  320. }
  321. bool IDictionary.IsReadOnly {
  322. get { return false; }
  323. }
  324. object IDictionary.this [object key] {
  325. get {
  326. if (!(key is TKey))
  327. throw new ArgumentException ("key is of not '" + typeof (TKey).ToString () + "'!");
  328. return this [(TKey) key];
  329. }
  330. set { this [(TKey) key] = (TValue) value; }
  331. }
  332. void IDictionary.Add (object key, object value)
  333. {
  334. if (!(key is TKey))
  335. throw new ArgumentException ("key is of not '" + typeof (TKey).ToString () + "'!");
  336. if (!(value is TValue))
  337. throw new ArgumentException ("value is of not '" + typeof (TValue).ToString () + "'!");
  338. this.Add ((TKey) key, (TValue) value);
  339. }
  340. bool IDictionary.Contains (object key)
  341. {
  342. return ContainsKey ((TKey) key);
  343. }
  344. void IDictionary.Remove (object key)
  345. {
  346. Remove ((TKey) key);
  347. }
  348. bool ICollection.IsSynchronized {
  349. get { return false; }
  350. }
  351. object ICollection.SyncRoot {
  352. get { return this; }
  353. }
  354. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
  355. get { return false; }
  356. }
  357. void ICollection<KeyValuePair<TKey, TValue>>.Add (KeyValuePair<TKey, TValue> keyValuePair)
  358. {
  359. Add (keyValuePair.Key, keyValuePair.Value);
  360. }
  361. bool ICollection<KeyValuePair<TKey, TValue>>.Contains (KeyValuePair<TKey, TValue> keyValuePair)
  362. {
  363. return this.ContainsKey (keyValuePair.Key);
  364. }
  365. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
  366. {
  367. this.CopyTo (array, index);
  368. }
  369. bool ICollection<KeyValuePair<TKey, TValue>>.Remove (KeyValuePair<TKey, TValue> keyValuePair)
  370. {
  371. return Remove (keyValuePair.Key);
  372. }
  373. void ICollection.CopyTo (Array array, int index)
  374. {
  375. // TODO: Verify this can be a KeyValuePair, and doesn't need to be
  376. // a DictionaryEntry type
  377. CopyTo ((KeyValuePair<TKey, TValue> []) array, index);
  378. }
  379. IEnumerator IEnumerable.GetEnumerator ()
  380. {
  381. return new Enumerator (this);
  382. }
  383. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
  384. {
  385. return new Enumerator (this);
  386. }
  387. IDictionaryEnumerator IDictionary.GetEnumerator ()
  388. {
  389. return new Enumerator (this);
  390. }
  391. public Enumerator GetEnumerator ()
  392. {
  393. return new Enumerator (this);
  394. }
  395. [Serializable]
  396. public struct Enumerator : IEnumerator<KeyValuePair<TKey,TValue>>,
  397. IDisposable, IDictionaryEnumerator, IEnumerator
  398. {
  399. Dictionary<TKey, TValue> dictionary;
  400. uint stamp;
  401. Slot current;
  402. int next_index;
  403. internal Enumerator (Dictionary<TKey, TValue> dictionary)
  404. {
  405. this.dictionary = dictionary;
  406. stamp = dictionary.generation;
  407. // The following stanza is identical to IEnumerator.Reset (),
  408. // but because of the definite assignment rule, we cannot call it here.
  409. next_index = 0;
  410. current = null;
  411. }
  412. public bool MoveNext ()
  413. {
  414. if (dictionary == null)
  415. throw new ObjectDisposedException (null);
  416. if (dictionary.generation != stamp)
  417. throw new InvalidOperationException ("out of sync");
  418. // Pre-condition: current == null => this is the first call
  419. // to MoveNext ()
  420. if (current != null)
  421. current = current.Next;
  422. while (current == null && next_index < dictionary.table.Length)
  423. current = dictionary.table [next_index++];
  424. // Post-condition: current == null => this is the last call
  425. // to MoveNext()
  426. return current != null;
  427. }
  428. public KeyValuePair<TKey, TValue> Current {
  429. get {
  430. VerifyState ();
  431. return current.Data;
  432. }
  433. }
  434. object IEnumerator.Current {
  435. get { return ((IDictionaryEnumerator) this).Entry; }
  436. }
  437. void IEnumerator.Reset ()
  438. {
  439. next_index = 0;
  440. current = null;
  441. }
  442. DictionaryEntry IDictionaryEnumerator.Entry {
  443. get {
  444. VerifyState ();
  445. return new DictionaryEntry (current.Data.Key, current.Data.Value);
  446. }
  447. }
  448. object IDictionaryEnumerator.Key {
  449. get { return Current.Key; }
  450. }
  451. object IDictionaryEnumerator.Value {
  452. get { return Current.Value; }
  453. }
  454. void VerifyState ()
  455. {
  456. if (dictionary == null)
  457. throw new ObjectDisposedException (null);
  458. if (dictionary.generation != stamp)
  459. throw new InvalidOperationException ("out of sync");
  460. if (current == null)
  461. throw new InvalidOperationException ();
  462. }
  463. public void Dispose ()
  464. {
  465. current = null;
  466. dictionary = null;
  467. }
  468. }
  469. // This collection is a read only collection
  470. [Serializable]
  471. public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable {
  472. Dictionary<TKey, TValue> dictionary;
  473. public KeyCollection (Dictionary<TKey, TValue> dictionary)
  474. {
  475. if (dictionary == null)
  476. throw new ArgumentNullException ("dictionary");
  477. this.dictionary = dictionary;
  478. }
  479. public void CopyTo (TKey [] array, int index)
  480. {
  481. if (array == null)
  482. throw new ArgumentNullException ("array");
  483. if (index < 0)
  484. throw new ArgumentOutOfRangeException ("index");
  485. if (index >= array.Length)
  486. throw new ArgumentException ("index larger than largest valid index of array");
  487. if (array.Length - index < dictionary.Count)
  488. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  489. foreach (TKey k in this)
  490. array [index++] = k;
  491. }
  492. public Enumerator GetEnumerator ()
  493. {
  494. return new Enumerator (dictionary);
  495. }
  496. void ICollection<TKey>.Add (TKey item)
  497. {
  498. throw new NotSupportedException ("this is a read-only collection");
  499. }
  500. void ICollection<TKey>.Clear ()
  501. {
  502. throw new NotSupportedException ("this is a read-only collection");
  503. }
  504. bool ICollection<TKey>.Contains (TKey item)
  505. {
  506. return dictionary.ContainsKey (item);
  507. }
  508. bool ICollection<TKey>.Remove (TKey item)
  509. {
  510. throw new NotSupportedException ("this is a read-only collection");
  511. }
  512. IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator ()
  513. {
  514. return this.GetEnumerator ();
  515. }
  516. void ICollection.CopyTo (Array array, int index)
  517. {
  518. CopyTo ((TKey []) array, index);
  519. }
  520. IEnumerator IEnumerable.GetEnumerator ()
  521. {
  522. return this.GetEnumerator ();
  523. }
  524. public int Count {
  525. get { return dictionary.Count; }
  526. }
  527. bool ICollection<TKey>.IsReadOnly {
  528. get { return true; }
  529. }
  530. bool ICollection.IsSynchronized {
  531. get { return false; }
  532. }
  533. object ICollection.SyncRoot {
  534. get { return ((ICollection) dictionary).SyncRoot; }
  535. }
  536. public struct Enumerator : IEnumerator<TKey>, IDisposable, IEnumerator {
  537. Dictionary<TKey, TValue>.Enumerator host_enumerator;
  538. internal Enumerator (Dictionary<TKey, TValue> host)
  539. {
  540. host_enumerator = host.GetEnumerator ();
  541. }
  542. public void Dispose ()
  543. {
  544. host_enumerator.Dispose ();
  545. }
  546. public bool MoveNext ()
  547. {
  548. return host_enumerator.MoveNext ();
  549. }
  550. public TKey Current {
  551. get { return host_enumerator.Current.Key; }
  552. }
  553. object IEnumerator.Current {
  554. get { return host_enumerator.Current.Key; }
  555. }
  556. void IEnumerator.Reset ()
  557. {
  558. ((IEnumerator)host_enumerator).Reset ();
  559. }
  560. }
  561. }
  562. // This collection is a read only collection
  563. [Serializable]
  564. public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable {
  565. Dictionary<TKey, TValue> dictionary;
  566. public ValueCollection (Dictionary<TKey, TValue> dictionary)
  567. {
  568. if (dictionary == null)
  569. throw new ArgumentNullException ("dictionary");
  570. this.dictionary = dictionary;
  571. }
  572. public void CopyTo (TValue [] array, int index)
  573. {
  574. if (array == null)
  575. throw new ArgumentNullException ("array");
  576. if (index < 0)
  577. throw new ArgumentOutOfRangeException ("index");
  578. if (index >= array.Length)
  579. throw new ArgumentException ("index larger than largest valid index of array");
  580. if (array.Length - index < dictionary.Count)
  581. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  582. foreach (TValue k in this)
  583. array [index++] = k;
  584. }
  585. public Enumerator GetEnumerator ()
  586. {
  587. return new Enumerator (dictionary);
  588. }
  589. void ICollection<TValue>.Add (TValue item)
  590. {
  591. throw new NotSupportedException ("this is a read-only collection");
  592. }
  593. void ICollection<TValue>.Clear ()
  594. {
  595. throw new NotSupportedException ("this is a read-only collection");
  596. }
  597. bool ICollection<TValue>.Contains (TValue item)
  598. {
  599. return dictionary.ContainsValue (item);
  600. }
  601. bool ICollection<TValue>.Remove (TValue item)
  602. {
  603. throw new NotSupportedException ("this is a read-only collection");
  604. }
  605. IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator ()
  606. {
  607. return this.GetEnumerator ();
  608. }
  609. void ICollection.CopyTo (Array array, int index)
  610. {
  611. CopyTo ((TValue []) array, index);
  612. }
  613. IEnumerator IEnumerable.GetEnumerator ()
  614. {
  615. return this.GetEnumerator ();
  616. }
  617. public int Count {
  618. get { return dictionary.Count; }
  619. }
  620. bool ICollection<TValue>.IsReadOnly {
  621. get { return true; }
  622. }
  623. bool ICollection.IsSynchronized {
  624. get { return false; }
  625. }
  626. object ICollection.SyncRoot {
  627. get { return ((ICollection) dictionary).SyncRoot; }
  628. }
  629. public struct Enumerator : IEnumerator<TValue>, IDisposable, IEnumerator {
  630. Dictionary<TKey, TValue>.Enumerator host_enumerator;
  631. internal Enumerator (Dictionary<TKey,TValue> host)
  632. {
  633. host_enumerator = host.GetEnumerator ();
  634. }
  635. public void Dispose ()
  636. {
  637. host_enumerator.Dispose();
  638. }
  639. public bool MoveNext ()
  640. {
  641. return host_enumerator.MoveNext ();
  642. }
  643. public TValue Current {
  644. get { return host_enumerator.Current.Value; }
  645. }
  646. object IEnumerator.Current {
  647. get { return host_enumerator.Current.Value; }
  648. }
  649. void IEnumerator.Reset ()
  650. {
  651. ((IEnumerator)host_enumerator).Reset ();
  652. }
  653. }
  654. }
  655. }
  656. }
  657. #endif