-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadOnlyMultiDictionary.cs
463 lines (394 loc) · 13.9 KB
/
ReadOnlyMultiDictionary.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Grammophone.GenericContentModel
{
/// <summary>
/// Implementation of <see cref="IReadOnlyMultiDictionary{K, E, C}"/>,
/// a read-only 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]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(DebuggerProxies.MultiDictionaryDebuggerProxy<, ,>))]
public class ReadOnlyMultiDictionary<K, E, C, CI> : IReadOnlyMultiDictionary<K, E, C>
where C : IReadOnlyBag<E>
where CI : C, IInitializable<E>, new()
{
#region Protected fields
protected readonly Dictionary<K, CI> itemsDictionary;
protected int count;
protected static readonly CI emptyItemsCollection = new CI();
#endregion
#region Construction
/// <summary>
/// Create an empty dictionary.
/// </summary>
public ReadOnlyMultiDictionary()
{
this.itemsDictionary = new Dictionary<K, CI>();
this.count = 0;
}
/// <summary>
/// Create an emty dictionary with an initial capacity of keys.
/// </summary>
/// <param name="keysCapacity">The expected number of keys.</param>
public ReadOnlyMultiDictionary(int keysCapacity)
{
this.itemsDictionary = new Dictionary<K, CI>(keysCapacity);
this.count = 0;
}
/// <summary>
/// Create from key-value pairs.
/// </summary>
public ReadOnlyMultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, E>> keyValuePairs)
: this()
{
if (keyValuePairs == null) throw new ArgumentNullException("keyValuePairs");
foreach (var pair in keyValuePairs)
{
AddItem(pair.Key, pair.Value);
}
}
/// <summary>
/// Create from key-collection pairs.
/// </summary>
public ReadOnlyMultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, C>> keyCollectionPairs)
: this(keyCollectionPairs.Count())
{
if (keyCollectionPairs == null) throw new ArgumentNullException("keyCollectionPairs");
foreach (var keyCollectionPair in keyCollectionPairs)
{
foreach (var item in keyCollectionPair.Value)
{
AddItem(keyCollectionPair.Key, item);
}
}
}
/// <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 ReadOnlyMultiDictionary(IEnumerable<E> items, Func<E, K> keyMapper)
: this()
{
if (items == null) throw new ArgumentNullException("items");
if (keyMapper == null) throw new ArgumentNullException("keyMapper");
foreach (var item in items)
{
AddItem(keyMapper(item), item);
}
}
/// <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 ReadOnlyMultiDictionary<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 ReadOnlyMultiDictionary<K, E, C, CI>(sourceCollection.Count());
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 ReadOnlyMultiDictionary<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 ReadOnlyMultiDictionary<K, E, C, CI>();
foreach (var item in sourceCollection)
{
dictionary.AddItem(keyMapper(item), valueMapper(item));
}
return dictionary;
}
#endregion
#region IReadOnlyMultiDictionary<K,E> Members
/// <summary>
/// The number of all items in the dictionary.
/// </summary>
/// <remarks>
/// This is the count of items, not keys.
/// </remarks>
public int Count
{
get { return count; }
}
/// <summary>
/// Get the collection of items by the specified <paramref name="key"/>.
/// If no items exist under the <paramref name="key"/>, an empty collection is returned.
/// </summary>
/// <param name="key">The key of the items.</param>
/// <returns>
/// Returns a read-only collection of items associated with the key, else returns an empty collection.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="key"/> was null.
/// </exception>
public C this[K key]
{
get
{
CI items;
if (itemsDictionary.TryGetValue(key, out items))
{
return items;
}
else
{
return emptyItemsCollection;
}
}
}
/// <summary>
/// The unique keys of all the items in the dictionary.
/// </summary>
public IEnumerable<K> Keys
{
get
{
return itemsDictionary.Keys;
}
}
/// <summary>
/// Returns true if at least one item is associated with the specified <paramref name="key"/>.
/// </summary>
public bool ContainsKey(K key)
{
return itemsDictionary.ContainsKey(key);
}
#endregion
#region IEnumerable<IReadOnlyKeyValuePair<K,C>> Members
public IEnumerator<IReadOnlyKeyValuePair<K, C>> GetEnumerator()
{
var query = from entry in itemsDictionary
select new ReadOnlyKeyValuePair<K, C>(entry.Key, entry.Value);
return query.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region Internal methods
/// <summary>
/// Called when an item is added to the collection.
/// </summary>
/// <param name="key">The key of the item.</param>
/// <param name="item">The added item.</param>
/// <returns>Returns true if the element was not already in the collection and added successfully, else false.</returns>
internal protected virtual bool AddItem(K key, E item)
{
if (key == null) throw new ArgumentNullException("key");
CI items;
if (!itemsDictionary.TryGetValue(key, out items))
{
items = new CI();
itemsDictionary.Add(key, items);
}
if (items.AddItem(item))
{
count++;
return true;
}
else
{
return false;
}
}
#endregion
}
/// <summary>
/// Implementation of <see cref="IReadOnlyMultiDictionary{K, E}"/>,
/// a read-only dictionary of items associated with keys, allowing for
/// multiple items having the same key.
/// Allows specification of the implementation of collection of items under a key.
/// </summary>
/// <typeparam name="K">The type of the keys.</typeparam>
/// <typeparam name="E">The type of the items.</typeparam>
/// <typeparam name="CI">The implementation of <see cref="IReadOnlyBag{E}"/>. Must have default a constructor.</typeparam>
[Serializable]
public class ReadOnlyMultiDictionary<K, E, CI> :
ReadOnlyMultiDictionary<K, E, IReadOnlyBag<E>, CI>, IReadOnlyMultiDictionary<K, E>
where CI : IReadOnlyBag<E>, IInitializable<E>, new()
{
#region Construction
/// <summary>
/// Create an empty dictionary.
/// </summary>
public ReadOnlyMultiDictionary()
{
}
/// <summary>
/// Create an emty dictionary with an initial capacity of keys.
/// </summary>
/// <param name="keysCapacity">The expected number of keys.</param>
public ReadOnlyMultiDictionary(int keysCapacity)
: base(keysCapacity)
{
}
/// <summary>
/// Create from key-value pairs.
/// </summary>
public ReadOnlyMultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, E>> keyValuePairs)
: base(keyValuePairs)
{
}
/// <summary>
/// Create from key-collection pairs.
/// </summary>
public ReadOnlyMultiDictionary(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 ReadOnlyMultiDictionary(IEnumerable<E> items, Func<E, K> keyMapper)
: base(items, keyMapper)
{
}
#endregion
}
/// <summary>
/// Implementation of <see cref="IReadOnlyMultiDictionary{K, E}"/>,
/// a read-only 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 ReadOnlyMultiDictionary<K, E> :
ReadOnlyMultiDictionary<K, E, ReadOnlyBag<E>>
{
#region Construction
/// <summary>
/// Create an empty dictionary.
/// </summary>
public ReadOnlyMultiDictionary()
{
}
/// <summary>
/// Create an emty dictionary with an initial capacity of keys.
/// </summary>
/// <param name="keysCapacity">The expected number of keys.</param>
public ReadOnlyMultiDictionary(int keysCapacity)
: base(keysCapacity)
{
}
/// <summary>
/// Create from key-value pairs.
/// </summary>
public ReadOnlyMultiDictionary(IEnumerable<IReadOnlyKeyValuePair<K, E>> keyValuePairs)
: base(keyValuePairs)
{
}
/// <summary>
/// Create from key-collection pairs.
/// </summary>
public ReadOnlyMultiDictionary(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 ReadOnlyMultiDictionary(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 ReadOnlyMultiDictionary<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 ReadOnlyMultiDictionary<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 ReadOnlyMultiDictionary<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 ReadOnlyMultiDictionary<K, E>();
foreach (var item in sourceCollection)
{
dictionary.AddItem(keyMapper(item), valueMapper(item));
}
return dictionary;
}
#endregion
}
}