ListDictionary.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Collections;
  2. using System.Collections.Generic;
  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 int Count
  68. {
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. get => _count;
  71. }
  72. public void Add(Key key, TValue value)
  73. {
  74. DictionaryNode last = null;
  75. DictionaryNode node;
  76. var checkExistingKeys = _checkExistingKeys;
  77. for (node = _head; node != null; node = node.Next)
  78. {
  79. var oldKey = node.Key;
  80. if (checkExistingKeys && oldKey == key)
  81. {
  82. ExceptionHelper.ThrowArgumentException();
  83. }
  84. last = node;
  85. }
  86. AddNode(key, value, last);
  87. }
  88. private void AddNode(Key key, TValue value, DictionaryNode last)
  89. {
  90. var newNode = new DictionaryNode
  91. {
  92. Key = key,
  93. Value = value
  94. };
  95. if (_head is null)
  96. {
  97. _head = newNode;
  98. }
  99. else
  100. {
  101. last.Next = newNode;
  102. }
  103. _count++;
  104. }
  105. public void Clear()
  106. {
  107. _count = 0;
  108. _head = null;
  109. }
  110. public bool ContainsKey(Key key)
  111. {
  112. for (var node = _head; node != null; node = node.Next)
  113. {
  114. var oldKey = node.Key;
  115. if (oldKey == key)
  116. {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. internal bool CheckExistingKeys
  123. {
  124. set => _checkExistingKeys = value;
  125. }
  126. public NodeEnumerator GetEnumerator()
  127. {
  128. return new NodeEnumerator(this);
  129. }
  130. IEnumerator<KeyValuePair<Key, TValue>> IEnumerable<KeyValuePair<Key, TValue>>.GetEnumerator()
  131. {
  132. return new NodeEnumerator(this);
  133. }
  134. IEnumerator IEnumerable.GetEnumerator()
  135. {
  136. return new NodeEnumerator(this);
  137. }
  138. public bool Remove(Key key)
  139. {
  140. DictionaryNode last = null;
  141. DictionaryNode node;
  142. for (node = _head; node != null; node = node.Next)
  143. {
  144. var oldKey = node.Key;
  145. if (oldKey == key)
  146. {
  147. break;
  148. }
  149. last = node;
  150. }
  151. if (node == null)
  152. {
  153. return false;
  154. }
  155. if (node == _head)
  156. {
  157. _head = node.Next;
  158. }
  159. else
  160. {
  161. last.Next = node.Next;
  162. }
  163. _count--;
  164. return true;
  165. }
  166. internal struct NodeEnumerator : IEnumerator<KeyValuePair<Key, TValue>>
  167. {
  168. private readonly ListDictionary<TValue> _list;
  169. private DictionaryNode _current;
  170. private bool _start;
  171. public NodeEnumerator(ListDictionary<TValue> list)
  172. {
  173. _list = list;
  174. _start = true;
  175. _current = null;
  176. }
  177. public KeyValuePair<Key, TValue> Current => new KeyValuePair<Key, TValue>(_current.Key, _current.Value);
  178. public bool MoveNext()
  179. {
  180. if (_start)
  181. {
  182. _current = _list._head;
  183. _start = false;
  184. }
  185. else if (_current != null)
  186. {
  187. _current = _current.Next;
  188. }
  189. return _current != null;
  190. }
  191. void IEnumerator.Reset()
  192. {
  193. _start = true;
  194. _current = null;
  195. }
  196. public void Dispose()
  197. {
  198. }
  199. object IEnumerator.Current => _current;
  200. }
  201. internal sealed class DictionaryNode
  202. {
  203. public Key Key;
  204. public TValue Value;
  205. public DictionaryNode Next;
  206. }
  207. }
  208. }