Hashtable.cs 20 KB

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