| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class BaseDictionary_Legacy<TKey, TValue> : ScriptableObject |
| | 5 | | { |
| | 6 | | public delegate void Added(TKey addedKey, TValue addedValue); |
| | 7 | |
|
| | 8 | | public delegate void Removed(TKey removedKey, TValue removedValue); |
| | 9 | |
|
| | 10 | | private event Added OnAddedElementValue; |
| | 11 | |
|
| | 12 | | private event Removed OnRemovedElementValue; |
| | 13 | |
|
| 42 | 14 | | public virtual event Added OnAdded { add => OnAddedElementValue += value; remove => OnAddedElementValue -= value; } |
| | 15 | |
|
| 0 | 16 | | public virtual event Removed OnRemoved { add => OnRemovedElementValue += value; remove => OnRemovedElementValue -= v |
| | 17 | |
|
| 0 | 18 | | private Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); |
| | 19 | |
|
| | 20 | | public void Add(TKey newKey, TValue newValue) |
| | 21 | | { |
| 172 | 22 | | if (dictionary.ContainsKey(newKey)) |
| 36 | 23 | | return; |
| | 24 | |
|
| 136 | 25 | | dictionary.Add(newKey, newValue); |
| 136 | 26 | | OnAddedElementValue?.Invoke(newKey, newValue); |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | public void Add(KeyValuePair<TKey, TValue>[] newValues) |
| | 30 | | { |
| 0 | 31 | | int count = newValues.Length; |
| 0 | 32 | | for (int i = 0; i < count; ++i) |
| | 33 | | { |
| 0 | 34 | | Add(newValues[i].Key, newValues[i].Value); |
| | 35 | | } |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Remove(TKey key) |
| | 39 | | { |
| 39 | 40 | | if (!dictionary.ContainsKey(key)) |
| 25 | 41 | | return; |
| | 42 | |
|
| 14 | 43 | | var value = dictionary[key]; |
| 14 | 44 | | dictionary.Remove(key); |
| | 45 | |
|
| 14 | 46 | | OnRemovedElementValue?.Invoke(key, value); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Remove(TKey[] keys) |
| | 50 | | { |
| 0 | 51 | | int count = keys.Length; |
| 0 | 52 | | for (int i = 0; i < count; ++i) |
| | 53 | | { |
| 0 | 54 | | Remove(keys[i]); |
| | 55 | | } |
| 0 | 56 | | } |
| | 57 | |
|
| 81 | 58 | | public TValue Get(TKey key) { return dictionary.ContainsKey(key) ? dictionary[key] : default(TValue); } |
| | 59 | |
|
| 73 | 60 | | public bool ContainsKey(TKey key) { return dictionary.ContainsKey(key); } |
| | 61 | |
|
| 45 | 62 | | public bool TryGetValue(TKey key, out TValue value) { return dictionary.TryGetValue(key, out value); } |
| | 63 | |
|
| | 64 | | public TValue GetOrDefault(TKey key) |
| | 65 | | { |
| 0 | 66 | | if (!dictionary.ContainsKey(key)) |
| 0 | 67 | | return default; |
| | 68 | |
|
| 0 | 69 | | return dictionary[key]; |
| | 70 | | } |
| | 71 | |
|
| 16 | 72 | | public Dictionary<TKey, TValue>.Enumerator GetEnumerator() { return dictionary.GetEnumerator(); } |
| | 73 | |
|
| 0 | 74 | | public int Count => dictionary.Count; |
| | 75 | |
|
| 0 | 76 | | public IEnumerable<TValue> GetValues() { return dictionary.Values; } |
| | 77 | |
|
| | 78 | | public void Clear() |
| | 79 | | { |
| 103 | 80 | | using (Dictionary<TKey, TValue>.Enumerator iterator = dictionary.GetEnumerator()) |
| | 81 | | { |
| 214 | 82 | | while (iterator.MoveNext()) |
| | 83 | | { |
| 111 | 84 | | OnRemovedElementValue?.Invoke(iterator.Current.Key, iterator.Current.Value); |
| | 85 | | } |
| 103 | 86 | | } |
| | 87 | |
|
| 103 | 88 | | dictionary.Clear(); |
| 103 | 89 | | } |
| | 90 | |
|
| | 91 | | #if UNITY_EDITOR |
| | 92 | | private void OnEnable() |
| | 93 | | { |
| 0 | 94 | | Application.quitting -= CleanUp; |
| 0 | 95 | | Application.quitting += CleanUp; |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void CleanUp() |
| | 99 | | { |
| 0 | 100 | | Application.quitting -= CleanUp; |
| 0 | 101 | | Resources.UnloadAsset(this); |
| 0 | 102 | | } |
| | 103 | | #endif |
| | 104 | | } |