123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #nullable disable
- using System.Collections;
- using System.Runtime.CompilerServices;
- using Jint.Runtime;
- namespace Jint.Collections;
- internal sealed class ListDictionary<TValue> : DictionaryBase<TValue>, IEnumerable<KeyValuePair<Key, TValue>>
- {
- private DictionaryNode _head;
- private int _count;
- private bool _checkExistingKeys;
- public ListDictionary(Key key, TValue value, bool checkExistingKeys)
- {
- _checkExistingKeys = checkExistingKeys;
- _head = new DictionaryNode
- {
- Key = key,
- Value = value
- };
- _count = 1;
- }
- public ListDictionary(DictionaryNode head, bool checkExistingKeys)
- {
- _checkExistingKeys = checkExistingKeys;
- _head = head;
- _count = 1;
- }
- public override ref TValue GetValueRefOrNullRef(Key key)
- {
- DictionaryNode node;
- for (node = _head; node != null; node = node.Next)
- {
- if (node.Key == key)
- {
- return ref node.Value;
- }
- }
- return ref Unsafe.NullRef<TValue>();
- }
- public override ref TValue GetValueRefOrAddDefault(Key key, out bool exists)
- {
- DictionaryNode last = null;
- DictionaryNode node;
- var checkExistingKeys = _checkExistingKeys;
- for (node = _head; node != null; node = node.Next)
- {
- var oldKey = node.Key;
- if (checkExistingKeys && oldKey == key)
- {
- exists = true;
- return ref node.Value;
- }
- last = node;
- }
- exists = false;
- return ref AddNode(key, default, last).Value;
- }
- public override int Count
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => _count;
- }
- public bool Add(Key key, TValue value, bool tryAdd = false)
- {
- DictionaryNode last = null;
- DictionaryNode node;
- var checkExistingKeys = _checkExistingKeys;
- for (node = _head; node != null; node = node.Next)
- {
- var oldKey = node.Key;
- if (checkExistingKeys && oldKey == key)
- {
- if (tryAdd)
- {
- return false;
- }
- Throw.ArgumentException();
- }
- last = node;
- }
- AddNode(key, value, last);
- return true;
- }
- /// <summary>
- /// Adds a new item and expects key to not exist.
- /// </summary>
- public void AddDangerous(Key key, TValue value)
- {
- DictionaryNode node;
- for (node = _head; node != null; node = node.Next)
- {
- if (node.Next is null)
- {
- AddNode(key, value, node);
- return;
- }
- }
- }
- private DictionaryNode AddNode(Key key, TValue value, DictionaryNode last)
- {
- var newNode = new DictionaryNode
- {
- Key = key,
- Value = value
- };
- if (_head is null)
- {
- _head = newNode;
- }
- else
- {
- last.Next = newNode;
- }
- _count++;
- return newNode;
- }
- public void Clear()
- {
- _count = 0;
- _head = null;
- }
- internal bool CheckExistingKeys
- {
- set => _checkExistingKeys = value;
- }
- public NodeEnumerator GetEnumerator()
- {
- return new NodeEnumerator(this);
- }
- IEnumerator<KeyValuePair<Key, TValue>> IEnumerable<KeyValuePair<Key, TValue>>.GetEnumerator()
- {
- return new NodeEnumerator(this);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new NodeEnumerator(this);
- }
- public bool Remove(Key key)
- {
- DictionaryNode last = null;
- DictionaryNode node;
- for (node = _head; node != null; node = node.Next)
- {
- var oldKey = node.Key;
- if (oldKey == key)
- {
- break;
- }
- last = node;
- }
- if (node == null)
- {
- return false;
- }
- if (node == _head)
- {
- _head = node.Next;
- }
- else
- {
- last.Next = node.Next;
- }
- _count--;
- return true;
- }
- internal struct NodeEnumerator : IEnumerator<KeyValuePair<Key, TValue>>
- {
- private readonly ListDictionary<TValue> _list;
- private DictionaryNode _current;
- private bool _start;
- public NodeEnumerator(ListDictionary<TValue> list)
- {
- _list = list;
- _start = true;
- _current = null;
- }
- public KeyValuePair<Key, TValue> Current => new KeyValuePair<Key, TValue>(_current.Key, _current.Value);
- public bool MoveNext()
- {
- if (_start)
- {
- _current = _list._head;
- _start = false;
- }
- else if (_current != null)
- {
- _current = _current.Next;
- }
- return _current != null;
- }
- void IEnumerator.Reset()
- {
- _start = true;
- _current = null;
- }
- public void Dispose()
- {
- }
- object IEnumerator.Current => _current;
- }
- internal sealed class DictionaryNode
- {
- public Key Key;
- public TValue Value;
- public DictionaryNode Next;
- }
- }
|