-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultimap.mustache
287 lines (253 loc) · 10.1 KB
/
Multimap.mustache
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{{>partial_header}}
using System;
using System.Collections;
using System.Collections.Generic;
namespace {{packageName}}.Client
{
/// <summary>
/// A dictionary in which one key has many associated values.
/// </summary>
/// <typeparam name="TKey">The type of the key</typeparam>
/// <typeparam name="TValue">The type of the value associated with the key.</typeparam>
public class Multimap<TKey, TValue> : IDictionary<TKey, IList<TValue>>
{
#region Private Fields
private readonly Dictionary<TKey, IList<TValue>> _dictionary;
#endregion Private Fields
#region Constructors
/// <summary>
/// Empty Constructor.
/// </summary>
public Multimap()
{
_dictionary = new Dictionary<TKey, IList<TValue>>();
}
/// <summary>
/// Constructor with comparer.
/// </summary>
/// <param name="comparer"></param>
public Multimap(IEqualityComparer<TKey> comparer)
{
_dictionary = new Dictionary<TKey, IList<TValue>>(comparer);
}
#endregion Constructors
#region Enumerators
/// <summary>
/// To get the enumerator.
/// </summary>
/// <returns>Enumerator</returns>
public IEnumerator<KeyValuePair<TKey, IList<TValue>>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
/// <summary>
/// To get the enumerator.
/// </summary>
/// <returns>Enumerator</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return _dictionary.GetEnumerator();
}
#endregion Enumerators
#region Public Members
/// <summary>
/// Add values to Multimap
/// </summary>
/// <param name="item">Key value pair</param>
public void Add(KeyValuePair<TKey, IList<TValue>> item)
{
if (!TryAdd(item.Key, item.Value))
throw new InvalidOperationException("Could not add values to Multimap.");
}
/// <summary>
/// Add Multimap to Multimap
/// </summary>
/// <param name="multimap">Multimap</param>
public void Add(Multimap<TKey, TValue> multimap)
{
foreach (var item in multimap)
{
if (!TryAdd(item.Key, item.Value))
throw new InvalidOperationException("Could not add values to Multimap.");
}
}
/// <summary>
/// Clear Multimap
/// </summary>
public void Clear()
{
_dictionary.Clear();
}
/// <summary>
/// Determines whether Multimap contains the specified item.
/// </summary>
/// <param name="item">Key value pair</param>
/// <exception cref="NotImplementedException">Method needs to be implemented</exception>
/// <returns>true if the Multimap contains the item; otherwise, false.</returns>
public bool Contains(KeyValuePair<TKey, IList<TValue>> item)
{
throw new NotImplementedException();
}
/// <summary>
/// Copy items of the Multimap to an array,
/// starting at a particular array index.
/// </summary>
/// <param name="array">The array that is the destination of the items copied
/// from Multimap. The array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
/// <exception cref="NotImplementedException">Method needs to be implemented</exception>
public void CopyTo(KeyValuePair<TKey, IList<TValue>>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
/// <summary>
/// Removes the specified item from the Multimap.
/// </summary>
/// <param name="item">Key value pair</param>
/// <returns>true if the item is successfully removed; otherwise, false.</returns>
/// <exception cref="NotImplementedException">Method needs to be implemented</exception>
public bool Remove(KeyValuePair<TKey, IList<TValue>> item)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the number of items contained in the Multimap.
/// </summary>
public int Count => _dictionary.Count;
/// <summary>
/// Gets a value indicating whether the Multimap is read-only.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Adds an item with the provided key and value to the Multimap.
/// </summary>
/// <param name="key">The object to use as the key of the item to add.</param>
/// <param name="value">The object to use as the value of the item to add.</param>
/// <exception cref="InvalidOperationException">Thrown when couldn't add the value to Multimap.</exception>
public void Add(TKey key, IList<TValue> value)
{
if (value != null && value.Count > 0)
{
if (_dictionary.TryGetValue(key, out var list))
{
foreach (var k in value) list.Add(k);
}
else
{
list = new List<TValue>(value);
if (!TryAdd(key, list))
throw new InvalidOperationException("Could not add values to Multimap.");
}
}
}
/// <summary>
/// Determines whether the Multimap contains an item with the specified key.
/// </summary>
/// <param name="key">The key to locate in the Multimap.</param>
/// <returns>true if the Multimap contains an item with
/// the key; otherwise, false.</returns>
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
/// <summary>
/// Removes item with the specified key from the Multimap.
/// </summary>
/// <param name="key">The key to locate in the Multimap.</param>
/// <returns>true if the item is successfully removed; otherwise, false.</returns>
public bool Remove(TKey key)
{
return TryRemove(key, out var _);
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the
/// key is found; otherwise, the default value for the type of the value parameter.
/// This parameter is passed uninitialized.</param>
/// <returns> true if the object that implements Multimap contains
/// an item with the specified key; otherwise, false.</returns>
public bool TryGetValue(TKey key, out IList<TValue> value)
{
return _dictionary.TryGetValue(key, out value);
}
/// <summary>
/// Gets or sets the item with the specified key.
/// </summary>
/// <param name="key">The key of the item to get or set.</param>
/// <returns>The value of the specified key.</returns>
public IList<TValue> this[TKey key]
{
get => _dictionary[key];
set => _dictionary[key] = value;
}
/// <summary>
/// Gets a System.Collections.Generic.ICollection containing the keys of the Multimap.
/// </summary>
public ICollection<TKey> Keys => _dictionary.Keys;
/// <summary>
/// Gets a System.Collections.Generic.ICollection containing the values of the Multimap.
/// </summary>
public ICollection<IList<TValue>> Values => _dictionary.Values;
/// <summary>
/// Copy the items of the Multimap to an System.Array,
/// starting at a particular System.Array index.
/// </summary>
/// <param name="array">The one-dimensional System.Array that is the destination of the items copied
/// from Multimap. The System.Array must have zero-based indexing.</param>
/// <param name="index">The zero-based index in array at which copying begins.</param>
public void CopyTo(Array array, int index)
{
((ICollection)_dictionary).CopyTo(array, index);
}
/// <summary>
/// Adds an item with the provided key and value to the Multimap.
/// </summary>
/// <param name="key">The object to use as the key of the item to add.</param>
/// <param name="value">The object to use as the value of the item to add.</param>
/// <exception cref="InvalidOperationException">Thrown when couldn't add value to Multimap.</exception>
public void Add(TKey key, TValue value)
{
if (value != null)
{
if (_dictionary.TryGetValue(key, out var list))
{
list.Add(value);
}
else
{
list = new List<TValue> { value };
if (!TryAdd(key, list))
throw new InvalidOperationException("Could not add value to Multimap.");
}
}
}
#endregion Public Members
#region Private Members
/**
* Helper method to encapsulate generator differences between dictionary types.
*/
private bool TryRemove(TKey key, out IList<TValue> value)
{
_dictionary.TryGetValue(key, out value);
return _dictionary.Remove(key);
}
/**
* Helper method to encapsulate generator differences between dictionary types.
*/
private bool TryAdd(TKey key, IList<TValue> value)
{
try
{
_dictionary.Add(key, value);
}
catch (ArgumentException)
{
return false;
}
return true;
}
#endregion Private Members
}
}