Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ImmutableSortedSet.cs
Go to the documentation of this file.
5using System.Linq;
7
9
10public static class ImmutableSortedSet
11{
13 {
14 return ImmutableSortedSet<T>.Empty;
15 }
16
18 {
19 return ImmutableSortedSet<T>.Empty.WithComparer(comparer);
20 }
21
23 {
24 return ImmutableSortedSet<T>.Empty.Add(item);
25 }
26
28 {
29 return ImmutableSortedSet<T>.Empty.WithComparer(comparer).Add(item);
30 }
31
33 {
34 return ImmutableSortedSet<T>.Empty.Union(items);
35 }
36
38 {
39 return ImmutableSortedSet<T>.Empty.WithComparer(comparer).Union(items);
40 }
41
42 public static ImmutableSortedSet<T> Create<T>(params T[] items)
43 {
44 return ImmutableSortedSet<T>.Empty.Union(items);
45 }
46
48 {
49 return ImmutableSortedSet<T>.Empty.WithComparer(comparer).Union(items);
50 }
51
53 {
54 return Create<T>().ToBuilder();
55 }
56
58 {
59 return Create(comparer).ToBuilder();
60 }
61
70
72 {
73 return source.ToImmutableSortedSet(null);
74 }
75
77 {
78 Requires.NotNull(builder, "builder");
79 return builder.ToImmutable();
80 }
81}
82[DebuggerDisplay("Count = {Count}")]
84public sealed class ImmutableSortedSet<T> : IImmutableSet<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable, ISortKeyCollection<T>, IReadOnlySet<T>, IReadOnlyList<T>, IList<T>, ICollection<T>, ISet<T>, IList, ICollection, IStrongEnumerable<T, ImmutableSortedSet<T>.Enumerator>
85{
86 [DebuggerDisplay("Count = {Count}")]
89 {
91
93
95
96 private int _version;
97
98 private object _syncRoot;
99
100 public int Count => Root.Count;
101
103
104 public T this[int index] => _root.ItemRef(index);
105
106 public T? Max => _root.Max;
107
108 public T? Min => _root.Min;
109
111 {
112 get
113 {
114 return _comparer;
115 }
116 set
117 {
118 Requires.NotNull(value, "value");
119 if (value == _comparer)
120 {
121 return;
122 }
125 {
126 while (enumerator.MoveNext())
127 {
128 T current = enumerator.Current;
129 node = node.Add(current, value, out var _);
130 }
131 }
132 _immutable = null;
134 Root = node;
135 }
136 }
137
138 internal int Version => _version;
139
140 private Node Root
141 {
142 get
143 {
144 return _root;
145 }
146 set
147 {
148 _version++;
149 if (_root != value)
150 {
151 _root = value;
152 _immutable = null;
153 }
154 }
155 }
156
158 bool ICollection.IsSynchronized => false;
159
161 object ICollection.SyncRoot
162 {
163 get
164 {
165 if (_syncRoot == null)
166 {
167 Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
168 }
169 return _syncRoot;
170 }
171 }
172
174 {
175 Requires.NotNull(set, "set");
176 _root = set._root;
177 _comparer = set.KeyComparer;
178 _immutable = set;
179 }
180
181 public ref readonly T ItemRef(int index)
182 {
183 return ref _root.ItemRef(index);
184 }
185
186 public bool Add(T item)
187 {
189 return mutated;
190 }
191
193 {
194 Requires.NotNull(other, "other");
195 foreach (T item in other)
196 {
198 }
199 }
200
202 {
203 Requires.NotNull(other, "other");
205 foreach (T item in other)
206 {
207 if (Contains(item))
208 {
210 }
211 }
212 Root = node;
213 }
214
216 {
217 return ToImmutable().IsProperSubsetOf(other);
218 }
219
221 {
222 return ToImmutable().IsProperSupersetOf(other);
223 }
224
226 {
227 return ToImmutable().IsSubsetOf(other);
228 }
229
231 {
232 return ToImmutable().IsSupersetOf(other);
233 }
234
236 {
237 return ToImmutable().Overlaps(other);
238 }
239
241 {
242 return ToImmutable().SetEquals(other);
243 }
244
246 {
247 Root = ToImmutable().SymmetricExcept(other)._root;
248 }
249
251 {
252 Requires.NotNull(other, "other");
253 foreach (T item in other)
254 {
256 }
257 }
258
260 {
261 Add(item);
262 }
263
264 public void Clear()
265 {
267 }
268
269 public bool Contains(T item)
270 {
271 return Root.Contains(item, _comparer);
272 }
273
278
279 public bool Remove(T item)
280 {
282 return mutated;
283 }
284
286 {
287 return Root.GetEnumerator(this);
288 }
289
294
299
301 {
302 return new ReverseEnumerable(_root);
303 }
304
306 {
307 if (_immutable == null)
308 {
310 }
311 return _immutable;
312 }
313
315 {
317 if (!node.IsEmpty)
318 {
319 actualValue = node.Key;
320 return true;
321 }
323 return false;
324 }
325
330 }
331
332 private sealed class ReverseEnumerable : IEnumerable<T>, IEnumerable
333 {
334 private readonly Node _root;
335
336 internal ReverseEnumerable(Node root)
337 {
338 Requires.NotNull(root, "root");
339 _root = root;
340 }
341
343 {
344 return _root.Reverse();
345 }
346
351 }
352
355 {
357
358 private readonly Builder _builder;
359
360 private readonly int _poolUserId;
361
362 private readonly bool _reverse;
363
364 private Node _root;
365
367
368 private Node _current;
369
371
372 int ISecurePooledObjectUser.PoolUserId => _poolUserId;
373
374 public T Current
375 {
376 get
377 {
379 if (_current != null)
380 {
381 return _current.Value;
382 }
383 throw new InvalidOperationException();
384 }
385 }
386
387 object? IEnumerator.Current => Current;
388
389 internal Enumerator(Node root, Builder? builder = null, bool reverse = false)
390 {
391 Requires.NotNull(root, "root");
392 _root = root;
394 _current = null;
396 _enumeratingBuilderVersion = builder?.Version ?? (-1);
398 _stack = null;
399 if (!s_enumeratingStacks.TryTake(this, out _stack))
400 {
401 _stack = s_enumeratingStacks.PrepNew(this, new Stack<RefAsValueType<Node>>(root.Height));
402 }
404 }
405
406 public void Dispose()
407 {
408 _root = null;
409 _current = null;
410 if (_stack != null && _stack.TryUse(ref this, out var value))
411 {
412 value.ClearFastWhenEmpty();
413 s_enumeratingStacks.TryAdd(this, _stack);
414 _stack = null;
415 }
416 }
417
418 public bool MoveNext()
419 {
422 Stack<RefAsValueType<Node>> stack = _stack.Use(ref this);
423 if (stack.Count > 0)
424 {
425 Node node = (_current = stack.Pop().Value);
426 PushNext(_reverse ? node.Left : node.Right);
427 return true;
428 }
429 _current = null;
430 return false;
431 }
432
433 public void Reset()
434 {
437 _current = null;
438 Stack<RefAsValueType<Node>> stack = _stack.Use(ref this);
439 stack.ClearFastWhenEmpty();
441 }
442
443 private void ThrowIfDisposed()
444 {
445 if (_root == null || (_stack != null && !_stack.IsOwned(ref this)))
446 {
447 Requires.FailObjectDisposed(this);
448 }
449 }
450
458
459 private void PushNext(Node node)
460 {
461 Requires.NotNull(node, "node");
462 Stack<RefAsValueType<Node>> stack = _stack.Use(ref this);
463 while (!node.IsEmpty)
464 {
465 stack.Push(new RefAsValueType<Node>(node));
466 node = (_reverse ? node.Right : node.Left);
467 }
468 }
469 }
470
471 [DebuggerDisplay("{_key}")]
472 internal sealed class Node : IBinaryTree<T>, IBinaryTree, IEnumerable<T>, IEnumerable
473 {
474 internal static readonly Node EmptyNode = new Node();
475
476 private readonly T _key;
477
478 private bool _frozen;
479
480 private byte _height;
481
482 private int _count;
483
484 private Node _left;
485
486 private Node _right;
487
488 public bool IsEmpty => _left == null;
489
490 public int Height => _height;
491
492 public Node? Left => _left;
493
494 IBinaryTree? IBinaryTree.Left => _left;
495
496 public Node? Right => _right;
497
498 IBinaryTree? IBinaryTree.Right => _right;
499
501
503
504 public T Value => _key;
505
506 public int Count => _count;
507
508 internal T Key => _key;
509
510 internal T? Max
511 {
512 get
513 {
514 if (IsEmpty)
515 {
516 return default(T);
517 }
518 Node node = this;
519 while (!node._right.IsEmpty)
520 {
521 node = node._right;
522 }
523 return node._key;
524 }
525 }
526
527 internal T? Min
528 {
529 get
530 {
531 if (IsEmpty)
532 {
533 return default(T);
534 }
535 Node node = this;
536 while (!node._left.IsEmpty)
537 {
538 node = node._left;
539 }
540 return node._key;
541 }
542 }
543
544 internal T this[int index]
545 {
546 get
547 {
548 Requires.Range(index >= 0 && index < Count, "index");
549 if (index < _left._count)
550 {
551 return _left[index];
552 }
553 if (index > _left._count)
554 {
555 return _right[index - _left._count - 1];
556 }
557 return _key;
558 }
559 }
560
561 private Node()
562 {
563 _frozen = true;
564 }
565
566 private Node(T key, Node left, Node right, bool frozen = false)
567 {
568 Requires.NotNull(left, "left");
569 Requires.NotNull(right, "right");
570 _key = key;
571 _left = left;
572 _right = right;
573 _height = checked((byte)(1 + Math.Max(left._height, right._height)));
574 _count = 1 + left._count + right._count;
575 _frozen = frozen;
576 }
577
578 internal ref readonly T ItemRef(int index)
579 {
580 Requires.Range(index >= 0 && index < Count, "index");
581 return ref ItemRefUnchecked(index);
582 }
583
584 private ref readonly T ItemRefUnchecked(int index)
585 {
586 if (index < _left._count)
587 {
589 }
590 if (index > _left._count)
591 {
593 }
594 return ref _key;
595 }
596
598 {
599 return new Enumerator(this);
600 }
601
607
613
615 {
616 return new Enumerator(this, builder);
617 }
618
619 internal void CopyTo(T[] array, int arrayIndex)
620 {
621 Requires.NotNull(array, "array");
622 Requires.Range(arrayIndex >= 0, "arrayIndex");
623 Requires.Range(array.Length >= arrayIndex + Count, "arrayIndex");
625 while (enumerator.MoveNext())
626 {
627 T current = enumerator.Current;
628 array[arrayIndex++] = current;
629 }
630 }
631
632 internal void CopyTo(Array array, int arrayIndex)
633 {
634 Requires.NotNull(array, "array");
635 Requires.Range(arrayIndex >= 0, "arrayIndex");
636 Requires.Range(array.Length >= arrayIndex + Count, "arrayIndex");
638 while (enumerator.MoveNext())
639 {
640 T current = enumerator.Current;
641 array.SetValue(current, arrayIndex++);
642 }
643 }
644
646 {
647 Requires.NotNull(comparer, "comparer");
648 if (IsEmpty)
649 {
650 mutated = true;
651 return new Node(key, this, this);
652 }
653 Node node = this;
654 int num = comparer.Compare(key, _key);
655 if (num > 0)
656 {
657 Node right = _right.Add(key, comparer, out mutated);
658 if (mutated)
659 {
660 node = Mutate(null, right);
661 }
662 }
663 else
664 {
665 if (num >= 0)
666 {
667 mutated = false;
668 return this;
669 }
670 Node left = _left.Add(key, comparer, out mutated);
671 if (mutated)
672 {
673 node = Mutate(left);
674 }
675 }
676 if (!mutated)
677 {
678 return node;
679 }
680 return MakeBalanced(node);
681 }
682
684 {
685 Requires.NotNull(comparer, "comparer");
686 if (IsEmpty)
687 {
688 mutated = false;
689 return this;
690 }
691 Node node = this;
692 int num = comparer.Compare(key, _key);
693 if (num == 0)
694 {
695 mutated = true;
697 {
698 node = EmptyNode;
699 }
700 else if (_right.IsEmpty && !_left.IsEmpty)
701 {
702 node = _left;
703 }
704 else if (!_right.IsEmpty && _left.IsEmpty)
705 {
706 node = _right;
707 }
708 else
709 {
710 Node node2 = _right;
711 while (!node2._left.IsEmpty)
712 {
713 node2 = node2._left;
714 }
715 bool mutated2;
716 Node right = _right.Remove(node2._key, comparer, out mutated2);
717 node = node2.Mutate(_left, right);
718 }
719 }
720 else if (num < 0)
721 {
723 if (mutated)
724 {
725 node = Mutate(left);
726 }
727 }
728 else
729 {
731 if (mutated)
732 {
733 node = Mutate(null, right2);
734 }
735 }
736 if (!node.IsEmpty)
737 {
738 return MakeBalanced(node);
739 }
740 return node;
741 }
742
744 {
745 Requires.NotNull(comparer, "comparer");
746 return !Search(key, comparer).IsEmpty;
747 }
748
749 internal void Freeze()
750 {
751 if (!_frozen)
752 {
753 _left.Freeze();
754 _right.Freeze();
755 _frozen = true;
756 }
757 }
758
760 {
761 Requires.NotNull(comparer, "comparer");
762 if (IsEmpty)
763 {
764 return this;
765 }
766 int num = comparer.Compare(key, _key);
767 if (num == 0)
768 {
769 return this;
770 }
771 if (num > 0)
772 {
773 return _right.Search(key, comparer);
774 }
775 return _left.Search(key, comparer);
776 }
777
779 {
780 Requires.NotNull(comparer, "comparer");
781 if (IsEmpty)
782 {
783 return -1;
784 }
785 int num = comparer.Compare(key, _key);
786 if (num == 0)
787 {
788 return _left.Count;
789 }
790 if (num > 0)
791 {
793 bool flag = num2 < 0;
794 if (flag)
795 {
796 num2 = ~num2;
797 }
798 num2 = _left.Count + 1 + num2;
799 if (flag)
800 {
801 num2 = ~num2;
802 }
803 return num2;
804 }
805 return _left.IndexOf(key, comparer);
806 }
807
809 {
810 return new Enumerator(this, null, reverse: true);
811 }
812
813 private static Node RotateLeft(Node tree)
814 {
815 Requires.NotNull(tree, "tree");
816 if (tree._right.IsEmpty)
817 {
818 return tree;
819 }
820 Node right = tree._right;
821 return right.Mutate(tree.Mutate(null, right._left));
822 }
823
824 private static Node RotateRight(Node tree)
825 {
826 Requires.NotNull(tree, "tree");
827 if (tree._left.IsEmpty)
828 {
829 return tree;
830 }
831 Node left = tree._left;
832 return left.Mutate(null, tree.Mutate(left._right));
833 }
834
835 private static Node DoubleLeft(Node tree)
836 {
837 Requires.NotNull(tree, "tree");
838 if (tree._right.IsEmpty)
839 {
840 return tree;
841 }
842 Node tree2 = tree.Mutate(null, RotateRight(tree._right));
843 return RotateLeft(tree2);
844 }
845
846 private static Node DoubleRight(Node tree)
847 {
848 Requires.NotNull(tree, "tree");
849 if (tree._left.IsEmpty)
850 {
851 return tree;
852 }
853 Node tree2 = tree.Mutate(RotateLeft(tree._left));
854 return RotateRight(tree2);
855 }
856
857 private static int Balance(Node tree)
858 {
859 Requires.NotNull(tree, "tree");
860 return tree._right._height - tree._left._height;
861 }
862
863 private static bool IsRightHeavy(Node tree)
864 {
865 Requires.NotNull(tree, "tree");
866 return Balance(tree) >= 2;
867 }
868
869 private static bool IsLeftHeavy(Node tree)
870 {
871 Requires.NotNull(tree, "tree");
872 return Balance(tree) <= -2;
873 }
874
875 private static Node MakeBalanced(Node tree)
876 {
877 Requires.NotNull(tree, "tree");
878 if (IsRightHeavy(tree))
879 {
880 if (Balance(tree._right) >= 0)
881 {
882 return RotateLeft(tree);
883 }
884 return DoubleLeft(tree);
885 }
886 if (IsLeftHeavy(tree))
887 {
888 if (Balance(tree._left) <= 0)
889 {
890 return RotateRight(tree);
891 }
892 return DoubleRight(tree);
893 }
894 return tree;
895 }
896
897 internal static Node NodeTreeFromList(IOrderedCollection<T> items, int start, int length)
898 {
899 Requires.NotNull(items, "items");
900 if (length == 0)
901 {
902 return EmptyNode;
903 }
904 int num = (length - 1) / 2;
905 int num2 = length - 1 - num;
906 Node left = NodeTreeFromList(items, start, num2);
907 Node right = NodeTreeFromList(items, start + num2 + 1, num);
908 return new Node(items[start + num2], left, right, frozen: true);
909 }
910
911 private Node Mutate(Node left = null, Node right = null)
912 {
913 if (_frozen)
914 {
915 return new Node(_key, left ?? _left, right ?? _right);
916 }
917 if (left != null)
918 {
919 _left = left;
920 }
921 if (right != null)
922 {
923 _right = right;
924 }
926 _count = 1 + _left._count + _right._count;
927 return this;
928 }
929 }
930
931 public static readonly ImmutableSortedSet<T> Empty = new ImmutableSortedSet<T>();
932
933 private readonly Node _root;
934
935 private readonly IComparer<T> _comparer;
936
937 public T? Max => _root.Max;
938
939 public T? Min => _root.Min;
940
941 public bool IsEmpty => _root.IsEmpty;
942
943 public int Count => _root.Count;
944
946
947 internal IBinaryTree Root => _root;
948
949 public T this[int index] => _root.ItemRef(index);
950
952
953 T IList<T>.this[int index]
954 {
955 get
956 {
957 return this[index];
958 }
959 set
960 {
961 throw new NotSupportedException();
962 }
963 }
964
965 bool IList.IsFixedSize => true;
966
967 bool IList.IsReadOnly => true;
968
970 object ICollection.SyncRoot => this;
971
973 bool ICollection.IsSynchronized => true;
974
975 object? IList.this[int index]
976 {
977 get
978 {
979 return this[index];
980 }
981 set
982 {
983 throw new NotSupportedException();
984 }
985 }
986
988 {
990 _comparer = comparer ?? Comparer<T>.Default;
991 }
992
994 {
995 Requires.NotNull(root, "root");
996 Requires.NotNull(comparer, "comparer");
997 root.Freeze();
998 _root = root;
1000 }
1001
1003 {
1004 if (!_root.IsEmpty)
1005 {
1006 return Empty.WithComparer(_comparer);
1007 }
1008 return this;
1009 }
1010
1011 public ref readonly T ItemRef(int index)
1012 {
1013 return ref _root.ItemRef(index);
1014 }
1015
1017 {
1018 return new Builder(this);
1019 }
1020
1022 {
1023 bool mutated;
1024 return Wrap(_root.Add(value, _comparer, out mutated));
1025 }
1026
1028 {
1029 bool mutated;
1031 }
1032
1034 {
1036 if (node.IsEmpty)
1037 {
1039 return false;
1040 }
1041 actualValue = node.Key;
1042 return true;
1043 }
1044
1046 {
1047 Requires.NotNull(other, "other");
1049 foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
1050 {
1051 if (Contains(item))
1052 {
1054 }
1055 }
1056 return immutableSortedSet;
1057 }
1058
1060 {
1061 Requires.NotNull(other, "other");
1062 Node node = _root;
1063 foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
1064 {
1066 }
1067 return Wrap(node);
1068 }
1069
1071 {
1072 Requires.NotNull(other, "other");
1076 {
1077 while (enumerator.MoveNext())
1078 {
1079 T current = enumerator.Current;
1080 if (!immutableSortedSet.Contains(current))
1081 {
1083 }
1084 }
1085 }
1086 foreach (T item in immutableSortedSet)
1087 {
1088 if (!Contains(item))
1089 {
1091 }
1092 }
1093 return immutableSortedSet2;
1094 }
1095
1097 {
1098 Requires.NotNull(other, "other");
1100 {
1101 if (other2.IsEmpty)
1102 {
1103 return this;
1104 }
1105 if (IsEmpty)
1106 {
1107 return other2;
1108 }
1109 if (other2.Count > Count)
1110 {
1111 return other2.Union(this);
1112 }
1113 }
1114 if (IsEmpty || (other.TryGetCount(out var count) && (float)(Count + count) * 0.15f > (float)Count))
1115 {
1116 return LeafToRootRefill(other);
1117 }
1118 return UnionIncremental(other);
1119 }
1120
1122 {
1123 if (comparer == null)
1124 {
1125 comparer = Comparer<T>.Default;
1126 }
1127 if (comparer == _comparer)
1128 {
1129 return this;
1130 }
1132 return immutableSortedSet.Union(this);
1133 }
1134
1136 {
1137 Requires.NotNull(other, "other");
1138 if (this == other)
1139 {
1140 return true;
1141 }
1143 if (Count != sortedSet.Count)
1144 {
1145 return false;
1146 }
1147 int num = 0;
1148 foreach (T item in sortedSet)
1149 {
1150 if (!Contains(item))
1151 {
1152 return false;
1153 }
1154 num++;
1155 }
1156 return num == Count;
1157 }
1158
1160 {
1161 Requires.NotNull(other, "other");
1162 if (IsEmpty)
1163 {
1164 return other.Any();
1165 }
1167 if (Count >= sortedSet.Count)
1168 {
1169 return false;
1170 }
1171 int num = 0;
1172 bool flag = false;
1173 foreach (T item in sortedSet)
1174 {
1175 if (Contains(item))
1176 {
1177 num++;
1178 }
1179 else
1180 {
1181 flag = true;
1182 }
1183 if (num == Count && flag)
1184 {
1185 return true;
1186 }
1187 }
1188 return false;
1189 }
1190
1192 {
1193 Requires.NotNull(other, "other");
1194 if (IsEmpty)
1195 {
1196 return false;
1197 }
1198 int num = 0;
1199 foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
1200 {
1201 num++;
1202 if (!Contains(item))
1203 {
1204 return false;
1205 }
1206 }
1207 return Count > num;
1208 }
1209
1211 {
1212 Requires.NotNull(other, "other");
1213 if (IsEmpty)
1214 {
1215 return true;
1216 }
1218 int num = 0;
1219 foreach (T item in sortedSet)
1220 {
1221 if (Contains(item))
1222 {
1223 num++;
1224 }
1225 }
1226 return num == Count;
1227 }
1228
1230 {
1231 Requires.NotNull(other, "other");
1232 foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
1233 {
1234 if (!Contains(item))
1235 {
1236 return false;
1237 }
1238 }
1239 return true;
1240 }
1241
1243 {
1244 Requires.NotNull(other, "other");
1245 if (IsEmpty)
1246 {
1247 return false;
1248 }
1249 foreach (T item in other.GetEnumerableDisposable<T, Enumerator>())
1250 {
1251 if (Contains(item))
1252 {
1253 return true;
1254 }
1255 }
1256 return false;
1257 }
1258
1260 {
1261 return new ReverseEnumerable(_root);
1262 }
1263
1264 public int IndexOf(T item)
1265 {
1266 return _root.IndexOf(item, _comparer);
1267 }
1268
1269 public bool Contains(T value)
1270 {
1271 return _root.Contains(value, _comparer);
1272 }
1273
1275 {
1276 return Clear();
1277 }
1278
1280 {
1281 return Add(value);
1282 }
1283
1288
1293
1298
1300 {
1301 return SymmetricExcept(other);
1302 }
1303
1308
1310 {
1311 throw new NotSupportedException();
1312 }
1313
1315 {
1316 throw new NotSupportedException();
1317 }
1318
1319 void ISet<T>.IntersectWith(IEnumerable<T> other)
1320 {
1321 throw new NotSupportedException();
1322 }
1323
1324 void ISet<T>.SymmetricExceptWith(IEnumerable<T> other)
1325 {
1326 throw new NotSupportedException();
1327 }
1328
1330 {
1331 throw new NotSupportedException();
1332 }
1333
1335 {
1337 }
1338
1340 {
1341 throw new NotSupportedException();
1342 }
1343
1345 {
1346 throw new NotSupportedException();
1347 }
1348
1350 {
1351 throw new NotSupportedException();
1352 }
1353
1354 void IList<T>.Insert(int index, T item)
1355 {
1356 throw new NotSupportedException();
1357 }
1358
1359 void IList<T>.RemoveAt(int index)
1360 {
1361 throw new NotSupportedException();
1362 }
1363
1364 int IList.Add(object value)
1365 {
1366 throw new NotSupportedException();
1367 }
1368
1370 {
1371 throw new NotSupportedException();
1372 }
1373
1374 bool IList.Contains(object value)
1375 {
1376 return Contains((T)value);
1377 }
1378
1379 int IList.IndexOf(object value)
1380 {
1381 return IndexOf((T)value);
1382 }
1383
1384 void IList.Insert(int index, object value)
1385 {
1386 throw new NotSupportedException();
1387 }
1388
1389 void IList.Remove(object value)
1390 {
1391 throw new NotSupportedException();
1392 }
1393
1395 {
1396 throw new NotSupportedException();
1397 }
1398
1400 {
1402 }
1403
1405 {
1406 if (!IsEmpty)
1407 {
1408 return GetEnumerator();
1409 }
1410 return Enumerable.Empty<T>().GetEnumerator();
1411 }
1412
1414 {
1415 return GetEnumerator();
1416 }
1417
1419 {
1420 return _root.GetEnumerator();
1421 }
1422
1424 {
1426 if (other != null)
1427 {
1428 return true;
1429 }
1431 {
1432 other = builder.ToImmutable();
1433 return true;
1434 }
1435 return false;
1436 }
1437
1439 {
1440 if (!root.IsEmpty)
1441 {
1442 return new ImmutableSortedSet<T>(root, comparer);
1443 }
1444 return Empty.WithComparer(comparer);
1445 }
1446
1448 {
1449 Requires.NotNull(items, "items");
1450 Node node = _root;
1451 foreach (T item in items.GetEnumerableDisposable<T, Enumerator>())
1452 {
1454 }
1455 return Wrap(node);
1456 }
1457
1459 {
1460 if (root != _root)
1461 {
1462 if (!root.IsEmpty)
1463 {
1464 return new ImmutableSortedSet<T>(root, _comparer);
1465 }
1466 return Clear();
1467 }
1468 return this;
1469 }
1470
1472 {
1473 Requires.NotNull(addedItems, "addedItems");
1474 List<T> list;
1475 if (IsEmpty)
1476 {
1477 if (addedItems.TryGetCount(out var count) && count == 0)
1478 {
1479 return this;
1480 }
1481 list = new List<T>(addedItems);
1482 if (list.Count == 0)
1483 {
1484 return this;
1485 }
1486 }
1487 else
1488 {
1489 list = new List<T>(this);
1490 list.AddRange(addedItems);
1491 }
1492 IComparer<T> keyComparer = KeyComparer;
1493 list.Sort(keyComparer);
1494 int num = 1;
1495 for (int i = 1; i < list.Count; i++)
1496 {
1497 if (keyComparer.Compare(list[i], list[i - 1]) != 0)
1498 {
1499 list[num++] = list[i];
1500 }
1501 }
1502 list.RemoveRange(num, list.Count - num);
1503 Node root = Node.NodeTreeFromList(list.AsOrderedCollection(), 0, list.Count);
1504 return Wrap(root);
1505 }
1506}
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
Node Add(T key, IComparer< T > comparer, out bool mutated)
Node(T key, Node left, Node right, bool frozen=false)
static Node NodeTreeFromList(IOrderedCollection< T > items, int start, int length)
Node Remove(T key, IComparer< T > comparer, out bool mutated)
static ImmutableSortedSet< TSource > ToImmutableSortedSet< TSource >(this IEnumerable< TSource > source, IComparer< TSource >? comparer)
IEnumerator< T > IEnumerable< T >. GetEnumerator()
bool TryGetValue(T equalValue, out T actualValue)
ImmutableSortedSet< T > WithComparer(IComparer< T >? comparer)
ImmutableSortedSet< T > Intersect(IEnumerable< T > other)
ImmutableSortedSet< T > LeafToRootRefill(IEnumerable< T > addedItems)
ImmutableSortedSet< T > UnionIncremental(IEnumerable< T > items)
static ImmutableSortedSet< T > Wrap(Node root, IComparer< T > comparer)
static bool TryCastToImmutableSortedSet(IEnumerable< T > sequence, [NotNullWhen(true)] out ImmutableSortedSet< T > other)
static ImmutableSortedSet< T > CreateRange< T >(IEnumerable< T > items)
ImmutableSortedSet< T > SymmetricExcept(IEnumerable< T > other)
ImmutableSortedSet< T > Except(IEnumerable< T > other)
ImmutableSortedSet(Node root, IComparer< T > comparer)
static ImmutableSortedSet< T >.Builder CreateBuilder< T >()
ImmutableSortedSet< T > Union(IEnumerable< T > other)
static void Range(bool condition, string? parameterName, string? message=null)
Definition Requires.cs:40
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string CollectionModifiedDuringEnumeration
Definition SR.cs:26
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
void CopyTo(Array array, int index)
void RemoveAt(int index)
void Insert(int index, object? value)
int Add(object? value)
bool Contains(object? value)
void Remove(object? value)
int IndexOf(object? value)
static readonly SecureObjectPool< Stack< RefAsValueType< Node > >, Enumerator > s_enumeratingStacks
Enumerator(Node root, Builder? builder=null, bool reverse=false)
SecurePooledObject< Stack< RefAsValueType< Node > > > _stack