ListDictionary.cs 5.9 KB

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