-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiDictionary.cs
300 lines (248 loc) · 9.36 KB
/
MultiDictionary.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Grammophone.GenericContentModel
{
/// <summary>
/// An implementation of <see cref="IMultiDictionary{K, E, C}"/>,
/// a dictionary of items associated with keys, allowing for
/// multiple items having the same key.
/// </summary>
/// <typeparam name="K">The type of the keys.</typeparam>
/// <typeparam name="E">The type of the items.</typeparam>
/// <typeparam name="C">The type of collection under a key. Must descend from <see cref="IReadOnlyBag{E}"/>.</typeparam>
/// <typeparam name="CI">The implementation of <typeparamref name="C"/>. Must have default a constructor.</typeparam>
[Serializable]
public class MultiDictionary<K, E, C, CI> : ReadOnlyMultiDictionary<K, E, C, CI>, IMultiDictionary<K, E, C>
where C : IReadOnlyBag<E>
where CI : C, IInitializable<E>, new()
{
#region Construction
/// <summary>
/// Create empty dictionary.
/// </summary>
public MultiDictionary()
{
}
/// <summary>
/// Create an emty dictionary with an initial capacity of keys.
/// </summary>
/// <param name="keysCapacity">The expected number of keys.</param>
public MultiDictionary(int keysCapacity)
: base(keysCapacity)
{
}
/// <summary>
/// Create from key-value pairs.
/// </summary>
public MultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, E>> keyValuePairs)
: base(keyValuePairs)
{
}
/// <summary>
/// Create from key-collection pairs.
/// </summary>
public MultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, C>> keyCollectionPairs)
: base(keyCollectionPairs)
{
}
/// <summary>
/// Create from items collection and key maper.
/// </summary>
/// <param name="items">The items.</param>
/// <param name="keyMapper">A function to return the key corresponding to an item.</param>
public MultiDictionary(IEnumerable<E> items, Func<E, K> keyMapper)
: base(items, keyMapper)
{
}
/// <summary>
/// Create multi-dictionary from a "master-detail" collection whose entries are
/// objects having a key and the corresponding values.
/// </summary>
/// <typeparam name="T">The type of the entries in the "master-detail" collection.</typeparam>
/// <param name="sourceCollection">The "master-detail" collection.</param>
/// <param name="keyMapper">Maps an entry in the <paramref name="sourceCollection"/> to its key.</param>
/// <param name="elementsMapper">Maps an entry in the <paramref name="sourceCollection"/> to its values.</param>
/// <returns>Returns the dictionary.</returns>
public static new MultiDictionary<K, E, C, CI> CreateByMasterDetailCollection<T>(
IEnumerable<T> sourceCollection,
Func<T, K> keyMapper,
Func<T, IEnumerable<E>> elementsMapper)
{
if (sourceCollection == null) throw new ArgumentNullException("sourceCollection");
if (keyMapper == null) throw new ArgumentNullException("keyMapper");
if (elementsMapper == null) throw new ArgumentNullException("elementsMapper");
var dictionary = new MultiDictionary<K, E, C, CI>();
foreach (var entry in sourceCollection)
{
var key = keyMapper(entry);
foreach (var element in elementsMapper(entry))
{
dictionary.AddItem(key, element);
}
}
return dictionary;
}
/// <summary>
/// Create using a source collection and functions to map keys and values from items of the source collection.
/// </summary>
/// <typeparam name="I">The type of items in the sourcecollection.</typeparam>
/// <param name="sourceCollection">The source collection.</param>
/// <param name="keyMapper">Function to get a key from an item in the <paramref name="sourceCollection"/>.</param>
/// <param name="valueMapper">Function to get a value from an item in the <paramref name="sourceCollection"/>.</param>
/// <returns>Returns the dictionary.</returns>
public static new MultiDictionary<K, E, C, CI> Create<I>(
IEnumerable<I> sourceCollection,
Func<I, K> keyMapper,
Func<I, E> valueMapper)
{
if (sourceCollection == null) throw new ArgumentNullException(nameof(sourceCollection));
if (keyMapper == null) throw new ArgumentNullException(nameof(keyMapper));
if (valueMapper == null) throw new ArgumentNullException(nameof(valueMapper));
var dictionary = new MultiDictionary<K, E, C, CI>();
foreach (var item in sourceCollection)
{
dictionary.AddItem(keyMapper(item), valueMapper(item));
}
return dictionary;
}
#endregion
#region IMultiDictionary<K,E> Members
/// <summary>
/// Add an item associated with a key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="item">The item.</param>
/// <returns>
/// Returns true if addition was successful and
/// no equal item preexisted under the same key, else returns false.
/// </returns>
public bool Add(K key, E item)
{
return AddItem(key, item);
}
/// <summary>
/// Clear all items in the dictionary.
/// </summary>
public void Clear()
{
itemsDictionary.Clear();
count = 0;
}
#endregion
}
/// <summary>
/// An implementation of <see cref="IMultiDictionary{K, E, C}"/>,
/// a dictionary of items associated with keys, allowing for
/// multiple items having the same key.
/// </summary>
/// <typeparam name="K">The type of the keys.</typeparam>
/// <typeparam name="E">The type of the items.</typeparam>
[Serializable]
public class MultiDictionary<K, E> : MultiDictionary<K, E, IReadOnlyBag<E>, ReadOnlyBag<E>>, IMultiDictionary<K, E>
{
#region Construction
/// <summary>
/// Create empty dictionary.
/// </summary>
public MultiDictionary()
{
}
/// <summary>
/// Create an emty dictionary with an initial capacity of keys.
/// </summary>
/// <param name="keysCapacity">The expected number of keys.</param>
public MultiDictionary(int keysCapacity)
: base(keysCapacity)
{
}
/// <summary>
/// Create from key-value pairs.
/// </summary>
public MultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, E>> keyValuePairs)
: base(keyValuePairs)
{
}
/// <summary>
/// Create from key-collection pairs.
/// </summary>
public MultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, ReadOnlyBag<E>>> keyCollectionPairs)
: base(keyCollectionPairs)
{
}
/// <summary>
/// Create from items collection and key maper.
/// </summary>
/// <param name="items">The items.</param>
/// <param name="keyMapper">A function to return the key corresponding to an item.</param>
public MultiDictionary(IEnumerable<E> items, Func<E, K> keyMapper)
: base(items, keyMapper)
{
}
/// <summary>
/// Create multi-dictionary from a "master-detail" collection whose entries are
/// objects having a key and the corresponding values.
/// </summary>
/// <typeparam name="T">The type of the entries in the "master-detail" collection.</typeparam>
/// <param name="sourceCollection">The "master-detail" collection.</param>
/// <param name="keyMapper">Maps an entry in the <paramref name="sourceCollection"/> to its key.</param>
/// <param name="elementsMapper">Maps an entry in the <paramref name="sourceCollection"/> to its values.</param>
/// <returns>Returns the dictionary.</returns>
public static new MultiDictionary<K, E> CreateByMasterDetailCollection<T>(
IEnumerable<T> sourceCollection,
Func<T, K> keyMapper,
Func<T, IEnumerable<E>> elementsMapper)
{
if (sourceCollection == null) throw new ArgumentNullException("sourceCollection");
if (keyMapper == null) throw new ArgumentNullException("keyMapper");
if (elementsMapper == null) throw new ArgumentNullException("elementsMapper");
var dictionary = new MultiDictionary<K, E>();
foreach (var entry in sourceCollection)
{
var key = keyMapper(entry);
foreach (var element in elementsMapper(entry))
{
dictionary.AddItem(key, element);
}
}
return dictionary;
}
/// <summary>
/// Create using a source collection and functions to map keys and values from items of the source collection.
/// </summary>
/// <typeparam name="I">The type of items in the sourcecollection.</typeparam>
/// <param name="sourceCollection">The source collection.</param>
/// <param name="keyMapper">Function to get a key from an item in the <paramref name="sourceCollection"/>.</param>
/// <param name="valueMapper">Function to get a value from an item in the <paramref name="sourceCollection"/>.</param>
/// <returns>Returns the dictionary.</returns>
public static new MultiDictionary<K, E> Create<I>(
IEnumerable<I> sourceCollection,
Func<I, K> keyMapper,
Func<I, E> valueMapper)
{
if (sourceCollection == null) throw new ArgumentNullException(nameof(sourceCollection));
if (keyMapper == null) throw new ArgumentNullException(nameof(keyMapper));
if (valueMapper == null) throw new ArgumentNullException(nameof(valueMapper));
var dictionary = new MultiDictionary<K, E>();
foreach (var item in sourceCollection)
{
dictionary.AddItem(keyMapper(item), valueMapper(item));
}
return dictionary;
}
#endregion
#region IReadOnlyMultiDictionary<K,E,IReadOnlyBag<E>> Members
IReadOnlyBag<E> IReadOnlyMultiDictionary<K, E, IReadOnlyBag<E>>.this[K key]
{
get { return this[key]; }
}
#endregion
#region IEnumerable<IReadOnlyKeyValuePair<K,IReadOnlyBag<E>>> Members
IEnumerator<IReadOnlyKeyValuePair<K, IReadOnlyBag<E>>> IEnumerable<IReadOnlyKeyValuePair<K, IReadOnlyBag<E>>>.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}