| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class BaseRefCounter<TKey> : IEnumerable<KeyValuePair<TKey, int>> |
| | 7 | | { |
| | 8 | | public event Action<TKey> OnAdded; |
| | 9 | | public event Action<TKey> OnRemoved; |
| | 10 | |
|
| 1521 | 11 | | internal readonly Dictionary<TKey, int> dictionary = new Dictionary<TKey, int>(); |
| | 12 | |
|
| 0 | 13 | | public int this[TKey key] { get => dictionary[key]; set => dictionary[key] = value; } |
| | 14 | |
|
| | 15 | | public IEnumerable<KeyValuePair<TKey, int>> Get() |
| | 16 | | { |
| 0 | 17 | | return dictionary; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public int Get(TKey key) |
| | 21 | | { |
| 0 | 22 | | if ( !dictionary.ContainsKey(key)) |
| 0 | 23 | | return 0; |
| | 24 | |
|
| 0 | 25 | | return dictionary[key]; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Adds one to the reference count for the given key. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="key">A given key to be ref counted.</param> |
| | 32 | | public void AddRefCount(TKey key) |
| | 33 | | { |
| 764 | 34 | | if ( key == null ) |
| 0 | 35 | | return; |
| | 36 | |
|
| 764 | 37 | | if ( !dictionary.ContainsKey(key) ) |
| | 38 | | { |
| 674 | 39 | | dictionary.Add(key, 0); |
| 674 | 40 | | OnAdded?.Invoke(key); |
| | 41 | | } |
| | 42 | |
|
| 764 | 43 | | dictionary[key]++; |
| 764 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Adds one to the reference count for the given key. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="key">A given key to be ref counted.</param> |
| | 50 | | public void AddRefCount(IEnumerable<TKey> keys) |
| | 51 | | { |
| 4186 | 52 | | foreach ( TKey key in keys ) |
| | 53 | | { |
| 764 | 54 | | AddRefCount(key); |
| | 55 | | } |
| 1329 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Adds one to the reference count for the given keys. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="keys">A given set of keys to be ref counted.</param> |
| | 62 | | public void RemoveRefCount(IEnumerable<TKey> keys) |
| | 63 | | { |
| 2172 | 64 | | foreach ( TKey key in keys ) |
| | 65 | | { |
| 309 | 66 | | RemoveRefCount(key); |
| | 67 | | } |
| 777 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Removes the reference count for the given key. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="key">A given key to be ref counted.</param> |
| | 74 | | /// <returns>true if the element count reached zero and was removed.</returns> |
| | 75 | | public bool RemoveRefCount(TKey key) |
| | 76 | | { |
| 309 | 77 | | if ( key == null || !dictionary.ContainsKey(key) ) |
| 59 | 78 | | return false; |
| | 79 | |
|
| 250 | 80 | | dictionary[key]--; |
| | 81 | |
|
| 250 | 82 | | if ( dictionary[key] != 0 ) |
| 53 | 83 | | return false; |
| | 84 | |
|
| 197 | 85 | | dictionary.Remove(key); |
| 197 | 86 | | OnRemoved?.Invoke(key); |
| 197 | 87 | | return true; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public bool ContainsKey(TKey key) |
| | 91 | | { |
| 0 | 92 | | return dictionary.ContainsKey(key); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public void Clear() |
| | 96 | | { |
| 0 | 97 | | dictionary.Clear(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public int Count() |
| | 101 | | { |
| 0 | 102 | | return dictionary.Count; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | public IEnumerator<KeyValuePair<TKey, int>> GetEnumerator() |
| | 106 | | { |
| 0 | 107 | | return dictionary.GetEnumerator(); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | IEnumerator IEnumerable.GetEnumerator() |
| | 111 | | { |
| 0 | 112 | | return dictionary.GetEnumerator(); |
| | 113 | | } |
| | 114 | | } |