Dictionary.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. using System.Runtime.InteropServices;
  40. namespace System.Collections.Generic {
  41. [ComVisible(true)]
  42. [Serializable]
  43. public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>,
  44. IDictionary,
  45. ICollection,
  46. ICollection<KeyValuePair<TKey, TValue>>,
  47. IEnumerable<KeyValuePair<TKey, TValue>>,
  48. ISerializable,
  49. IDeserializationCallback
  50. {
  51. const int INITIAL_SIZE = 10;
  52. const float DEFAULT_LOAD_FACTOR = (90f / 100);
  53. private class Slot {
  54. // Can't use KeyValuePair here since the JIT won't inline the Key/Value accessors
  55. public TKey Key;
  56. public TValue Value;
  57. public Slot Next;
  58. public Slot (TKey Key, TValue Value, Slot Next)
  59. {
  60. this.Key = Key;
  61. this.Value = Value;
  62. this.Next = Next;
  63. }
  64. }
  65. Slot [] table;
  66. int used_slots;
  67. private int threshold;
  68. IEqualityComparer<TKey> hcp;
  69. SerializationInfo serialization_info;
  70. private uint generation;
  71. public int Count {
  72. get { return used_slots; }
  73. /* FIXME: this should be 'private' not 'internal'. */
  74. internal set {
  75. used_slots = value;
  76. ++generation;
  77. }
  78. }
  79. // This is perf critical so inline calls etc.
  80. public TValue this [TKey key] {
  81. get {
  82. if (key == null)
  83. throw new ArgumentNullException ("key");
  84. uint size = (uint)table.Length;
  85. uint i = ((uint)hcp.GetHashCode (key) & Int32.MaxValue) % size;
  86. Slot slot = table [i];
  87. while (slot != null) {
  88. // The ordering is important for compatibility with MS and strange
  89. // Object.Equals () implementations
  90. if (hcp.Equals (slot.Key, key))
  91. return slot.Value;
  92. slot = slot.Next;
  93. }
  94. throw new KeyNotFoundException ();
  95. }
  96. set {
  97. int index;
  98. Slot prev = GetPrev (key, out index);
  99. Slot slot = prev == null ? table [index] : prev.Next;
  100. if (slot == null) {
  101. if (Count++ >= threshold) {
  102. Resize ();
  103. index = DoHash (key);
  104. }
  105. table [index] = new Slot (key, value, table [index]);
  106. } else {
  107. ++generation;
  108. if (prev != null) {
  109. // move-to-front on update
  110. prev.Next = slot.Next;
  111. slot.Next = table [index];
  112. table [index] = slot;
  113. }
  114. slot.Key = key;
  115. slot.Value = value;
  116. }
  117. }
  118. }
  119. public Dictionary ()
  120. {
  121. Init (INITIAL_SIZE, null);
  122. }
  123. public Dictionary (IEqualityComparer<TKey> comparer)
  124. {
  125. Init (INITIAL_SIZE, comparer);
  126. }
  127. public Dictionary (IDictionary<TKey, TValue> dictionary)
  128. : this (dictionary, null)
  129. {
  130. }
  131. public Dictionary (int capacity)
  132. {
  133. Init (capacity, null);
  134. }
  135. public Dictionary (IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
  136. {
  137. if (dictionary == null)
  138. throw new ArgumentNullException ("dictionary");
  139. int capacity = dictionary.Count;
  140. Init (capacity, comparer);
  141. foreach (KeyValuePair<TKey, TValue> entry in dictionary)
  142. this.Add (entry.Key, entry.Value);
  143. }
  144. public Dictionary (int capacity, IEqualityComparer<TKey> comparer)
  145. {
  146. Init (capacity, comparer);
  147. }
  148. protected Dictionary (SerializationInfo info, StreamingContext context)
  149. {
  150. serialization_info = info;
  151. }
  152. void SetThreshold ()
  153. {
  154. threshold = (int)(table.Length * DEFAULT_LOAD_FACTOR);
  155. if (threshold == 0 && table.Length > 0)
  156. threshold = 1;
  157. }
  158. private void Init (int capacity, IEqualityComparer<TKey> hcp)
  159. {
  160. if (capacity < 0)
  161. throw new ArgumentOutOfRangeException ("capacity");
  162. this.hcp = (hcp != null) ? hcp : EqualityComparer<TKey>.Default;
  163. if (capacity == 0)
  164. capacity = INITIAL_SIZE;
  165. /* Modify capacity so 'capacity' elements can be added without resizing */
  166. capacity = (int)(capacity / DEFAULT_LOAD_FACTOR) + 1;
  167. table = new Slot [capacity];
  168. SetThreshold ();
  169. generation = 0;
  170. }
  171. void CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
  172. {
  173. if (array == null)
  174. throw new ArgumentNullException ("array");
  175. if (index < 0)
  176. throw new ArgumentOutOfRangeException ("index");
  177. // we want no exception for index==array.Length && Count == 0
  178. if (index > array.Length)
  179. throw new ArgumentException ("index larger than largest valid index of array");
  180. if (array.Length - index < Count)
  181. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  182. for (int i = 0; i < table.Length; ++i) {
  183. for (Slot slot = table [i]; slot != null; slot = slot.Next)
  184. array [index++] = new KeyValuePair<TKey, TValue> (slot.Key, slot.Value);
  185. }
  186. }
  187. private void Resize ()
  188. {
  189. // From the SDK docs:
  190. // Hashtable is automatically increased
  191. // to the smallest prime number that is larger
  192. // than twice the current number of Hashtable buckets
  193. uint newSize = (uint) Hashtable.ToPrime ((table.Length << 1) | 1);
  194. Slot nextslot = null;
  195. Slot [] oldTable = table;
  196. table = new Slot [newSize];
  197. SetThreshold ();
  198. int index;
  199. for (int i = 0; i < oldTable.Length; i++) {
  200. for (Slot slot = oldTable [i]; slot != null; slot = nextslot) {
  201. nextslot = slot.Next;
  202. index = DoHash (slot.Key);
  203. slot.Next = table [index];
  204. table [index] = slot;
  205. }
  206. }
  207. }
  208. public void Add (TKey key, TValue value)
  209. {
  210. int index;
  211. Slot slot = GetSlot (key, out index);
  212. if (slot != null)
  213. throw new ArgumentException ("An element with the same key already exists in the dictionary.");
  214. DoAdd (index, key, value);
  215. }
  216. void DoAdd (int index, TKey key, TValue value)
  217. {
  218. if (Count++ >= threshold) {
  219. Resize ();
  220. index = DoHash (key);
  221. }
  222. table [index] = new Slot (key, value, table [index]);
  223. }
  224. private int DoHash (TKey key)
  225. {
  226. uint size = (uint)this.table.Length;
  227. uint h = (uint)hcp.GetHashCode (key) & Int32.MaxValue;
  228. int spot = (int) (h % size);
  229. return spot;
  230. }
  231. public IEqualityComparer<TKey> Comparer {
  232. get { return hcp; }
  233. }
  234. public void Clear ()
  235. {
  236. Count = 0;
  237. for (int i = 0; i < table.Length; i++)
  238. table [i] = null;
  239. }
  240. public bool ContainsKey (TKey key)
  241. {
  242. int index;
  243. return GetSlot (key, out index) != null;
  244. }
  245. public bool ContainsValue (TValue value)
  246. {
  247. IEqualityComparer<TValue> cmp = EqualityComparer<TValue>.Default;
  248. for (int i = 0; i < table.Length; ++i) {
  249. for (Slot slot = table [i]; slot != null; slot = slot.Next) {
  250. if (cmp.Equals (slot.Value, value))
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
  257. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  258. {
  259. if (info == null)
  260. throw new ArgumentNullException ("info");
  261. info.AddValue ("hcp", hcp);
  262. KeyValuePair<TKey, TValue> [] data = null;
  263. if (Count > 0) {
  264. data = new KeyValuePair<TKey,TValue> [Count];
  265. CopyTo (data, 0);
  266. }
  267. info.AddValue ("data", data);
  268. info.AddValue ("buckets_hint", table.Length);
  269. }
  270. public virtual void OnDeserialization (object sender)
  271. {
  272. if (serialization_info == null)
  273. return;
  274. hcp = (IEqualityComparer<TKey>) serialization_info.GetValue ("hcp", typeof (IEqualityComparer<TKey>));
  275. KeyValuePair<TKey, TValue> [] data =
  276. (KeyValuePair<TKey, TValue> [])
  277. serialization_info.GetValue ("data", typeof (KeyValuePair<TKey, TValue> []));
  278. int buckets = serialization_info.GetInt32 ("buckets_hint");
  279. if (buckets < INITIAL_SIZE)
  280. buckets = INITIAL_SIZE;
  281. table = new Slot [buckets];
  282. SetThreshold ();
  283. Count = 0;
  284. if (data != null) {
  285. for (int i = 0; i < data.Length; ++i)
  286. Add (data [i].Key, data [i].Value);
  287. }
  288. serialization_info = null;
  289. }
  290. public bool Remove (TKey key)
  291. {
  292. int index;
  293. Slot prev = GetPrev (key, out index);
  294. Slot slot = prev == null ? table [index] : prev.Next;
  295. if (slot == null)
  296. return false;
  297. --Count;
  298. if (prev == null)
  299. table [index] = slot.Next;
  300. else
  301. prev.Next = slot.Next;
  302. return true;
  303. }
  304. //
  305. // Return the predecessor of the slot containing 'key', and set 'index' to the
  306. // chain the key was found in.
  307. // If the key is not found, return null and set 'index' to the chain that would've
  308. // contained the key.
  309. //
  310. private Slot GetPrev (TKey key, out int index)
  311. {
  312. // This method is perf critical so inline DoHash () etc.
  313. if (key == null)
  314. throw new ArgumentNullException ("key");
  315. uint size = (uint)table.Length;
  316. // Use uint to help ABCREM
  317. uint i = ((uint)hcp.GetHashCode (key) & Int32.MaxValue) % size;
  318. Slot slot = table [i];
  319. if (slot != null) {
  320. Slot prev = null;
  321. do {
  322. // The ordering is important for compatibility with MS and strange
  323. // Object.Equals () implementations
  324. if (hcp.Equals (slot.Key, key))
  325. break;
  326. prev = slot;
  327. slot = slot.Next;
  328. } while (slot != null);
  329. index = (int)i;
  330. return prev;
  331. }
  332. else {
  333. index = (int)i;
  334. return null;
  335. }
  336. }
  337. private Slot GetSlot (TKey key, out int index)
  338. {
  339. Slot prev = GetPrev (key, out index);
  340. return prev == null ? table [index] : prev.Next;
  341. }
  342. public bool TryGetValue (TKey key, out TValue value)
  343. {
  344. int index;
  345. Slot slot = GetSlot (key, out index);
  346. bool found = slot != null;
  347. value = found ? slot.Value : default (TValue);
  348. return found;
  349. }
  350. ICollection<TKey> IDictionary<TKey, TValue>.Keys {
  351. get { return Keys; }
  352. }
  353. ICollection<TValue> IDictionary<TKey, TValue>.Values {
  354. get { return Values; }
  355. }
  356. public KeyCollection Keys {
  357. get { return new KeyCollection (this); }
  358. }
  359. public ValueCollection Values {
  360. get { return new ValueCollection (this); }
  361. }
  362. ICollection IDictionary.Keys {
  363. get { return Keys; }
  364. }
  365. ICollection IDictionary.Values {
  366. get { return Values; }
  367. }
  368. bool IDictionary.IsFixedSize {
  369. get { return false; }
  370. }
  371. bool IDictionary.IsReadOnly {
  372. get { return false; }
  373. }
  374. TKey ToTKey (object key)
  375. {
  376. if (key == null)
  377. throw new ArgumentNullException ("key");
  378. if (!(key is TKey))
  379. throw new ArgumentException ("not of type: " + typeof (TKey).ToString (), "key");
  380. return (TKey) key;
  381. }
  382. TValue ToTValue (object value)
  383. {
  384. if (!(value is TValue))
  385. throw new ArgumentException ("not of type: " + typeof (TValue).ToString (), "value");
  386. return (TValue) value;
  387. }
  388. object IDictionary.this [object key] {
  389. get {
  390. if (key is TKey && ContainsKey((TKey) key))
  391. return this [ToTKey (key)];
  392. return null;
  393. }
  394. set { this [ToTKey (key)] = ToTValue (value); }
  395. }
  396. void IDictionary.Add (object key, object value)
  397. {
  398. this.Add (ToTKey (key), ToTValue (value));
  399. }
  400. bool IDictionary.Contains (object key)
  401. {
  402. if (key == null)
  403. throw new ArgumentNullException ("key");
  404. if (key is TKey)
  405. return ContainsKey ((TKey) key);
  406. return false;
  407. }
  408. void IDictionary.Remove (object key)
  409. {
  410. if (key == null)
  411. throw new ArgumentNullException ("key");
  412. if (key is TKey)
  413. Remove ((TKey) key);
  414. }
  415. bool ICollection.IsSynchronized {
  416. get { return false; }
  417. }
  418. object ICollection.SyncRoot {
  419. get { return this; }
  420. }
  421. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
  422. get { return false; }
  423. }
  424. void ICollection<KeyValuePair<TKey, TValue>>.Add (KeyValuePair<TKey, TValue> keyValuePair)
  425. {
  426. Add (keyValuePair.Key, keyValuePair.Value);
  427. }
  428. bool ICollection<KeyValuePair<TKey, TValue>>.Contains (KeyValuePair<TKey, TValue> keyValuePair)
  429. {
  430. return this.ContainsKey (keyValuePair.Key);
  431. }
  432. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
  433. {
  434. this.CopyTo (array, index);
  435. }
  436. bool ICollection<KeyValuePair<TKey, TValue>>.Remove (KeyValuePair<TKey, TValue> keyValuePair)
  437. {
  438. return Remove (keyValuePair.Key);
  439. }
  440. void ICollection.CopyTo (Array array, int index)
  441. {
  442. if (array == null)
  443. throw new ArgumentNullException ("array");
  444. if (index < 0)
  445. throw new ArgumentOutOfRangeException ("index");
  446. // we want no exception for index==array.Length && Count == 0
  447. if (index > array.Length)
  448. throw new ArgumentException ("index larger than largest valid index of array");
  449. if (array.Length - index < Count)
  450. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  451. for (int i = 0; i < table.Length; ++i) {
  452. for (Slot slot = table [i]; slot != null; slot = slot.Next)
  453. array.SetValue (new DictionaryEntry (slot.Key, slot.Value), index++);
  454. }
  455. }
  456. IEnumerator IEnumerable.GetEnumerator ()
  457. {
  458. return new Enumerator (this);
  459. }
  460. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
  461. {
  462. return new Enumerator (this);
  463. }
  464. IDictionaryEnumerator IDictionary.GetEnumerator ()
  465. {
  466. return new ShimEnumerator (this);
  467. }
  468. public Enumerator GetEnumerator ()
  469. {
  470. return new Enumerator (this);
  471. }
  472. [Serializable]
  473. private class ShimEnumerator : IDictionaryEnumerator, IEnumerator
  474. {
  475. Enumerator host_enumerator;
  476. public ShimEnumerator (Dictionary<TKey, TValue> host)
  477. {
  478. host_enumerator = host.GetEnumerator ();
  479. }
  480. public void Dispose ()
  481. {
  482. host_enumerator.Dispose ();
  483. }
  484. public bool MoveNext ()
  485. {
  486. return host_enumerator.MoveNext ();
  487. }
  488. public DictionaryEntry Entry {
  489. get { return ((IDictionaryEnumerator) host_enumerator).Entry; }
  490. }
  491. public object Key {
  492. get { return host_enumerator.Current.Key; }
  493. }
  494. public object Value {
  495. get { return host_enumerator.Current.Value; }
  496. }
  497. // This is the raison d' etre of this $%!@$%@^@ class.
  498. // We want: IDictionary.GetEnumerator ().Current is DictionaryEntry
  499. public object Current {
  500. get { return Entry; }
  501. }
  502. public void Reset ()
  503. {
  504. ((IEnumerator)host_enumerator).Reset ();
  505. }
  506. }
  507. [Serializable]
  508. public struct Enumerator : IEnumerator<KeyValuePair<TKey,TValue>>,
  509. IDisposable, IDictionaryEnumerator, IEnumerator
  510. {
  511. Dictionary<TKey, TValue> dictionary;
  512. uint stamp;
  513. Slot current;
  514. int next_index;
  515. internal Enumerator (Dictionary<TKey, TValue> dictionary)
  516. {
  517. this.dictionary = dictionary;
  518. stamp = dictionary.generation;
  519. // The following stanza is identical to IEnumerator.Reset (),
  520. // but because of the definite assignment rule, we cannot call it here.
  521. next_index = 0;
  522. current = null;
  523. }
  524. public bool MoveNext ()
  525. {
  526. VerifyState ();
  527. // Pre-condition: current == null => this is the first call to MoveNext ()
  528. if (current != null)
  529. current = current.Next;
  530. while (current == null && next_index < dictionary.table.Length)
  531. current = dictionary.table [next_index++];
  532. // Post-condition: current == null => this is the last call to MoveNext()
  533. return current != null;
  534. }
  535. public KeyValuePair<TKey, TValue> Current {
  536. get {
  537. Slot s = CurrentSlot ();
  538. return new KeyValuePair <TKey, TValue> (s.Key, s.Value);
  539. }
  540. }
  541. object IEnumerator.Current {
  542. get { return Current; }
  543. }
  544. void IEnumerator.Reset ()
  545. {
  546. next_index = 0;
  547. current = null;
  548. }
  549. DictionaryEntry IDictionaryEnumerator.Entry {
  550. get {
  551. Slot s = CurrentSlot ();
  552. return new DictionaryEntry (s.Key, s.Value);
  553. }
  554. }
  555. object IDictionaryEnumerator.Key {
  556. get { return Current.Key; }
  557. }
  558. object IDictionaryEnumerator.Value {
  559. get { return Current.Value; }
  560. }
  561. void VerifyState ()
  562. {
  563. if (dictionary == null)
  564. throw new ObjectDisposedException (null);
  565. if (dictionary.generation != stamp)
  566. throw new InvalidOperationException ("out of sync");
  567. }
  568. Slot CurrentSlot ()
  569. {
  570. VerifyState ();
  571. if (current == null)
  572. throw new InvalidOperationException ("Current is not valid");
  573. return current;
  574. }
  575. public void Dispose ()
  576. {
  577. current = null;
  578. dictionary = null;
  579. }
  580. }
  581. // This collection is a read only collection
  582. [Serializable]
  583. public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable {
  584. Dictionary<TKey, TValue> dictionary;
  585. public KeyCollection (Dictionary<TKey, TValue> dictionary)
  586. {
  587. if (dictionary == null)
  588. throw new ArgumentNullException ("dictionary");
  589. this.dictionary = dictionary;
  590. }
  591. public void CopyTo (TKey [] array, int index)
  592. {
  593. if (array == null)
  594. throw new ArgumentNullException ("array");
  595. if (index < 0)
  596. throw new ArgumentOutOfRangeException ("index");
  597. // we want no exception for index==array.Length && dictionary.Count == 0
  598. if (index > array.Length)
  599. throw new ArgumentException ("index larger than largest valid index of array");
  600. if (array.Length - index < dictionary.Count)
  601. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  602. foreach (TKey k in this)
  603. array [index++] = k;
  604. }
  605. public Enumerator GetEnumerator ()
  606. {
  607. return new Enumerator (dictionary);
  608. }
  609. void ICollection<TKey>.Add (TKey item)
  610. {
  611. throw new NotSupportedException ("this is a read-only collection");
  612. }
  613. void ICollection<TKey>.Clear ()
  614. {
  615. throw new NotSupportedException ("this is a read-only collection");
  616. }
  617. bool ICollection<TKey>.Contains (TKey item)
  618. {
  619. return dictionary.ContainsKey (item);
  620. }
  621. bool ICollection<TKey>.Remove (TKey item)
  622. {
  623. throw new NotSupportedException ("this is a read-only collection");
  624. }
  625. IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator ()
  626. {
  627. return this.GetEnumerator ();
  628. }
  629. void ICollection.CopyTo (Array array, int index)
  630. {
  631. CopyTo ((TKey []) array, index);
  632. }
  633. IEnumerator IEnumerable.GetEnumerator ()
  634. {
  635. return this.GetEnumerator ();
  636. }
  637. public int Count {
  638. get { return dictionary.Count; }
  639. }
  640. bool ICollection<TKey>.IsReadOnly {
  641. get { return true; }
  642. }
  643. bool ICollection.IsSynchronized {
  644. get { return false; }
  645. }
  646. object ICollection.SyncRoot {
  647. get { return ((ICollection) dictionary).SyncRoot; }
  648. }
  649. [Serializable]
  650. public struct Enumerator : IEnumerator<TKey>, IDisposable, IEnumerator {
  651. Dictionary<TKey, TValue>.Enumerator host_enumerator;
  652. internal Enumerator (Dictionary<TKey, TValue> host)
  653. {
  654. host_enumerator = host.GetEnumerator ();
  655. }
  656. public void Dispose ()
  657. {
  658. host_enumerator.Dispose ();
  659. }
  660. public bool MoveNext ()
  661. {
  662. return host_enumerator.MoveNext ();
  663. }
  664. public TKey Current {
  665. get { return host_enumerator.Current.Key; }
  666. }
  667. object IEnumerator.Current {
  668. get { return host_enumerator.Current.Key; }
  669. }
  670. void IEnumerator.Reset ()
  671. {
  672. ((IEnumerator)host_enumerator).Reset ();
  673. }
  674. }
  675. }
  676. // This collection is a read only collection
  677. [Serializable]
  678. public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable {
  679. Dictionary<TKey, TValue> dictionary;
  680. public ValueCollection (Dictionary<TKey, TValue> dictionary)
  681. {
  682. if (dictionary == null)
  683. throw new ArgumentNullException ("dictionary");
  684. this.dictionary = dictionary;
  685. }
  686. public void CopyTo (TValue [] array, int index)
  687. {
  688. if (array == null)
  689. throw new ArgumentNullException ("array");
  690. if (index < 0)
  691. throw new ArgumentOutOfRangeException ("index");
  692. // we want no exception for index==array.Length && dictionary.Count == 0
  693. if (index > array.Length)
  694. throw new ArgumentException ("index larger than largest valid index of array");
  695. if (array.Length - index < dictionary.Count)
  696. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  697. foreach (TValue k in this)
  698. array [index++] = k;
  699. }
  700. public Enumerator GetEnumerator ()
  701. {
  702. return new Enumerator (dictionary);
  703. }
  704. void ICollection<TValue>.Add (TValue item)
  705. {
  706. throw new NotSupportedException ("this is a read-only collection");
  707. }
  708. void ICollection<TValue>.Clear ()
  709. {
  710. throw new NotSupportedException ("this is a read-only collection");
  711. }
  712. bool ICollection<TValue>.Contains (TValue item)
  713. {
  714. return dictionary.ContainsValue (item);
  715. }
  716. bool ICollection<TValue>.Remove (TValue item)
  717. {
  718. throw new NotSupportedException ("this is a read-only collection");
  719. }
  720. IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator ()
  721. {
  722. return this.GetEnumerator ();
  723. }
  724. void ICollection.CopyTo (Array array, int index)
  725. {
  726. CopyTo ((TValue []) array, index);
  727. }
  728. IEnumerator IEnumerable.GetEnumerator ()
  729. {
  730. return this.GetEnumerator ();
  731. }
  732. public int Count {
  733. get { return dictionary.Count; }
  734. }
  735. bool ICollection<TValue>.IsReadOnly {
  736. get { return true; }
  737. }
  738. bool ICollection.IsSynchronized {
  739. get { return false; }
  740. }
  741. object ICollection.SyncRoot {
  742. get { return ((ICollection) dictionary).SyncRoot; }
  743. }
  744. [Serializable]
  745. public struct Enumerator : IEnumerator<TValue>, IDisposable, IEnumerator {
  746. Dictionary<TKey, TValue>.Enumerator host_enumerator;
  747. internal Enumerator (Dictionary<TKey,TValue> host)
  748. {
  749. host_enumerator = host.GetEnumerator ();
  750. }
  751. public void Dispose ()
  752. {
  753. host_enumerator.Dispose();
  754. }
  755. public bool MoveNext ()
  756. {
  757. return host_enumerator.MoveNext ();
  758. }
  759. public TValue Current {
  760. get { return host_enumerator.Current.Value; }
  761. }
  762. object IEnumerator.Current {
  763. get { return host_enumerator.Current.Value; }
  764. }
  765. void IEnumerator.Reset ()
  766. {
  767. ((IEnumerator)host_enumerator).Reset ();
  768. }
  769. }
  770. }
  771. }
  772. }
  773. #endif