ListDictionary.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #nullable disable
  2. using System.Collections;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Runtime;
  5. namespace Jint.Collections
  6. {
  7. internal sealed class ListDictionary<TValue> : IEnumerable<KeyValuePair<Key, TValue>>
  8. {
  9. private DictionaryNode _head;
  10. private int _count;
  11. private bool _checkExistingKeys;
  12. public ListDictionary(Key key, TValue value, bool checkExistingKeys)
  13. {
  14. _checkExistingKeys = checkExistingKeys;
  15. _head = new DictionaryNode
  16. {
  17. Key = key,
  18. Value = value
  19. };
  20. _count = 1;
  21. }
  22. public TValue this[Key key]
  23. {
  24. get
  25. {
  26. TryGetValue(key, out var value);
  27. return value;
  28. }
  29. set
  30. {
  31. DictionaryNode last = null;
  32. DictionaryNode node;
  33. var checkExistingKeys = _checkExistingKeys;
  34. for (node = _head; node != null; node = node.Next)
  35. {
  36. var oldKey = node.Key;
  37. if (checkExistingKeys && oldKey == key)
  38. {
  39. break;
  40. }
  41. last = node;
  42. }
  43. if (node != null)
  44. {
  45. // Found it
  46. node.Value = value;
  47. return;
  48. }
  49. AddNode(key, value, last);
  50. }
  51. }
  52. public bool TryGetValue(Key key, out TValue value)
  53. {
  54. var node = _head;
  55. while (node != null)
  56. {
  57. if (node.Key == key)
  58. {
  59. value = node.Value;
  60. return true;
  61. }
  62. node = node.Next;
  63. }
  64. value = default;
  65. return false;
  66. }
  67. public void SetOrUpdateValue<TState>(Key key, Func<TValue, TState, TValue> updater, TState state)
  68. {
  69. DictionaryNode last = null;
  70. DictionaryNode node;
  71. for (node = _head; node != null; node = node.Next)
  72. {
  73. if (node.Key == key)
  74. {
  75. node.Value = updater(node.Value, state);
  76. return;
  77. }
  78. last = node;
  79. }
  80. AddNode(key, updater(default, state), last);
  81. }
  82. public int Count
  83. {
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. get => _count;
  86. }
  87. public bool Add(Key key, TValue value, bool tryAdd = false)
  88. {
  89. DictionaryNode last = null;
  90. DictionaryNode node;
  91. var checkExistingKeys = _checkExistingKeys;
  92. for (node = _head; node != null; node = node.Next)
  93. {
  94. var oldKey = node.Key;
  95. if (checkExistingKeys && oldKey == key)
  96. {
  97. if (tryAdd)
  98. {
  99. return false;
  100. }
  101. ExceptionHelper.ThrowArgumentException();
  102. }
  103. last = node;
  104. }
  105. AddNode(key, value, last);
  106. return true;
  107. }
  108. private void AddNode(Key key, TValue value, DictionaryNode last)
  109. {
  110. var newNode = new DictionaryNode
  111. {
  112. Key = key,
  113. Value = value
  114. };
  115. if (_head is null)
  116. {
  117. _head = newNode;
  118. }
  119. else
  120. {
  121. last.Next = newNode;
  122. }
  123. _count++;
  124. }
  125. public void Clear()
  126. {
  127. _count = 0;
  128. _head = null;
  129. }
  130. public bool ContainsKey(Key key)
  131. {
  132. for (var node = _head; node != null; node = node.Next)
  133. {
  134. var oldKey = node.Key;
  135. if (oldKey == key)
  136. {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. internal bool CheckExistingKeys
  143. {
  144. set => _checkExistingKeys = value;
  145. }
  146. public NodeEnumerator GetEnumerator()
  147. {
  148. return new NodeEnumerator(this);
  149. }
  150. IEnumerator<KeyValuePair<Key, TValue>> IEnumerable<KeyValuePair<Key, TValue>>.GetEnumerator()
  151. {
  152. return new NodeEnumerator(this);
  153. }
  154. IEnumerator IEnumerable.GetEnumerator()
  155. {
  156. return new NodeEnumerator(this);
  157. }
  158. public bool Remove(Key key)
  159. {
  160. DictionaryNode last = null;
  161. DictionaryNode node;
  162. for (node = _head; node != null; node = node.Next)
  163. {
  164. var oldKey = node.Key;
  165. if (oldKey == key)
  166. {
  167. break;
  168. }
  169. last = node;
  170. }
  171. if (node == null)
  172. {
  173. return false;
  174. }
  175. if (node == _head)
  176. {
  177. _head = node.Next;
  178. }
  179. else
  180. {
  181. last.Next = node.Next;
  182. }
  183. _count--;
  184. return true;
  185. }
  186. internal struct NodeEnumerator : IEnumerator<KeyValuePair<Key, TValue>>
  187. {
  188. private readonly ListDictionary<TValue> _list;
  189. private DictionaryNode _current;
  190. private bool _start;
  191. public NodeEnumerator(ListDictionary<TValue> list)
  192. {
  193. _list = list;
  194. _start = true;
  195. _current = null;
  196. }
  197. public KeyValuePair<Key, TValue> Current => new KeyValuePair<Key, TValue>(_current.Key, _current.Value);
  198. public bool MoveNext()
  199. {
  200. if (_start)
  201. {
  202. _current = _list._head;
  203. _start = false;
  204. }
  205. else if (_current != null)
  206. {
  207. _current = _current.Next;
  208. }
  209. return _current != null;
  210. }
  211. void IEnumerator.Reset()
  212. {
  213. _start = true;
  214. _current = null;
  215. }
  216. public void Dispose()
  217. {
  218. }
  219. object IEnumerator.Current => _current;
  220. }
  221. internal sealed class DictionaryNode
  222. {
  223. public Key Key;
  224. public TValue Value;
  225. public DictionaryNode Next;
  226. }
  227. }
  228. }