Hashtable.cs 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 ("Key duplication");
  478. }
  479. return;
  480. }
  481. if (freeIndx == -1) {
  482. table [indx].hashMix |= CHAIN_MARKER;
  483. }
  484. spot+= step;
  485. }
  486. if (freeIndx!= -1) {
  487. table [freeIndx].key = key;
  488. table [freeIndx].value = value;
  489. table [freeIndx].hashMix |= h;
  490. ++this.inUse;
  491. ++this.modificationCount;
  492. }
  493. }
  494. private void CopyToArray (Array arr, int i,
  495. EnumeratorMode mode)
  496. {
  497. IEnumerator it = new Enumerator (this, mode);
  498. while (it.MoveNext ()) {
  499. arr.SetValue (it.Current, i++);
  500. }
  501. }
  502. //
  503. // Private static methods
  504. //
  505. private static bool TestPrime (int x)
  506. {
  507. if ((x & 1) != 0) {
  508. for (int n = 3; n< (int)Math.Sqrt (x); n += 2) {
  509. if ((x % n) == 0)
  510. return false;
  511. }
  512. return true;
  513. }
  514. // There is only one even prime - 2.
  515. return (x == 2);
  516. }
  517. private static int CalcPrime (int x)
  518. {
  519. for (int i = (x & (~1))-1; i< Int32.MaxValue; i += 2) {
  520. if (TestPrime (i)) return i;
  521. }
  522. return x;
  523. }
  524. private static int ToPrime (int x)
  525. {
  526. for (int i = 0; i < primeTbl.Length; i++) {
  527. if (x <= primeTbl [i])
  528. return primeTbl [i];
  529. }
  530. return CalcPrime (x);
  531. }
  532. //
  533. // Inner classes
  534. //
  535. public enum EnumeratorMode : int {KEY_MODE = 0, VALUE_MODE, ENTRY_MODE};
  536. protected sealed class Enumerator : IDictionaryEnumerator, IEnumerator {
  537. private Hashtable host;
  538. private int stamp;
  539. private int pos;
  540. private int size;
  541. private EnumeratorMode mode;
  542. private Object currentKey;
  543. private Object currentValue;
  544. private readonly static string xstr = "Hashtable.Enumerator: snapshot out of sync.";
  545. public Enumerator (Hashtable host, EnumeratorMode mode) {
  546. this.host = host;
  547. stamp = host.modificationCount;
  548. size = host.table.Length;
  549. this.mode = mode;
  550. Reset ();
  551. }
  552. public Enumerator (Hashtable host)
  553. : this (host, EnumeratorMode.KEY_MODE) {}
  554. private void FailFast ()
  555. {
  556. if (host.modificationCount != stamp) {
  557. throw new InvalidOperationException (xstr);
  558. }
  559. }
  560. public void Reset ()
  561. {
  562. FailFast ();
  563. pos = -1;
  564. currentKey = null;
  565. currentValue = null;
  566. }
  567. public bool MoveNext ()
  568. {
  569. FailFast ();
  570. if (pos < size) {
  571. while (++pos < size) {
  572. Slot entry = host.table [pos];
  573. if (entry.key != null && entry.key != REMOVED_MARKER) {
  574. currentKey = entry.key;
  575. currentValue = entry.value;
  576. return true;
  577. }
  578. }
  579. }
  580. currentKey = null;
  581. currentValue = null;
  582. return false;
  583. }
  584. public DictionaryEntry Entry
  585. {
  586. get {
  587. FailFast ();
  588. return new DictionaryEntry (currentKey, currentValue);
  589. }
  590. }
  591. public Object Key {
  592. get {
  593. FailFast ();
  594. return currentKey;
  595. }
  596. }
  597. public Object Value {
  598. get {
  599. FailFast ();
  600. return currentValue;
  601. }
  602. }
  603. public Object Current {
  604. get {
  605. FailFast ();
  606. switch (mode) {
  607. case EnumeratorMode.KEY_MODE:
  608. return currentKey;
  609. case EnumeratorMode.VALUE_MODE:
  610. return currentValue;
  611. case EnumeratorMode.ENTRY_MODE:
  612. return new DictionaryEntry (currentKey, currentValue);
  613. }
  614. throw new Exception ("should never happen");
  615. }
  616. }
  617. }
  618. protected class HashKeys : ICollection, IEnumerable {
  619. private Hashtable host;
  620. private int count;
  621. public HashKeys (Hashtable host) {
  622. if (host == null)
  623. throw new ArgumentNullException ();
  624. this.host = host;
  625. this.count = host.Count;
  626. }
  627. // ICollection
  628. public virtual int Count {
  629. get {
  630. return count;
  631. }
  632. }
  633. public virtual bool IsSynchronized {
  634. get {
  635. return host.IsSynchronized;
  636. }
  637. }
  638. public virtual Object SyncRoot {
  639. get {return host.SyncRoot;}
  640. }
  641. public virtual void CopyTo (Array array, int arrayIndex)
  642. {
  643. host.CopyToArray (array, arrayIndex, EnumeratorMode.KEY_MODE);
  644. }
  645. // IEnumerable
  646. public virtual IEnumerator GetEnumerator ()
  647. {
  648. return new Hashtable.Enumerator (host, EnumeratorMode.KEY_MODE);
  649. }
  650. }
  651. protected class HashValues : ICollection, IEnumerable {
  652. private Hashtable host;
  653. private int count;
  654. public HashValues (Hashtable host) {
  655. if (host == null)
  656. throw new ArgumentNullException ();
  657. this.host = host;
  658. this.count = host.Count;
  659. }
  660. // ICollection
  661. public virtual int Count {
  662. get {
  663. return count;
  664. }
  665. }
  666. public virtual bool IsSynchronized {
  667. get {
  668. return host.IsSynchronized;
  669. }
  670. }
  671. public virtual Object SyncRoot {
  672. get {
  673. return host.SyncRoot;
  674. }
  675. }
  676. public virtual void CopyTo (Array array, int arrayIndex)
  677. {
  678. host.CopyToArray (array, arrayIndex, EnumeratorMode.VALUE_MODE);
  679. }
  680. // IEnumerable
  681. public virtual IEnumerator GetEnumerator ()
  682. {
  683. return new Hashtable.Enumerator (host, EnumeratorMode.VALUE_MODE);
  684. }
  685. }
  686. protected class SynchedHashtable : Hashtable, IEnumerable {
  687. private Hashtable host;
  688. public SynchedHashtable (Hashtable host) {
  689. if (host == null)
  690. throw new ArgumentNullException ();
  691. this.host = host;
  692. }
  693. // ICollection
  694. public override int Count {
  695. get {
  696. return host.Count;
  697. }
  698. }
  699. public override bool IsSynchronized {
  700. get {
  701. return true;
  702. }
  703. }
  704. public override Object SyncRoot {
  705. get {
  706. return host.SyncRoot;
  707. }
  708. }
  709. // IDictionary
  710. public override bool IsFixedSize {
  711. get {
  712. return host.IsFixedSize;
  713. }
  714. }
  715. public override bool IsReadOnly {
  716. get {
  717. return host.IsReadOnly;
  718. }
  719. }
  720. public override ICollection Keys {
  721. get {
  722. ICollection keys = null;
  723. lock (host.SyncRoot) {
  724. keys = host.Keys;
  725. }
  726. return keys;
  727. }
  728. }
  729. public override ICollection Values {
  730. get {
  731. ICollection vals = null;
  732. lock (host.SyncRoot) {
  733. vals = host.Values;
  734. }
  735. return vals;
  736. }
  737. }
  738. public override Object this [Object key] {
  739. get {
  740. return host.GetImpl (key);
  741. }
  742. set {
  743. lock (host.SyncRoot) {
  744. host.PutImpl (key, value, true);
  745. }
  746. }
  747. }
  748. // IEnumerable
  749. IEnumerator IEnumerable.GetEnumerator ()
  750. {
  751. return new Enumerator (host, EnumeratorMode.KEY_MODE);
  752. }
  753. // ICollection
  754. public override void CopyTo (Array array, int arrayIndex)
  755. {
  756. host.CopyTo (array, arrayIndex);
  757. }
  758. // IDictionary
  759. public override void Add (Object key, Object value)
  760. {
  761. lock (host.SyncRoot) {
  762. host.PutImpl (key, value, false);
  763. }
  764. }
  765. public override void Clear ()
  766. {
  767. lock (host.SyncRoot) {
  768. host.Clear ();
  769. }
  770. }
  771. public override bool Contains (Object key)
  772. {
  773. return (host.Find (key) >= 0);
  774. }
  775. public override IDictionaryEnumerator GetEnumerator ()
  776. {
  777. return new Enumerator (host, EnumeratorMode.ENTRY_MODE);
  778. }
  779. public override void Remove (Object key)
  780. {
  781. lock (host.SyncRoot) {
  782. host.Remove (key);
  783. }
  784. }
  785. public override bool ContainsKey (object key)
  786. {
  787. return host.Contains (key);
  788. }
  789. public override bool ContainsValue (object value)
  790. {
  791. return host.ContainsValue (value);
  792. }
  793. // ICloneable
  794. public override object Clone ()
  795. {
  796. return (host.Clone () as Hashtable);
  797. }
  798. } // SynchedHashtable
  799. } // Hashtable
  800. }