| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | public class BaseRefCountedCollection<T> : IEnumerable<KeyValuePair<T, int>> |
| | 7 | | { |
| | 8 | | public event Action<T, int> OnRefCountUpdated; |
| | 9 | |
|
| 699 | 10 | | internal readonly Dictionary<T, int> dictionary = new Dictionary<T, int>(); |
| | 11 | |
|
| 7 | 12 | | public int this[T key] { get => dictionary[key]; set => dictionary[key] = value; } |
| | 13 | |
|
| 1398 | 14 | | public BaseRefCountedCollection() { } |
| | 15 | |
|
| 0 | 16 | | public BaseRefCountedCollection(IEnumerable<(T, int)> elements) |
| | 17 | | { |
| 0 | 18 | | dictionary = new Dictionary<T, int>(); |
| 0 | 19 | | foreach ((T key, int refCount) in elements) |
| | 20 | | { |
| 0 | 21 | | dictionary.Add(key, refCount); |
| | 22 | | } |
| 0 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | public IEnumerable<KeyValuePair<T, int>> GetAllRefCounts() => dictionary; |
| | 26 | |
|
| | 27 | | public int GetRefCount(T key) |
| | 28 | | { |
| 9 | 29 | | if (!dictionary.ContainsKey(key)) |
| 0 | 30 | | return 0; |
| 9 | 31 | | return dictionary[key]; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public void SetRefCounts(IEnumerable<(T, int)> elements) |
| | 35 | | { |
| 1 | 36 | | dictionary.Clear(); |
| 8 | 37 | | foreach ((T key, int value) in elements) |
| | 38 | | { |
| 3 | 39 | | SetRefCount(key, value); |
| | 40 | | } |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | public void SetRefCount(T key, int count) |
| | 44 | | { |
| 13 | 45 | | if (dictionary.ContainsKey(key)) |
| 4 | 46 | | dictionary[key] = count; |
| | 47 | | else |
| 9 | 48 | | dictionary.Add(key, count); |
| | 49 | |
|
| 13 | 50 | | OnRefCountUpdated?.Invoke(key, count); |
| 7 | 51 | | } |
| | 52 | |
|
| | 53 | | public void IncreaseRefCount(T key) |
| | 54 | | { |
| 14 | 55 | | if (dictionary.ContainsKey(key)) |
| 4 | 56 | | dictionary[key]++; |
| | 57 | | else |
| 10 | 58 | | dictionary.Add(key, 1); |
| | 59 | |
|
| 14 | 60 | | OnRefCountUpdated?.Invoke(key, dictionary[key]); |
| 2 | 61 | | } |
| | 62 | |
|
| | 63 | | public void DecreaseRefCount(T key) |
| | 64 | | { |
| 23 | 65 | | if (!dictionary.ContainsKey(key)) |
| 6 | 66 | | return; |
| | 67 | |
|
| 17 | 68 | | int newCount = Math.Max(0, dictionary[key] - 1); |
| 17 | 69 | | if (newCount == 0) |
| 11 | 70 | | dictionary.Remove(key); |
| | 71 | | else |
| 6 | 72 | | dictionary[key] = newCount; |
| | 73 | |
|
| 17 | 74 | | OnRefCountUpdated?.Invoke(key, newCount); |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Clear() |
| | 78 | | { |
| 1 | 79 | | T[] keys = dictionary.Keys.ToArray(); |
| 8 | 80 | | foreach (T key in keys) |
| | 81 | | { |
| 3 | 82 | | SetRefCount(key, 0); |
| | 83 | | } |
| | 84 | |
|
| 1 | 85 | | dictionary.Clear(); |
| 1 | 86 | | } |
| | 87 | |
|
| 0 | 88 | | public int Count() => dictionary.Count; |
| | 89 | |
|
| 0 | 90 | | public IEnumerator<KeyValuePair<T, int>> GetEnumerator() { return dictionary.GetEnumerator(); } |
| | 91 | |
|
| 0 | 92 | | IEnumerator IEnumerable.GetEnumerator() { return dictionary.GetEnumerator(); } |
| | 93 | | } |