Map.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. //
  2. // System.Collections.Generic.MapEx
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email]tsureshkumar@novell.com[/email])
  6. // Marek Safar ([email]marek.safar@gmail.com[/email])
  7. // Ankit Jain ([email]radical@corewars.org[/email])
  8. // David Waite ([email]mass@akuma.org[/email])
  9. // Juraj Skripsky ([email]js@hotfeet.ch[/email])
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. // Copyright (C) 2005 David Waite
  14. // Copyright (C) 2007 HotFeet GmbH (http://www.hotfeet.ch)
  15. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using System;
  37. using System.Collections;
  38. using System.Collections.Generic;
  39. using System.Runtime.Serialization;
  40. using System.Security.Permissions;
  41. using System.Runtime.InteropServices;
  42. using System.Diagnostics;
  43. namespace Utility
  44. {
  45. //
  46. // HashPrimeNumbers.cs
  47. //
  48. // Author:
  49. // Sergey Chaban ([email]serge@wildwestsoftware.com[/email])
  50. //
  51. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  52. //
  53. static class HashPrimeNumbers
  54. {
  55. static readonly int[] primeTbl = {
  56. 11,
  57. 19,
  58. 37,
  59. 73,
  60. 109,
  61. 163,
  62. 251,
  63. 367,
  64. 557,
  65. 823,
  66. 1237,
  67. 1861,
  68. 2777,
  69. 4177,
  70. 6247,
  71. 9371,
  72. 14057,
  73. 21089,
  74. 31627,
  75. 47431,
  76. 71143,
  77. 106721,
  78. 160073,
  79. 240101,
  80. 360163,
  81. 540217,
  82. 810343,
  83. 1215497,
  84. 1823231,
  85. 2734867,
  86. 4102283,
  87. 6153409,
  88. 9230113,
  89. 13845163
  90. };
  91. //
  92. // Private static methods
  93. //
  94. public static bool TestPrime(int x)
  95. {
  96. if ((x & 1) != 0)
  97. {
  98. int top = (int)Math.Sqrt(x);
  99. for (int n = 3; n < top; n += 2)
  100. {
  101. if ((x % n) == 0)
  102. return false;
  103. }
  104. return true;
  105. }
  106. // There is only one even prime - 2.
  107. return (x == 2);
  108. }
  109. public static int CalcPrime(int x)
  110. {
  111. for (int i = (x & (~1)) - 1; i < Int32.MaxValue; i += 2)
  112. {
  113. if (TestPrime(i)) return i;
  114. }
  115. return x;
  116. }
  117. public static int ToPrime(int x)
  118. {
  119. for (int i = 0; i < primeTbl.Length; i++)
  120. {
  121. if (x <= primeTbl[i])
  122. return primeTbl[i];
  123. }
  124. return CalcPrime(x);
  125. }
  126. }
  127. //
  128. // CollectionDebuggerView.cs
  129. //
  130. // Authors:
  131. // Marek Safar <[email]marek.safar@gmail.com[/email]>
  132. //
  133. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  134. //
  135. internal sealed class CollectionDebuggerView<T, U>
  136. {
  137. // Fields
  138. private readonly ICollection<KeyValuePair<T, U>> c;
  139. // Methods
  140. public CollectionDebuggerView(ICollection<KeyValuePair<T, U>> col)
  141. {
  142. this.c = col;
  143. }
  144. // Properties
  145. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  146. public KeyValuePair<T, U>[] Items
  147. {
  148. get
  149. {
  150. KeyValuePair<T, U>[] array = new KeyValuePair<T, U>[this.c.Count];
  151. this.c.CopyTo(array, 0);
  152. return array;
  153. }
  154. }
  155. }
  156. /*
  157. * Declare this outside the main class so it doesn't have to be inflated for each
  158. * instantiation of MapEx.
  159. */
  160. internal struct Link
  161. {
  162. public int HashCode;
  163. public int Next;
  164. }
  165. [ComVisible(false)]
  166. [Serializable]
  167. [DebuggerDisplay("Count={Count}")]
  168. [DebuggerTypeProxy(typeof(CollectionDebuggerView<,>))]
  169. public class Map<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, ISerializable, IDeserializationCallback
  170. #if NET_4_5
  171. , IReadOnlyDictionary<TKey, TValue>
  172. #endif
  173. {
  174. // The implementation of this class uses a hash table and linked lists
  175. // (see: http://msdn2.microsoft.com/en-us/library/ms379571(VS.80).aspx).
  176. //
  177. // We use a kind of "mini-heap" instead of reference-based linked lists:
  178. // "keySlots" and "valueSlots" is the heap itself, it stores the data
  179. // "linkSlots" contains information about how the slots in the heap
  180. // are connected into linked lists
  181. // In addition, the HashCode field can be used to check if the
  182. // corresponding key and value are present (HashCode has the
  183. // HASH_FLAG bit set in this case), so, to iterate over all the
  184. // items in the mapContainer, simply iterate the linkSlots array
  185. // and check for the HASH_FLAG bit in the HashCode field.
  186. // For this reason, each time a hashcode is calculated, it needs
  187. // to be ORed with HASH_FLAG before comparing it with the save hashcode.
  188. // "touchedSlots" and "emptySlot" manage the free space in the heap
  189. const int INITIAL_SIZE = 10;
  190. const float DEFAULT_LOAD_FACTOR = (90f / 100);
  191. const int NO_SLOT = -1;
  192. const int HASH_FLAG = -2147483648;
  193. // The hash table contains indices into the linkSlots array
  194. int[] table;
  195. // All (key,value) pairs are chained into linked lists. The connection
  196. // information is stored in "linkSlots" along with the key's hash code
  197. // (for performance reasons).
  198. // TODO: get rid of the hash code in Link (this depends on a few
  199. // JIT-compiler optimizations)
  200. // Every link in "linkSlots" corresponds to the (key,value) pair
  201. // in "keySlots"/"valueSlots" with the same index.
  202. Link[] linkSlots;
  203. TKey[] keySlots;
  204. TValue[] valueSlots;
  205. //Leave those 2 fields here to improve heap layout.
  206. IEqualityComparer<TKey> hcp;
  207. SerializationInfo serialization_info;
  208. // The number of slots in "linkSlots" and "keySlots"/"valueSlots" that
  209. // are in use (i.e. filled with data) or have been used and marked as
  210. // "empty" later on.
  211. int touchedSlots;
  212. // The index of the first slot in the "empty slots chain".
  213. // "Remove()" prepends the cleared slots to the empty chain.
  214. // "Add()" fills the first slot in the empty slots chain with the
  215. // added item (or increases "touchedSlots" if the chain itself is empty).
  216. int emptySlot;
  217. // The number of (key,value) pairs in this mapContainer.
  218. int count;
  219. // The number of (key,value) pairs the mapContainer can hold without
  220. // resizing the hash table and the slots arrays.
  221. int threshold;
  222. // The number of changes made to this mapContainer. Used by enumerators
  223. // to detect changes and invalidate themselves.
  224. int generation;
  225. public int Count
  226. {
  227. get { return count; }
  228. }
  229. public TValue this[TKey key]
  230. {
  231. get
  232. {
  233. if (key == null)
  234. throw new ArgumentNullException("key");
  235. // get first item of linked list corresponding to given key
  236. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  237. int cur = table[(hashCode & int.MaxValue) % table.Length] - 1;
  238. // walk linked list until right slot is found or end is reached
  239. while (cur != NO_SLOT)
  240. {
  241. // The ordering is important for compatibility with MS and strange
  242. // Object.Equals () implementations
  243. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  244. return valueSlots[cur];
  245. cur = linkSlots[cur].Next;
  246. }
  247. throw new KeyNotFoundException();
  248. }
  249. set
  250. {
  251. if (key == null)
  252. throw new ArgumentNullException("key");
  253. // get first item of linked list corresponding to given key
  254. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  255. int index = (hashCode & int.MaxValue) % table.Length;
  256. int cur = table[index] - 1;
  257. // walk linked list until right slot (and its predecessor) is
  258. // found or end is reached
  259. int prev = NO_SLOT;
  260. if (cur != NO_SLOT)
  261. {
  262. do
  263. {
  264. // The ordering is important for compatibility with MS and strange
  265. // Object.Equals () implementations
  266. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  267. break;
  268. prev = cur;
  269. cur = linkSlots[cur].Next;
  270. } while (cur != NO_SLOT);
  271. }
  272. // is there no slot for the given key yet?
  273. if (cur == NO_SLOT)
  274. {
  275. // there is no existing slot for the given key,
  276. // allocate one and prepend it to its corresponding linked
  277. // list
  278. if (++count > threshold)
  279. {
  280. Resize();
  281. index = (hashCode & int.MaxValue) % table.Length;
  282. }
  283. // find an empty slot
  284. cur = emptySlot;
  285. if (cur == NO_SLOT)
  286. cur = touchedSlots++;
  287. else
  288. emptySlot = linkSlots[cur].Next;
  289. // prepend the added item to its linked list,
  290. // update the hash table
  291. linkSlots[cur].Next = table[index] - 1;
  292. table[index] = cur + 1;
  293. // store the new item and its hash code
  294. linkSlots[cur].HashCode = hashCode;
  295. keySlots[cur] = key;
  296. }
  297. else
  298. {
  299. // we already have a slot for the given key,
  300. // update the existing slot
  301. // if the slot is not at the front of its linked list,
  302. // we move it there
  303. if (prev != NO_SLOT)
  304. {
  305. linkSlots[prev].Next = linkSlots[cur].Next;
  306. linkSlots[cur].Next = table[index] - 1;
  307. table[index] = cur + 1;
  308. }
  309. }
  310. // store the item's data itself
  311. valueSlots[cur] = value;
  312. generation++;
  313. }
  314. }
  315. public Map()
  316. {
  317. Init(INITIAL_SIZE, null);
  318. }
  319. public Map(IEqualityComparer<TKey> comparer)
  320. {
  321. Init(INITIAL_SIZE, comparer);
  322. }
  323. public Map(IDictionary<TKey, TValue> mapContainer)
  324. : this(mapContainer, null)
  325. {
  326. }
  327. public Map(int capacity)
  328. {
  329. Init(capacity, null);
  330. }
  331. public Map(IDictionary<TKey, TValue> mapContainer, IEqualityComparer<TKey> comparer)
  332. {
  333. if (mapContainer == null)
  334. throw new ArgumentNullException("mapContainer");
  335. int capacity = mapContainer.Count;
  336. Init(capacity, comparer);
  337. foreach (KeyValuePair<TKey, TValue> entry in mapContainer)
  338. this.Add(entry.Key, entry.Value);
  339. }
  340. public Map(int capacity, IEqualityComparer<TKey> comparer)
  341. {
  342. Init(capacity, comparer);
  343. }
  344. protected Map(SerializationInfo info, StreamingContext context)
  345. {
  346. serialization_info = info;
  347. }
  348. public Map(IList<TKey> keyList, IList<TValue> valList)
  349. {
  350. if (keyList.Count != valList.Count)
  351. {
  352. throw new Exception(
  353. "Key of the number not equal to value of the number");
  354. }
  355. Init(keyList.Count, null);
  356. int count = keyList.Count;
  357. for (int index = 0; index < count; ++index)
  358. {
  359. Add(keyList[index], valList[index]);
  360. }
  361. }
  362. public void Reset(int size)
  363. {
  364. Clear();
  365. if (size == 0)
  366. size = 1;
  367. InitArrays(size);
  368. }
  369. private void Init(int capacity, IEqualityComparer<TKey> hcp)
  370. {
  371. if (capacity < 0)
  372. throw new ArgumentOutOfRangeException("capacity");
  373. this.hcp = (hcp != null) ? hcp : EqualityComparer<TKey>.Default;
  374. //if (capacity == 0)
  375. // capacity = INITIAL_SIZE;
  376. /* Modify capacity so 'capacity' elements can be added without resizing */
  377. capacity = (int)(capacity / DEFAULT_LOAD_FACTOR) + 1;
  378. InitArrays(capacity);
  379. generation = 0;
  380. }
  381. private void InitArrays(int size)
  382. {
  383. table = new int[size];
  384. linkSlots = new Link[size];
  385. emptySlot = NO_SLOT;
  386. keySlots = new TKey[size];
  387. valueSlots = new TValue[size];
  388. touchedSlots = 0;
  389. threshold = (int)(table.Length * DEFAULT_LOAD_FACTOR);
  390. if (threshold == 0 && table.Length > 0)
  391. threshold = 1;
  392. }
  393. void CopyToCheck(Array array, int index)
  394. {
  395. if (array == null)
  396. throw new ArgumentNullException("array");
  397. if (index < 0)
  398. throw new ArgumentOutOfRangeException("index");
  399. // we want no exception for index==array.Length && Count == 0
  400. if (index > array.Length)
  401. throw new ArgumentException("index larger than largest valid index of array");
  402. if (array.Length - index < Count)
  403. throw new ArgumentException("Destination array cannot hold the requested elements!");
  404. }
  405. void CopyKeys(TKey[] array, int index)
  406. {
  407. for (int i = 0; i < touchedSlots; i++)
  408. {
  409. if ((linkSlots[i].HashCode & HASH_FLAG) != 0)
  410. array[index++] = keySlots[i];
  411. }
  412. }
  413. void CopyValues(TValue[] array, int index)
  414. {
  415. for (int i = 0; i < touchedSlots; i++)
  416. {
  417. if ((linkSlots[i].HashCode & HASH_FLAG) != 0)
  418. array[index++] = valueSlots[i];
  419. }
  420. }
  421. delegate TRet Transform<TRet>(TKey key, TValue value);
  422. static KeyValuePair<TKey, TValue> make_pair(TKey key, TValue value)
  423. {
  424. return new KeyValuePair<TKey, TValue>(key, value);
  425. }
  426. static TKey pick_key(TKey key, TValue value)
  427. {
  428. return key;
  429. }
  430. static TValue pick_value(TKey key, TValue value)
  431. {
  432. return value;
  433. }
  434. void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
  435. {
  436. CopyToCheck(array, index);
  437. for (int i = 0; i < touchedSlots; i++)
  438. {
  439. if ((linkSlots[i].HashCode & HASH_FLAG) != 0)
  440. array[index++] = new KeyValuePair<TKey, TValue>(keySlots[i], valueSlots[i]);
  441. }
  442. }
  443. void Do_ICollectionCopyTo<TRet>(Array array, int index, Transform<TRet> transform)
  444. {
  445. Type src = typeof(TRet);
  446. Type tgt = array.GetType().GetElementType();
  447. try
  448. {
  449. if ((src.IsPrimitive || tgt.IsPrimitive) && !tgt.IsAssignableFrom(src))
  450. throw new Exception(); // we don't care. it'll get transformed to an ArgumentException below
  451. #if BOOTSTRAP_BASIC
  452. // BOOTSTRAP: gmcs 2.4.x seems to have trouble compiling the alternative
  453. throw new Exception ();
  454. #else
  455. object[] dest = (object[])array;
  456. for (int i = 0; i < touchedSlots; i++)
  457. {
  458. if ((linkSlots[i].HashCode & HASH_FLAG) != 0)
  459. dest[index++] = transform(keySlots[i], valueSlots[i]);
  460. }
  461. #endif
  462. }
  463. catch (Exception e)
  464. {
  465. throw new ArgumentException("Cannot copy source collection elements to destination array", "array", e);
  466. }
  467. }
  468. private void Resize()
  469. {
  470. // From the SDK docs:
  471. // Hashtable is automatically increased
  472. // to the smallest prime number that is larger
  473. // than twice the current number of Hashtable buckets
  474. int newSize = HashPrimeNumbers.ToPrime((table.Length << 1) | 1);
  475. // allocate new hash table and link slots array
  476. int[] newTable = new int[newSize];
  477. Link[] newLinkSlots = new Link[newSize];
  478. for (int i = 0; i < table.Length; i++)
  479. {
  480. int cur = table[i] - 1;
  481. while (cur != NO_SLOT)
  482. {
  483. int hashCode = newLinkSlots[cur].HashCode = hcp.GetHashCode(keySlots[cur]) | HASH_FLAG;
  484. int index = (hashCode & int.MaxValue) % newSize;
  485. newLinkSlots[cur].Next = newTable[index] - 1;
  486. newTable[index] = cur + 1;
  487. cur = linkSlots[cur].Next;
  488. }
  489. }
  490. table = newTable;
  491. linkSlots = newLinkSlots;
  492. // allocate new data slots, copy data
  493. TKey[] newKeySlots = new TKey[newSize];
  494. TValue[] newValueSlots = new TValue[newSize];
  495. Array.Copy(keySlots, 0, newKeySlots, 0, touchedSlots);
  496. Array.Copy(valueSlots, 0, newValueSlots, 0, touchedSlots);
  497. keySlots = newKeySlots;
  498. valueSlots = newValueSlots;
  499. threshold = (int)(newSize * DEFAULT_LOAD_FACTOR);
  500. }
  501. public void Add(TKey key, TValue value)
  502. {
  503. if (key == null)
  504. throw new ArgumentNullException("key");
  505. // get first item of linked list corresponding to given key
  506. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  507. int index = (hashCode & int.MaxValue) % table.Length;
  508. int cur = table[index] - 1;
  509. // walk linked list until end is reached (throw an exception if a
  510. // existing slot is found having an equivalent key)
  511. while (cur != NO_SLOT)
  512. {
  513. // The ordering is important for compatibility with MS and strange
  514. // Object.Equals () implementations
  515. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  516. throw new ArgumentException("An element with the same key already exists in the mapContainer.");
  517. cur = linkSlots[cur].Next;
  518. }
  519. if (++count > threshold)
  520. {
  521. Resize();
  522. index = (hashCode & int.MaxValue) % table.Length;
  523. }
  524. // find an empty slot
  525. cur = emptySlot;
  526. if (cur == NO_SLOT)
  527. cur = touchedSlots++;
  528. else
  529. emptySlot = linkSlots[cur].Next;
  530. // store the hash code of the added item,
  531. // prepend the added item to its linked list,
  532. // update the hash table
  533. linkSlots[cur].HashCode = hashCode;
  534. linkSlots[cur].Next = table[index] - 1;
  535. table[index] = cur + 1;
  536. // store item's data
  537. keySlots[cur] = key;
  538. valueSlots[cur] = value;
  539. generation++;
  540. }
  541. public IEqualityComparer<TKey> Comparer
  542. {
  543. get { return hcp; }
  544. }
  545. public void Clear()
  546. {
  547. count = 0;
  548. // clear the hash table
  549. Array.Clear(table, 0, table.Length);
  550. // clear arrays
  551. Array.Clear(keySlots, 0, keySlots.Length);
  552. Array.Clear(valueSlots, 0, valueSlots.Length);
  553. Array.Clear(linkSlots, 0, linkSlots.Length);
  554. // empty the "empty slots chain"
  555. emptySlot = NO_SLOT;
  556. touchedSlots = 0;
  557. generation++;
  558. }
  559. public bool ContainsKey(TKey key)
  560. {
  561. if (key == null)
  562. throw new ArgumentNullException("key");
  563. // get first item of linked list corresponding to given key
  564. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  565. int cur = table[(hashCode & int.MaxValue) % table.Length] - 1;
  566. // walk linked list until right slot is found or end is reached
  567. while (cur != NO_SLOT)
  568. {
  569. // The ordering is important for compatibility with MS and strange
  570. // Object.Equals () implementations
  571. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  572. return true;
  573. cur = linkSlots[cur].Next;
  574. }
  575. return false;
  576. }
  577. public bool ContainsValue(TValue value)
  578. {
  579. IEqualityComparer<TValue> cmp = EqualityComparer<TValue>.Default;
  580. for (int i = 0; i < table.Length; i++)
  581. {
  582. int cur = table[i] - 1;
  583. while (cur != NO_SLOT)
  584. {
  585. if (cmp.Equals(valueSlots[cur], value))
  586. return true;
  587. cur = linkSlots[cur].Next;
  588. }
  589. }
  590. return false;
  591. }
  592. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  593. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  594. {
  595. if (info == null)
  596. throw new ArgumentNullException("info");
  597. info.AddValue("Version", generation);
  598. info.AddValue("Comparer", hcp);
  599. // MS.NET expects either *no* KeyValuePairs field (when count = 0)
  600. // or a non-null KeyValuePairs field. We don't omit the field to
  601. // remain compatible with older monos, but we also doesn't serialize
  602. // it as null to make MS.NET happy.
  603. KeyValuePair<TKey, TValue>[] data = new KeyValuePair<TKey, TValue>[count];
  604. if (count > 0)
  605. CopyTo(data, 0);
  606. info.AddValue("HashSize", table.Length);
  607. info.AddValue("KeyValuePairs", data);
  608. }
  609. public virtual void OnDeserialization(object sender)
  610. {
  611. if (serialization_info == null)
  612. return;
  613. int hashSize = 0;
  614. KeyValuePair<TKey, TValue>[] data = null;
  615. // We must use the enumerator because MS.NET doesn't
  616. // serialize "KeyValuePairs" for count = 0.
  617. SerializationInfoEnumerator e = serialization_info.GetEnumerator();
  618. while (e.MoveNext())
  619. {
  620. switch (e.Name)
  621. {
  622. case "Version":
  623. generation = (int)e.Value;
  624. break;
  625. case "Comparer":
  626. hcp = (IEqualityComparer<TKey>)e.Value;
  627. break;
  628. case "HashSize":
  629. hashSize = (int)e.Value;
  630. break;
  631. case "KeyValuePairs":
  632. data = (KeyValuePair<TKey, TValue>[])e.Value;
  633. break;
  634. }
  635. }
  636. if (hcp == null)
  637. hcp = EqualityComparer<TKey>.Default;
  638. if (hashSize < INITIAL_SIZE)
  639. hashSize = INITIAL_SIZE;
  640. InitArrays(hashSize);
  641. count = 0;
  642. if (data != null)
  643. {
  644. for (int i = 0; i < data.Length; ++i)
  645. Add(data[i].Key, data[i].Value);
  646. }
  647. generation++;
  648. serialization_info = null;
  649. }
  650. public bool Remove(TKey key)
  651. {
  652. if (key == null)
  653. throw new ArgumentNullException("key");
  654. // get first item of linked list corresponding to given key
  655. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  656. int index = (hashCode & int.MaxValue) % table.Length;
  657. int cur = table[index] - 1;
  658. // if there is no linked list, return false
  659. if (cur == NO_SLOT)
  660. return false;
  661. // walk linked list until right slot (and its predecessor) is
  662. // found or end is reached
  663. int prev = NO_SLOT;
  664. do
  665. {
  666. // The ordering is important for compatibility with MS and strange
  667. // Object.Equals () implementations
  668. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  669. break;
  670. prev = cur;
  671. cur = linkSlots[cur].Next;
  672. } while (cur != NO_SLOT);
  673. // if we reached the end of the chain, return false
  674. if (cur == NO_SLOT)
  675. return false;
  676. count--;
  677. // remove slot from linked list
  678. // is slot at beginning of linked list?
  679. if (prev == NO_SLOT)
  680. table[index] = linkSlots[cur].Next + 1;
  681. else
  682. linkSlots[prev].Next = linkSlots[cur].Next;
  683. // mark slot as empty and prepend it to "empty slots chain"
  684. linkSlots[cur].Next = emptySlot;
  685. emptySlot = cur;
  686. linkSlots[cur].HashCode = 0;
  687. // clear empty key and value slots
  688. keySlots[cur] = default(TKey);
  689. valueSlots[cur] = default(TValue);
  690. generation++;
  691. return true;
  692. }
  693. public bool TryGetValue(TKey key, out TValue value)
  694. {
  695. if (key == null)
  696. throw new ArgumentNullException("key");
  697. // get first item of linked list corresponding to given key
  698. int hashCode = hcp.GetHashCode(key) | HASH_FLAG;
  699. int cur = table[(hashCode & int.MaxValue) % table.Length] - 1;
  700. // walk linked list until right slot is found or end is reached
  701. while (cur != NO_SLOT)
  702. {
  703. // The ordering is important for compatibility with MS and strange
  704. // Object.Equals () implementations
  705. if (linkSlots[cur].HashCode == hashCode && hcp.Equals(keySlots[cur], key))
  706. {
  707. value = valueSlots[cur];
  708. return true;
  709. }
  710. cur = linkSlots[cur].Next;
  711. }
  712. // we did not find the slot
  713. value = default(TValue);
  714. return false;
  715. }
  716. ICollection<TKey> IDictionary<TKey, TValue>.Keys
  717. {
  718. get { return Keys; }
  719. }
  720. ICollection<TValue> IDictionary<TKey, TValue>.Values
  721. {
  722. get { return Values; }
  723. }
  724. #if NET_4_5
  725. IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
  726. get { return Keys; }
  727. }
  728. IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
  729. get { return Values; }
  730. }
  731. #endif
  732. public KeyCollection Keys
  733. {
  734. get { return new KeyCollection(this); }
  735. }
  736. public ValueCollection Values
  737. {
  738. get { return new ValueCollection(this); }
  739. }
  740. ICollection IDictionary.Keys
  741. {
  742. get { return Keys; }
  743. }
  744. ICollection IDictionary.Values
  745. {
  746. get { return Values; }
  747. }
  748. bool IDictionary.IsFixedSize
  749. {
  750. get { return false; }
  751. }
  752. bool IDictionary.IsReadOnly
  753. {
  754. get { return false; }
  755. }
  756. static TKey ToTKey(object key)
  757. {
  758. if (key == null)
  759. throw new ArgumentNullException("key");
  760. if (!(key is TKey))
  761. throw new ArgumentException("not of type: " + typeof(TKey).ToString(), "key");
  762. return (TKey)key;
  763. }
  764. static TValue ToTValue(object value)
  765. {
  766. if (value == null && !typeof(TValue).IsValueType)
  767. return default(TValue);
  768. if (!(value is TValue))
  769. throw new ArgumentException("not of type: " + typeof(TValue).ToString(), "value");
  770. return (TValue)value;
  771. }
  772. object IDictionary.this[object key]
  773. {
  774. get
  775. {
  776. if (key is TKey && ContainsKey((TKey)key))
  777. return this[ToTKey(key)];
  778. return null;
  779. }
  780. set { this[ToTKey(key)] = ToTValue(value); }
  781. }
  782. void IDictionary.Add(object key, object value)
  783. {
  784. this.Add(ToTKey(key), ToTValue(value));
  785. }
  786. bool IDictionary.Contains(object key)
  787. {
  788. if (key == null)
  789. throw new ArgumentNullException("key");
  790. if (key is TKey)
  791. return ContainsKey((TKey)key);
  792. return false;
  793. }
  794. void IDictionary.Remove(object key)
  795. {
  796. if (key == null)
  797. throw new ArgumentNullException("key");
  798. if (key is TKey)
  799. Remove((TKey)key);
  800. }
  801. bool ICollection.IsSynchronized
  802. {
  803. get { return false; }
  804. }
  805. object ICollection.SyncRoot
  806. {
  807. get { return this; }
  808. }
  809. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
  810. {
  811. get { return false; }
  812. }
  813. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)
  814. {
  815. Add(keyValuePair.Key, keyValuePair.Value);
  816. }
  817. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
  818. {
  819. return ContainsKeyValuePair(keyValuePair);
  820. }
  821. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
  822. {
  823. this.CopyTo(array, index);
  824. }
  825. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
  826. {
  827. if (!ContainsKeyValuePair(keyValuePair))
  828. return false;
  829. return Remove(keyValuePair.Key);
  830. }
  831. bool ContainsKeyValuePair(KeyValuePair<TKey, TValue> pair)
  832. {
  833. TValue value;
  834. if (!TryGetValue(pair.Key, out value))
  835. return false;
  836. return EqualityComparer<TValue>.Default.Equals(pair.Value, value);
  837. }
  838. void ICollection.CopyTo(Array array, int index)
  839. {
  840. KeyValuePair<TKey, TValue>[] pairs = array as KeyValuePair<TKey, TValue>[];
  841. if (pairs != null)
  842. {
  843. this.CopyTo(pairs, index);
  844. return;
  845. }
  846. CopyToCheck(array, index);
  847. DictionaryEntry[] entries = array as DictionaryEntry[];
  848. if (entries != null)
  849. {
  850. for (int i = 0; i < touchedSlots; i++)
  851. {
  852. if ((linkSlots[i].HashCode & HASH_FLAG) != 0)
  853. entries[index++] = new DictionaryEntry(keySlots[i], valueSlots[i]);
  854. }
  855. return;
  856. }
  857. Do_ICollectionCopyTo<KeyValuePair<TKey, TValue>>(array, index, make_pair);
  858. }
  859. IEnumerator IEnumerable.GetEnumerator()
  860. {
  861. return new Enumerator(this);
  862. }
  863. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  864. {
  865. return new Enumerator(this);
  866. }
  867. IDictionaryEnumerator IDictionary.GetEnumerator()
  868. {
  869. return new ShimEnumerator(this);
  870. }
  871. public Enumerator GetEnumerator()
  872. {
  873. return new Enumerator(this);
  874. }
  875. int iteratorIndex = -1;
  876. bool iteratorState = false;
  877. public void Begin()
  878. {
  879. iteratorIndex = -1;
  880. iteratorState = true;
  881. }
  882. public bool Next()
  883. {
  884. if (iteratorIndex >= touchedSlots)
  885. {
  886. iteratorIndex = -1;
  887. iteratorState = false;
  888. return false;
  889. }
  890. while (++iteratorIndex < touchedSlots)
  891. {
  892. if ((linkSlots[iteratorIndex].HashCode & HASH_FLAG) != 0)
  893. {
  894. return true;
  895. }
  896. }
  897. iteratorIndex = -1;
  898. iteratorState = false;
  899. return false;
  900. }
  901. public TKey Key
  902. {
  903. get
  904. {
  905. if (!iteratorState)
  906. {
  907. throw new Exception("Key must access in iterator");
  908. }
  909. if (iteratorIndex < 0 || iteratorIndex >= touchedSlots)
  910. {
  911. throw new IndexOutOfRangeException("iterator invalid");
  912. }
  913. return keySlots[iteratorIndex];
  914. }
  915. }
  916. public TValue Value
  917. {
  918. get
  919. {
  920. if (!iteratorState)
  921. {
  922. throw new Exception("Value must access in iterator");
  923. }
  924. if (iteratorIndex < 0 || iteratorIndex >= touchedSlots)
  925. {
  926. throw new IndexOutOfRangeException("iterator invalid");
  927. }
  928. return valueSlots[iteratorIndex];
  929. }
  930. }
  931. [Serializable]
  932. private class ShimEnumerator : IDictionaryEnumerator, IEnumerator
  933. {
  934. Enumerator host_enumerator;
  935. public ShimEnumerator(Map<TKey, TValue> host)
  936. {
  937. host_enumerator = host.GetEnumerator();
  938. }
  939. public void Dispose()
  940. {
  941. host_enumerator.Dispose();
  942. }
  943. public bool MoveNext()
  944. {
  945. return host_enumerator.MoveNext();
  946. }
  947. public DictionaryEntry Entry
  948. {
  949. get { return ((IDictionaryEnumerator)host_enumerator).Entry; }
  950. }
  951. public object Key
  952. {
  953. get { return host_enumerator.Current.Key; }
  954. }
  955. public object Value
  956. {
  957. get { return host_enumerator.Current.Value; }
  958. }
  959. // This is the raison d' etre of this $%!@$%@^@ class.
  960. // We want: IDictionary.GetEnumerator ().Current is DictionaryEntry
  961. public object Current
  962. {
  963. get { return Entry; }
  964. }
  965. public void Reset()
  966. {
  967. host_enumerator.Reset();
  968. }
  969. }
  970. [Serializable]
  971. public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>,
  972. IDisposable, IDictionaryEnumerator, IEnumerator
  973. {
  974. Map<TKey, TValue> mapContainer;
  975. int next;
  976. int stamp;
  977. internal KeyValuePair<TKey, TValue> current;
  978. internal Enumerator(Map<TKey, TValue> mapContainer)
  979. : this()
  980. {
  981. this.mapContainer = mapContainer;
  982. stamp = mapContainer.generation;
  983. }
  984. public bool MoveNext()
  985. {
  986. VerifyState();
  987. if (next < 0)
  988. return false;
  989. while (next < mapContainer.touchedSlots)
  990. {
  991. int cur = next++;
  992. if ((mapContainer.linkSlots[cur].HashCode & HASH_FLAG) != 0)
  993. {
  994. current = new KeyValuePair<TKey, TValue>(
  995. mapContainer.keySlots[cur],
  996. mapContainer.valueSlots[cur]
  997. );
  998. return true;
  999. }
  1000. }
  1001. next = -1;
  1002. return false;
  1003. }
  1004. // No error checking happens. Usually, Current is immediately preceded by a MoveNext(), so it's wasteful to check again
  1005. public KeyValuePair<TKey, TValue> Current
  1006. {
  1007. get { return current; }
  1008. }
  1009. internal TKey CurrentKey
  1010. {
  1011. get
  1012. {
  1013. VerifyCurrent();
  1014. return current.Key;
  1015. }
  1016. }
  1017. internal TValue CurrentValue
  1018. {
  1019. get
  1020. {
  1021. VerifyCurrent();
  1022. return current.Value;
  1023. }
  1024. }
  1025. object IEnumerator.Current
  1026. {
  1027. get
  1028. {
  1029. VerifyCurrent();
  1030. return current;
  1031. }
  1032. }
  1033. void IEnumerator.Reset()
  1034. {
  1035. Reset();
  1036. }
  1037. internal void Reset()
  1038. {
  1039. VerifyState();
  1040. next = 0;
  1041. }
  1042. DictionaryEntry IDictionaryEnumerator.Entry
  1043. {
  1044. get
  1045. {
  1046. VerifyCurrent();
  1047. return new DictionaryEntry(current.Key, current.Value);
  1048. }
  1049. }
  1050. object IDictionaryEnumerator.Key
  1051. {
  1052. get { return CurrentKey; }
  1053. }
  1054. object IDictionaryEnumerator.Value
  1055. {
  1056. get { return CurrentValue; }
  1057. }
  1058. void VerifyState()
  1059. {
  1060. if (mapContainer == null)
  1061. throw new ObjectDisposedException(null);
  1062. if (mapContainer.generation != stamp)
  1063. throw new InvalidOperationException("out of sync");
  1064. }
  1065. void VerifyCurrent()
  1066. {
  1067. VerifyState();
  1068. if (next <= 0)
  1069. throw new InvalidOperationException("Current is not valid");
  1070. }
  1071. public void Dispose()
  1072. {
  1073. mapContainer = null;
  1074. }
  1075. }
  1076. // This collection is a read only collection
  1077. [Serializable]
  1078. [DebuggerDisplay("Count={Count}")]
  1079. [DebuggerTypeProxy(typeof(CollectionDebuggerView<,>))]
  1080. public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable
  1081. {
  1082. Map<TKey, TValue> mapContainer;
  1083. public KeyCollection(Map<TKey, TValue> mapContainer)
  1084. {
  1085. if (mapContainer == null)
  1086. throw new ArgumentNullException("mapContainer");
  1087. this.mapContainer = mapContainer;
  1088. }
  1089. public void CopyTo(TKey[] array, int index)
  1090. {
  1091. mapContainer.CopyToCheck(array, index);
  1092. mapContainer.CopyKeys(array, index);
  1093. }
  1094. public Enumerator GetEnumerator()
  1095. {
  1096. return new Enumerator(mapContainer);
  1097. }
  1098. void ICollection<TKey>.Add(TKey item)
  1099. {
  1100. throw new NotSupportedException("this is a read-only collection");
  1101. }
  1102. void ICollection<TKey>.Clear()
  1103. {
  1104. throw new NotSupportedException("this is a read-only collection");
  1105. }
  1106. bool ICollection<TKey>.Contains(TKey item)
  1107. {
  1108. return mapContainer.ContainsKey(item);
  1109. }
  1110. bool ICollection<TKey>.Remove(TKey item)
  1111. {
  1112. throw new NotSupportedException("this is a read-only collection");
  1113. }
  1114. IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator()
  1115. {
  1116. return this.GetEnumerator();
  1117. }
  1118. void ICollection.CopyTo(Array array, int index)
  1119. {
  1120. var target = array as TKey[];
  1121. if (target != null)
  1122. {
  1123. CopyTo(target, index);
  1124. return;
  1125. }
  1126. mapContainer.CopyToCheck(array, index);
  1127. mapContainer.Do_ICollectionCopyTo<TKey>(array, index, pick_key);
  1128. }
  1129. IEnumerator IEnumerable.GetEnumerator()
  1130. {
  1131. return this.GetEnumerator();
  1132. }
  1133. public int Count
  1134. {
  1135. get { return mapContainer.Count; }
  1136. }
  1137. bool ICollection<TKey>.IsReadOnly
  1138. {
  1139. get { return true; }
  1140. }
  1141. bool ICollection.IsSynchronized
  1142. {
  1143. get { return false; }
  1144. }
  1145. object ICollection.SyncRoot
  1146. {
  1147. get { return ((ICollection)mapContainer).SyncRoot; }
  1148. }
  1149. [Serializable]
  1150. public struct Enumerator : IEnumerator<TKey>, IDisposable, IEnumerator
  1151. {
  1152. Map<TKey, TValue>.Enumerator host_enumerator;
  1153. internal Enumerator(Map<TKey, TValue> host)
  1154. {
  1155. host_enumerator = host.GetEnumerator();
  1156. }
  1157. public void Dispose()
  1158. {
  1159. host_enumerator.Dispose();
  1160. }
  1161. public bool MoveNext()
  1162. {
  1163. return host_enumerator.MoveNext();
  1164. }
  1165. public TKey Current
  1166. {
  1167. get { return host_enumerator.current.Key; }
  1168. }
  1169. object IEnumerator.Current
  1170. {
  1171. get { return host_enumerator.CurrentKey; }
  1172. }
  1173. void IEnumerator.Reset()
  1174. {
  1175. host_enumerator.Reset();
  1176. }
  1177. }
  1178. }
  1179. // This collection is a read only collection
  1180. [Serializable]
  1181. [DebuggerDisplay("Count={Count}")]
  1182. [DebuggerTypeProxy(typeof(CollectionDebuggerView<,>))]
  1183. public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable
  1184. {
  1185. Map<TKey, TValue> mapContainer;
  1186. public ValueCollection(Map<TKey, TValue> mapContainer)
  1187. {
  1188. if (mapContainer == null)
  1189. throw new ArgumentNullException("mapContainer");
  1190. this.mapContainer = mapContainer;
  1191. }
  1192. public void CopyTo(TValue[] array, int index)
  1193. {
  1194. mapContainer.CopyToCheck(array, index);
  1195. mapContainer.CopyValues(array, index);
  1196. }
  1197. public Enumerator GetEnumerator()
  1198. {
  1199. return new Enumerator(mapContainer);
  1200. }
  1201. void ICollection<TValue>.Add(TValue item)
  1202. {
  1203. throw new NotSupportedException("this is a read-only collection");
  1204. }
  1205. void ICollection<TValue>.Clear()
  1206. {
  1207. throw new NotSupportedException("this is a read-only collection");
  1208. }
  1209. bool ICollection<TValue>.Contains(TValue item)
  1210. {
  1211. return mapContainer.ContainsValue(item);
  1212. }
  1213. bool ICollection<TValue>.Remove(TValue item)
  1214. {
  1215. throw new NotSupportedException("this is a read-only collection");
  1216. }
  1217. IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator()
  1218. {
  1219. return this.GetEnumerator();
  1220. }
  1221. void ICollection.CopyTo(Array array, int index)
  1222. {
  1223. var target = array as TValue[];
  1224. if (target != null)
  1225. {
  1226. CopyTo(target, index);
  1227. return;
  1228. }
  1229. mapContainer.CopyToCheck(array, index);
  1230. mapContainer.Do_ICollectionCopyTo<TValue>(array, index, pick_value);
  1231. }
  1232. IEnumerator IEnumerable.GetEnumerator()
  1233. {
  1234. return this.GetEnumerator();
  1235. }
  1236. public int Count
  1237. {
  1238. get { return mapContainer.Count; }
  1239. }
  1240. bool ICollection<TValue>.IsReadOnly
  1241. {
  1242. get { return true; }
  1243. }
  1244. bool ICollection.IsSynchronized
  1245. {
  1246. get { return false; }
  1247. }
  1248. object ICollection.SyncRoot
  1249. {
  1250. get { return ((ICollection)mapContainer).SyncRoot; }
  1251. }
  1252. [Serializable]
  1253. public struct Enumerator : IEnumerator<TValue>, IDisposable, IEnumerator
  1254. {
  1255. Map<TKey, TValue>.Enumerator host_enumerator;
  1256. internal Enumerator(Map<TKey, TValue> host)
  1257. {
  1258. host_enumerator = host.GetEnumerator();
  1259. }
  1260. public void Dispose()
  1261. {
  1262. host_enumerator.Dispose();
  1263. }
  1264. public bool MoveNext()
  1265. {
  1266. return host_enumerator.MoveNext();
  1267. }
  1268. public TValue Current
  1269. {
  1270. get { return host_enumerator.current.Value; }
  1271. }
  1272. object IEnumerator.Current
  1273. {
  1274. get { return host_enumerator.CurrentValue; }
  1275. }
  1276. void IEnumerator.Reset()
  1277. {
  1278. host_enumerator.Reset();
  1279. }
  1280. }
  1281. }
  1282. public TValue GetValueByIndex(int index)
  1283. {
  1284. return valueSlots[index];
  1285. }
  1286. }
  1287. }