HybridDictionary.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #nullable disable
  2. using System.Collections;
  3. using System.Runtime.CompilerServices;
  4. namespace Jint.Collections;
  5. internal sealed class HybridDictionary<TValue> : IEngineDictionary<Key, TValue>, IEnumerable<KeyValuePair<Key, TValue>>
  6. {
  7. private const int CutoverPoint = 9;
  8. private const int InitialDictionarySize = 13;
  9. private const int FixedSizeCutoverPoint = 6;
  10. private readonly bool _checkExistingKeys;
  11. private ListDictionary<TValue> _list;
  12. internal StringDictionarySlim<TValue> _dictionary;
  13. public HybridDictionary() : this(0, checkExistingKeys: true)
  14. {
  15. }
  16. public HybridDictionary(int initialSize, bool checkExistingKeys)
  17. {
  18. _checkExistingKeys = checkExistingKeys;
  19. if (initialSize >= FixedSizeCutoverPoint)
  20. {
  21. _dictionary = new StringDictionarySlim<TValue>(initialSize);
  22. }
  23. }
  24. public HybridDictionary(StringDictionarySlim<TValue> dictionary)
  25. {
  26. _checkExistingKeys = true;
  27. _dictionary = dictionary;
  28. }
  29. public ref TValue this[Key key]
  30. {
  31. get
  32. {
  33. if (_dictionary != null)
  34. {
  35. return ref _dictionary[key];
  36. }
  37. if (_list != null)
  38. {
  39. if (_list.Count >= CutoverPoint - 1)
  40. {
  41. return ref SwitchToDictionary(key);
  42. }
  43. return ref _list[key];
  44. }
  45. var head = new ListDictionary<TValue>.DictionaryNode { Key = key, Value = default };
  46. _list = new ListDictionary<TValue>(head, _checkExistingKeys);
  47. return ref head.Value;
  48. }
  49. }
  50. public bool TryGetValue(Key key, out TValue value)
  51. {
  52. if (_dictionary != null)
  53. {
  54. return _dictionary.TryGetValue(key, out value);
  55. }
  56. if (_list != null)
  57. {
  58. return _list.TryGetValue(key, out value);
  59. }
  60. value = default;
  61. return false;
  62. }
  63. public ref TValue GetValueRefOrNullRef(Key key)
  64. {
  65. if (_dictionary != null)
  66. {
  67. return ref _dictionary.GetValueRefOrNullRef(key);
  68. }
  69. if (_list != null)
  70. {
  71. return ref _list.GetValueRefOrNullRef(key);
  72. }
  73. return ref Unsafe.NullRef<TValue>();
  74. }
  75. public ref TValue GetValueRefOrAddDefault(Key key, out bool exists)
  76. {
  77. if (_dictionary != null)
  78. {
  79. return ref _dictionary.GetValueRefOrAddDefault(key, out exists);
  80. }
  81. if (_list != null)
  82. {
  83. return ref _list.GetValueRefOrAddDefault(key, out exists);
  84. }
  85. var head = new ListDictionary<TValue>.DictionaryNode
  86. {
  87. Key = key,
  88. };
  89. _list = new ListDictionary<TValue>(head, _checkExistingKeys);
  90. exists = false;
  91. return ref head.Value;
  92. }
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. public void SetOrUpdateValue<TState>(Key key, Func<TValue, TState, TValue> updater, TState state)
  95. {
  96. ref var currentValue = ref GetValueRefOrAddDefault(key, out _);
  97. currentValue = updater(currentValue, state);
  98. }
  99. private bool SwitchToDictionary(Key key, TValue value, bool tryAdd, int capacity = InitialDictionarySize)
  100. {
  101. SwitchToDictionary(capacity);
  102. if (tryAdd)
  103. {
  104. return _dictionary.TryAdd(key, value);
  105. }
  106. _dictionary[key] = value;
  107. return true;
  108. }
  109. private ref TValue SwitchToDictionary(Key key, int capacity = InitialDictionarySize)
  110. {
  111. SwitchToDictionary(capacity);
  112. return ref _dictionary[key];
  113. }
  114. private void SwitchToDictionary(int capacity = InitialDictionarySize)
  115. {
  116. var dictionary = new StringDictionarySlim<TValue>(capacity);
  117. if (_list is not null)
  118. {
  119. foreach (var pair in _list)
  120. {
  121. dictionary[pair.Key] = pair.Value;
  122. }
  123. }
  124. _dictionary = dictionary;
  125. _list = null;
  126. }
  127. public int Count
  128. {
  129. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  130. get => _dictionary?.Count ?? _list?.Count ?? 0;
  131. }
  132. public void EnsureCapacity(int capacity)
  133. {
  134. if (_dictionary is not null)
  135. {
  136. // not implemented yet
  137. return;
  138. }
  139. if (capacity >= CutoverPoint)
  140. {
  141. SwitchToDictionary(capacity);
  142. }
  143. }
  144. public bool TryAdd(Key key, TValue value)
  145. {
  146. if (_dictionary != null)
  147. {
  148. return _dictionary.TryAdd(key, value);
  149. }
  150. _list ??= new ListDictionary<TValue>(key, value, _checkExistingKeys);
  151. if (_list.Count + 1 >= CutoverPoint)
  152. {
  153. return SwitchToDictionary(key, value, tryAdd: true);
  154. }
  155. return _list.Add(key, value, tryAdd: true);
  156. }
  157. /// <summary>
  158. /// Adds a new item and expects key to not exist.
  159. /// </summary>
  160. public void AddDangerous(Key key, TValue value)
  161. {
  162. if (_dictionary != null)
  163. {
  164. _dictionary.AddDangerous(key, value);
  165. }
  166. else
  167. {
  168. if (_list == null)
  169. {
  170. _list = new ListDictionary<TValue>(key, value, _checkExistingKeys);
  171. }
  172. else
  173. {
  174. if (_list.Count + 1 >= CutoverPoint)
  175. {
  176. SwitchToDictionary(key) = value;
  177. }
  178. else
  179. {
  180. _list.AddDangerous(key, value);
  181. }
  182. }
  183. }
  184. }
  185. public void Clear()
  186. {
  187. _dictionary?.Clear();
  188. _list?.Clear();
  189. }
  190. public bool ContainsKey(Key key)
  191. {
  192. ref var valueRefOrNullRef = ref GetValueRefOrNullRef(key);
  193. return !Unsafe.IsNullRef(ref valueRefOrNullRef);
  194. }
  195. IEnumerator<KeyValuePair<Key, TValue>> IEnumerable<KeyValuePair<Key, TValue>>.GetEnumerator()
  196. {
  197. if (_dictionary != null)
  198. {
  199. return _dictionary.GetEnumerator();
  200. }
  201. if (_list != null)
  202. {
  203. return _list.GetEnumerator();
  204. }
  205. return System.Linq.Enumerable.Empty<KeyValuePair<Key, TValue>>().GetEnumerator();
  206. }
  207. IEnumerator IEnumerable.GetEnumerator()
  208. {
  209. if (_dictionary != null)
  210. {
  211. return _dictionary.GetEnumerator();
  212. }
  213. if (_list != null)
  214. {
  215. return _list.GetEnumerator();
  216. }
  217. return System.Linq.Enumerable.Empty<KeyValuePair<Key, TValue>>().GetEnumerator();
  218. }
  219. public bool Remove(Key key)
  220. {
  221. if (_dictionary != null)
  222. {
  223. return _dictionary.Remove(key);
  224. }
  225. return _list != null && _list.Remove(key);
  226. }
  227. /// <summary>
  228. /// Optimization when no need to check for existing items.
  229. /// </summary>
  230. public bool CheckExistingKeys
  231. {
  232. set
  233. {
  234. if (_list != null)
  235. {
  236. _list.CheckExistingKeys = value;
  237. }
  238. }
  239. }
  240. }