123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #nullable disable
- using System.Collections;
- using System.Runtime.CompilerServices;
- using Jint.Runtime;
- namespace Jint.Collections;
- internal sealed class ListDictionary<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 TValue this[Key key]
- {
- get
- {
- TryGetValue(key, out var value);
- return value;
- }
- set
- {
- DictionaryNode last = null;
- DictionaryNode node;
- var checkExistingKeys = _checkExistingKeys;
- for (node = _head; node != null; node = node.Next)
- {
- var oldKey = node.Key;
- if (checkExistingKeys && oldKey == key)
- {
- break;
- }
- last = node;
- }
- if (node != null)
- {
- // Found it
- node.Value = value;
- return;
- }
- AddNode(key, value, last);
- }
- }
- public bool TryGetValue(Key key, out TValue value)
- {
- var node = _head;
- while (node != null)
- {
- if (node.Key == key)
- {
- value = node.Value;
- return true;
- }
- node = node.Next;
- }
- value = default;
- return false;
- }
- public void SetOrUpdateValue<TState>(Key key, Func<TValue, TState, TValue> updater, TState state)
- {
- DictionaryNode last = null;
- DictionaryNode node;
- for (node = _head; node != null; node = node.Next)
- {
- if (node.Key == key)
- {
- node.Value = updater(node.Value, state);
- return;
- }
- last = node;
- }
- AddNode(key, updater(default, state), last);
- }
- public 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;
- }
- ExceptionHelper.ThrowArgumentException();
- }
- last = node;
- }
- AddNode(key, value, last);
- return true;
- }
- private void 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++;
- }
- public void Clear()
- {
- _count = 0;
- _head = null;
- }
- public bool ContainsKey(Key key)
- {
- for (var node = _head; node != null; node = node.Next)
- {
- var oldKey = node.Key;
- if (oldKey == key)
- {
- return true;
- }
- }
- return false;
- }
- 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;
- }
- }
|