| | | 1 | | //======================================================================= |
| | | 2 | | // OrderedStringMap.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | |
| | | 12 | | namespace Chronicler.Serialization; |
| | | 13 | | |
| | | 14 | | internal sealed class OrderedStringMap<TValue> : IEnumerable<KeyValuePair<string, TValue>> |
| | | 15 | | { |
| | | 16 | | private readonly List<KeyValuePair<string, TValue>> _entries; |
| | | 17 | | private readonly Dictionary<string, int> _indexes; |
| | | 18 | | |
| | 1792 | 19 | | public OrderedStringMap(int capacity, IEqualityComparer<string> comparer) |
| | | 20 | | { |
| | 1792 | 21 | | _entries = new List<KeyValuePair<string, TValue>>(capacity); |
| | 1792 | 22 | | _indexes = new Dictionary<string, int>(capacity, comparer); |
| | 1792 | 23 | | } |
| | | 24 | | |
| | 511 | 25 | | public int Count => _entries.Count; |
| | | 26 | | |
| | | 27 | | public TValue this[string key] |
| | | 28 | | { |
| | 1828 | 29 | | set => Set(key, value); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public bool TryGetValue(string key, out TValue value) |
| | | 33 | | { |
| | 575 | 34 | | if (!_indexes.TryGetValue(key, out int index)) |
| | | 35 | | { |
| | 175 | 36 | | value = default!; |
| | 175 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | |
| | 400 | 40 | | value = _entries[index].Value; |
| | 400 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public bool Remove(string key) |
| | | 45 | | { |
| | 43 | 46 | | if (!_indexes.TryGetValue(key, out int index)) |
| | 7 | 47 | | return false; |
| | | 48 | | |
| | 36 | 49 | | _entries.RemoveAt(index); |
| | 36 | 50 | | _indexes.Remove(key); |
| | | 51 | | |
| | 108 | 52 | | for (int i = index; i < _entries.Count; i++) |
| | 18 | 53 | | _indexes[_entries[i].Key] = i; |
| | | 54 | | |
| | 36 | 55 | | return true; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator() |
| | | 59 | | { |
| | 738 | 60 | | return _entries.GetEnumerator(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 64 | | { |
| | 1 | 65 | | return GetEnumerator(); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private void Set(string key, TValue value) |
| | | 69 | | { |
| | 1828 | 70 | | if (key == null) |
| | 1 | 71 | | throw new ArgumentNullException(nameof(key)); |
| | | 72 | | |
| | 1827 | 73 | | if (_indexes.TryGetValue(key, out int index)) |
| | | 74 | | { |
| | 63 | 75 | | string existingKey = _entries[index].Key; |
| | 63 | 76 | | _entries[index] = new KeyValuePair<string, TValue>(existingKey, value); |
| | 63 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 1764 | 80 | | _indexes.Add(key, _entries.Count); |
| | 1764 | 81 | | _entries.Add(new KeyValuePair<string, TValue>(key, value)); |
| | 1764 | 82 | | } |
| | | 83 | | } |