| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | public class BaseDictionary<TKey, TValue> : IBaseDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>> |
| | 7 | | { |
| | 8 | | public event Action<IEnumerable<KeyValuePair<TKey, TValue>>> OnSet; |
| | 9 | | public event Action<TKey, TValue> OnAdded; |
| | 10 | | public event Action<TKey, TValue> OnRemoved; |
| | 11 | |
|
| | 12 | | internal readonly Dictionary<TKey, TValue> dictionary; |
| | 13 | |
|
| 2765 | 14 | | public TValue this[TKey key] { get => dictionary[key]; set => dictionary[key] = value; } |
| | 15 | |
|
| 3781 | 16 | | public BaseDictionary() |
| | 17 | | { |
| 3781 | 18 | | dictionary = new Dictionary<TKey, TValue>(); |
| 3781 | 19 | | } |
| | 20 | |
|
| 491 | 21 | | public BaseDictionary(IEqualityComparer<TKey> comparer) |
| | 22 | | { |
| 491 | 23 | | dictionary = new Dictionary<TKey, TValue>(new Dictionary<TKey, TValue>(), comparer); |
| 491 | 24 | | } |
| | 25 | |
|
| 3 | 26 | | public BaseDictionary(IEnumerable<(TKey, TValue)> elements) |
| | 27 | | { |
| 3 | 28 | | dictionary = new Dictionary<TKey, TValue>(); |
| 18 | 29 | | foreach ((TKey key, TValue value) in elements) |
| | 30 | | { |
| 6 | 31 | | dictionary.Add(key, value); |
| | 32 | | } |
| 3 | 33 | | } |
| | 34 | |
|
| 474 | 35 | | public IEnumerable<KeyValuePair<TKey, TValue>> Get() => dictionary; |
| | 36 | |
|
| 2 | 37 | | public TValue Get(TKey key) => dictionary[key]; |
| | 38 | |
|
| 321 | 39 | | public bool TryGetValue(TKey key, out TValue value) => dictionary.TryGetValue(key, out value); |
| | 40 | |
|
| | 41 | | public void Set(IEnumerable<(TKey, TValue)> elements) |
| | 42 | | { |
| 3 | 43 | | dictionary.Clear(); |
| 22 | 44 | | foreach ((TKey key, TValue value) in elements) |
| | 45 | | { |
| 8 | 46 | | dictionary.Add(key, value); |
| | 47 | | } |
| | 48 | |
|
| 3 | 49 | | OnSet?.Invoke(dictionary); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public void AddOrSet(TKey key, TValue value) |
| | 53 | | { |
| 37 | 54 | | if ( !dictionary.ContainsKey(key)) |
| 30 | 55 | | dictionary.Add(key, value); |
| | 56 | | else |
| 7 | 57 | | dictionary[key] = value; |
| | 58 | |
|
| 37 | 59 | | OnAdded?.Invoke(key, value); |
| 20 | 60 | | } |
| | 61 | |
|
| | 62 | | public void Add(TKey key, TValue value) |
| | 63 | | { |
| 3142 | 64 | | dictionary.Add(key, value); |
| 3142 | 65 | | OnAdded?.Invoke(key, value); |
| 21 | 66 | | } |
| | 67 | |
|
| | 68 | | public bool Remove(TKey key) |
| | 69 | | { |
| 106 | 70 | | if (!dictionary.ContainsKey(key)) |
| 36 | 71 | | return false; |
| | 72 | |
|
| 70 | 73 | | TValue value = dictionary[key]; |
| 70 | 74 | | dictionary.Remove(key); |
| 70 | 75 | | OnRemoved?.Invoke(key, value); |
| 70 | 76 | | return true; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public void Clear() |
| | 80 | | { |
| 34 | 81 | | dictionary.Clear(); |
| 34 | 82 | | OnSet?.Invoke(new Dictionary<TKey, TValue>()); |
| 0 | 83 | | } |
| | 84 | |
|
| 3376 | 85 | | public bool ContainsKey(TKey key) => dictionary.ContainsKey(key); |
| 0 | 86 | | public bool ContainsValue(TValue value) => dictionary.ContainsValue(value); |
| | 87 | |
|
| 41 | 88 | | public int Count() => dictionary.Count; |
| | 89 | |
|
| 10 | 90 | | public List<TValue> GetValues() => dictionary.Values.ToList(); |
| | 91 | |
|
| | 92 | | public IEnumerable<TKey> GetKeys() => |
| 6 | 93 | | dictionary.Keys; |
| | 94 | |
|
| | 95 | | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| | 96 | | { |
| 132 | 97 | | return dictionary.GetEnumerator(); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | IEnumerator IEnumerable.GetEnumerator() |
| | 101 | | { |
| 0 | 102 | | return dictionary.GetEnumerator(); |
| | 103 | | } |
| | 104 | | } |