Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SortedSet.cs
Go to the documentation of this file.
5
7
10[DebuggerDisplay("Count = {Count}")]
11[TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
13{
14 internal sealed class Node
15 {
16 public T Item { get; set; }
17
18 public Node Left { get; set; }
19
20 public Node Right { get; set; }
21
22 public NodeColor Color { get; set; }
23
24 public bool IsBlack => Color == NodeColor.Black;
25
26 public bool IsRed => Color == NodeColor.Red;
27
28 public bool Is2Node
29 {
30 get
31 {
33 {
34 return IsNullOrBlack(Right);
35 }
36 return false;
37 }
38 }
39
40 public bool Is4Node
41 {
42 get
43 {
44 if (IsNonNullRed(Left))
45 {
46 return IsNonNullRed(Right);
47 }
48 return false;
49 }
50 }
51
52 public Node(T item, NodeColor color)
53 {
54 Item = item;
55 Color = color;
56 }
57
58 public static bool IsNonNullRed(Node node)
59 {
60 return node?.IsRed ?? false;
61 }
62
63 public static bool IsNullOrBlack(Node node)
64 {
65 return node?.IsBlack ?? true;
66 }
67
68 public void ColorBlack()
69 {
70 Color = NodeColor.Black;
71 }
72
73 public void ColorRed()
74 {
75 Color = NodeColor.Red;
76 }
77
78 public Node DeepClone(int count)
79 {
80 Stack<Node> stack = new Stack<Node>(2 * SortedSet<T>.Log2(count) + 2);
83 Node node2 = this;
84 Node node3 = node;
85 while (node2 != null)
86 {
87 stack.Push(node2);
88 stack2.Push(node3);
89 node3.Left = node2.Left?.ShallowClone();
90 node2 = node2.Left;
91 node3 = node3.Left;
92 }
93 while (stack.Count != 0)
94 {
95 node2 = stack.Pop();
96 node3 = stack2.Pop();
97 Node node4 = node2.Right;
98 Node node6 = (node3.Right = node4?.ShallowClone());
99 while (node4 != null)
100 {
101 stack.Push(node4);
102 stack2.Push(node6);
103 node6.Left = node4.Left?.ShallowClone();
104 node4 = node4.Left;
105 node6 = node6.Left;
106 }
107 }
108 return node;
109 }
110
112 {
113 bool flag = Left == current;
114 if (!IsNonNullRed(sibling.Left))
115 {
116 if (!flag)
117 {
118 return TreeRotation.LeftRight;
119 }
120 return TreeRotation.Left;
121 }
122 if (!flag)
123 {
124 return TreeRotation.Right;
125 }
126 return TreeRotation.RightLeft;
127 }
128
130 {
131 if (node != Left)
132 {
133 return Left;
134 }
135 return Right;
136 }
137
139 {
140 return new Node(Item, Color);
141 }
142
143 public void Split4Node()
144 {
145 ColorRed();
148 }
149
150 public Node Rotate(TreeRotation rotation)
151 {
152 switch (rotation)
153 {
154 case TreeRotation.Right:
155 {
156 Node right = Left.Left;
157 right.ColorBlack();
158 return RotateRight();
159 }
160 case TreeRotation.Left:
161 {
162 Node right = Right.Right;
163 right.ColorBlack();
164 return RotateLeft();
165 }
166 case TreeRotation.RightLeft:
167 return RotateRightLeft();
168 case TreeRotation.LeftRight:
169 return RotateLeftRight();
170 default:
171 return null;
172 }
173 }
174
176 {
177 Node right = Right;
178 Right = right.Left;
179 right.Left = this;
180 return right;
181 }
182
184 {
185 Node left = Left;
186 Node right = left.Right;
187 Left = right.Right;
188 right.Right = this;
189 left.Right = right.Left;
190 right.Left = left;
191 return right;
192 }
193
195 {
196 Node left = Left;
197 Left = left.Right;
198 left.Right = this;
199 return left;
200 }
201
203 {
204 Node right = Right;
205 Node left = right.Left;
206 Right = left.Left;
207 left.Left = this;
208 right.Left = left.Right;
209 left.Right = right;
210 return left;
211 }
212
213 public void Merge2Nodes()
214 {
215 ColorBlack();
216 Left.ColorRed();
217 Right.ColorRed();
218 }
219
220 public void ReplaceChild(Node child, Node newChild)
221 {
222 if (Left == child)
223 {
224 Left = newChild;
225 }
226 else
227 {
228 Right = newChild;
229 }
230 }
231 }
232
234 {
235 private readonly SortedSet<T> _tree;
236
237 private readonly int _version;
238
239 private readonly Stack<Node> _stack;
240
241 private Node _current;
242
243 private readonly bool _reverse;
244
245 public T Current
246 {
247 get
248 {
249 if (_current != null)
250 {
251 return _current.Item;
252 }
253 return default(T);
254 }
255 }
256
258 {
259 get
260 {
261 if (_current == null)
262 {
264 }
265 return _current.Item;
266 }
267 }
268
269 internal bool NotStartedOrEnded => _current == null;
270
272 : this(set, reverse: false)
273 {
274 }
275
276 internal Enumerator(SortedSet<T> set, bool reverse)
277 {
278 _tree = set;
279 set.VersionCheck();
280 _version = set.version;
281 _stack = new Stack<Node>(2 * SortedSet<T>.Log2(set.TotalCount() + 1));
282 _current = null;
284 Initialize();
285 }
286
291
293 {
295 }
296
297 private void Initialize()
298 {
299 _current = null;
300 Node node = _tree.root;
301 Node node2 = null;
302 Node node3 = null;
303 while (node != null)
304 {
305 node2 = (_reverse ? node.Right : node.Left);
306 node3 = (_reverse ? node.Left : node.Right);
307 if (_tree.IsWithinRange(node.Item))
308 {
309 _stack.Push(node);
310 node = node2;
311 }
312 else
313 {
314 node = ((node2 != null && _tree.IsWithinRange(node2.Item)) ? node2 : node3);
315 }
316 }
317 }
318
319 public bool MoveNext()
320 {
321 _tree.VersionCheck();
322 if (_version != _tree.version)
323 {
325 }
326 if (_stack.Count == 0)
327 {
328 _current = null;
329 return false;
330 }
331 _current = _stack.Pop();
333 Node node2 = null;
334 Node node3 = null;
335 while (node != null)
336 {
337 node2 = (_reverse ? node.Right : node.Left);
338 node3 = (_reverse ? node.Left : node.Right);
339 if (_tree.IsWithinRange(node.Item))
340 {
341 _stack.Push(node);
342 node = node2;
343 }
344 else
345 {
346 node = ((node3 != null && _tree.IsWithinRange(node3.Item)) ? node3 : node2);
347 }
348 }
349 return true;
350 }
351
352 public void Dispose()
353 {
354 }
355
356 internal void Reset()
357 {
358 if (_version != _tree.version)
359 {
361 }
362 _stack.Clear();
363 Initialize();
364 }
365
367 {
368 Reset();
369 }
370 }
371
372 internal struct ElementCount
373 {
374 internal int UniqueCount;
375
376 internal int UnfoundCount;
377 }
378
380 {
381 private readonly SortedSet<T> _underlying;
382
383 private readonly T _min;
384
385 private readonly T _max;
386
387 private int _countVersion;
388
389 private readonly bool _lBoundActive;
390
391 private readonly bool _uBoundActive;
392
393 internal override T MinInternal
394 {
395 get
396 {
397 VersionCheck();
398 Node node = root;
399 T result = default(T);
400 while (node != null)
401 {
402 int num = (_lBoundActive ? base.Comparer.Compare(_min, node.Item) : (-1));
403 if (num > 0)
404 {
405 node = node.Right;
406 continue;
407 }
408 result = node.Item;
409 if (num == 0)
410 {
411 break;
412 }
413 node = node.Left;
414 }
415 return result;
416 }
417 }
418
419 internal override T MaxInternal
420 {
421 get
422 {
423 VersionCheck();
424 Node node = root;
425 T result = default(T);
426 while (node != null)
427 {
428 int num = ((!_uBoundActive) ? 1 : base.Comparer.Compare(_max, node.Item));
429 if (num < 0)
430 {
431 node = node.Left;
432 continue;
433 }
434 result = node.Item;
435 if (num == 0)
436 {
437 break;
438 }
439 node = node.Right;
440 }
441 return result;
442 }
443 }
444
458
459 internal override bool AddIfNotPresent(T item)
460 {
461 if (!IsWithinRange(item))
462 {
463 throw new ArgumentOutOfRangeException("item");
464 }
465 bool result = _underlying.AddIfNotPresent(item);
466 VersionCheck();
467 return result;
468 }
469
470 public override bool Contains(T item)
471 {
472 VersionCheck();
473 return base.Contains(item);
474 }
475
476 internal override bool DoRemove(T item)
477 {
478 if (!IsWithinRange(item))
479 {
480 return false;
481 }
482 bool result = _underlying.Remove(item);
483 VersionCheck();
484 return result;
485 }
486
487 public override void Clear()
488 {
489 if (base.Count != 0)
490 {
491 List<T> toRemove = new List<T>();
493 {
494 toRemove.Add(n.Item);
495 return true;
496 });
497 while (toRemove.Count != 0)
498 {
500 toRemove.RemoveAt(toRemove.Count - 1);
501 }
502 root = null;
503 count = 0;
504 version = _underlying.version;
505 }
506 }
507
508 internal override bool IsWithinRange(T item)
509 {
510 int num = (_lBoundActive ? base.Comparer.Compare(_min, item) : (-1));
511 if (num > 0)
512 {
513 return false;
514 }
515 num = ((!_uBoundActive) ? 1 : base.Comparer.Compare(_max, item));
516 return num >= 0;
517 }
518
520 {
521 VersionCheck();
522 if (root == null)
523 {
524 return true;
525 }
526 Stack<Node> stack = new Stack<Node>(2 * Log2(count + 1));
527 Node node = root;
528 while (node != null)
529 {
530 if (IsWithinRange(node.Item))
531 {
532 stack.Push(node);
533 node = node.Left;
534 }
535 else
536 {
537 node = ((!_lBoundActive || base.Comparer.Compare(_min, node.Item) <= 0) ? node.Left : node.Right);
538 }
539 }
540 while (stack.Count != 0)
541 {
542 node = stack.Pop();
543 if (!action(node))
544 {
545 return false;
546 }
547 Node node2 = node.Right;
548 while (node2 != null)
549 {
550 if (IsWithinRange(node2.Item))
551 {
552 stack.Push(node2);
553 node2 = node2.Left;
554 }
555 else
556 {
557 node2 = ((!_lBoundActive || base.Comparer.Compare(_min, node2.Item) <= 0) ? node2.Left : node2.Right);
558 }
559 }
560 }
561 return true;
562 }
563
565 {
566 VersionCheck();
567 if (root == null)
568 {
569 return true;
570 }
572 queue.Enqueue(root);
573 while (queue.Count != 0)
574 {
575 Node node = queue.Dequeue();
576 if (IsWithinRange(node.Item) && !action(node))
577 {
578 return false;
579 }
580 if (node.Left != null && (!_lBoundActive || base.Comparer.Compare(_min, node.Item) < 0))
581 {
582 queue.Enqueue(node.Left);
583 }
584 if (node.Right != null && (!_uBoundActive || base.Comparer.Compare(_max, node.Item) > 0))
585 {
586 queue.Enqueue(node.Right);
587 }
588 }
589 return true;
590 }
591
592 internal override Node FindNode(T item)
593 {
594 if (!IsWithinRange(item))
595 {
596 return null;
597 }
598 VersionCheck();
599 return base.FindNode(item);
600 }
601
602 internal override int InternalIndexOf(T item)
603 {
604 int num = -1;
606 {
607 while (enumerator.MoveNext())
608 {
609 T current = enumerator.Current;
610 num++;
611 if (base.Comparer.Compare(item, current) == 0)
612 {
613 return num;
614 }
615 }
616 }
617 return -1;
618 }
619
620 internal override void VersionCheck(bool updateCount = false)
621 {
623 }
624
625 private void VersionCheckImpl(bool updateCount)
626 {
627 if (version != _underlying.version)
628 {
630 version = _underlying.version;
631 }
632 if (updateCount && _countVersion != _underlying.version)
633 {
634 count = 0;
636 {
637 count++;
638 return true;
639 });
640 _countVersion = _underlying.version;
641 }
642 }
643
644 internal override int TotalCount()
645 {
646 return _underlying.Count;
647 }
648
650 {
651 if (_lBoundActive && base.Comparer.Compare(_min, lowerValue) > 0)
652 {
653 throw new ArgumentOutOfRangeException("lowerValue");
654 }
655 if (_uBoundActive && base.Comparer.Compare(_max, upperValue) < 0)
656 {
657 throw new ArgumentOutOfRangeException("upperValue");
658 }
659 return (TreeSubSet)_underlying.GetViewBetween(lowerValue, upperValue);
660 }
661
666
667 protected override void GetObjectData(SerializationInfo info, StreamingContext context)
668 {
670 }
671
673 {
675 }
676
677 protected override void OnDeserialization(object sender)
678 {
680 }
681 }
682
683 private Node root;
684
686
687 private int count;
688
689 private int version;
690
692
693 public int Count
694 {
695 get
696 {
698 return count;
699 }
700 }
701
703
705
707
708 object ICollection.SyncRoot => this;
709
710 public T? Min => MinInternal;
711
712 internal virtual T? MinInternal
713 {
714 get
715 {
716 if (root == null)
717 {
718 return default(T);
719 }
720 Node left = root;
721 while (left.Left != null)
722 {
723 left = left.Left;
724 }
725 return left.Item;
726 }
727 }
728
729 public T? Max => MaxInternal;
730
731 internal virtual T? MaxInternal
732 {
733 get
734 {
735 if (root == null)
736 {
737 return default(T);
738 }
739 Node right = root;
740 while (right.Right != null)
741 {
742 right = right.Right;
743 }
744 return right.Item;
745 }
746 }
747
748 public SortedSet()
749 {
750 comparer = Comparer<T>.Default;
751 }
752
754 {
756 }
757
762
764 : this(comparer)
765 {
766 if (collection == null)
767 {
768 throw new ArgumentNullException("collection");
769 }
771 {
772 if (sortedSet.Count > 0)
773 {
774 count = sortedSet.count;
775 root = sortedSet.root.DeepClone(count);
776 }
777 return;
778 }
779 int length;
781 if (length <= 0)
782 {
783 return;
784 }
785 comparer = this.comparer;
787 int num = 1;
788 for (int i = 1; i < length; i++)
789 {
790 if (comparer.Compare(array[i], array[i - 1]) != 0)
791 {
792 array[num++] = array[i];
793 }
794 }
795 length = num;
797 count = length;
798 }
799
801 {
802 siInfo = info;
803 }
804
806 {
807 foreach (T item in collection)
808 {
809 if (!Contains(item))
810 {
811 Add(item);
812 }
813 }
814 }
815
817 {
818 T min = Min;
819 T max = Max;
820 foreach (T item in collection)
821 {
822 if (comparer.Compare(item, min) >= 0 && comparer.Compare(item, max) <= 0 && Contains(item))
823 {
824 Remove(item);
825 }
826 }
827 }
828
830 {
831 foreach (T item in collection)
832 {
833 if (!Contains(item))
834 {
835 return false;
836 }
837 }
838 return true;
839 }
840
842 {
843 if (root == null)
844 {
845 return true;
846 }
847 Stack<Node> stack = new Stack<Node>(2 * Log2(Count + 1));
848 for (Node left = root; left != null; left = left.Left)
849 {
850 stack.Push(left);
851 }
852 while (stack.Count != 0)
853 {
854 Node left = stack.Pop();
855 if (!action(left))
856 {
857 return false;
858 }
859 for (Node node = left.Right; node != null; node = node.Left)
860 {
861 stack.Push(node);
862 }
863 }
864 return true;
865 }
866
868 {
869 if (root == null)
870 {
871 return true;
872 }
874 queue.Enqueue(root);
875 while (queue.Count != 0)
876 {
877 Node node = queue.Dequeue();
878 if (!action(node))
879 {
880 return false;
881 }
882 if (node.Left != null)
883 {
884 queue.Enqueue(node.Left);
885 }
886 if (node.Right != null)
887 {
888 queue.Enqueue(node.Right);
889 }
890 }
891 return true;
892 }
893
894 internal virtual void VersionCheck(bool updateCount = false)
895 {
896 }
897
898 internal virtual int TotalCount()
899 {
900 return Count;
901 }
902
903 internal virtual bool IsWithinRange(T item)
904 {
905 return true;
906 }
907
908 public bool Add(T item)
909 {
910 return AddIfNotPresent(item);
911 }
912
914 {
915 Add(item);
916 }
917
918 internal virtual bool AddIfNotPresent(T item)
919 {
920 if (root == null)
921 {
922 root = new Node(item, NodeColor.Black);
923 count = 1;
924 version++;
925 return true;
926 }
927 Node node = root;
928 Node parent = null;
929 Node node2 = null;
930 Node greatGrandParent = null;
931 version++;
932 int num = 0;
933 while (node != null)
934 {
935 num = comparer.Compare(item, node.Item);
936 if (num == 0)
937 {
939 return false;
940 }
941 if (node.Is4Node)
942 {
943 node.Split4Node();
944 if (Node.IsNonNullRed(parent))
945 {
947 }
948 }
950 node2 = parent;
951 parent = node;
952 node = ((num < 0) ? node.Left : node.Right);
953 }
954 Node node3 = new Node(item, NodeColor.Red);
955 if (num > 0)
956 {
958 }
959 else
960 {
962 }
963 if (parent.IsRed)
964 {
966 }
968 count++;
969 return true;
970 }
971
972 public bool Remove(T item)
973 {
974 return DoRemove(item);
975 }
976
977 internal virtual bool DoRemove(T item)
978 {
979 if (root == null)
980 {
981 return false;
982 }
983 version++;
984 Node node = root;
985 Node node2 = null;
986 Node node3 = null;
987 Node node4 = null;
988 Node parentOfMatch = null;
989 bool flag = false;
990 while (node != null)
991 {
992 if (node.Is2Node)
993 {
994 if (node2 == null)
995 {
996 node.ColorRed();
997 }
998 else
999 {
1000 Node sibling = node2.GetSibling(node);
1001 if (sibling.IsRed)
1002 {
1003 if (node2.Right == sibling)
1004 {
1005 node2.RotateLeft();
1006 }
1007 else
1008 {
1009 node2.RotateRight();
1010 }
1011 node2.ColorRed();
1012 sibling.ColorBlack();
1014 node3 = sibling;
1015 if (node2 == node4)
1016 {
1018 }
1019 sibling = node2.GetSibling(node);
1020 }
1021 if (sibling.Is2Node)
1022 {
1023 node2.Merge2Nodes();
1024 }
1025 else
1026 {
1027 Node node5 = node2.Rotate(node2.GetRotation(node, sibling));
1028 node5.Color = node2.Color;
1029 node2.ColorBlack();
1030 node.ColorRed();
1032 if (node2 == node4)
1033 {
1035 }
1036 node3 = node5;
1037 }
1038 }
1039 }
1040 int num = (flag ? (-1) : comparer.Compare(item, node.Item));
1041 if (num == 0)
1042 {
1043 flag = true;
1044 node4 = node;
1046 }
1047 node3 = node2;
1048 node2 = node;
1049 node = ((num < 0) ? node.Left : node.Right);
1050 }
1051 if (node4 != null)
1052 {
1054 count--;
1055 }
1056 root?.ColorBlack();
1057 return flag;
1058 }
1059
1060 public virtual void Clear()
1061 {
1062 root = null;
1063 count = 0;
1064 version++;
1065 }
1066
1067 public virtual bool Contains(T item)
1068 {
1069 return FindNode(item) != null;
1070 }
1071
1072 public void CopyTo(T[] array)
1073 {
1074 CopyTo(array, 0, Count);
1075 }
1076
1077 public void CopyTo(T[] array, int index)
1078 {
1080 }
1081
1082 public void CopyTo(T[] array, int index, int count)
1083 {
1084 T[] array2 = array;
1085 if (array2 == null)
1086 {
1087 throw new ArgumentNullException("array");
1088 }
1089 if (index < 0)
1090 {
1092 }
1093 if (count < 0)
1094 {
1096 }
1097 if (count > array2.Length - index)
1098 {
1100 }
1101 count += index;
1103 {
1104 if (index >= count)
1105 {
1106 return false;
1107 }
1108 array2[index++] = node.Item;
1109 return true;
1110 });
1111 }
1112
1114 {
1115 if (array == null)
1116 {
1117 throw new ArgumentNullException("array");
1118 }
1119 if (array.Rank != 1)
1120 {
1122 }
1123 if (array.GetLowerBound(0) != 0)
1124 {
1126 }
1127 if (index < 0)
1128 {
1130 }
1131 if (array.Length - index < Count)
1132 {
1134 }
1135 if (array is T[] array2)
1136 {
1138 return;
1139 }
1140 object[] objects = array as object[];
1141 if (objects == null)
1142 {
1144 }
1145 try
1146 {
1148 {
1149 objects[index++] = node.Item;
1150 return true;
1151 });
1152 }
1154 {
1156 }
1157 }
1158
1160 {
1161 return new Enumerator(this);
1162 }
1163
1168
1170 {
1171 return GetEnumerator();
1172 }
1173
1175 {
1176 bool flag = grandParent.Right == parent;
1177 bool flag2 = parent.Right == current;
1178 Node node;
1179 if (flag == flag2)
1180 {
1181 node = (flag2 ? grandParent.RotateLeft() : grandParent.RotateRight());
1182 }
1183 else
1184 {
1185 node = (flag2 ? grandParent.RotateLeftRight() : grandParent.RotateRightLeft());
1186 parent = greatGrandParent;
1187 }
1188 grandParent.ColorRed();
1189 node.ColorBlack();
1191 }
1192
1193 private void ReplaceChildOrRoot(Node parent, Node child, Node newChild)
1194 {
1195 if (parent != null)
1196 {
1197 parent.ReplaceChild(child, newChild);
1198 }
1199 else
1200 {
1201 root = newChild;
1202 }
1203 }
1204
1206 {
1207 if (successor == match)
1208 {
1209 successor = match.Left;
1210 }
1211 else
1212 {
1213 successor.Right?.ColorBlack();
1214 if (parentOfSuccessor != match)
1215 {
1217 successor.Right = match.Right;
1218 }
1219 successor.Left = match.Left;
1220 }
1221 if (successor != null)
1222 {
1223 successor.Color = match.Color;
1224 }
1226 }
1227
1228 internal virtual Node FindNode(T item)
1229 {
1230 Node node = root;
1231 while (node != null)
1232 {
1233 int num = comparer.Compare(item, node.Item);
1234 if (num == 0)
1235 {
1236 return node;
1237 }
1238 node = ((num < 0) ? node.Left : node.Right);
1239 }
1240 return null;
1241 }
1242
1243 internal virtual int InternalIndexOf(T item)
1244 {
1245 Node node = root;
1246 int num = 0;
1247 while (node != null)
1248 {
1249 int num2 = comparer.Compare(item, node.Item);
1250 if (num2 == 0)
1251 {
1252 return num;
1253 }
1254 node = ((num2 < 0) ? node.Left : node.Right);
1255 num = ((num2 < 0) ? (2 * num + 1) : (2 * num + 2));
1256 }
1257 return -1;
1258 }
1259
1261 {
1262 Node node = root;
1263 while (node != null)
1264 {
1265 if (lowerBoundActive && comparer.Compare(from, node.Item) > 0)
1266 {
1267 node = node.Right;
1268 continue;
1269 }
1270 if (upperBoundActive && comparer.Compare(to, node.Item) < 0)
1271 {
1272 node = node.Left;
1273 continue;
1274 }
1275 return node;
1276 }
1277 return null;
1278 }
1279
1280 internal void UpdateVersion()
1281 {
1282 version++;
1283 }
1284
1286 {
1287 return CreateSetComparer(null);
1288 }
1289
1294
1296 {
1297 if (set1 == null)
1298 {
1299 return set2 == null;
1300 }
1301 if (set2 == null)
1302 {
1303 return false;
1304 }
1305 if (set1.HasEqualComparer(set2))
1306 {
1307 if (set1.Count == set2.Count)
1308 {
1309 return set1.SetEquals(set2);
1310 }
1311 return false;
1312 }
1313 bool flag = false;
1314 foreach (T item in set1)
1315 {
1316 flag = false;
1317 foreach (T item2 in set2)
1318 {
1319 if (comparer.Compare(item, item2) == 0)
1320 {
1321 flag = true;
1322 break;
1323 }
1324 }
1325 if (!flag)
1326 {
1327 return false;
1328 }
1329 }
1330 return true;
1331 }
1332
1334 {
1335 if (Comparer != other.Comparer)
1336 {
1337 return Comparer.Equals(other.Comparer);
1338 }
1339 return true;
1340 }
1341
1343 {
1344 if (other == null)
1345 {
1346 throw new ArgumentNullException("other");
1347 }
1350 if (treeSubSet != null)
1351 {
1352 VersionCheck();
1353 }
1354 if (sortedSet != null && treeSubSet == null && Count == 0)
1355 {
1357 root = sortedSet2.root;
1358 count = sortedSet2.count;
1359 version++;
1360 }
1361 else if (sortedSet != null && treeSubSet == null && HasEqualComparer(sortedSet) && sortedSet.Count > Count / 2)
1362 {
1363 T[] array = new T[sortedSet.Count + Count];
1364 int num = 0;
1367 bool flag = !enumerator.MoveNext();
1368 bool flag2 = !enumerator2.MoveNext();
1369 while (!flag && !flag2)
1370 {
1371 int num2 = Comparer.Compare(enumerator.Current, enumerator2.Current);
1372 if (num2 < 0)
1373 {
1374 array[num++] = enumerator.Current;
1375 flag = !enumerator.MoveNext();
1376 }
1377 else if (num2 == 0)
1378 {
1379 array[num++] = enumerator2.Current;
1380 flag = !enumerator.MoveNext();
1381 flag2 = !enumerator2.MoveNext();
1382 }
1383 else
1384 {
1385 array[num++] = enumerator2.Current;
1386 flag2 = !enumerator2.MoveNext();
1387 }
1388 }
1389 if (!flag || !flag2)
1390 {
1392 do
1393 {
1394 array[num++] = enumerator3.Current;
1395 }
1396 while (enumerator3.MoveNext());
1397 }
1398 root = null;
1399 root = ConstructRootFromSortedArray(array, 0, num - 1, null);
1400 count = num;
1401 version++;
1402 }
1403 else
1404 {
1406 }
1407 }
1408
1410 {
1411 int num = endIndex - startIndex + 1;
1412 Node node;
1413 switch (num)
1414 {
1415 case 0:
1416 return null;
1417 case 1:
1418 node = new Node(arr[startIndex], NodeColor.Black);
1419 if (redNode != null)
1420 {
1422 }
1423 break;
1424 case 2:
1425 node = new Node(arr[startIndex], NodeColor.Black);
1426 node.Right = new Node(arr[endIndex], NodeColor.Black);
1427 node.Right.ColorRed();
1428 if (redNode != null)
1429 {
1431 }
1432 break;
1433 case 3:
1434 node = new Node(arr[startIndex + 1], NodeColor.Black);
1435 node.Left = new Node(arr[startIndex], NodeColor.Black);
1436 node.Right = new Node(arr[endIndex], NodeColor.Black);
1437 if (redNode != null)
1438 {
1440 }
1441 break;
1442 default:
1443 {
1444 int num2 = (startIndex + endIndex) / 2;
1445 node = new Node(arr[num2], NodeColor.Black);
1447 node.Right = ((num % 2 == 0) ? ConstructRootFromSortedArray(arr, num2 + 2, endIndex, new Node(arr[num2 + 1], NodeColor.Red)) : ConstructRootFromSortedArray(arr, num2 + 1, endIndex, null));
1448 break;
1449 }
1450 }
1451 return node;
1452 }
1453
1455 {
1456 if (other == null)
1457 {
1458 throw new ArgumentNullException("other");
1459 }
1460 if (Count == 0 || other == this)
1461 {
1462 return;
1463 }
1466 if (treeSubSet != null)
1467 {
1468 VersionCheck();
1469 }
1470 if (sortedSet != null && treeSubSet == null && HasEqualComparer(sortedSet))
1471 {
1472 T[] array = new T[Count];
1473 int num = 0;
1476 bool flag = !enumerator.MoveNext();
1477 bool flag2 = !enumerator2.MoveNext();
1478 T max = Max;
1479 while (!flag && !flag2 && Comparer.Compare(enumerator2.Current, max) <= 0)
1480 {
1481 int num2 = Comparer.Compare(enumerator.Current, enumerator2.Current);
1482 if (num2 < 0)
1483 {
1484 flag = !enumerator.MoveNext();
1485 }
1486 else if (num2 == 0)
1487 {
1488 array[num++] = enumerator2.Current;
1489 flag = !enumerator.MoveNext();
1490 flag2 = !enumerator2.MoveNext();
1491 }
1492 else
1493 {
1494 flag2 = !enumerator2.MoveNext();
1495 }
1496 }
1497 root = null;
1498 root = ConstructRootFromSortedArray(array, 0, num - 1, null);
1499 count = num;
1500 version++;
1501 }
1502 else
1503 {
1505 }
1506 }
1507
1509 {
1510 List<T> list = new List<T>(Count);
1511 foreach (T item in other)
1512 {
1513 if (Contains(item))
1514 {
1515 list.Add(item);
1516 }
1517 }
1518 Clear();
1519 foreach (T item2 in list)
1520 {
1521 Add(item2);
1522 }
1523 }
1524
1526 {
1527 if (other == null)
1528 {
1529 throw new ArgumentNullException("other");
1530 }
1531 if (count == 0)
1532 {
1533 return;
1534 }
1535 if (other == this)
1536 {
1537 Clear();
1538 }
1539 else
1540 {
1542 {
1543 if (comparer.Compare(sortedSet.Max, Min) < 0 || comparer.Compare(sortedSet.Min, Max) > 0)
1544 {
1545 return;
1546 }
1547 T min = Min;
1548 T max = Max;
1549 {
1550 foreach (T item in other)
1551 {
1552 if (comparer.Compare(item, min) >= 0)
1553 {
1554 if (comparer.Compare(item, max) > 0)
1555 {
1556 break;
1557 }
1558 Remove(item);
1559 }
1560 }
1561 return;
1562 }
1563 }
1565 }
1566 }
1567
1569 {
1570 if (other == null)
1571 {
1572 throw new ArgumentNullException("other");
1573 }
1574 if (Count == 0)
1575 {
1577 return;
1578 }
1579 if (other == this)
1580 {
1581 Clear();
1582 return;
1583 }
1585 {
1587 return;
1588 }
1589 int length;
1593 }
1594
1596 {
1597 foreach (T item in other)
1598 {
1599 bool flag = (Contains(item) ? Remove(item) : Add(item));
1600 }
1601 }
1602
1604 {
1605 if (count == 0)
1606 {
1607 return;
1608 }
1609 T y = other[0];
1610 for (int i = 0; i < count; i++)
1611 {
1612 for (; i < count && i != 0 && comparer.Compare(other[i], y) == 0; i++)
1613 {
1614 }
1615 if (i < count)
1616 {
1617 T val = other[i];
1618 bool flag = (Contains(val) ? Remove(val) : Add(val));
1619 y = val;
1620 continue;
1621 }
1622 break;
1623 }
1624 }
1625
1627 {
1628 if (other == null)
1629 {
1630 throw new ArgumentNullException("other");
1631 }
1632 if (Count == 0)
1633 {
1634 return true;
1635 }
1637 {
1638 if (Count > sortedSet.Count)
1639 {
1640 return false;
1641 }
1643 }
1645 if (elementCount.UniqueCount == Count)
1646 {
1647 return elementCount.UnfoundCount >= 0;
1648 }
1649 return false;
1650 }
1651
1653 {
1654 SortedSet<T> viewBetween = asSorted.GetViewBetween(Min, Max);
1656 {
1657 while (enumerator.MoveNext())
1658 {
1659 T current = enumerator.Current;
1660 if (!viewBetween.Contains(current))
1661 {
1662 return false;
1663 }
1664 }
1665 }
1666 return true;
1667 }
1668
1670 {
1671 if (other == null)
1672 {
1673 throw new ArgumentNullException("other");
1674 }
1675 if (other is ICollection collection && Count == 0)
1676 {
1677 return collection.Count > 0;
1678 }
1680 {
1681 if (Count >= sortedSet.Count)
1682 {
1683 return false;
1684 }
1686 }
1688 if (elementCount.UniqueCount == Count)
1689 {
1690 return elementCount.UnfoundCount > 0;
1691 }
1692 return false;
1693 }
1694
1696 {
1697 if (other == null)
1698 {
1699 throw new ArgumentNullException("other");
1700 }
1701 if (other is ICollection { Count: 0 })
1702 {
1703 return true;
1704 }
1706 {
1707 if (Count < sortedSet.Count)
1708 {
1709 return false;
1710 }
1712 foreach (T item in sortedSet)
1713 {
1715 {
1716 return false;
1717 }
1718 }
1719 return true;
1720 }
1721 return ContainsAllElements(other);
1722 }
1723
1725 {
1726 if (other == null)
1727 {
1728 throw new ArgumentNullException("other");
1729 }
1730 if (Count == 0)
1731 {
1732 return false;
1733 }
1734 if (other is ICollection { Count: 0 })
1735 {
1736 return true;
1737 }
1739 {
1740 if (sortedSet.Count >= Count)
1741 {
1742 return false;
1743 }
1745 foreach (T item in sortedSet)
1746 {
1748 {
1749 return false;
1750 }
1751 }
1752 return true;
1753 }
1755 if (elementCount.UniqueCount < Count)
1756 {
1757 return elementCount.UnfoundCount == 0;
1758 }
1759 return false;
1760 }
1761
1763 {
1764 if (other == null)
1765 {
1766 throw new ArgumentNullException("other");
1767 }
1769 {
1772 bool flag = !enumerator.MoveNext();
1773 bool flag2 = !enumerator2.MoveNext();
1774 while (!flag && !flag2)
1775 {
1776 if (Comparer.Compare(enumerator.Current, enumerator2.Current) != 0)
1777 {
1778 return false;
1779 }
1780 flag = !enumerator.MoveNext();
1781 flag2 = !enumerator2.MoveNext();
1782 }
1783 return flag && flag2;
1784 }
1786 if (elementCount.UniqueCount == Count)
1787 {
1788 return elementCount.UnfoundCount == 0;
1789 }
1790 return false;
1791 }
1792
1794 {
1795 if (other == null)
1796 {
1797 throw new ArgumentNullException("other");
1798 }
1799 if (Count == 0)
1800 {
1801 return false;
1802 }
1803 if (other is ICollection<T> { Count: 0 })
1804 {
1805 return false;
1806 }
1808 {
1809 return false;
1810 }
1811 foreach (T item in other)
1812 {
1813 if (Contains(item))
1814 {
1815 return true;
1816 }
1817 }
1818 return false;
1819 }
1820
1822 {
1823 Unsafe.SkipInit(out ElementCount result);
1824 if (Count == 0)
1825 {
1826 int num = 0;
1828 {
1829 if (enumerator.MoveNext())
1830 {
1831 T current = enumerator.Current;
1832 num++;
1833 }
1834 }
1836 result.UnfoundCount = num;
1837 return result;
1838 }
1839 int n = Count;
1841 Span<int> span = stackalloc int[100];
1843 int num3 = 0;
1844 int num4 = 0;
1845 foreach (T item in other)
1846 {
1847 int num5 = InternalIndexOf(item);
1848 if (num5 >= 0)
1849 {
1850 if (!bitHelper.IsMarked(num5))
1851 {
1852 bitHelper.MarkBit(num5);
1853 num4++;
1854 }
1855 }
1856 else
1857 {
1858 num3++;
1859 if (returnIfUnfound)
1860 {
1861 break;
1862 }
1863 }
1864 }
1867 return result;
1868 }
1869
1871 {
1873 if (match2 == null)
1874 {
1875 throw new ArgumentNullException("match");
1876 }
1877 List<T> matches = new List<T>(Count);
1879 {
1880 if (match2(n.Item))
1881 {
1882 matches.Add(n.Item);
1883 }
1884 return true;
1885 });
1886 int num = 0;
1887 for (int num2 = matches.Count - 1; num2 >= 0; num2--)
1888 {
1889 if (Remove(matches[num2]))
1890 {
1891 num++;
1892 }
1893 }
1894 return num;
1895 }
1896
1898 {
1899 Enumerator e = new Enumerator(this, reverse: true);
1900 while (e.MoveNext())
1901 {
1902 yield return e.Current;
1903 }
1904 }
1905
1907 {
1909 {
1911 }
1912 return new TreeSubSet(this, lowerValue, upperValue, lowerBoundActive: true, upperBoundActive: true);
1913 }
1914
1919
1920 protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
1921 {
1922 if (info == null)
1923 {
1924 throw new ArgumentNullException("info");
1925 }
1926 info.AddValue("Count", count);
1927 info.AddValue("Comparer", comparer, typeof(IComparer<T>));
1928 info.AddValue("Version", version);
1929 if (root != null)
1930 {
1931 T[] array = new T[Count];
1932 CopyTo(array, 0);
1933 info.AddValue("Items", array, typeof(T[]));
1934 }
1935 }
1936
1938 {
1939 OnDeserialization(sender);
1940 }
1941
1942 protected virtual void OnDeserialization(object? sender)
1943 {
1944 if (comparer != null)
1945 {
1946 return;
1947 }
1948 if (siInfo == null)
1949 {
1951 }
1953 int @int = siInfo.GetInt32("Count");
1954 if (@int != 0)
1955 {
1956 T[] array = (T[])siInfo.GetValue("Items", typeof(T[]));
1957 if (array == null)
1958 {
1960 }
1961 for (int i = 0; i < array.Length; i++)
1962 {
1963 Add(array[i]);
1964 }
1965 }
1966 version = siInfo.GetInt32("Version");
1967 if (count != @int)
1968 {
1970 }
1971 siInfo = null;
1972 }
1973
1975 {
1977 if (node != null)
1978 {
1979 actualValue = node.Item;
1980 return true;
1981 }
1982 actualValue = default(T);
1983 return false;
1984 }
1985
1986 private static int Log2(int value)
1987 {
1988 int num = 0;
1989 while (value > 0)
1990 {
1991 num++;
1992 value >>= 1;
1993 }
1994 return num;
1995 }
1996}
static void Sort(Array array)
Definition Array.cs:2329
IEqualityComparer< TKey > Comparer
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
static bool IsNullOrBlack(Node node)
Definition SortedSet.cs:63
TreeRotation GetRotation(Node current, Node sibling)
Definition SortedSet.cs:111
Node Rotate(TreeRotation rotation)
Definition SortedSet.cs:150
static bool IsNonNullRed(Node node)
Definition SortedSet.cs:58
void ReplaceChild(Node child, Node newChild)
Definition SortedSet.cs:220
override void VersionCheck(bool updateCount=false)
Definition SortedSet.cs:620
override bool BreadthFirstTreeWalk(TreeWalkPredicate< T > action)
Definition SortedSet.cs:564
override SortedSet< T > GetViewBetween(T lowerValue, T upperValue)
Definition SortedSet.cs:649
override void GetObjectData(SerializationInfo info, StreamingContext context)
Definition SortedSet.cs:667
TreeSubSet(SortedSet< T > Underlying, T Min, T Max, bool lowerBoundActive, bool upperBoundActive)
Definition SortedSet.cs:445
void ISerializable. GetObjectData(SerializationInfo info, StreamingContext context)
Definition SortedSet.cs:662
override bool InOrderTreeWalk(TreeWalkPredicate< T > action)
Definition SortedSet.cs:519
override void OnDeserialization(object sender)
Definition SortedSet.cs:677
bool ContainsAllElements(IEnumerable< T > collection)
Definition SortedSet.cs:829
SortedSet(IEnumerable< T > collection, IComparer< T >? comparer)
Definition SortedSet.cs:763
bool SetEquals(IEnumerable< T > other)
int RemoveWhere(Predicate< T > match)
virtual bool BreadthFirstTreeWalk(TreeWalkPredicate< T > action)
Definition SortedSet.cs:867
Node FindRange(T from, T to, bool lowerBoundActive, bool upperBoundActive)
static IEqualityComparer< SortedSet< T > > CreateSetComparer()
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
void ReplaceChildOrRoot(Node parent, Node child, Node newChild)
void ISerializable. GetObjectData(SerializationInfo info, StreamingContext context)
void CopyTo(T[] array, int index)
void InsertionBalance(Node current, ref Node parent, Node grandParent, Node greatGrandParent)
SortedSet(SerializationInfo info, StreamingContext context)
Definition SortedSet.cs:800
bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue)
bool Overlaps(IEnumerable< T > other)
void SymmetricExceptWithSameComparer(SortedSet< T > other)
void ReplaceNode(Node match, Node parentOfMatch, Node successor, Node parentOfSuccessor)
static bool SortedSetEquals(SortedSet< T > set1, SortedSet< T > set2, IComparer< T > comparer)
bool IsSupersetOf(IEnumerable< T > other)
static Node ConstructRootFromSortedArray(T[] arr, int startIndex, int endIndex, Node redNode)
SortedSet(IEnumerable< T > collection)
Definition SortedSet.cs:758
ElementCount CheckUniqueAndUnfoundElements(IEnumerable< T > other, bool returnIfUnfound)
SortedSet(IComparer< T >? comparer)
Definition SortedSet.cs:753
bool IsProperSupersetOf(IEnumerable< T > other)
bool IsSubsetOfSortedSetWithSameComparer(SortedSet< T > asSorted)
virtual int InternalIndexOf(T item)
void AddAllElements(IEnumerable< T > collection)
Definition SortedSet.cs:805
static IEqualityComparer< SortedSet< T > > CreateSetComparer(IEqualityComparer< T >? memberEqualityComparer)
void UnionWith(IEnumerable< T > other)
void SymmetricExceptWith(IEnumerable< T > other)
virtual void IntersectWith(IEnumerable< T > other)
void CopyTo(T[] array, int index, int count)
void RemoveAllElements(IEnumerable< T > collection)
Definition SortedSet.cs:816
void SymmetricExceptWithSameComparer(T[] other, int count)
virtual bool AddIfNotPresent(T item)
Definition SortedSet.cs:918
virtual SortedSet< T > GetViewBetween(T? lowerValue, T? upperValue)
virtual void IntersectWithEnumerable(IEnumerable< T > other)
void ExceptWith(IEnumerable< T > other)
bool IsProperSubsetOf(IEnumerable< T > other)
virtual void OnDeserialization(object? sender)
virtual bool IsWithinRange(T item)
Definition SortedSet.cs:903
bool HasEqualComparer(SortedSet< T > other)
bool IsSubsetOf(IEnumerable< T > other)
virtual bool InOrderTreeWalk(TreeWalkPredicate< T > action)
Definition SortedSet.cs:841
void IDeserializationCallback. OnDeserialization(object sender)
virtual void VersionCheck(bool updateCount=false)
Definition SortedSet.cs:894
static string Serialization_MissingValues
Definition SR.cs:74
static string Arg_ArrayPlusOffTooSmall
Definition SR.cs:16
static string Argument_InvalidArrayType
Definition SR.cs:54
static string Arg_NonZeroLowerBound
Definition SR.cs:14
static string Serialization_InvalidOnDeser
Definition SR.cs:38
static string InvalidOperation_EnumFailedVersion
Definition SR.cs:44
static string Arg_RankMultiDimNotSupported
Definition SR.cs:18
static string InvalidOperation_EnumOpCantHappen
Definition SR.cs:48
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string Serialization_MismatchedCount
Definition SR.cs:72
static string SortedSet_LowerValueGreaterThanUpperValue
Definition SR.cs:68
Definition SR.cs:7
void CopyTo(T[] array, int arrayIndex)
new IEnumerator< T > GetEnumerator()
void GetObjectData(SerializationInfo info, StreamingContext context)
Enumerator(SortedSet< T > set, bool reverse)
Definition SortedSet.cs:276