ListDictionary.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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> : DictionaryBase<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 ListDictionary(DictionaryNode head, bool checkExistingKeys)
  22. {
  23. _checkExistingKeys = checkExistingKeys;
  24. _head = head;
  25. _count = 1;
  26. }
  27. public override ref TValue GetValueRefOrNullRef(Key key)
  28. {
  29. DictionaryNode node;
  30. for (node = _head; node != null; node = node.Next)
  31. {
  32. if (node.Key == key)
  33. {
  34. return ref node.Value;
  35. }
  36. }
  37. return ref Unsafe.NullRef<TValue>();
  38. }
  39. public override ref TValue GetValueRefOrAddDefault(Key key, out bool exists)
  40. {
  41. DictionaryNode last = null;
  42. DictionaryNode node;
  43. var checkExistingKeys = _checkExistingKeys;
  44. for (node = _head; node != null; node = node.Next)
  45. {
  46. var oldKey = node.Key;
  47. if (checkExistingKeys && oldKey == key)
  48. {
  49. exists = true;
  50. return ref node.Value;
  51. }
  52. last = node;
  53. }
  54. exists = false;
  55. return ref AddNode(key, default, last).Value;
  56. }
  57. public override int Count
  58. {
  59. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  60. get => _count;
  61. }
  62. public bool Add(Key key, TValue value, bool tryAdd = false)
  63. {
  64. DictionaryNode last = null;
  65. DictionaryNode node;
  66. var checkExistingKeys = _checkExistingKeys;
  67. for (node = _head; node != null; node = node.Next)
  68. {
  69. var oldKey = node.Key;
  70. if (checkExistingKeys && oldKey == key)
  71. {
  72. if (tryAdd)
  73. {
  74. return false;
  75. }
  76. Throw.ArgumentException();
  77. }
  78. last = node;
  79. }
  80. AddNode(key, value, last);
  81. return true;
  82. }
  83. /// <summary>
  84. /// Adds a new item and expects key to not exist.
  85. /// </summary>
  86. public void AddDangerous(Key key, TValue value)
  87. {
  88. DictionaryNode node;
  89. for (node = _head; node != null; node = node.Next)
  90. {
  91. if (node.Next is null)
  92. {
  93. AddNode(key, value, node);
  94. return;
  95. }
  96. }
  97. }
  98. private DictionaryNode AddNode(Key key, TValue value, DictionaryNode last)
  99. {
  100. var newNode = new DictionaryNode
  101. {
  102. Key = key,
  103. Value = value
  104. };
  105. if (_head is null)
  106. {
  107. _head = newNode;
  108. }
  109. else
  110. {
  111. last.Next = newNode;
  112. }
  113. _count++;
  114. return newNode;
  115. }
  116. public void Clear()
  117. {
  118. _count = 0;
  119. _head = null;
  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. }