Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ImmutableArray.cs
Go to the documentation of this file.
4using System.Linq;
7
9
10public static class ImmutableArray
11{
12 internal static readonly byte[] TwoElementArray = new byte[2];
13
15 {
16 return ImmutableArray<T>.Empty;
17 }
18
20 {
21 T[] items = new T[1] { item };
22 return new ImmutableArray<T>(items);
23 }
24
26 {
27 T[] items = new T[2] { item1, item2 };
28 return new ImmutableArray<T>(items);
29 }
30
32 {
33 T[] items = new T[3] { item1, item2, item3 };
34 return new ImmutableArray<T>(items);
35 }
36
38 {
39 T[] items = new T[4] { item1, item2, item3, item4 };
40 return new ImmutableArray<T>(items);
41 }
42
44 {
45 Requires.NotNull(items, "items");
47 {
49 if (array == null)
50 {
52 }
53 return new ImmutableArray<T>((T[])array);
54 }
55 if (items.TryGetCount(out var count))
56 {
57 return new ImmutableArray<T>(items.ToArray(count));
58 }
59 return new ImmutableArray<T>(items.ToArray());
60 }
61
62 public static ImmutableArray<T> Create<T>(params T[]? items)
63 {
64 if (items == null || items.Length == 0)
65 {
66 return ImmutableArray<T>.Empty;
67 }
68 T[] array = new T[items.Length];
69 Array.Copy(items, array, items.Length);
70 return new ImmutableArray<T>(array);
71 }
72
73 public static ImmutableArray<T> Create<T>(T[] items, int start, int length)
74 {
75 Requires.NotNull(items, "items");
76 Requires.Range(start >= 0 && start <= items.Length, "start");
77 Requires.Range(length >= 0 && start + length <= items.Length, "length");
78 if (length == 0)
79 {
80 return Create<T>();
81 }
82 T[] array = new T[length];
83 for (int i = 0; i < array.Length; i++)
84 {
85 array[i] = items[start + i];
86 }
87 return new ImmutableArray<T>(array);
88 }
89
91 {
92 Requires.Range(start >= 0 && start <= items.Length, "start");
93 Requires.Range(length >= 0 && start + length <= items.Length, "length");
94 if (length == 0)
95 {
96 return Create<T>();
97 }
98 if (start == 0 && length == items.Length)
99 {
100 return items;
101 }
102 T[] array = new T[length];
103 Array.Copy(items.array, start, array, 0, length);
104 return new ImmutableArray<T>(array);
105 }
106
108 {
109 Requires.NotNull(selector, "selector");
110 int length = items.Length;
111 if (length == 0)
112 {
113 return Create<TResult>();
114 }
115 TResult[] array = new TResult[length];
116 for (int i = 0; i < array.Length; i++)
117 {
118 array[i] = selector(items[i]);
119 }
120 return new ImmutableArray<TResult>(array);
121 }
122
124 {
125 int length2 = items.Length;
126 Requires.Range(start >= 0 && start <= length2, "start");
127 Requires.Range(length >= 0 && start + length <= length2, "length");
128 Requires.NotNull(selector, "selector");
129 if (length == 0)
130 {
131 return Create<TResult>();
132 }
133 TResult[] array = new TResult[length];
134 for (int i = 0; i < array.Length; i++)
135 {
136 array[i] = selector(items[i + start]);
137 }
138 return new ImmutableArray<TResult>(array);
139 }
140
142 {
143 Requires.NotNull(selector, "selector");
144 int length = items.Length;
145 if (length == 0)
146 {
147 return Create<TResult>();
148 }
149 TResult[] array = new TResult[length];
150 for (int i = 0; i < array.Length; i++)
151 {
152 array[i] = selector(items[i], arg);
153 }
154 return new ImmutableArray<TResult>(array);
155 }
156
158 {
159 int length2 = items.Length;
160 Requires.Range(start >= 0 && start <= length2, "start");
161 Requires.Range(length >= 0 && start + length <= length2, "length");
162 Requires.NotNull(selector, "selector");
163 if (length == 0)
164 {
165 return Create<TResult>();
166 }
167 TResult[] array = new TResult[length];
168 for (int i = 0; i < array.Length; i++)
169 {
170 array[i] = selector(items[i + start], arg);
171 }
172 return new ImmutableArray<TResult>(array);
173 }
174
175 public static ImmutableArray<T>.Builder CreateBuilder<T>()
176 {
177 return Create<T>().ToBuilder();
178 }
179
181 {
182 return new ImmutableArray<T>.Builder(initialCapacity);
183 }
184
186 {
187 if (items is ImmutableArray<TSource>)
188 {
189 return (ImmutableArray<TSource>)(object)items;
190 }
191 return CreateRange(items);
192 }
193
195 {
196 Requires.NotNull(builder, "builder");
197 return builder.ToImmutable();
198 }
199
200 public static int BinarySearch<T>(this ImmutableArray<T> array, T value)
201 {
202 return Array.BinarySearch(array.array, value);
203 }
204
206 {
207 return Array.BinarySearch(array.array, value, comparer);
208 }
209
210 public static int BinarySearch<T>(this ImmutableArray<T> array, int index, int length, T value)
211 {
212 return Array.BinarySearch(array.array, index, length, value);
213 }
214
216 {
218 }
219}
220[DebuggerDisplay("{DebuggerDisplay,nq}")]
221[System.Runtime.Versioning.NonVersionable]
223{
224 [DebuggerDisplay("Count = {Count}")]
227 {
228 private T[] _elements;
229
230 private int _count;
231
232 public int Capacity
233 {
234 get
235 {
236 return _elements.Length;
237 }
238 set
239 {
240 if (value < _count)
241 {
243 }
244 if (value == _elements.Length)
245 {
246 return;
247 }
248 if (value > 0)
249 {
250 T[] array = new T[value];
251 if (_count > 0)
252 {
254 }
256 }
257 else
258 {
259 _elements = ImmutableArray<T>.Empty.array;
260 }
261 }
262 }
263
264 public int Count
265 {
266 get
267 {
268 return _count;
269 }
270 set
271 {
272 Requires.Range(value >= 0, "value");
273 if (value < _count)
274 {
275 if (_count - value > 64)
276 {
278 }
279 else
280 {
281 for (int i = value; i < Count; i++)
282 {
283 _elements[i] = default(T);
284 }
285 }
286 }
287 else if (value > _count)
288 {
290 }
291 _count = value;
292 }
293 }
294
295 public T this[int index]
296 {
297 get
298 {
299 if (index >= Count)
300 {
302 }
303 return _elements[index];
304 }
305 set
306 {
307 if (index >= Count)
308 {
310 }
312 }
313 }
314
316
317 internal Builder(int capacity)
318 {
319 Requires.Range(capacity >= 0, "capacity");
320 _elements = new T[capacity];
321 _count = 0;
322 }
323
324 internal Builder()
325 : this(8)
326 {
327 }
328
329 private static void ThrowIndexOutOfRangeException()
330 {
331 throw new IndexOutOfRangeException();
332 }
333
334 public ref readonly T ItemRef(int index)
335 {
336 if (index >= Count)
337 {
339 }
340 return ref _elements[index];
341 }
342
344 {
345 return new ImmutableArray<T>(ToArray());
346 }
347
349 {
350 if (Capacity != Count)
351 {
353 }
354 T[] elements = _elements;
355 _elements = ImmutableArray<T>.Empty.array;
356 _count = 0;
357 return new ImmutableArray<T>(elements);
358 }
359
360 public void Clear()
361 {
362 Count = 0;
363 }
364
365 public void Insert(int index, T item)
366 {
367 Requires.Range(index >= 0 && index <= Count, "index");
369 if (index < Count)
370 {
372 }
373 _count++;
375 }
376
377 public void Add(T item)
378 {
379 int num = _count + 1;
380 EnsureCapacity(num);
382 _count = num;
383 }
384
385 public void AddRange(IEnumerable<T> items)
386 {
387 Requires.NotNull(items, "items");
388 if (items.TryGetCount(out var count))
389 {
391 if (items.TryCopyTo(_elements, _count))
392 {
393 _count += count;
394 return;
395 }
396 }
397 foreach (T item in items)
398 {
399 Add(item);
400 }
401 }
402
403 public void AddRange(params T[] items)
404 {
405 Requires.NotNull(items, "items");
406 int count = Count;
407 Count += items.Length;
408 Array.Copy(items, 0, _elements, count, items.Length);
409 }
410
411 public void AddRange<TDerived>(TDerived[] items) where TDerived : T
412 {
413 Requires.NotNull(items, "items");
414 int count = Count;
415 Count += items.Length;
416 Array.Copy(items, 0, _elements, count, items.Length);
417 }
418
419 public void AddRange(T[] items, int length)
420 {
421 Requires.NotNull(items, "items");
422 Requires.Range(length >= 0 && length <= items.Length, "length");
423 int count = Count;
424 Count += length;
425 Array.Copy(items, 0, _elements, count, length);
426 }
427
428 public void AddRange(ImmutableArray<T> items)
429 {
430 AddRange(items, items.Length);
431 }
432
433 public void AddRange(ImmutableArray<T> items, int length)
434 {
435 Requires.Range(length >= 0, "length");
436 if (items.array != null)
437 {
438 AddRange(items.array, length);
439 }
440 }
441
443 {
444 if (items.array != null)
445 {
446 this.AddRange<TDerived>(items.array);
447 }
448 }
449
450 public void AddRange(Builder items)
451 {
452 Requires.NotNull(items, "items");
453 AddRange(items._elements, items.Count);
454 }
455
457 {
458 Requires.NotNull(items, "items");
459 AddRange<TDerived>(items._elements, items.Count);
460 }
461
462 public bool Remove(T element)
463 {
464 int num = IndexOf(element);
465 if (num >= 0)
466 {
467 RemoveAt(num);
468 return true;
469 }
470 return false;
471 }
472
473 public void RemoveAt(int index)
474 {
475 Requires.Range(index >= 0 && index < Count, "index");
476 if (index < Count - 1)
477 {
479 }
480 Count--;
481 }
482
483 public bool Contains(T item)
484 {
485 return IndexOf(item) >= 0;
486 }
487
488 public T[] ToArray()
489 {
490 if (Count == 0)
491 {
492 return ImmutableArray<T>.Empty.array;
493 }
494 T[] array = new T[Count];
496 return array;
497 }
498
499 public void CopyTo(T[] array, int index)
500 {
501 Requires.NotNull(array, "array");
502 Requires.Range(index >= 0 && index + Count <= array.Length, "index");
504 }
505
506 private void EnsureCapacity(int capacity)
507 {
508 if (_elements.Length < capacity)
509 {
510 int newSize = Math.Max(_elements.Length * 2, capacity);
511 Array.Resize(ref _elements, newSize);
512 }
513 }
514
515 public int IndexOf(T item)
516 {
518 }
519
520 public int IndexOf(T item, int startIndex)
521 {
523 }
524
525 public int IndexOf(T item, int startIndex, int count)
526 {
528 }
529
531 {
532 if (count == 0 && startIndex == 0)
533 {
534 return -1;
535 }
536 Requires.Range(startIndex >= 0 && startIndex < Count, "startIndex");
537 Requires.Range(count >= 0 && startIndex + count <= Count, "count");
540 {
542 }
543 for (int i = startIndex; i < startIndex + count; i++)
544 {
545 if (equalityComparer.Equals(_elements[i], item))
546 {
547 return i;
548 }
549 }
550 return -1;
551 }
552
553 public int LastIndexOf(T item)
554 {
555 if (Count == 0)
556 {
557 return -1;
558 }
560 }
561
562 public int LastIndexOf(T item, int startIndex)
563 {
564 if (Count == 0 && startIndex == 0)
565 {
566 return -1;
567 }
568 Requires.Range(startIndex >= 0 && startIndex < Count, "startIndex");
570 }
571
572 public int LastIndexOf(T item, int startIndex, int count)
573 {
575 }
576
578 {
579 if (count == 0 && startIndex == 0)
580 {
581 return -1;
582 }
583 Requires.Range(startIndex >= 0 && startIndex < Count, "startIndex");
584 Requires.Range(count >= 0 && startIndex - count + 1 >= 0, "count");
587 {
589 }
590 for (int num = startIndex; num >= startIndex - count + 1; num--)
591 {
592 if (equalityComparer.Equals(item, _elements[num]))
593 {
594 return num;
595 }
596 }
597 return -1;
598 }
599
600 public void Reverse()
601 {
602 int num = 0;
603 int num2 = _count - 1;
604 T[] elements = _elements;
605 while (num < num2)
606 {
607 T val = elements[num];
608 elements[num] = elements[num2];
609 elements[num2] = val;
610 num++;
611 num2--;
612 }
613 }
614
615 public void Sort()
616 {
617 if (Count > 1)
618 {
620 }
621 }
622
624 {
625 Requires.NotNull(comparison, "comparison");
626 if (Count > 1)
627 {
629 }
630 }
631
633 {
634 if (Count > 1)
635 {
637 }
638 }
639
640 public void Sort(int index, int count, IComparer<T>? comparer)
641 {
642 Requires.Range(index >= 0, "index");
643 Requires.Range(count >= 0 && index + count <= Count, "count");
644 if (count > 1)
645 {
647 }
648 }
649
651 {
652 for (int i = 0; i < Count; i++)
653 {
654 yield return this[i];
655 }
656 }
657
662
667
668 private void AddRange<TDerived>(TDerived[] items, int length) where TDerived : T
669 {
671 int count = Count;
672 Count += length;
673 T[] elements = _elements;
674 for (int i = 0; i < length; i++)
675 {
676 elements[count + i] = (T)(object)items[i];
677 }
678 }
679 }
680
681 public struct Enumerator
682 {
683 private readonly T[] _array;
684
685 private int _index;
686
687 public T Current => _array[_index];
688
689 internal Enumerator(T[] array)
690 {
691 _array = array;
692 _index = -1;
693 }
694
695 public bool MoveNext()
696 {
697 return ++_index < _array.Length;
698 }
699 }
700
702 {
704
705 private readonly T[] _array;
706
707 private int _index;
708
709 public T Current
710 {
711 get
712 {
713 if ((uint)_index < (uint)_array.Length)
714 {
715 return _array[_index];
716 }
717 throw new InvalidOperationException();
718 }
719 }
720
721 object IEnumerator.Current => Current;
722
724 {
725 _index = -1;
726 _array = array;
727 }
728
729 public bool MoveNext()
730 {
731 int num = _index + 1;
732 int num2 = _array.Length;
733 if ((uint)num <= (uint)num2)
734 {
735 _index = num;
736 return (uint)num < (uint)num2;
737 }
738 return false;
739 }
740
742 {
743 _index = -1;
744 }
745
746 public void Dispose()
747 {
748 }
749
750 internal static IEnumerator<T> Create(T[] array)
751 {
752 if (array.Length != 0)
753 {
754 return new EnumeratorObject(array);
755 }
756 return s_EmptyEnumerator;
757 }
758 }
759
760 public static readonly ImmutableArray<T> Empty = new ImmutableArray<T>(new T[0]);
761
763 internal readonly T[]? array;
764
765 T IList<T>.this[int index]
766 {
767 get
768 {
770 immutableArray.ThrowInvalidOperationIfNotInitialized();
771 return immutableArray[index];
772 }
773 set
774 {
775 throw new NotSupportedException();
776 }
777 }
778
781
784 {
785 get
786 {
788 immutableArray.ThrowInvalidOperationIfNotInitialized();
789 return immutableArray.Length;
790 }
791 }
792
795 {
796 get
797 {
799 immutableArray.ThrowInvalidOperationIfNotInitialized();
800 return immutableArray.Length;
801 }
802 }
803
805 {
806 get
807 {
809 immutableArray.ThrowInvalidOperationIfNotInitialized();
810 return immutableArray[index];
811 }
812 }
813
815 bool IList.IsFixedSize => true;
816
818 bool IList.IsReadOnly => true;
819
821 int ICollection.Count
822 {
823 get
824 {
826 immutableArray.ThrowInvalidOperationIfNotInitialized();
827 return immutableArray.Length;
828 }
829 }
830
832 bool ICollection.IsSynchronized => true;
833
835 object ICollection.SyncRoot
836 {
837 get
838 {
839 throw new NotSupportedException();
840 }
841 }
842
843 object? IList.this[int index]
844 {
845 get
846 {
848 immutableArray.ThrowInvalidOperationIfNotInitialized();
849 return immutableArray[index];
850 }
851 set
852 {
853 throw new NotSupportedException();
854 }
855 }
856
857 public T this[int index]
858 {
859 [System.Runtime.Versioning.NonVersionable]
860 get
861 {
862 return array[index];
863 }
864 }
865
867 public bool IsEmpty
868 {
869 [System.Runtime.Versioning.NonVersionable]
870 get
871 {
872 return array.Length == 0;
873 }
874 }
875
877 public int Length
878 {
879 [System.Runtime.Versioning.NonVersionable]
880 get
881 {
882 return array.Length;
883 }
884 }
885
887 public bool IsDefault => array == null;
888
891 {
892 get
893 {
895 if (immutableArray.array != null)
896 {
897 return immutableArray.array.Length == 0;
898 }
899 return true;
900 }
901 }
902
904 Array? IImmutableArray.Array => array;
905
907 private string DebuggerDisplay
908 {
909 get
910 {
912 if (!immutableArray.IsDefault)
913 {
914 return $"Length = {immutableArray.Length}";
915 }
916 return "Uninitialized";
917 }
918 }
919
921 {
922 return new ReadOnlySpan<T>(array);
923 }
924
926 {
927 return new ReadOnlyMemory<T>(array);
928 }
929
930 public int IndexOf(T item)
931 {
933 return immutableArray.IndexOf(item, 0, immutableArray.Length, EqualityComparer<T>.Default);
934 }
935
941
942 public int IndexOf(T item, int startIndex)
943 {
946 }
947
948 public int IndexOf(T item, int startIndex, int count)
949 {
951 }
952
954 {
956 immutableArray.ThrowNullRefIfNotInitialized();
957 if (count == 0 && startIndex == 0)
958 {
959 return -1;
960 }
961 Requires.Range(startIndex >= 0 && startIndex < immutableArray.Length, "startIndex");
962 Requires.Range(count >= 0 && startIndex + count <= immutableArray.Length, "count");
965 {
967 }
968 for (int i = startIndex; i < startIndex + count; i++)
969 {
970 if (equalityComparer.Equals(immutableArray.array[i], item))
971 {
972 return i;
973 }
974 }
975 return -1;
976 }
977
978 public int LastIndexOf(T item)
979 {
981 if (immutableArray.Length == 0)
982 {
983 return -1;
984 }
985 return immutableArray.LastIndexOf(item, immutableArray.Length - 1, immutableArray.Length, EqualityComparer<T>.Default);
986 }
987
988 public int LastIndexOf(T item, int startIndex)
989 {
991 if (immutableArray.Length == 0 && startIndex == 0)
992 {
993 return -1;
994 }
996 }
997
998 public int LastIndexOf(T item, int startIndex, int count)
999 {
1001 }
1002
1004 {
1006 immutableArray.ThrowNullRefIfNotInitialized();
1007 if (startIndex == 0 && count == 0)
1008 {
1009 return -1;
1010 }
1011 Requires.Range(startIndex >= 0 && startIndex < immutableArray.Length, "startIndex");
1012 Requires.Range(count >= 0 && startIndex - count + 1 >= 0, "count");
1015 {
1017 }
1018 for (int num = startIndex; num >= startIndex - count + 1; num--)
1019 {
1020 if (equalityComparer.Equals(item, immutableArray.array[num]))
1021 {
1022 return num;
1023 }
1024 }
1025 return -1;
1026 }
1027
1028 public bool Contains(T item)
1029 {
1030 return IndexOf(item) >= 0;
1031 }
1032
1034 {
1036 immutableArray.ThrowNullRefIfNotInitialized();
1037 Requires.Range(index >= 0 && index <= immutableArray.Length, "index");
1038 if (immutableArray.Length == 0)
1039 {
1040 return ImmutableArray.Create(item);
1041 }
1042 T[] array = new T[immutableArray.Length + 1];
1043 array[index] = item;
1044 if (index != 0)
1045 {
1047 }
1048 if (index != immutableArray.Length)
1049 {
1050 Array.Copy(immutableArray.array, index, array, index + 1, immutableArray.Length - index);
1051 }
1052 return new ImmutableArray<T>(array);
1053 }
1054
1056 {
1057 ImmutableArray<T> result = this;
1058 result.ThrowNullRefIfNotInitialized();
1059 Requires.Range(index >= 0 && index <= result.Length, "index");
1060 Requires.NotNull(items, "items");
1061 if (result.Length == 0)
1062 {
1063 return ImmutableArray.CreateRange(items);
1064 }
1065 int count = ImmutableExtensions.GetCount(ref items);
1066 if (count == 0)
1067 {
1068 return result;
1069 }
1070 T[] array = new T[result.Length + count];
1071 if (index != 0)
1072 {
1073 Array.Copy(result.array, array, index);
1074 }
1075 if (index != result.Length)
1076 {
1077 Array.Copy(result.array, index, array, index + count, result.Length - index);
1078 }
1079 if (!items.TryCopyTo(array, index))
1080 {
1081 int num = index;
1082 foreach (T item in items)
1083 {
1084 array[num++] = item;
1085 }
1086 }
1087 return new ImmutableArray<T>(array);
1088 }
1089
1091 {
1092 ImmutableArray<T> result = this;
1093 result.ThrowNullRefIfNotInitialized();
1094 items.ThrowNullRefIfNotInitialized();
1095 Requires.Range(index >= 0 && index <= result.Length, "index");
1096 if (result.IsEmpty)
1097 {
1098 return items;
1099 }
1100 if (items.IsEmpty)
1101 {
1102 return result;
1103 }
1104 T[] array = new T[result.Length + items.Length];
1105 if (index != 0)
1106 {
1107 Array.Copy(result.array, array, index);
1108 }
1109 if (index != result.Length)
1110 {
1111 Array.Copy(result.array, index, array, index + items.Length, result.Length - index);
1112 }
1113 Array.Copy(items.array, 0, array, index, items.Length);
1114 return new ImmutableArray<T>(array);
1115 }
1116
1118 {
1120 if (immutableArray.Length == 0)
1121 {
1122 return ImmutableArray.Create(item);
1123 }
1124 return immutableArray.Insert(immutableArray.Length, item);
1125 }
1126
1128 {
1130 return immutableArray.InsertRange(immutableArray.Length, items);
1131 }
1132
1134 {
1136 return immutableArray.InsertRange(immutableArray.Length, items);
1137 }
1138
1140 {
1142 immutableArray.ThrowNullRefIfNotInitialized();
1143 Requires.Range(index >= 0 && index < immutableArray.Length, "index");
1144 T[] array = new T[immutableArray.Length];
1146 array[index] = item;
1147 return new ImmutableArray<T>(array);
1148 }
1149
1154
1156 {
1158 int num = immutableArray.IndexOf(oldValue, 0, immutableArray.Length, equalityComparer);
1159 if (num < 0)
1160 {
1161 throw new ArgumentException(System.SR.CannotFindOldValue, "oldValue");
1162 }
1163 return immutableArray.SetItem(num, newValue);
1164 }
1165
1167 {
1169 }
1170
1172 {
1173 ImmutableArray<T> result = this;
1174 result.ThrowNullRefIfNotInitialized();
1175 int num = result.IndexOf(item, 0, result.Length, equalityComparer);
1176 if (num >= 0)
1177 {
1178 return result.RemoveAt(num);
1179 }
1180 return result;
1181 }
1182
1184 {
1185 return RemoveRange(index, 1);
1186 }
1187
1189 {
1190 ImmutableArray<T> result = this;
1191 result.ThrowNullRefIfNotInitialized();
1192 Requires.Range(index >= 0 && index <= result.Length, "index");
1193 Requires.Range(length >= 0 && index + length <= result.Length, "length");
1194 if (length == 0)
1195 {
1196 return result;
1197 }
1198 T[] array = new T[result.Length - length];
1199 Array.Copy(result.array, array, index);
1200 Array.Copy(result.array, index + length, array, index, result.Length - index - length);
1201 return new ImmutableArray<T>(array);
1202 }
1203
1205 {
1207 }
1208
1210 {
1212 immutableArray.ThrowNullRefIfNotInitialized();
1213 Requires.NotNull(items, "items");
1215 foreach (T item in items)
1216 {
1217 int num = immutableArray.IndexOf(item, 0, immutableArray.Length, equalityComparer);
1218 while (num >= 0 && !sortedSet.Add(num) && num + 1 < immutableArray.Length)
1219 {
1220 num = immutableArray.IndexOf(item, num + 1, equalityComparer);
1221 }
1222 }
1223 return immutableArray.RemoveAtRange(sortedSet);
1224 }
1225
1230
1232 {
1233 ImmutableArray<T> result = this;
1234 Requires.NotNull(items.array, "items");
1235 if (items.IsEmpty)
1236 {
1237 result.ThrowNullRefIfNotInitialized();
1238 return result;
1239 }
1240 if (items.Length == 1)
1241 {
1242 return result.Remove(items[0], equalityComparer);
1243 }
1244 return result.RemoveRange(items.array, equalityComparer);
1245 }
1246
1248 {
1249 ImmutableArray<T> result = this;
1250 result.ThrowNullRefIfNotInitialized();
1251 Requires.NotNull(match, "match");
1252 if (result.IsEmpty)
1253 {
1254 return result;
1255 }
1256 List<int> list = null;
1257 for (int i = 0; i < result.array.Length; i++)
1258 {
1259 if (match(result.array[i]))
1260 {
1261 if (list == null)
1262 {
1263 list = new List<int>();
1264 }
1265 list.Add(i);
1266 }
1267 }
1268 if (list == null)
1269 {
1270 return result;
1271 }
1272 return result.RemoveAtRange(list);
1273 }
1274
1276 {
1277 return Empty;
1278 }
1279
1281 {
1283 return immutableArray.Sort(0, immutableArray.Length, Comparer<T>.Default);
1284 }
1285
1287 {
1288 Requires.NotNull(comparison, "comparison");
1290 return immutableArray.Sort(Comparer<T>.Create(comparison));
1291 }
1292
1294 {
1296 return immutableArray.Sort(0, immutableArray.Length, comparer);
1297 }
1298
1300 {
1301 ImmutableArray<T> result = this;
1302 result.ThrowNullRefIfNotInitialized();
1303 Requires.Range(index >= 0, "index");
1304 Requires.Range(count >= 0 && index + count <= result.Length, "count");
1305 if (count > 1)
1306 {
1307 if (comparer == null)
1308 {
1309 comparer = Comparer<T>.Default;
1310 }
1311 bool flag = false;
1312 for (int i = index + 1; i < index + count; i++)
1313 {
1314 if (comparer.Compare(result.array[i - 1], result.array[i]) > 0)
1315 {
1316 flag = true;
1317 break;
1318 }
1319 }
1320 if (flag)
1321 {
1322 T[] array = new T[result.Length];
1323 Array.Copy(result.array, array, result.Length);
1325 return new ImmutableArray<T>(array);
1326 }
1327 }
1328 return result;
1329 }
1330
1332 {
1334 if (immutableArray.array == null || immutableArray.array.Length == 0)
1335 {
1336 return Enumerable.Empty<TResult>();
1337 }
1338 return immutableArray.array.OfType<TResult>();
1339 }
1340
1341 void IList<T>.Insert(int index, T item)
1342 {
1343 throw new NotSupportedException();
1344 }
1345
1346 void IList<T>.RemoveAt(int index)
1347 {
1348 throw new NotSupportedException();
1349 }
1350
1352 {
1353 throw new NotSupportedException();
1354 }
1355
1357 {
1358 throw new NotSupportedException();
1359 }
1360
1362 {
1363 throw new NotSupportedException();
1364 }
1365
1367 {
1369 immutableArray.ThrowInvalidOperationIfNotInitialized();
1370 return immutableArray.Clear();
1371 }
1372
1374 {
1376 immutableArray.ThrowInvalidOperationIfNotInitialized();
1377 return immutableArray.Add(value);
1378 }
1379
1381 {
1383 immutableArray.ThrowInvalidOperationIfNotInitialized();
1384 return immutableArray.AddRange(items);
1385 }
1386
1388 {
1390 immutableArray.ThrowInvalidOperationIfNotInitialized();
1391 return immutableArray.Insert(index, element);
1392 }
1393
1395 {
1397 immutableArray.ThrowInvalidOperationIfNotInitialized();
1398 return immutableArray.InsertRange(index, items);
1399 }
1400
1407
1409 {
1411 immutableArray.ThrowInvalidOperationIfNotInitialized();
1412 return immutableArray.RemoveAll(match);
1413 }
1414
1416 {
1418 immutableArray.ThrowInvalidOperationIfNotInitialized();
1419 return immutableArray.RemoveRange(items, equalityComparer);
1420 }
1421
1423 {
1425 immutableArray.ThrowInvalidOperationIfNotInitialized();
1426 return immutableArray.RemoveRange(index, count);
1427 }
1428
1430 {
1432 immutableArray.ThrowInvalidOperationIfNotInitialized();
1433 return immutableArray.RemoveAt(index);
1434 }
1435
1437 {
1439 immutableArray.ThrowInvalidOperationIfNotInitialized();
1440 return immutableArray.SetItem(index, value);
1441 }
1442
1444 {
1446 immutableArray.ThrowInvalidOperationIfNotInitialized();
1448 }
1449
1450 int IList.Add(object value)
1451 {
1452 throw new NotSupportedException();
1453 }
1454
1456 {
1457 throw new NotSupportedException();
1458 }
1459
1460 bool IList.Contains(object value)
1461 {
1463 immutableArray.ThrowInvalidOperationIfNotInitialized();
1464 return immutableArray.Contains((T)value);
1465 }
1466
1467 int IList.IndexOf(object value)
1468 {
1470 immutableArray.ThrowInvalidOperationIfNotInitialized();
1471 return immutableArray.IndexOf((T)value);
1472 }
1473
1474 void IList.Insert(int index, object value)
1475 {
1476 throw new NotSupportedException();
1477 }
1478
1479 void IList.Remove(object value)
1480 {
1481 throw new NotSupportedException();
1482 }
1483
1485 {
1486 throw new NotSupportedException();
1487 }
1488
1490 {
1492 immutableArray.ThrowInvalidOperationIfNotInitialized();
1493 Array.Copy(immutableArray.array, 0, array, index, immutableArray.Length);
1494 }
1495
1497 {
1501 {
1502 array = immutableArray2.Array;
1503 if (immutableArray.array == null && array == null)
1504 {
1505 return true;
1506 }
1507 if (immutableArray.array == null)
1508 {
1509 return false;
1510 }
1511 }
1513 return structuralEquatable.Equals(array, comparer);
1514 }
1515
1521
1523 {
1527 {
1528 array = immutableArray2.Array;
1529 if (immutableArray.array == null && array == null)
1530 {
1531 return 0;
1532 }
1533 if ((immutableArray.array == null) ^ (array == null))
1534 {
1536 }
1537 }
1538 if (array != null)
1539 {
1541 if (structuralComparable == null)
1542 {
1544 }
1545 return structuralComparable.CompareTo(array, comparer);
1546 }
1547 throw new ArgumentException(System.SR.ArrayLengthsNotEqual, "other");
1548 }
1549
1551 {
1552 ImmutableArray<T> result = this;
1553 result.ThrowNullRefIfNotInitialized();
1554 Requires.NotNull(indicesToRemove, "indicesToRemove");
1555 if (indicesToRemove.Count == 0)
1556 {
1557 return result;
1558 }
1559 T[] array = new T[result.Length - indicesToRemove.Count];
1560 int num = 0;
1561 int num2 = 0;
1562 int num3 = -1;
1563 foreach (int item in indicesToRemove)
1564 {
1565 int num4 = ((num3 == -1) ? item : (item - num3 - 1));
1566 Array.Copy(result.array, num + num2, array, num, num4);
1567 num2++;
1568 num += num4;
1569 num3 = item;
1570 }
1571 Array.Copy(result.array, num + num2, array, num, result.Length - (num + num2));
1572 return new ImmutableArray<T>(array);
1573 }
1574
1575 internal ImmutableArray(T[]? items)
1576 {
1577 array = items;
1578 }
1579
1580 [System.Runtime.Versioning.NonVersionable]
1581 public static bool operator ==(ImmutableArray<T> left, ImmutableArray<T> right)
1582 {
1583 return left.Equals(right);
1584 }
1585
1586 [System.Runtime.Versioning.NonVersionable]
1587 public static bool operator !=(ImmutableArray<T> left, ImmutableArray<T> right)
1588 {
1589 return !left.Equals(right);
1590 }
1591
1592 public static bool operator ==(ImmutableArray<T>? left, ImmutableArray<T>? right)
1593 {
1594 return left.GetValueOrDefault().Equals(right.GetValueOrDefault());
1595 }
1596
1597 public static bool operator !=(ImmutableArray<T>? left, ImmutableArray<T>? right)
1598 {
1599 return !left.GetValueOrDefault().Equals(right.GetValueOrDefault());
1600 }
1601
1602 public ref readonly T ItemRef(int index)
1603 {
1604 return ref array[index];
1605 }
1606
1607 public void CopyTo(T[] destination)
1608 {
1610 immutableArray.ThrowNullRefIfNotInitialized();
1612 }
1613
1615 {
1617 immutableArray.ThrowNullRefIfNotInitialized();
1619 }
1620
1621 public void CopyTo(int sourceIndex, T[] destination, int destinationIndex, int length)
1622 {
1624 immutableArray.ThrowNullRefIfNotInitialized();
1626 }
1627
1629 {
1630 ImmutableArray<T> items = this;
1631 if (items.Length == 0)
1632 {
1633 return new Builder();
1634 }
1635 Builder builder = new Builder(items.Length);
1636 builder.AddRange(items);
1637 return builder;
1638 }
1639
1640 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1642 {
1644 immutableArray.ThrowNullRefIfNotInitialized();
1645 return new Enumerator(immutableArray.array);
1646 }
1647
1648 public override int GetHashCode()
1649 {
1651 if (immutableArray.array != null)
1652 {
1653 return immutableArray.array.GetHashCode();
1654 }
1655 return 0;
1656 }
1657
1658 public override bool Equals([NotNullWhen(true)] object? obj)
1659 {
1661 {
1662 return array == immutableArray.Array;
1663 }
1664 return false;
1665 }
1666
1667 [System.Runtime.Versioning.NonVersionable]
1669 {
1670 return array == other.array;
1671 }
1672
1674 {
1675 T[] items2 = (T[])(object)items.array;
1676 return new ImmutableArray<T>(items2);
1677 }
1678
1680 {
1681 return new ImmutableArray<TOther>((TOther[])(object)array);
1682 }
1683
1688
1690 {
1692 immutableArray.ThrowInvalidOperationIfNotInitialized();
1694 }
1695
1697 {
1699 immutableArray.ThrowInvalidOperationIfNotInitialized();
1701 }
1702
1704 {
1705 _ = array.Length;
1706 }
1707
1709 {
1710 if (IsDefault)
1711 {
1713 }
1714 }
1715}
static void Sort(Array array)
Definition Array.cs:2329
int IList. IndexOf(object value)
Definition Array.cs:1228
static int LastIndexOf(Array array, object? value)
Definition Array.cs:1995
static unsafe void Clear(Array array)
Definition Array.cs:755
static int BinarySearch(Array array, object? value)
Definition Array.cs:1320
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
void AddRange(IEnumerable< KeyValuePair< TKey, TValue > > collection)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
int LastIndexOf(T item, int startIndex, int count, IEqualityComparer< T >? equalityComparer)
void AddRange(ImmutableArray< T > items, int length)
void Sort(int index, int count, IComparer< T >? comparer)
int IndexOf(T item, int startIndex, int count)
int IndexOf(T item, int startIndex, int count, IEqualityComparer< T >? equalityComparer)
int LastIndexOf(T item, int startIndex, int count)
static ImmutableArray< TResult > CreateRange< TSource, TResult >(ImmutableArray< TSource > items, Func< TSource, TResult > selector)
ImmutableArray< T > SetItem(int index, T item)
void CopyTo(int sourceIndex, T[] destination, int destinationIndex, int length)
ImmutableArray< T > Sort(Comparison< T > comparison)
ImmutableArray< TOther > CastArray< TOther >()
ImmutableArray< T > RemoveRange(IEnumerable< T > items, IEqualityComparer< T >? equalityComparer)
static ImmutableArray< TSource > ToImmutableArray< TSource >(this IEnumerable< TSource > items)
static int BinarySearch< T >(this ImmutableArray< T > array, T value)
ImmutableArray< T > RemoveRange(ImmutableArray< T > items)
int LastIndexOf(T item, int startIndex, int count)
override bool Equals([NotNullWhen(true)] object? obj)
static ImmutableArray< T >.Builder CreateBuilder< T >()
ImmutableArray< T > RemoveRange(IEnumerable< T > items)
static ImmutableArray< T > CastUp< TDerived >(ImmutableArray< TDerived > items)
static bool operator==(ImmutableArray< T > left, ImmutableArray< T > right)
static ImmutableArray< T > CreateRange< T >(IEnumerable< T > items)
ImmutableArray< T > Sort(IComparer< T >? comparer)
ImmutableArray< T > RemoveAt(int index)
ImmutableArray< T > AddRange(ImmutableArray< T > items)
static ImmutableArray< TResult > CreateRange< TSource, TArg, TResult >(ImmutableArray< TSource > items, Func< TSource, TArg, TResult > selector, TArg arg)
int LastIndexOf(T item, int startIndex, int count, IEqualityComparer< T >? equalityComparer)
int IndexOf(T item, int startIndex, IEqualityComparer< T >? equalityComparer)
ImmutableArray< T > Replace(T oldValue, T newValue)
int IndexOf(T item, int startIndex, int count, IEqualityComparer< T >? equalityComparer)
int IndexOf(T item, int startIndex, int count)
ImmutableArray< T > RemoveRange(ImmutableArray< T > items, IEqualityComparer< T >? equalityComparer)
ImmutableArray< T > InsertRange(int index, IEnumerable< T > items)
ImmutableArray< T > RemoveRange(int index, int length)
void CopyTo(T[] destination, int destinationIndex)
ImmutableArray< T > InsertRange(int index, ImmutableArray< T > items)
ImmutableArray< T > Replace(T oldValue, T newValue, IEqualityComparer< T >? equalityComparer)
ImmutableArray< T > Remove(T item, IEqualityComparer< T >? equalityComparer)
static bool operator!=(ImmutableArray< T > left, ImmutableArray< T > right)
static readonly ImmutableArray< T > Empty
ImmutableArray< T >.Builder ToBuilder()
ImmutableArray< T > AddRange(IEnumerable< T > items)
ImmutableArray< T > RemoveAll(Predicate< T > match)
ImmutableArray< T > Sort(int index, int count, IComparer< T >? comparer)
ImmutableArray< T > Insert(int index, T item)
static ImmutableArray< T > Create< T >()
ImmutableArray< T > RemoveAtRange(ICollection< int > indicesToRemove)
bool Equals(ImmutableArray< 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 InvalidOperationOnDefaultArray
Definition SR.cs:32
static string CapacityMustEqualCountOnMove
Definition SR.cs:24
static string ArrayLengthsNotEqual
Definition SR.cs:18
static string CapacityMustBeGreaterThanOrEqualToCount
Definition SR.cs:22
static string ArrayInitializedStateNotEqual
Definition SR.cs:16
static string CannotFindOldValue
Definition SR.cs:20
Definition SR.cs:7
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)
int CompareTo(object? other, IComparer comparer)
int GetHashCode(IEqualityComparer comparer)
bool Equals(object? other, IEqualityComparer comparer)