StringDictionarySlim.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. namespace Jint.Collections
  11. {
  12. /// <summary>
  13. /// DictionarySlim<string, TValue> is similar to Dictionary<TKey, TValue> but optimized in three ways:
  14. /// 1) It allows access to the value by ref replacing the common TryGetValue and Add pattern.
  15. /// 2) It does not store the hash code (assumes it is cheap to equate values).
  16. /// 3) It does not accept an equality comparer (assumes Object.GetHashCode() and Object.Equals() or overridden implementation are cheap and sufficient).
  17. /// </summary>
  18. [DebuggerTypeProxy(typeof(DictionarySlimDebugView<>))]
  19. [DebuggerDisplay("Count = {Count}")]
  20. internal sealed class StringDictionarySlim<TValue> : IReadOnlyCollection<KeyValuePair<string, TValue>>
  21. {
  22. // We want to initialize without allocating arrays. We also want to avoid null checks.
  23. // Array.Empty would give divide by zero in modulo operation. So we use static one element arrays.
  24. // The first add will cause a resize replacing these with real arrays of three elements.
  25. // Arrays are wrapped in a class to avoid being duplicated for each <TKey, TValue>
  26. private static readonly Entry[] InitialEntries = new Entry[1];
  27. private int _count;
  28. // 0-based index into _entries of head of free chain: -1 means empty
  29. private int _freeList = -1;
  30. // 1-based index into _entries; 0 means empty
  31. private int[] _buckets;
  32. private Entry[] _entries;
  33. [DebuggerDisplay("({key}, {value})->{next}")]
  34. private struct Entry
  35. {
  36. public Key key;
  37. public TValue value;
  38. // 0-based index of next entry in chain: -1 means end of chain
  39. // also encodes whether this entry _itself_ is part of the free list by changing sign and subtracting 3,
  40. // so -2 means end of free list, -3 means index 0 but on free list, -4 means index 1 but on free list, etc.
  41. public int next;
  42. }
  43. public StringDictionarySlim()
  44. {
  45. _buckets = HashHelpers.SizeOneIntArray;
  46. _entries = InitialEntries;
  47. }
  48. public StringDictionarySlim(int capacity)
  49. {
  50. if (capacity < 2)
  51. capacity = 2; // 1 would indicate the dummy array
  52. capacity = HashHelpers.PowerOf2(capacity);
  53. _buckets = new int[capacity];
  54. _entries = new Entry[capacity];
  55. }
  56. public int Count => _count;
  57. /// <summary>
  58. /// Clears the dictionary. Note that this invalidates any active enumerators.
  59. /// </summary>
  60. public void Clear()
  61. {
  62. _count = 0;
  63. _freeList = -1;
  64. _buckets = HashHelpers.SizeOneIntArray;
  65. _entries = InitialEntries;
  66. }
  67. public bool ContainsKey(in Key key)
  68. {
  69. Entry[] entries = _entries;
  70. for (int i = _buckets[key.HashCode & (_buckets.Length-1)] - 1;
  71. (uint)i < (uint)entries.Length; i = entries[i].next)
  72. {
  73. if (key == entries[i].key)
  74. return true;
  75. }
  76. return false;
  77. }
  78. public bool TryGetValue(in Key key, out TValue value)
  79. {
  80. Entry[] entries = _entries;
  81. for (int i = _buckets[key.HashCode & (_buckets.Length - 1)] - 1;
  82. (uint)i < (uint)entries.Length; i = entries[i].next)
  83. {
  84. if (key == entries[i].key)
  85. {
  86. value = entries[i].value;
  87. return true;
  88. }
  89. }
  90. value = default;
  91. return false;
  92. }
  93. public bool Remove(in Key key)
  94. {
  95. Entry[] entries = _entries;
  96. int bucketIndex = key.HashCode & (_buckets.Length - 1);
  97. int entryIndex = _buckets[bucketIndex] - 1;
  98. int lastIndex = -1;
  99. while (entryIndex != -1)
  100. {
  101. Entry candidate = entries[entryIndex];
  102. if (candidate.key == key)
  103. {
  104. if (lastIndex != -1)
  105. { // Fixup preceding element in chain to point to next (if any)
  106. entries[lastIndex].next = candidate.next;
  107. }
  108. else
  109. { // Fixup bucket to new head (if any)
  110. _buckets[bucketIndex] = candidate.next + 1;
  111. }
  112. entries[entryIndex] = default;
  113. entries[entryIndex].next = -3 - _freeList; // New head of free list
  114. _freeList = entryIndex;
  115. _count--;
  116. return true;
  117. }
  118. lastIndex = entryIndex;
  119. entryIndex = candidate.next;
  120. }
  121. return false;
  122. }
  123. // Not safe for concurrent _reads_ (at least, if either of them add)
  124. // For concurrent reads, prefer TryGetValue(key, out value)
  125. /// <summary>
  126. /// Gets the value for the specified key, or, if the key is not present,
  127. /// adds an entry and returns the value by ref. This makes it possible to
  128. /// add or update a value in a single look up operation.
  129. /// </summary>
  130. /// <param name="key">Key to look for</param>
  131. /// <returns>Reference to the new or existing value</returns>
  132. public ref TValue GetOrAddValueRef(in Key key)
  133. {
  134. Entry[] entries = _entries;
  135. int bucketIndex = key.HashCode & (_buckets.Length - 1);
  136. for (int i = _buckets[bucketIndex] - 1;
  137. (uint)i < (uint)entries.Length; i = entries[i].next)
  138. {
  139. if (key == entries[i].key)
  140. return ref entries[i].value;
  141. }
  142. return ref AddKey(key, bucketIndex);
  143. }
  144. public ref TValue this[in Key key]
  145. {
  146. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  147. get => ref GetOrAddValueRef(key);
  148. }
  149. [MethodImpl(MethodImplOptions.NoInlining)]
  150. private ref TValue AddKey(in Key key, int bucketIndex)
  151. {
  152. Entry[] entries = _entries;
  153. int entryIndex;
  154. if (_freeList != -1)
  155. {
  156. entryIndex = _freeList;
  157. _freeList = -3 - entries[_freeList].next;
  158. }
  159. else
  160. {
  161. if (_count == entries.Length || entries.Length == 1)
  162. {
  163. entries = Resize();
  164. bucketIndex = key.HashCode & (_buckets.Length - 1);
  165. // entry indexes were not changed by Resize
  166. }
  167. entryIndex = _count;
  168. }
  169. entries[entryIndex].key = key;
  170. entries[entryIndex].next = _buckets[bucketIndex] - 1;
  171. _buckets[bucketIndex] = entryIndex + 1;
  172. _count++;
  173. return ref entries[entryIndex].value;
  174. }
  175. private Entry[] Resize()
  176. {
  177. Debug.Assert(_entries.Length == _count || _entries.Length == 1); // We only copy _count, so if it's longer we will miss some
  178. int count = _count;
  179. int newSize = _entries.Length * 2;
  180. if ((uint)newSize > (uint)int.MaxValue) // uint cast handles overflow
  181. throw new InvalidOperationException("Capacity Overflow");
  182. var entries = new Entry[newSize];
  183. Array.Copy(_entries, 0, entries, 0, count);
  184. var newBuckets = new int[entries.Length];
  185. while (count-- > 0)
  186. {
  187. int bucketIndex = entries[count].key.HashCode & (newBuckets.Length - 1);
  188. entries[count].next = newBuckets[bucketIndex] - 1;
  189. newBuckets[bucketIndex] = count + 1;
  190. }
  191. _buckets = newBuckets;
  192. _entries = entries;
  193. return entries;
  194. }
  195. /// <summary>
  196. /// Gets an enumerator over the dictionary
  197. /// </summary>
  198. public Enumerator GetEnumerator() => new Enumerator(this); // avoid boxing
  199. /// <summary>
  200. /// Gets an enumerator over the dictionary
  201. /// </summary>
  202. IEnumerator<KeyValuePair<string, TValue>> IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator() =>
  203. new Enumerator(this);
  204. /// <summary>
  205. /// Gets an enumerator over the dictionary
  206. /// </summary>
  207. IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
  208. public struct Enumerator : IEnumerator<KeyValuePair<string, TValue>>
  209. {
  210. private readonly StringDictionarySlim<TValue> _dictionary;
  211. private int _index;
  212. private KeyValuePair<string, TValue> _current;
  213. internal Enumerator(StringDictionarySlim<TValue> dictionary)
  214. {
  215. _dictionary = dictionary;
  216. _index = 0;
  217. _current = default;
  218. }
  219. public bool MoveNext()
  220. {
  221. if (_index == _dictionary._count)
  222. {
  223. _current = default;
  224. return false;
  225. }
  226. _current = new KeyValuePair<string, TValue>(
  227. _dictionary._entries[_index].key,
  228. _dictionary._entries[_index++].value);
  229. return true;
  230. }
  231. public KeyValuePair<string, TValue> Current => _current;
  232. object IEnumerator.Current => _current;
  233. void IEnumerator.Reset()
  234. {
  235. _index = 0;
  236. }
  237. public void Dispose() { }
  238. }
  239. internal static class HashHelpers
  240. {
  241. internal static readonly int[] SizeOneIntArray = new int[1];
  242. internal static int PowerOf2(int v)
  243. {
  244. if ((v & (v - 1)) == 0) return v;
  245. int i = 2;
  246. while (i < v) i <<= 1;
  247. return i;
  248. }
  249. }
  250. internal sealed class DictionarySlimDebugView<V>
  251. {
  252. private readonly StringDictionarySlim<V> _dictionary;
  253. public DictionarySlimDebugView(StringDictionarySlim<V> dictionary)
  254. {
  255. _dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary));
  256. }
  257. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  258. public KeyValuePair<string, V>[] Items
  259. {
  260. get
  261. {
  262. return _dictionary.ToArray();
  263. }
  264. }
  265. }
  266. }
  267. }