Hashtable.cs 20 KB

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