Hashtable.cs 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. //
  2. // System.Collections.Hashtable
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Runtime.Serialization;
  10. namespace System.Collections {
  11. [Serializable]
  12. public class Hashtable : IDictionary, ICollection,
  13. IEnumerable, ICloneable, ISerializable, IDeserializationCallback
  14. {
  15. [Serializable]
  16. internal struct Slot {
  17. internal Object key;
  18. internal Object value;
  19. // Hashcode. Chains are also marked through this.
  20. internal int hashMix;
  21. }
  22. [Serializable]
  23. internal class KeyMarker: IObjectReference
  24. {
  25. public static KeyMarker Removed = new KeyMarker();
  26. public object GetRealObject (StreamingContext context)
  27. { return KeyMarker.Removed; }
  28. }
  29. //
  30. // Private data
  31. //
  32. private readonly static int CHAIN_MARKER = ~Int32.MaxValue;
  33. private int inUse;
  34. private int modificationCount;
  35. private float loadFactor;
  36. private Slot [] table;
  37. private int threshold;
  38. private IHashCodeProvider hcpRef;
  39. private IComparer comparerRef;
  40. private static int [] primeTbl = {
  41. 11,
  42. 19,
  43. 37,
  44. 73,
  45. 109,
  46. 163,
  47. 251,
  48. 367,
  49. 557,
  50. 823,
  51. 1237,
  52. 1861,
  53. 2777,
  54. 4177,
  55. 6247,
  56. 9371,
  57. 14057,
  58. 21089,
  59. 31627,
  60. 47431,
  61. 71143,
  62. 106721,
  63. 160073,
  64. 240101,
  65. 360163,
  66. 540217,
  67. 810343,
  68. 1215497,
  69. 1823231,
  70. 2734867,
  71. 4102283,
  72. 6153409,
  73. 9230113,
  74. 13845163
  75. };
  76. // Class constructor
  77. static Hashtable () {
  78. // NOTE: previously this static constructor was used
  79. // to calculate primeTbl, now primeTbl is
  80. // hardcoded and constructor does nothing
  81. // useful except for forcing compiler to
  82. // eliminate beforefieldinit from signature.
  83. }
  84. //
  85. // Constructors
  86. //
  87. public Hashtable () : this (0, 1.0f) {}
  88. public Hashtable (int capacity, float loadFactor, IHashCodeProvider hcp, IComparer comparer) {
  89. if (capacity<0)
  90. throw new ArgumentOutOfRangeException ("negative capacity");
  91. if (loadFactor < 0.1f || loadFactor > 1.0f || Single.IsNaN (loadFactor))
  92. throw new ArgumentOutOfRangeException ("load factor");
  93. if (capacity == 0) ++capacity;
  94. this.loadFactor = 0.75f*loadFactor;
  95. double tableSize = capacity / this.loadFactor;
  96. if (tableSize > Int32.MaxValue)
  97. throw new ArgumentException ("Size is too big");
  98. int size = (int) tableSize;
  99. size = ToPrime (size);
  100. this.SetTable (new Slot [size]);
  101. this.hcp = hcp;
  102. this.comparer = comparer;
  103. this.inUse = 0;
  104. this.modificationCount = 0;
  105. }
  106. public Hashtable (int capacity, float loadFactor) :
  107. this (capacity, loadFactor, null, null)
  108. {
  109. }
  110. public Hashtable (int capacity) : this (capacity, 1.0f)
  111. {
  112. }
  113. public Hashtable (int capacity,
  114. IHashCodeProvider hcp,
  115. IComparer comparer)
  116. : this (capacity, 1.0f, hcp, comparer)
  117. {
  118. }
  119. public Hashtable (IDictionary d, float loadFactor,
  120. IHashCodeProvider hcp, IComparer comparer)
  121. : this (d!=null ? d.Count : 0,
  122. loadFactor, hcp, comparer)
  123. {
  124. if (d == null)
  125. throw new ArgumentNullException ("dictionary");
  126. IDictionaryEnumerator it = d.GetEnumerator ();
  127. while (it.MoveNext ()) {
  128. Add (it.Key, it.Value);
  129. }
  130. }
  131. public Hashtable (IDictionary d, float loadFactor)
  132. : this (d, loadFactor, null, null)
  133. {
  134. }
  135. public Hashtable (IDictionary d) : this (d, 1.0f)
  136. {
  137. }
  138. public Hashtable (IDictionary d, IHashCodeProvider hcp, IComparer comparer)
  139. : this (d, 1.0f, hcp, comparer)
  140. {
  141. }
  142. public Hashtable (IHashCodeProvider hcp, IComparer comparer)
  143. : this (1, 1.0f, hcp, comparer)
  144. {
  145. }
  146. protected Hashtable (SerializationInfo info, StreamingContext context)
  147. {
  148. loadFactor = (float) info.GetValue ("LoadFactor", typeof(float));
  149. modificationCount = (int) info.GetValue ("Version", typeof(int));
  150. comparerRef = (IComparer) info.GetValue ("Comparer", typeof (object));
  151. hcpRef = (IHashCodeProvider) info.GetValue ("HashCodeProvider", typeof (object));
  152. inUse = (int) info.GetValue ("HashSize", typeof(int));
  153. table = (Slot[]) info.GetValue("Table", typeof(Slot[]));
  154. threshold = (int) info.GetValue("Treshold", typeof(int));
  155. }
  156. //
  157. // Properties
  158. //
  159. protected IComparer comparer {
  160. set {
  161. comparerRef = value;
  162. }
  163. get {
  164. return comparerRef;
  165. }
  166. }
  167. protected IHashCodeProvider hcp {
  168. set {
  169. hcpRef = value;
  170. }
  171. get {
  172. return hcpRef;
  173. }
  174. }
  175. // ICollection
  176. public virtual int Count {
  177. get {
  178. return inUse;
  179. }
  180. }
  181. public virtual bool IsSynchronized {
  182. get {
  183. return false;
  184. }
  185. }
  186. public virtual Object SyncRoot {
  187. get {
  188. return this;
  189. }
  190. }
  191. // IDictionary
  192. public virtual bool IsFixedSize {
  193. get {
  194. return false;
  195. }
  196. }
  197. public virtual bool IsReadOnly {
  198. get {
  199. return false;
  200. }
  201. }
  202. public virtual ICollection Keys {
  203. get {
  204. return new HashKeys (this);
  205. }
  206. }
  207. public virtual ICollection Values {
  208. get {
  209. return new HashValues (this);
  210. }
  211. }
  212. public virtual Object this [Object key] {
  213. get {
  214. return GetImpl (key);
  215. }
  216. set {
  217. PutImpl (key, value, true);
  218. }
  219. }
  220. //
  221. // Interface methods
  222. //
  223. // IEnumerable
  224. IEnumerator IEnumerable.GetEnumerator ()
  225. {
  226. return new Enumerator (this, EnumeratorMode.ENTRY_MODE);
  227. }
  228. // ICollection
  229. public virtual void CopyTo (Array array, int arrayIndex)
  230. {
  231. if (null == array)
  232. throw new ArgumentNullException ("array");
  233. if (arrayIndex < 0)
  234. throw new ArgumentOutOfRangeException ("arrayIndex");
  235. if (array.Rank > 1)
  236. throw new ArgumentException ("array is multidimensional");
  237. if (arrayIndex >= array.Length)
  238. throw new ArgumentException ("arrayIndex is equal to or greater than array.Length");
  239. if (arrayIndex + this.inUse > array.Length)
  240. throw new ArgumentException ("Not enough room from arrayIndex to end of array for this Hashtable");
  241. IDictionaryEnumerator it = GetEnumerator ();
  242. int i = arrayIndex;
  243. while (it.MoveNext ()) {
  244. array.SetValue (it.Entry, i++);
  245. }
  246. }
  247. // IDictionary
  248. public virtual void Add (Object key, Object value)
  249. {
  250. PutImpl (key, value, false);
  251. }
  252. public virtual void Clear ()
  253. {
  254. for (int i = 0;i<table.Length;i++) {
  255. table [i].key = null;
  256. table [i].value = null;
  257. table [i].hashMix = 0;
  258. }
  259. inUse = 0;
  260. modificationCount++;
  261. }
  262. public virtual bool Contains (Object key)
  263. {
  264. return (Find (key) >= 0);
  265. }
  266. public virtual IDictionaryEnumerator GetEnumerator ()
  267. {
  268. return new Enumerator (this, EnumeratorMode.ENTRY_MODE);
  269. }
  270. public virtual void Remove (Object key)
  271. {
  272. int i = Find (key);
  273. Slot [] table = this.table;
  274. if (i >= 0) {
  275. int h = table [i].hashMix;
  276. h &= CHAIN_MARKER;
  277. table [i].hashMix = h;
  278. table [i].key = (h != 0)
  279. ? KeyMarker.Removed
  280. : null;
  281. table [i].value = null;
  282. --inUse;
  283. ++modificationCount;
  284. }
  285. }
  286. public virtual bool ContainsKey (object key)
  287. {
  288. return Contains (key);
  289. }
  290. public virtual bool ContainsValue (object value)
  291. {
  292. int size = this.table.Length;
  293. Slot [] table = this.table;
  294. for (int i = 0; i < size; i++) {
  295. Slot entry = table [i];
  296. if (entry.key != null && entry.key!= KeyMarker.Removed
  297. && value.Equals (entry.value)) {
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. // ICloneable
  304. public virtual object Clone ()
  305. {
  306. Hashtable ht = new Hashtable (Count, hcp, comparer);
  307. ht.modificationCount = this.modificationCount;
  308. ht.inUse = this.inUse;
  309. ht.AdjustThreshold ();
  310. // FIXME: maybe it's faster to simply
  311. // copy the back-end array?
  312. IDictionaryEnumerator it = GetEnumerator ();
  313. while (it.MoveNext ()) {
  314. ht [it.Key] = it.Value;
  315. }
  316. return ht;
  317. }
  318. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  319. {
  320. info.AddValue ("LoadFactor", loadFactor);
  321. info.AddValue ("Version", modificationCount);
  322. info.AddValue ("Comparer", comparerRef);
  323. info.AddValue ("HashCodeProvider", hcpRef);
  324. info.AddValue ("HashSize", inUse);
  325. info.AddValue ("Table", table);
  326. info.AddValue ("Treshold", threshold);
  327. }
  328. public virtual void OnDeserialization (object sender)
  329. {
  330. }
  331. /// <summary>
  332. /// Returns a synchronized (thread-safe)
  333. /// wrapper for the Hashtable.
  334. /// </summary>
  335. public static Hashtable Synchronized (Hashtable table)
  336. {
  337. return new SynchedHashtable (table);
  338. }
  339. //
  340. // Protected instance methods
  341. //
  342. /// <summary>Returns the hash code for the specified key.</summary>
  343. protected virtual int GetHash (Object key)
  344. {
  345. IHashCodeProvider hcp = this.hcp;
  346. return (hcp!= null)
  347. ? hcp.GetHashCode (key)
  348. : key.GetHashCode ();
  349. }
  350. /// <summary>
  351. /// Compares a specific Object with a specific key
  352. /// in the Hashtable.
  353. /// </summary>
  354. protected virtual bool KeyEquals (Object item, Object key)
  355. {
  356. IComparer c = this.comparer;
  357. if (c!= null)
  358. return (c.Compare (item, key) == 0);
  359. else
  360. return item.Equals (key);
  361. }
  362. //
  363. // Private instance methods
  364. //
  365. private void AdjustThreshold ()
  366. {
  367. int size = table.Length;
  368. threshold = (int) (size*loadFactor);
  369. if (this.threshold >= size)
  370. threshold = size-1;
  371. }
  372. private void SetTable (Slot [] table)
  373. {
  374. if (table == null)
  375. throw new ArgumentNullException ("table");
  376. this.table = table;
  377. AdjustThreshold ();
  378. }
  379. private Object GetImpl (Object key)
  380. {
  381. int i = Find (key);
  382. if (i >= 0)
  383. return table [i].value;
  384. else
  385. return null;
  386. }
  387. private int Find (Object key)
  388. {
  389. if (key == null)
  390. throw new ArgumentNullException ("null key");
  391. uint size = (uint) this.table.Length;
  392. int h = this.GetHash (key) & Int32.MaxValue;
  393. uint spot = (uint)h;
  394. uint step = (uint) ((h >> 5)+1) % (size-1)+1;
  395. Slot[] table = this.table;
  396. for (int i = 0; i < size;i++) {
  397. int indx = (int) (spot % size);
  398. Slot entry = table [indx];
  399. Object k = entry.key;
  400. if (k == null)
  401. return -1;
  402. if ((entry.hashMix & Int32.MaxValue) == h
  403. && this.KeyEquals (key, k)) {
  404. return indx;
  405. }
  406. if ((entry.hashMix & CHAIN_MARKER) == 0)
  407. return -1;
  408. spot+= step;
  409. }
  410. return -1;
  411. }
  412. private void Rehash ()
  413. {
  414. int oldSize = this.table.Length;
  415. // From the SDK docs:
  416. // Hashtable is automatically increased
  417. // to the smallest prime number that is larger
  418. // than twice the current number of Hashtable buckets
  419. uint newSize = (uint)ToPrime ((oldSize<<1)|1);
  420. Slot [] newTable = new Slot [newSize];
  421. Slot [] table = this.table;
  422. for (int i = 0;i<oldSize;i++) {
  423. Slot s = table [i];
  424. if (s.key!= null) {
  425. int h = s.hashMix & Int32.MaxValue;
  426. uint spot = (uint)h;
  427. uint step = ((uint) (h>>5)+1)% (newSize-1)+1;
  428. for (uint j = spot%newSize;;spot+= step, j = spot%newSize) {
  429. // No check for KeyMarker.Removed here,
  430. // because the table is just allocated.
  431. if (newTable [j].key == null) {
  432. newTable [j].key = s.key;
  433. newTable [j].value = s.value;
  434. newTable [j].hashMix |= h;
  435. break;
  436. } else {
  437. newTable [j].hashMix |= CHAIN_MARKER;
  438. }
  439. }
  440. }
  441. }
  442. ++this.modificationCount;
  443. this.SetTable (newTable);
  444. }
  445. private void PutImpl (Object key, Object value, bool overwrite)
  446. {
  447. if (key == null)
  448. throw new ArgumentNullException ("null key");
  449. uint size = (uint)this.table.Length;
  450. if (this.inUse >= this.threshold) {
  451. this.Rehash ();
  452. size = (uint)this.table.Length;
  453. }
  454. int h = this.GetHash (key) & Int32.MaxValue;
  455. uint spot = (uint)h;
  456. uint step = (uint) ((spot>>5)+1)% (size-1)+1;
  457. Slot [] table = this.table;
  458. Slot entry;
  459. int freeIndx = -1;
  460. for (int i = 0; i < size; i++) {
  461. int indx = (int) (spot % size);
  462. entry = table [indx];
  463. if (freeIndx == -1
  464. && entry.key == KeyMarker.Removed
  465. && (entry.hashMix & CHAIN_MARKER)!= 0)
  466. freeIndx = indx;
  467. if (entry.key == null ||
  468. (entry.key == KeyMarker.Removed
  469. && (entry.hashMix & CHAIN_MARKER)!= 0)) {
  470. if (freeIndx == -1)
  471. freeIndx = indx;
  472. break;
  473. }
  474. if ((entry.hashMix & Int32.MaxValue) == h && KeyEquals (key, entry.key)) {
  475. if (overwrite) {
  476. table [indx].value = value;
  477. ++this.modificationCount;
  478. } else {
  479. // Handle Add ():
  480. // An entry with the same key already exists in the Hashtable.
  481. throw new ArgumentException (
  482. "Key duplication when adding: " + key);
  483. }
  484. return;
  485. }
  486. if (freeIndx == -1) {
  487. table [indx].hashMix |= CHAIN_MARKER;
  488. }
  489. spot+= step;
  490. }
  491. if (freeIndx!= -1) {
  492. table [freeIndx].key = key;
  493. table [freeIndx].value = value;
  494. table [freeIndx].hashMix |= h;
  495. ++this.inUse;
  496. ++this.modificationCount;
  497. }
  498. }
  499. private void CopyToArray (Array arr, int i,
  500. EnumeratorMode mode)
  501. {
  502. IEnumerator it = new Enumerator (this, mode);
  503. while (it.MoveNext ()) {
  504. arr.SetValue (it.Current, i++);
  505. }
  506. }
  507. //
  508. // Private static methods
  509. //
  510. private static bool TestPrime (int x)
  511. {
  512. if ((x & 1) != 0) {
  513. for (int n = 3; n< (int)Math.Sqrt (x); n += 2) {
  514. if ((x % n) == 0)
  515. return false;
  516. }
  517. return true;
  518. }
  519. // There is only one even prime - 2.
  520. return (x == 2);
  521. }
  522. private static int CalcPrime (int x)
  523. {
  524. for (int i = (x & (~1))-1; i< Int32.MaxValue; i += 2) {
  525. if (TestPrime (i)) return i;
  526. }
  527. return x;
  528. }
  529. private static int ToPrime (int x)
  530. {
  531. for (int i = 0; i < primeTbl.Length; i++) {
  532. if (x <= primeTbl [i])
  533. return primeTbl [i];
  534. }
  535. return CalcPrime (x);
  536. }
  537. //
  538. // Inner classes
  539. //
  540. public enum EnumeratorMode : int {KEY_MODE = 0, VALUE_MODE, ENTRY_MODE};
  541. protected sealed class Enumerator : IDictionaryEnumerator, IEnumerator {
  542. private Hashtable host;
  543. private int stamp;
  544. private int pos;
  545. private int size;
  546. private EnumeratorMode mode;
  547. private Object currentKey;
  548. private Object currentValue;
  549. private readonly static string xstr = "Hashtable.Enumerator: snapshot out of sync.";
  550. public Enumerator (Hashtable host, EnumeratorMode mode) {
  551. this.host = host;
  552. stamp = host.modificationCount;
  553. size = host.table.Length;
  554. this.mode = mode;
  555. Reset ();
  556. }
  557. public Enumerator (Hashtable host)
  558. : this (host, EnumeratorMode.KEY_MODE) {}
  559. private void FailFast ()
  560. {
  561. if (host.modificationCount != stamp) {
  562. throw new InvalidOperationException (xstr);
  563. }
  564. }
  565. public void Reset ()
  566. {
  567. FailFast ();
  568. pos = -1;
  569. currentKey = null;
  570. currentValue = null;
  571. }
  572. public bool MoveNext ()
  573. {
  574. FailFast ();
  575. if (pos < size) {
  576. while (++pos < size) {
  577. Slot entry = host.table [pos];
  578. if (entry.key != null && entry.key != KeyMarker.Removed) {
  579. currentKey = entry.key;
  580. currentValue = entry.value;
  581. return true;
  582. }
  583. }
  584. }
  585. currentKey = null;
  586. currentValue = null;
  587. return false;
  588. }
  589. public DictionaryEntry Entry
  590. {
  591. get {
  592. if (currentKey == null) throw new InvalidOperationException ();
  593. FailFast ();
  594. return new DictionaryEntry (currentKey, currentValue);
  595. }
  596. }
  597. public Object Key {
  598. get {
  599. if (currentKey == null) throw new InvalidOperationException ();
  600. FailFast ();
  601. return currentKey;
  602. }
  603. }
  604. public Object Value {
  605. get {
  606. if (currentKey == null) throw new InvalidOperationException ();
  607. FailFast ();
  608. return currentValue;
  609. }
  610. }
  611. public Object Current {
  612. get {
  613. if (currentKey == null) throw new InvalidOperationException ();
  614. switch (mode) {
  615. case EnumeratorMode.KEY_MODE:
  616. return currentKey;
  617. case EnumeratorMode.VALUE_MODE:
  618. return currentValue;
  619. case EnumeratorMode.ENTRY_MODE:
  620. return new DictionaryEntry (currentKey, currentValue);
  621. }
  622. throw new Exception ("should never happen");
  623. }
  624. }
  625. }
  626. protected class HashKeys : ICollection, IEnumerable {
  627. private Hashtable host;
  628. public HashKeys (Hashtable host) {
  629. if (host == null)
  630. throw new ArgumentNullException ();
  631. this.host = host;
  632. }
  633. // ICollection
  634. public virtual int Count {
  635. get {
  636. return host.Count;
  637. }
  638. }
  639. public virtual bool IsSynchronized {
  640. get {
  641. return host.IsSynchronized;
  642. }
  643. }
  644. public virtual Object SyncRoot {
  645. get {return host.SyncRoot;}
  646. }
  647. public virtual void CopyTo (Array array, int arrayIndex)
  648. {
  649. if (array == null)
  650. throw new ArgumentNullException ("array");
  651. if (array.Rank != 1)
  652. throw new ArgumentException ("array");
  653. if (arrayIndex < 0)
  654. throw new ArgumentOutOfRangeException ("arrayIndex");
  655. if (array.Length - arrayIndex < Count)
  656. throw new ArgumentException ("not enough space");
  657. host.CopyToArray (array, arrayIndex, EnumeratorMode.KEY_MODE);
  658. }
  659. // IEnumerable
  660. public virtual IEnumerator GetEnumerator ()
  661. {
  662. return new Hashtable.Enumerator (host, EnumeratorMode.KEY_MODE);
  663. }
  664. }
  665. protected class HashValues : ICollection, IEnumerable {
  666. private Hashtable host;
  667. public HashValues (Hashtable host) {
  668. if (host == null)
  669. throw new ArgumentNullException ();
  670. this.host = host;
  671. }
  672. // ICollection
  673. public virtual int Count {
  674. get {
  675. return host.Count;
  676. }
  677. }
  678. public virtual bool IsSynchronized {
  679. get {
  680. return host.IsSynchronized;
  681. }
  682. }
  683. public virtual Object SyncRoot {
  684. get {
  685. return host.SyncRoot;
  686. }
  687. }
  688. public virtual void CopyTo (Array array, int arrayIndex)
  689. {
  690. if (array == null)
  691. throw new ArgumentNullException ("array");
  692. if (array.Rank != 1)
  693. throw new ArgumentException ("array");
  694. if (arrayIndex < 0)
  695. throw new ArgumentOutOfRangeException ("arrayIndex");
  696. if (array.Length - arrayIndex < Count)
  697. throw new ArgumentException ("not enough space");
  698. host.CopyToArray (array, arrayIndex, EnumeratorMode.VALUE_MODE);
  699. }
  700. // IEnumerable
  701. public virtual IEnumerator GetEnumerator ()
  702. {
  703. return new Hashtable.Enumerator (host, EnumeratorMode.VALUE_MODE);
  704. }
  705. }
  706. [Serializable]
  707. protected class SynchedHashtable : Hashtable, IEnumerable {
  708. private Hashtable host;
  709. public SynchedHashtable (Hashtable host) {
  710. if (host == null)
  711. throw new ArgumentNullException ();
  712. this.host = host;
  713. }
  714. // ICollection
  715. public override int Count {
  716. get {
  717. return host.Count;
  718. }
  719. }
  720. public override bool IsSynchronized {
  721. get {
  722. return true;
  723. }
  724. }
  725. public override Object SyncRoot {
  726. get {
  727. return host.SyncRoot;
  728. }
  729. }
  730. // IDictionary
  731. public override bool IsFixedSize {
  732. get {
  733. return host.IsFixedSize;
  734. }
  735. }
  736. public override bool IsReadOnly {
  737. get {
  738. return host.IsReadOnly;
  739. }
  740. }
  741. public override ICollection Keys {
  742. get {
  743. ICollection keys = null;
  744. lock (host.SyncRoot) {
  745. keys = host.Keys;
  746. }
  747. return keys;
  748. }
  749. }
  750. public override ICollection Values {
  751. get {
  752. ICollection vals = null;
  753. lock (host.SyncRoot) {
  754. vals = host.Values;
  755. }
  756. return vals;
  757. }
  758. }
  759. public override Object this [Object key] {
  760. get {
  761. return host.GetImpl (key);
  762. }
  763. set {
  764. lock (host.SyncRoot) {
  765. host.PutImpl (key, value, true);
  766. }
  767. }
  768. }
  769. // IEnumerable
  770. IEnumerator IEnumerable.GetEnumerator ()
  771. {
  772. return new Enumerator (host, EnumeratorMode.KEY_MODE);
  773. }
  774. // ICollection
  775. public override void CopyTo (Array array, int arrayIndex)
  776. {
  777. host.CopyTo (array, arrayIndex);
  778. }
  779. // IDictionary
  780. public override void Add (Object key, Object value)
  781. {
  782. lock (host.SyncRoot) {
  783. host.PutImpl (key, value, false);
  784. }
  785. }
  786. public override void Clear ()
  787. {
  788. lock (host.SyncRoot) {
  789. host.Clear ();
  790. }
  791. }
  792. public override bool Contains (Object key)
  793. {
  794. return (host.Find (key) >= 0);
  795. }
  796. public override IDictionaryEnumerator GetEnumerator ()
  797. {
  798. return new Enumerator (host, EnumeratorMode.ENTRY_MODE);
  799. }
  800. public override void Remove (Object key)
  801. {
  802. lock (host.SyncRoot) {
  803. host.Remove (key);
  804. }
  805. }
  806. public override bool ContainsKey (object key)
  807. {
  808. return host.Contains (key);
  809. }
  810. public override bool ContainsValue (object value)
  811. {
  812. return host.ContainsValue (value);
  813. }
  814. // ICloneable
  815. public override object Clone ()
  816. {
  817. return (host.Clone () as Hashtable);
  818. }
  819. } // SynchedHashtable
  820. } // Hashtable
  821. }