-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultiValueDictionary.cs
187 lines (112 loc) · 4.54 KB
/
MultiValueDictionary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using Gsemac.Collections.Extensions;
using Gsemac.Collections.Properties;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if ENABLE_POLYFILLS
using Gsemac.Polyfills.System.Collections.Generic;
using Gsemac.Polyfills.System.Collections.ObjectModel;
#else
using System.Collections.ObjectModel;
#endif
namespace Gsemac.Collections {
public class MultiValueDictionary<TKey, TValue> :
IReadOnlyDictionary<TKey, IReadOnlyCollection<TValue>> {
// Public members
public IReadOnlyCollection<TValue> this[TKey key] => GetValue(key);
public int Count => underlyingDict.Count;
public bool IsReadOnly => underlyingDict.IsReadOnly;
public IEnumerable<TKey> Keys => GetKeys();
public IEnumerable<IReadOnlyCollection<TValue>> Values => GetValues();
public MultiValueDictionary() {
underlyingDict = new Dictionary<TKey, IList<TValue>>();
}
public MultiValueDictionary(int capacity) {
underlyingDict = new Dictionary<TKey, IList<TValue>>(capacity);
}
public MultiValueDictionary(IEqualityComparer<TKey> comparer) {
underlyingDict = new Dictionary<TKey, IList<TValue>>(comparer);
}
public MultiValueDictionary(int capacity, IEqualityComparer<TKey> comparer) {
underlyingDict = new Dictionary<TKey, IList<TValue>>(capacity, comparer);
}
public void Add(TKey key, TValue value) {
if (underlyingDict.TryGetValue(key, out IList<TValue> list)) {
list.Add(value);
}
else {
underlyingDict[key] = new List<TValue> {
value
};
}
}
public void AddRange(TKey key, IEnumerable<TValue> values) {
if (underlyingDict.TryGetValue(key, out IList<TValue> list)) {
list.AddRange(values);
}
else {
underlyingDict[key] = new List<TValue>(values);
}
}
public void Clear() {
underlyingDict.Clear();
}
public bool Contains(TKey key, TValue value) {
return underlyingDict.TryGetValue(key, out IList<TValue> list) &&
list.Contains(value);
}
public bool ContainsKey(TKey key) {
return underlyingDict.ContainsKey(key);
}
public bool ContainsValue(TValue value) {
return underlyingDict.Values.Any(list => list.Contains(value));
}
public bool Remove(TKey key, TValue value) {
bool removed = false;
if (underlyingDict.TryGetValue(key, out IList<TValue> list)) {
removed = list.Remove(value);
if (list.Count <= 0)
Remove(key);
}
return removed;
}
public bool Remove(TKey key) {
return underlyingDict.Remove(key);
}
public IEnumerator<KeyValuePair<TKey, IReadOnlyCollection<TValue>>> GetEnumerator() {
return underlyingDict.Select(pair => new KeyValuePair<TKey, IReadOnlyCollection<TValue>>(pair.Key, CreateReadOnlyCollection(pair.Value)))
.GetEnumerator();
}
public bool TryGetValue(TKey key, out IReadOnlyCollection<TValue> value) {
if (underlyingDict.TryGetValue(key, out IList<TValue> list)) {
value = CreateReadOnlyCollection(list);
return true;
}
else {
value = null;
return false;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private readonly IDictionary<TKey, IList<TValue>> underlyingDict;
private IReadOnlyCollection<TValue> GetValue(TKey key) {
if (underlyingDict.TryGetValue(key, out IList<TValue> value))
return CreateReadOnlyCollection(value);
throw new KeyNotFoundException(ExceptionMessages.KeyNotFound);
}
private ICollection<TKey> GetKeys() {
return underlyingDict.Keys;
}
private ICollection<IReadOnlyCollection<TValue>> GetValues() {
return underlyingDict.Values
.Select(list => CreateReadOnlyCollection(list))
.ToList();
}
private IReadOnlyCollection<TValue> CreateReadOnlyCollection(IList<TValue> list) {
return new ReadOnlyCollection<TValue>(list);
}
}
}