| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class Multimap<KT, VT> |
| | 9 | | { |
| 0 | 10 | | private readonly Dictionary<KT, List<VT>> map = new Dictionary<KT, List<VT>>(); |
| | 11 | |
|
| | 12 | | public IEnumerable<KT> Keys |
| | 13 | | { |
| 0 | 14 | | get { return map.Keys; } |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public IEnumerable<VT> AllValues |
| | 18 | | { |
| 0 | 19 | | get { return map.Values.SelectMany(list => list); } |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public int Count |
| | 23 | | { |
| 0 | 24 | | get { return map.Count; } |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public int GetCountFor(KT key) |
| | 28 | | { |
| 0 | 29 | | if (map.ContainsKey(key)) |
| 0 | 30 | | return map[key].Count; |
| 0 | 31 | | return 0; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public int ValuesCount |
| | 35 | | { |
| | 36 | | get |
| | 37 | | { |
| 0 | 38 | | int count = 0; |
| 0 | 39 | | foreach (var list in map.Values) |
| | 40 | | { |
| 0 | 41 | | count += list.Count; |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | return count; |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public IEnumerable<VT> GetValues(KT key) |
| | 49 | | { |
| 0 | 50 | | return map.ContainsKey(key) ? map[key] : null; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public void Add(KT key, VT value) |
| | 54 | | { |
| 0 | 55 | | if(map.ContainsKey(key)) |
| 0 | 56 | | map[key].Add(value); |
| | 57 | | else |
| | 58 | | { |
| 0 | 59 | | map[key] = new List<VT>(); |
| 0 | 60 | | map[key].Add(value); |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public void ReplaceFirst(KT key, VT value, Predicate<VT> predicate) |
| | 65 | | { |
| 0 | 66 | | if (map.ContainsKey(key)) |
| | 67 | | { |
| 0 | 68 | | var list = map[key]; |
| 0 | 69 | | var index = list.FindIndex(predicate); |
| 0 | 70 | | if (index != -1) |
| 0 | 71 | | list[index] = value; |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Sort(KT key, Comparison<VT> comparison) |
| | 76 | | { |
| 0 | 77 | | if(map.ContainsKey(key)) |
| 0 | 78 | | map[key].Sort(comparison); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void RemoveRange(KT key, int startIndex, int count) |
| | 82 | | { |
| 0 | 83 | | if(map.ContainsKey(key)) |
| 0 | 84 | | map[key].RemoveRange(startIndex, count); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void Remove(KT key, VT value) |
| | 88 | | { |
| 0 | 89 | | if (map.ContainsKey(key)) |
| | 90 | | { |
| 0 | 91 | | map[key].Remove(value); |
| 0 | 92 | | if (map[key].Count == 0) |
| 0 | 93 | | map.Remove(key); |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void RemoveFromEverywhere(VT value) |
| | 98 | | { |
| 0 | 99 | | foreach (var kvp in map.ToList()) |
| | 100 | | { |
| 0 | 101 | | var list = kvp.Value; |
| 0 | 102 | | list.RemoveAll(v => v.Equals(value)); |
| 0 | 103 | | if (list.Count == 0) |
| 0 | 104 | | map.Remove(kvp.Key); |
| | 105 | | } |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public void Remove(KT key) |
| | 109 | | { |
| 0 | 110 | | map.Remove(key); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public bool ContainsKey(KT key) |
| | 114 | | { |
| 0 | 115 | | return map.ContainsKey(key); |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public bool ContainsValue(VT value) |
| | 119 | | { |
| 0 | 120 | | foreach (var list in map.Values) |
| | 121 | | { |
| 0 | 122 | | if (list.Contains(value)) |
| 0 | 123 | | return true; |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | return false; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public void Clear() |
| | 130 | | { |
| 0 | 131 | | map.Clear(); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public void ClearLists() |
| | 135 | | { |
| 0 | 136 | | foreach(var key in map.Keys) |
| 0 | 137 | | map[key].Clear(); |
| 0 | 138 | | } |
| | 139 | | } |
| | 140 | | } |