| | 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 | |
|
| 3777 | 12 | | internal readonly Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); |
| | 13 | |
|
| 2871 | 14 | | public TValue this[TKey key] { get => dictionary[key]; set => dictionary[key] = value; } |
| | 15 | |
|
| 7550 | 16 | | public BaseDictionary() { } |
| | 17 | |
|
| 2 | 18 | | public BaseDictionary(IEnumerable<(TKey, TValue)> elements) |
| | 19 | | { |
| 2 | 20 | | dictionary = new Dictionary<TKey, TValue>(); |
| 14 | 21 | | foreach ((TKey key, TValue value) in elements) |
| | 22 | | { |
| 5 | 23 | | dictionary.Add(key, value); |
| | 24 | | } |
| 2 | 25 | | } |
| | 26 | |
|
| 10 | 27 | | public IEnumerable<KeyValuePair<TKey, TValue>> Get() => dictionary; |
| | 28 | |
|
| 21 | 29 | | public TValue Get(TKey key) => dictionary[key]; |
| | 30 | |
|
| 1841 | 31 | | public bool TryGetValue(TKey key, out TValue value) => dictionary.TryGetValue(key, out value); |
| | 32 | |
|
| | 33 | | public void Set(IEnumerable<(TKey, TValue)> elements) |
| | 34 | | { |
| 57 | 35 | | dictionary.Clear(); |
| 246 | 36 | | foreach ((TKey key, TValue value) in elements) |
| | 37 | | { |
| 66 | 38 | | dictionary.Add(key, value); |
| | 39 | | } |
| | 40 | |
|
| 57 | 41 | | OnSet?.Invoke(dictionary); |
| 5 | 42 | | } |
| | 43 | |
|
| | 44 | | public void AddOrSet(TKey key, TValue value) |
| | 45 | | { |
| 17 | 46 | | if ( !dictionary.ContainsKey(key)) |
| 5 | 47 | | dictionary.Add(key, value); |
| | 48 | | else |
| 12 | 49 | | dictionary[key] = value; |
| | 50 | |
|
| 17 | 51 | | OnAdded?.Invoke(key, value); |
| 3 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Add(TKey key, TValue value) |
| | 55 | | { |
| 3554 | 56 | | dictionary.Add(key, value); |
| 3554 | 57 | | OnAdded?.Invoke(key, value); |
| 18 | 58 | | } |
| | 59 | |
|
| | 60 | | public bool Remove(TKey key) |
| | 61 | | { |
| 272 | 62 | | if (!dictionary.ContainsKey(key)) |
| 43 | 63 | | return false; |
| | 64 | |
|
| 229 | 65 | | TValue value = dictionary[key]; |
| 229 | 66 | | dictionary.Remove(key); |
| 229 | 67 | | OnRemoved?.Invoke(key, value); |
| 229 | 68 | | return true; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public void Clear() |
| | 72 | | { |
| 891 | 73 | | dictionary.Clear(); |
| 891 | 74 | | OnSet?.Invoke(new Dictionary<TKey, TValue>()); |
| 0 | 75 | | } |
| | 76 | |
|
| 4069 | 77 | | public bool ContainsKey(TKey key) => dictionary.ContainsKey(key); |
| 0 | 78 | | public bool ContainsValue(TValue value) => dictionary.ContainsValue(value); |
| | 79 | |
|
| 0 | 80 | | public int Count() => dictionary.Count; |
| | 81 | |
|
| 51 | 82 | | public List<TValue> GetValues() => dictionary.Values.ToList(); |
| | 83 | |
|
| | 84 | | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| | 85 | | { |
| 0 | 86 | | return dictionary.GetEnumerator(); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | IEnumerator IEnumerable.GetEnumerator() |
| | 90 | | { |
| 0 | 91 | | return dictionary.GetEnumerator(); |
| | 92 | | } |
| | 93 | | } |