/
SortedBucketCollection.cs
477 lines (365 loc) · 14.5 KB
/
SortedBucketCollection.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**************************************************************************************
Storage.SortedBucketCollection
==============================
SortedBucketCollection stores items (TValue) which have 2 keys. Conceptually, a bucket stores items having the same
value for TKey1. Within one bucket, each item must have a unique TKey2. However, internally, the items are not stored
in buckets, but a sorted linked list. SortedBucketCollection supports 3 indexers:
[TKey1]: returns an IEnumerable<TValue> looping over all items with the same TKey1
[TKey1a, TKey1b]: returns an IEnumerable<TValue> looping over all items with TKey1 in the range of TKey1a and TKey1b
[TKey1, TKey2]: returns one particular item.
Written in 2021 by Jürgpeter Huber
Contact: PeterCode at Peterbox dot com
To the extent possible under law, the author(s) have dedicated all copyright and
related and neighboring rights to this software to the public domain worldwide under
the Creative Commons 0 license (details see COPYING.txt file, see also
<http://creativecommons.org/publicdomain/zero/1.0/>).
This software is distributed without any warranty.
**************************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace StorageLib {
#region IReadOnlySortedBucketCollection
// -------------------------------
/// <summary>
/// Provides readonly access to a BucketCollection, which works like a Dictionary, but each item needs 2 keys.
/// </summary>
public interface IReadOnlySortedBucketCollection<TKey1, TKey2, TValue>: IReadOnlyCollection<TValue> {
#region Properties
// ----------
/// <summary>
/// A collection of all TKey1 values stored.
/// </summary>
ICollection<TKey1> Keys {get;}
/// <summary>
/// Number of all TKeys stored
/// </summary>
public int Key1Count {get;}
/// <summary>
/// Returns all items with TKey==key1
/// </summary>
public IEnumerable<TValue> this[TKey1 key1] {get;}
/// <summary>
/// Returns all items where TKey>=key1Lower && TKey<=key1Higher
/// </summary>
public IEnumerable<TValue> this[TKey1 key1Lower, TKey1 key1Higher] { get; }
/// <summary>
/// Returns the item stored for key1, key2
/// </summary>
public TValue? this[TKey1 key1, TKey2 key2] {get;}
#endregion
#region Methods
// -------
/// <summary>
/// Does at least 1 item exist in SortedBuckers with item.Key1==key1 ?
/// </summary>
public bool Contains(TKey1 key1);
#endregion
/// <summary>
/// Does an item exist in SortedBuckers with item.Key1==key1 and item.Key2==key2 ?
/// </summary>
public bool Contains(TKey1 key1, TKey2 key2);
/// <summary>
/// Does an item with item.Key1==key1 and item.Key2==key2 exist in SortedBuckers ?
/// </summary>
public bool Contains(TValue item);
/// <summary>
/// Returns true if StoredBuckets stores an item with item.Key1==key1 and item Key2==key2
/// </summary>
public bool TryGetValue(TKey1 key1, TKey2 key2, [MaybeNullWhen(false)] out TValue value);
}
#endregion
#region SortedBucketCollection
// ----------------------
/// <summary>
/// Like a SortedList, SortedBucketCollection stores TValue items which can be retrieved by their TKey1 value. In SortedList,
/// each TKey accesses at most 1 item. In SortedBucketCollection, each TKey1 accesses a bucket, which can contain 0 to many items
/// with the same TKey1, but different, unique TKey2. Items are sorted by TKey1, then TKey2. TKey1 and TKey2 are
/// properties within TValue.
/// </summary>
public class SortedBucketCollection<TKey1, TKey2, TValue>: ICollection<TValue>, IReadOnlySortedBucketCollection<TKey1, TKey2, TValue>
where TKey1: notnull, IComparable<TKey1>
where TKey2: notnull, IComparable<TKey2>
where TValue: class
{
#region Properties
// ----------
/// <summary>
/// A collection of all TKey1 values stored.
/// </summary>
public ICollection<TKey1> Keys => buckets.Keys;
/// <summary>
/// Number of all TValues stored
/// </summary>
public int Count { get; private set;}
/// <summary>
/// Number of all TKeys stored
/// </summary>
public int Key1Count => buckets.Count;
/// <summary>
/// Readonly versions of SortedBucketCollection don't exist
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Returns all items with TKey==key1
/// </summary>
public IEnumerable<TValue> this[TKey1 key1] => getValuesFor(key1);
private IEnumerable<TValue> getValuesFor(TKey1 key1) {
if (!buckets.TryGetValue(key1, out var bucketItem)) yield break;
var versionCopy = version;
do {
yield return bucketItem.Item;
if (versionCopy!=version) {
throw new InvalidOperationException();
}
bucketItem = bucketItem.Next;
} while (bucketItem is not null);
}
/// <summary>
/// Returns all items where TKey>=key1Lower && TKey<=key1Higher
/// </summary>
public IEnumerable<TValue> this[TKey1 key1Lower, TKey1 key1Higher] => getValuesFor(key1Lower, key1Higher);
private IEnumerable<TValue> getValuesFor(TKey1 key1Lower, TKey1 key1Higher) {
var versionCopy = version;
foreach (var keyValuePairBucketItem in buckets) {
if (keyValuePairBucketItem.Key.CompareTo(key1Lower)<0) continue;
if (keyValuePairBucketItem.Key.CompareTo(key1Higher)>0) break;
var bucketItem = keyValuePairBucketItem.Value;
yield return bucketItem.Item;
if (versionCopy!=version) {
throw new InvalidOperationException();
}
while (bucketItem.Next is not null) {
bucketItem = bucketItem.Next;
yield return bucketItem.Item;
if (versionCopy!=version) {
throw new InvalidOperationException();
}
}
}
}
/// <summary>
/// Returns the item stored for key1, key2
/// </summary>
public TValue? this[TKey1 key1, TKey2 key2] => getValueFor(key1, key2);
private TValue? getValueFor(TKey1 key1, TKey2 key2) {
if (!buckets.TryGetValue(key1, out var bucketItem)) return null;
var compareResult = getKey2(bucketItem.Item).CompareTo(key2);
while (compareResult==-1) {
//Key2 is greater than bucketItem.Key2
if (bucketItem.Next is null) return null;
bucketItem = bucketItem.Next;
compareResult = getKey2(bucketItem.Item).CompareTo(key2);
}
return compareResult==0 ? bucketItem.Item : null;
}
#endregion
#region Bucket
// ------
/// <summary>
/// Basic storage unit in StoredBuckets.
/// </summary>
private class BucketItem{
public readonly TValue Item;
public BucketItem? Next;
public BucketItem(TValue item, BucketItem? next) {
Item = item;
Next = next;
}
public override string ToString() {
if (Next is null) {
return $"{Item}; Next: ";
} else {
var nextItem = Next.Item; //bug in C# compiler with $"Next: {this.Next?.Item}"
return $"{Item}; Next: {nextItem}";
}
}
}
#endregion
#region Constructor
// -----------
readonly Func<TValue, TKey1> getKey1;
readonly Func<TValue, TKey2> getKey2;
readonly SortedDictionary<TKey1, BucketItem> buckets;
int version;
/// <summary>
/// The getKeyX() delegates provide access to the TKeyX property in TValue
/// </summary>
public SortedBucketCollection(Func<TValue, TKey1> getKey1, Func<TValue, TKey2> getKey2) {
this.getKey1 = getKey1;
this.getKey2 = getKey2;
buckets = new();
Count = 0;
}
#endregion
#region Methods
// -------
/// <summary>
/// Adds value to SortedBucketCollection. It throws an exception if an item is stored already with value.Key1 and value.Key2.
/// </summary>
public void Add(TValue item) {
var key1 = getKey1(item);
if (buckets.TryGetValue(key1, out var foundBucketItem)) {
var key2 = getKey2(item);
var foundBucketItemKey2 = getKey2(foundBucketItem.Item);
var compareResult = key2.CompareTo(foundBucketItemKey2);
if (compareResult==0) throw new ArgumentException($"SortedBucketCollection.Add({item}): Key2 {key2} is used by already stored {foundBucketItem}.");
if (compareResult<0) {
//bucket.Key2<foundBucket.Key2
//insert bucket into buckets, link foundBucket from bucket
buckets[key1] = new BucketItem(item, foundBucketItem);
} else {
//bucket.Key2>foundBucket.Key2
//find where in the linked list bucket needs to get inserted
var nextBucketItem = foundBucketItem.Next;
while (nextBucketItem is not null) {
var nextBucketItemKey2 = getKey2(nextBucketItem.Item);
compareResult = key2.CompareTo(nextBucketItemKey2);
if (compareResult==0) throw new ArgumentException($"SortedBucketCollection.Add({item}): Key2 {key2} is used by already stored {nextBucketItem}.");
if (compareResult<0) {
//bucket.Key2<nextBucketItem.Key2
//insert bucket between foundBucketItem and nextBucketItem
foundBucketItem.Next = new BucketItem(item, nextBucketItem);
Count++;
version++;
return;
}
foundBucketItem = nextBucketItem;
foundBucketItemKey2 = nextBucketItemKey2;
nextBucketItem = foundBucketItem.Next;
}
//bucket has bigger Key2 than any existing bucketItem with same Key1
foundBucketItem.Next = new BucketItem(item, null);
}
} else {
//first item for that Key1
buckets.Add(key1, new BucketItem(item, null));
}
Count++;
version++;
}
/// <summary>
/// Does at least 1 item exist in SortedBuckers with item.Key1==key1 ?
/// </summary>
public bool Contains(TKey1 key1) {
return buckets.ContainsKey(key1);
}
/// <summary>
/// Does an item exist in SortedBuckers with item.Key1==key1 and item.Key2==key2 ?
/// </summary>
public bool Contains(TKey1 key1, TKey2 key2) {
return this[key1, key2] is not null;
}
/// <summary>
/// Does an item with item.Key1==key1 and item.Key2==key2 exist in SortedBuckers ?
/// </summary>
public bool Contains(TValue item) {
var key1 = getKey1(item);
var key2 = getKey2(item);
return this[key1, key2] is not null;
}
/// <summary>
/// Returns true if StoredBuckets stores an item with item.Key1==key1 and item Key2==key2
/// </summary>
public bool TryGetValue(TKey1 key1, TKey2 key2, [MaybeNullWhen(false)] out TValue value) {
value = getValueFor(key1, key2);
return value is not null;
}
/// <summary>
/// Enumerates over all items in SortedBucketCollection, sorted by Key1, Key2
/// </summary>
public IEnumerator<TValue> GetEnumerator() {
var versionCopy = version;
foreach (var keyValuePairBucketItem in buckets) {
var bucketItem = keyValuePairBucketItem.Value;
yield return bucketItem.Item;
if (versionCopy!=version) {
throw new InvalidOperationException();
}
while (bucketItem.Next is not null) {
bucketItem = bucketItem.Next;
yield return bucketItem.Item;
if (versionCopy!=version) {
throw new InvalidOperationException();
}
}
}
}
/// <summary>
/// Enumerates over all items in SortedBucketCollection, sorted by Key1, Key2
/// </summary>
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
/// <summary>
/// Returns true if an item with item.Key1==key1 and item.Key2==key2 was found. Item gets removed.
/// </summary>
public bool Remove(TValue item) {
var key1 = getKey1(item);
if (!buckets.TryGetValue(key1, out var existingBucketItem)) return false; //Key1 is completely missing in buckets
var key2 = getKey2(item);
var existingBucketItemKey2 = getKey2(existingBucketItem.Item);
var compareResult = key2.CompareTo(existingBucketItemKey2);
if (compareResult==0) {
if (existingBucketItem.Next is null) {
//only one item for Key1, remove it
buckets.Remove(key1);
} else {
//replace found item with its next item
buckets[key1] = existingBucketItem.Next;
}
Count--;
version++;
return true;
} else if (compareResult<0) {
//Key2<existingBucket.Key2, Key2 is missing
return false;
}
//Key2>existingBucket.Key2
//search linked list bucket for Key2
var nextBucketItem = existingBucketItem.Next;
while (nextBucketItem is not null) {
var nextBucketItemKey2 = getKey2(nextBucketItem.Item);
compareResult = key2.CompareTo(nextBucketItemKey2);
if (compareResult<0) {
//Key2<nextBucketItem.Key2
//Key2 cannot be found
return false;
} else if (compareResult==0) {
//item matching Key1 and Key2 found. Remove it from linked list
existingBucketItem.Next = nextBucketItem.Next;
Count--;
version++;
return true;
}
existingBucketItem = nextBucketItem;
existingBucketItemKey2 = nextBucketItemKey2;
nextBucketItem = existingBucketItem.Next;
}
//end of linked list reached, key2 not found
return false;
}
/// <summary>
/// Releases all items from SortedBucketCollection
/// </summary>
public void Clear() {
buckets.Clear();//by removing the start of every linked list, all other items in the linked list become unreachable too
Count = 0;
version++;
}
/// <summary>
/// Copies the elements of the SortedBucketCollection to an Array, starting at a particular Array index.
/// </summary>
public void CopyTo(TValue[] array, int arrayIndex) {
if (array is null) throw new ArgumentNullException(nameof(array));
if (arrayIndex<0) throw new ArgumentOutOfRangeException(nameof(array), $"value needs to be greater 0, but was {arrayIndex}.");
if (array.Length - arrayIndex < Count) throw new ArgumentException("array is too small.");
foreach (var item in this) {
array[arrayIndex++] = item;
}
}
#endregion
}
#endregion
}