| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | /* |
| | 6 | | * `KeyValueSet` works as an alternative of `Dictionary` when a iteration through all it values is performed frequently. |
| | 7 | | * CONS: It's much slower to modify the data compared to a `Dictionary` |
| | 8 | | * PRO: It's much faster to iterate through all the data compared to a `Dictionary` and |
| | 9 | | * it doesn't alloc extra memory while doing so. |
| | 10 | | * Benchmark against a `Dictionary`: |
| | 11 | | * Adding element: ~32% slower |
| | 12 | | * Removing element: ~164% slower |
| | 13 | | * Iterating through all elements: ~34% faster |
| | 14 | | */ |
| | 15 | |
|
| | 16 | | public readonly struct KeyValueSetPair<TKey, TValue> |
| | 17 | | { |
| | 18 | | public readonly TKey key; |
| | 19 | | public readonly TValue value; |
| | 20 | |
|
| | 21 | | public KeyValueSetPair(TKey key, TValue value) |
| | 22 | | { |
| | 23 | | this.key = key; |
| | 24 | | this.value = value; |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public interface IReadOnlyKeyValueSet<TKey, TValue> : IEnumerable<KeyValueSetPair<TKey, TValue>> |
| | 29 | | { |
| | 30 | | TValue this[TKey key] { get; } |
| | 31 | | int Count { get; } |
| | 32 | | IReadOnlyList<KeyValueSetPair<TKey, TValue>> Pairs { get; } |
| | 33 | | bool ContainsKey(TKey key); |
| | 34 | | bool TryGetValue(TKey key, out TValue value); |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public class KeyValueSet<TKey, TValue> : IReadOnlyKeyValueSet<TKey, TValue> |
| | 38 | | { |
| | 39 | | private readonly Dictionary<TKey, int> indexer; |
| | 40 | | private readonly List<KeyValueSetPair<TKey, TValue>> pairs; |
| | 41 | |
|
| 0 | 42 | | public IReadOnlyList<KeyValueSetPair<TKey, TValue>> Pairs => pairs; |
| | 43 | |
|
| 0 | 44 | | public int Count => pairs.Count; |
| | 45 | |
|
| | 46 | | public TValue this[TKey key] |
| | 47 | | { |
| 9 | 48 | | set => AddInternal(key, value, false); |
| | 49 | | get |
| | 50 | | { |
| 8 | 51 | | TryGetValue(key, out TValue value); |
| 8 | 52 | | return value; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| 136 | 56 | | public KeyValueSet() : this(0) { } |
| | 57 | |
|
| 97 | 58 | | public KeyValueSet(int capacity) |
| | 59 | | { |
| 97 | 60 | | indexer = new Dictionary<TKey, int>(capacity); |
| 97 | 61 | | pairs = new List<KeyValueSetPair<TKey, TValue>>(capacity); |
| 97 | 62 | | } |
| | 63 | |
|
| | 64 | | public bool ContainsKey(TKey key) |
| | 65 | | { |
| 9 | 66 | | return indexer.ContainsKey(key); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public bool TryGetValue(TKey key, out TValue value) |
| | 70 | | { |
| 326 | 71 | | if (!indexer.TryGetValue(key, out int index)) |
| | 72 | | { |
| 148 | 73 | | value = default; |
| 148 | 74 | | return false; |
| | 75 | | } |
| 178 | 76 | | value = pairs[index].value; |
| 178 | 77 | | return true; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public void Add(TKey key, TValue value) |
| | 81 | | { |
| 149 | 82 | | AddInternal(key, value, true); |
| 149 | 83 | | } |
| | 84 | |
|
| | 85 | | public void RemoveAt(int index) |
| | 86 | | { |
| 4 | 87 | | int count = Count; |
| 4 | 88 | | if (index >= count || index < 0) |
| | 89 | | { |
| 0 | 90 | | throw new IndexOutOfRangeException(); |
| | 91 | | } |
| | 92 | |
|
| 4 | 93 | | TKey key = pairs[index].key; |
| | 94 | |
|
| 4 | 95 | | if (count > 1 && index < count - 1) |
| | 96 | | { |
| 0 | 97 | | int lastValueIndex = count - 1; |
| 0 | 98 | | pairs[index] = pairs[lastValueIndex]; |
| 0 | 99 | | indexer[pairs[lastValueIndex].key] = index; |
| 0 | 100 | | pairs.RemoveAt(lastValueIndex); |
| 0 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 4 | 104 | | pairs.RemoveAt(index); |
| | 105 | | } |
| | 106 | |
|
| 4 | 107 | | indexer.Remove(key); |
| 4 | 108 | | } |
| | 109 | |
|
| | 110 | | public bool Remove(TKey key) |
| | 111 | | { |
| 2 | 112 | | if (!indexer.TryGetValue(key, out int index)) |
| | 113 | | { |
| 1 | 114 | | return false; |
| | 115 | | } |
| | 116 | |
|
| 1 | 117 | | RemoveAt(index); |
| 1 | 118 | | return true; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | public void Clear() |
| | 122 | | { |
| 0 | 123 | | indexer.Clear(); |
| 0 | 124 | | pairs.Clear(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public IEnumerator<KeyValueSetPair<TKey, TValue>> GetEnumerator() |
| | 128 | | { |
| 0 | 129 | | for (int i = 0; i < Count; i++) |
| | 130 | | { |
| 0 | 131 | | yield return pairs[i]; |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | IEnumerator IEnumerable.GetEnumerator() |
| | 136 | | { |
| 0 | 137 | | return GetEnumerator(); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | private void AddInternal(TKey key, TValue value, bool add) |
| | 141 | | { |
| 158 | 142 | | if (!add && indexer.TryGetValue(key, out int index)) |
| | 143 | | { |
| 2 | 144 | | pairs[index] = new KeyValueSetPair<TKey, TValue>(key, value); |
| 2 | 145 | | return; |
| | 146 | | } |
| 156 | 147 | | indexer.Add(key, pairs.Count); |
| 156 | 148 | | pairs.Add(new KeyValueSetPair<TKey, TValue>(key, value)); |
| 156 | 149 | | } |
| | 150 | | } |