2
0

ListDictionary.cs 5.7 KB

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