/
ImmutableSortedSet_1.cs
1277 lines (1117 loc) · 44.6 KB
/
ImmutableSortedSet_1.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
namespace System.Collections.Immutable
{
/// <summary>
/// An immutable sorted set implementation.
/// </summary>
/// <typeparam name="T">The type of elements in the set.</typeparam>
/// <devremarks>
/// We implement <see cref="IReadOnlyList{T}"/> because it adds an ordinal indexer.
/// We implement <see cref="IList{T}"/> because it gives us <see cref="IList{T}.IndexOf"/>, which is important for some folks.
/// </devremarks>
[CollectionBuilder(typeof(ImmutableSortedSet), nameof(ImmutableSortedSet.Create))]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))]
#if NETCOREAPP
public sealed partial class ImmutableSortedSet<T> : IImmutableSet<T>, ISortKeyCollection<T>, IReadOnlySet<T>, IReadOnlyList<T>, IList<T>, ISet<T>, IList, IStrongEnumerable<T, ImmutableSortedSet<T>.Enumerator>
#else
public sealed partial class ImmutableSortedSet<T> : IImmutableSet<T>, ISortKeyCollection<T>, IReadOnlyList<T>, IList<T>, ISet<T>, IList, IStrongEnumerable<T, ImmutableSortedSet<T>.Enumerator>
#endif
{
/// <summary>
/// This is the factor between the small collection's size and the large collection's size in a bulk operation,
/// under which recreating the entire collection using a fast method rather than some incremental update
/// (that requires tree rebalancing) is preferable.
/// </summary>
private const float RefillOverIncrementalThreshold = 0.15f;
/// <summary>
/// An empty sorted set with the default sort comparer.
/// </summary>
public static readonly ImmutableSortedSet<T> Empty = new ImmutableSortedSet<T>();
/// <summary>
/// The root node of the AVL tree that stores this set.
/// </summary>
private readonly Node _root;
/// <summary>
/// The comparer used to sort elements in this set.
/// </summary>
private readonly IComparer<T> _comparer;
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableSortedSet{T}"/> class.
/// </summary>
/// <param name="comparer">The comparer.</param>
internal ImmutableSortedSet(IComparer<T>? comparer = null)
{
_root = Node.EmptyNode;
_comparer = comparer ?? Comparer<T>.Default;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableSortedSet{T}"/> class.
/// </summary>
/// <param name="root">The root of the AVL tree with the contents of this set.</param>
/// <param name="comparer">The comparer.</param>
private ImmutableSortedSet(Node root, IComparer<T> comparer)
{
Requires.NotNull(root, nameof(root));
Requires.NotNull(comparer, nameof(comparer));
root.Freeze();
_root = root;
_comparer = comparer;
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Clear()
{
return _root.IsEmpty ? this : Empty.WithComparer(_comparer);
}
/// <summary>
/// Gets the maximum value in the collection, as defined by the comparer.
/// </summary>
/// <value>The maximum value in the set.</value>
public T? Max
{
get { return _root.Max; }
}
/// <summary>
/// Gets the minimum value in the collection, as defined by the comparer.
/// </summary>
/// <value>The minimum value in the set.</value>
public T? Min
{
get { return _root.Min; }
}
#region IImmutableSet<T> Properties
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public bool IsEmpty
{
get { return _root.IsEmpty; }
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public int Count
{
get { return _root.Count; }
}
#endregion
#region ISortKeyCollection<T> Properties
/// <summary>
/// See the <see cref="ISortKeyCollection{T}"/> interface.
/// </summary>
public IComparer<T> KeyComparer
{
get { return _comparer; }
}
#endregion
/// <summary>
/// Gets the root node (for testing purposes).
/// </summary>
internal IBinaryTree Root
{
get { return _root; }
}
#region IReadOnlyList<T> Indexers
/// <summary>
/// Gets the element of the set at the given index.
/// </summary>
/// <param name="index">The 0-based index of the element in the set to return.</param>
/// <returns>The element at the given position.</returns>
public T this[int index]
{
get
{
return _root.ItemRef(index);
}
}
/// <summary>
/// Gets a read-only reference of the element of the set at the given index.
/// </summary>
/// <param name="index">The 0-based index of the element in the set to return.</param>
/// <returns>A read-only reference of the element at the given position.</returns>
public ref readonly T ItemRef(int index)
{
return ref _root.ItemRef(index);
}
#endregion
#region Public methods
/// <summary>
/// Creates a collection with the same contents as this collection that
/// can be efficiently mutated across multiple operations using standard
/// mutable interfaces.
/// </summary>
/// <remarks>
/// This is an O(1) operation and results in only a single (small) memory allocation.
/// The mutable collection that is returned is *not* thread-safe.
/// </remarks>
public Builder ToBuilder()
{
// We must not cache the instance created here and return it to various callers.
// Those who request a mutable collection must get references to the collection
// that version independently of each other.
return new Builder(this);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Add(T value)
{
return this.Wrap(_root.Add(value, _comparer, out _));
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Remove(T value)
{
return this.Wrap(_root.Remove(value, _comparer, out _));
}
/// <summary>
/// Searches the set for a given value and returns the equal value it finds, if any.
/// </summary>
/// <param name="equalValue">The value to search for.</param>
/// <param name="actualValue">The value from the set that the search found, or the original value if the search yielded no match.</param>
/// <returns>A value indicating whether the search was successful.</returns>
/// <remarks>
/// This can be useful when you want to reuse a previously stored reference instead of
/// a newly constructed one (so that more sharing of references can occur) or to look up
/// a value that has more complete data than the value you currently have, although their
/// comparer functions indicate they are equal.
/// </remarks>
public bool TryGetValue(T equalValue, out T actualValue)
{
Node searchResult = _root.Search(equalValue, _comparer);
if (searchResult.IsEmpty)
{
actualValue = equalValue;
return false;
}
else
{
actualValue = searchResult.Key;
return true;
}
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Intersect(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
ImmutableSortedSet<T> newSet = this.Clear();
foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
{
if (this.Contains(item))
{
newSet = newSet.Add(item);
}
}
Debug.Assert(newSet != null);
return newSet;
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Except(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
ImmutableSortedSet<T>.Node result = _root;
foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
{
bool mutated;
result = result.Remove(item, _comparer, out mutated);
}
return this.Wrap(result);
}
/// <summary>
/// Produces a set that contains elements either in this set or a given sequence, but not both.
/// </summary>
/// <param name="other">The other sequence of items.</param>
/// <returns>The new set.</returns>
public ImmutableSortedSet<T> SymmetricExcept(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
ImmutableSortedSet<T> otherAsSet = ImmutableSortedSet.CreateRange(_comparer, other);
ImmutableSortedSet<T> result = this.Clear();
foreach (T item in this)
{
if (!otherAsSet.Contains(item))
{
result = result.Add(item);
}
}
foreach (T item in otherAsSet)
{
if (!this.Contains(item))
{
result = result.Add(item);
}
}
return result;
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> Union(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
ImmutableSortedSet<T>? immutableSortedSet;
if (TryCastToImmutableSortedSet(other, out immutableSortedSet) && immutableSortedSet.KeyComparer == this.KeyComparer) // argument is a compatible immutable sorted set
{
if (immutableSortedSet.IsEmpty)
{
return this;
}
else if (this.IsEmpty)
{
// Adding the argument to this collection is equivalent to returning the argument.
return immutableSortedSet;
}
else if (immutableSortedSet.Count > this.Count)
{
// We're adding a larger set to a smaller set, so it would be faster to simply
// add the smaller set to the larger set.
return immutableSortedSet.Union(this);
}
}
int count;
if (this.IsEmpty || (other.TryGetCount(out count) && (this.Count + count) * RefillOverIncrementalThreshold > this.Count))
{
// The payload being added is so large compared to this collection's current size
// that we likely won't see much memory reuse in the node tree by performing an
// incremental update. So just recreate the entire node tree since that will
// likely be faster.
return this.LeafToRootRefill(other);
}
return this.UnionIncremental(other);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
internal ImmutableSortedSet<T> Union(ReadOnlySpan<T> other)
{
if (this.IsEmpty || (this.Count + other.Length) * RefillOverIncrementalThreshold > this.Count)
{
// The payload being added is so large compared to this collection's current size
// that we likely won't see much memory reuse in the node tree by performing an
// incremental update. So just recreate the entire node tree since that will
// likely be faster.
return this.LeafToRootRefill(other);
}
return this.UnionIncremental(other);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public ImmutableSortedSet<T> WithComparer(IComparer<T>? comparer)
{
comparer ??= Comparer<T>.Default;
if (comparer == _comparer)
{
return this;
}
else
{
var result = new ImmutableSortedSet<T>(Node.EmptyNode, comparer);
result = result.Union(this);
Debug.Assert(result != null);
return result;
}
}
/// <summary>
/// Checks whether a given sequence of items entirely describe the contents of this set.
/// </summary>
/// <param name="other">The sequence of items to check against this set.</param>
/// <returns>A value indicating whether the sets are equal.</returns>
public bool SetEquals(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (object.ReferenceEquals(this, other))
{
return true;
}
var otherSet = new SortedSet<T>(other, this.KeyComparer);
if (this.Count != otherSet.Count)
{
return false;
}
int matches = 0;
foreach (T item in otherSet)
{
if (!this.Contains(item))
{
return false;
}
matches++;
}
return matches == this.Count;
}
/// <summary>
/// Determines whether the current set is a property (strict) subset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>true if the current set is a correct subset of other; otherwise, false.</returns>
public bool IsProperSubsetOf(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (this.IsEmpty)
{
return other.Any();
}
// To determine whether everything we have is also in another sequence,
// we enumerate the sequence and "tag" whether it's in this collection,
// then consider whether every element in this collection was tagged.
// Since this collection is immutable we cannot directly tag. So instead
// we simply count how many "hits" we have and ensure it's equal to the
// size of this collection. Of course for this to work we need to ensure
// the uniqueness of items in the given sequence, so we create a set based
// on the sequence first.
var otherSet = new SortedSet<T>(other, this.KeyComparer);
if (this.Count >= otherSet.Count)
{
return false;
}
int matches = 0;
bool extraFound = false;
foreach (T item in otherSet)
{
if (this.Contains(item))
{
matches++;
}
else
{
extraFound = true;
}
if (matches == this.Count && extraFound)
{
return true;
}
}
return false;
}
/// <summary>
/// Determines whether the current set is a correct superset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>true if the current set is a correct superset of other; otherwise, false.</returns>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (this.IsEmpty)
{
return false;
}
int count = 0;
foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
{
count++;
if (!this.Contains(item))
{
return false;
}
}
return this.Count > count;
}
/// <summary>
/// Determines whether a set is a subset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>true if the current set is a subset of other; otherwise, false.</returns>
public bool IsSubsetOf(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (this.IsEmpty)
{
return true;
}
// To determine whether everything we have is also in another sequence,
// we enumerate the sequence and "tag" whether it's in this collection,
// then consider whether every element in this collection was tagged.
// Since this collection is immutable we cannot directly tag. So instead
// we simply count how many "hits" we have and ensure it's equal to the
// size of this collection. Of course for this to work we need to ensure
// the uniqueness of items in the given sequence, so we create a set based
// on the sequence first.
var otherSet = new SortedSet<T>(other, this.KeyComparer);
int matches = 0;
foreach (T item in otherSet)
{
if (this.Contains(item))
{
matches++;
}
}
return matches == this.Count;
}
/// <summary>
/// Determines whether the current set is a superset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>true if the current set is a superset of other; otherwise, false.</returns>
public bool IsSupersetOf(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
{
if (!this.Contains(item))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether the current set overlaps with the specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>true if the current set and other share at least one common element; otherwise, false.</returns>
public bool Overlaps(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (this.IsEmpty)
{
return false;
}
foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
{
if (this.Contains(item))
{
return true;
}
}
return false;
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/> that iterates over this
/// collection in reverse order.
/// </summary>
/// <returns>
/// An enumerator that iterates over the <see cref="ImmutableSortedSet{T}"/>
/// in reverse order.
/// </returns>
public IEnumerable<T> Reverse()
{
return new ReverseEnumerable(_root);
}
/// <summary>
/// Gets the position within this set that the specified value does or would appear.
/// </summary>
/// <param name="item">The value whose position is being sought.</param>
/// <returns>
/// The index of the specified <paramref name="item"/> in the sorted set,
/// if <paramref name="item"/> is found. If <paramref name="item"/> is not
/// found and <paramref name="item"/> is less than one or more elements in this set,
/// a negative number which is the bitwise complement of the index of the first
/// element that is larger than value. If <paramref name="item"/> is not found
/// and <paramref name="item"/> is greater than any of the elements in the set,
/// a negative number which is the bitwise complement of (the index of the last
/// element plus 1).
/// </returns>
public int IndexOf(T item)
{
return _root.IndexOf(item, _comparer);
}
#endregion
#region IImmutableSet<T> Members
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
public bool Contains(T value)
{
return _root.Contains(value, _comparer);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Clear()
{
return this.Clear();
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Add(T value)
{
return this.Add(value);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Remove(T value)
{
return this.Remove(value);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Intersect(IEnumerable<T> other)
{
return this.Intersect(other);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Except(IEnumerable<T> other)
{
return this.Except(other);
}
/// <summary>
/// Produces a set that contains elements either in this set or a given sequence, but not both.
/// </summary>
/// <param name="other">The other sequence of items.</param>
/// <returns>The new set.</returns>
IImmutableSet<T> IImmutableSet<T>.SymmetricExcept(IEnumerable<T> other)
{
return this.SymmetricExcept(other);
}
/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
IImmutableSet<T> IImmutableSet<T>.Union(IEnumerable<T> other)
{
return this.Union(other);
}
#endregion
#region ISet<T> Members
/// <summary>
/// See <see cref="ISet{T}"/>
/// </summary>
bool ISet<T>.Add(T item)
{
throw new NotSupportedException();
}
/// <summary>
/// See <see cref="ISet{T}"/>
/// </summary>
void ISet<T>.ExceptWith(IEnumerable<T> other)
{
throw new NotSupportedException();
}
/// <summary>
/// See <see cref="ISet{T}"/>
/// </summary>
void ISet<T>.IntersectWith(IEnumerable<T> other)
{
throw new NotSupportedException();
}
/// <summary>
/// See <see cref="ISet{T}"/>
/// </summary>
void ISet<T>.SymmetricExceptWith(IEnumerable<T> other)
{
throw new NotSupportedException();
}
/// <summary>
/// See <see cref="ISet{T}"/>
/// </summary>
void ISet<T>.UnionWith(IEnumerable<T> other)
{
throw new NotSupportedException();
}
#endregion
#region ICollection<T> members
/// <summary>
/// See the <see cref="ICollection{T}"/> interface.
/// </summary>
bool ICollection<T>.IsReadOnly
{
get { return true; }
}
/// <summary>
/// See the <see cref="ICollection{T}"/> interface.
/// </summary>
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
_root.CopyTo(array, arrayIndex);
}
/// <summary>
/// See the <see cref="IList{T}"/> interface.
/// </summary>
void ICollection<T>.Add(T item)
{
throw new NotSupportedException();
}
/// <summary>
/// See the <see cref="ICollection{T}"/> interface.
/// </summary>
void ICollection<T>.Clear()
{
throw new NotSupportedException();
}
/// <summary>
/// See the <see cref="IList{T}"/> interface.
/// </summary>
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region IList<T> methods
/// <summary>
/// See the <see cref="IList{T}"/> interface.
/// </summary>
T IList<T>.this[int index]
{
get { return this[index]; }
set { throw new NotSupportedException(); }
}
/// <summary>
/// See the <see cref="IList{T}"/> interface.
/// </summary>
void IList<T>.Insert(int index, T item)
{
throw new NotSupportedException();
}
/// <summary>
/// See the <see cref="IList{T}"/> interface.
/// </summary>
void IList<T>.RemoveAt(int index)
{
throw new NotSupportedException();
}
#endregion
#region IList properties
/// <summary>
/// Gets a value indicating whether the <see cref="IList"/> has a fixed size.
/// </summary>
/// <returns>true if the <see cref="IList"/> has a fixed size; otherwise, false.</returns>
bool IList.IsFixedSize
{
get { return true; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="ICollection{T}"/> is read-only.
/// </summary>
/// <returns>true if the <see cref="ICollection{T}"/> is read-only; otherwise, false.
/// </returns>
bool IList.IsReadOnly
{
get { return true; }
}
#endregion
#region ICollection Properties
/// <summary>
/// See <see cref="ICollection"/>.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
object ICollection.SyncRoot
{
get { return this; }
}
/// <summary>
/// See the <see cref="ICollection"/> interface.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
bool ICollection.IsSynchronized
{
get
{
// This is immutable, so it is always thread-safe.
return true;
}
}
#endregion
#region IList methods
/// <summary>
/// Adds an item to the <see cref="IList"/>.
/// </summary>
/// <param name="value">The object to add to the <see cref="IList"/>.</param>
/// <returns>
/// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection,
/// </returns>
/// <exception cref="System.NotSupportedException"></exception>
int IList.Add(object? value)
{
throw new NotSupportedException();
}
/// <summary>
/// Clears this instance.
/// </summary>
/// <exception cref="System.NotSupportedException"></exception>
void IList.Clear()
{
throw new NotSupportedException();
}
private static bool IsCompatibleObject(object? value)
{
// Non-null values are fine. Only accept nulls if T is a class or Nullable<U>.
// Note that default(T) is not equal to null for value types except when T is Nullable<U>.
return (value is T) || (default(T) == null && value == null);
}
/// <summary>
/// Determines whether the <see cref="IList"/> contains a specific value.
/// </summary>
/// <param name="value">The object to locate in the <see cref="IList"/>.</param>
/// <returns>
/// true if the <see cref="object"/> is found in the <see cref="IList"/>; otherwise, false.
/// </returns>
bool IList.Contains(object? value)
{
if (IsCompatibleObject(value))
{
return this.Contains((T)value!);
}
return false;
}
/// <summary>
/// Determines the index of a specific item in the <see cref="IList"/>.
/// </summary>
/// <param name="value">The object to locate in the <see cref="IList"/>.</param>
/// <returns>
/// The index of <paramref name="value"/> if found in the list; otherwise, -1.
/// </returns>
int IList.IndexOf(object? value)
{
if (IsCompatibleObject(value))
{
return this.IndexOf((T)value!);
}
return -1;
}
/// <summary>
/// Inserts an item to the <see cref="IList"/> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which <paramref name="value"/> should be inserted.</param>
/// <param name="value">The object to insert into the <see cref="IList"/>.</param>
/// <exception cref="System.NotSupportedException"></exception>
void IList.Insert(int index, object? value)
{
throw new NotSupportedException();
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="IList"/>.
/// </summary>
/// <param name="value">The object to remove from the <see cref="IList"/>.</param>
/// <exception cref="System.NotSupportedException"></exception>
void IList.Remove(object? value)
{
throw new NotSupportedException();
}
/// <summary>
/// Removes at.
/// </summary>
/// <param name="index">The index.</param>
/// <exception cref="System.NotSupportedException"></exception>
void IList.RemoveAt(int index)
{
throw new NotSupportedException();
}
/// <summary>
/// Gets or sets the <see cref="object"/> at the specified index.
/// </summary>
/// <value>
/// The <see cref="object"/>.
/// </value>
/// <param name="index">The index.</param>
/// <exception cref="System.NotSupportedException"></exception>
object? IList.this[int index]
{
get { return this[index]; }
set { throw new NotSupportedException(); }
}
#endregion
#region ICollection Methods
/// <summary>
/// Copies the elements of the <see cref="ICollection"/> to an <see cref="Array"/>, starting at a particular <see cref="Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="ICollection"/>. The <see cref="Array"/> must have zero-based indexing.</param>
/// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
void ICollection.CopyTo(Array array, int index)
{
_root.CopyTo(array, index);
}
#endregion
#region IEnumerable<T> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
/// </returns>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this.IsEmpty ?
Enumerable.Empty<T>().GetEnumerator() :
this.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
/// </returns>
/// <remarks>
/// CAUTION: when this enumerator is actually used as a valuetype (not boxed) do NOT copy it by assigning to a second variable