This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ArrayList.cs
3031 lines (2635 loc) · 107 KB
/
ArrayList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*============================================================
**
** Class: ArrayList
**
** Purpose: Implements a dynamically sized List as an array,
** and provides many convenience methods for treating
** an array as an IList.
**
===========================================================*/
using System.Security;
using System.Diagnostics;
#if FEATURE_NETCORE
using System.Runtime.CompilerServices;
#endif
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
namespace System.Collections
{
// Implements a variable-size List that uses an array of objects to store the
// elements. A ArrayList has a capacity, which is the allocated length
// of the internal array. As elements are added to a ArrayList, the capacity
// of the ArrayList is automatically increased as required by reallocating the
// internal array.
//
#if FEATURE_NETCORE
[FriendAccessAllowed]
#endif
[DebuggerTypeProxy(typeof(System.Collections.ArrayList.ArrayListDebugView))]
[DebuggerDisplay("Count = {Count}")]
public class ArrayList : IList
{
private Object[] _items;
[ContractPublicPropertyName("Count")]
private int _size;
private int _version;
private Object _syncRoot;
private const int _defaultCapacity = 4;
// Copy of Array.MaxArrayLength
internal const int MaxArrayLength = 0X7FEFFFFF;
// Note: this constructor is a bogus constructor that does nothing
// and is for use only with SyncArrayList.
internal ArrayList(bool trash)
{
}
// Constructs a ArrayList. The list is initially empty and has a capacity
// of zero. Upon adding the first element to the list the capacity is
// increased to _defaultCapacity, and then increased in multiples of two as required.
public ArrayList()
{
_items = Array.Empty<Object>();
}
// Constructs a ArrayList with a given initial capacity. The list is
// initially empty, but will have room for the given number of elements
// before any reallocations are required.
//
public ArrayList(int capacity)
{
if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity), SR.Format(SR.ArgumentOutOfRange_MustBeNonNegNum, nameof(capacity)));
Contract.EndContractBlock();
if (capacity == 0)
_items = Array.Empty<Object>();
else
_items = new Object[capacity];
}
// Constructs a ArrayList, copying the contents of the given collection. The
// size and capacity of the new list will both be equal to the size of the
// given collection.
//
public ArrayList(ICollection c)
{
if (c == null)
throw new ArgumentNullException(nameof(c), SR.ArgumentNull_Collection);
Contract.EndContractBlock();
int count = c.Count;
if (count == 0)
{
_items = Array.Empty<Object>();
}
else
{
_items = new Object[count];
AddRange(c);
}
}
// Gets and sets the capacity of this list. The capacity is the size of
// the internal array used to hold items. When set, the internal
// array of the list is reallocated to the given capacity.
//
public virtual int Capacity
{
get
{
Contract.Ensures(Contract.Result<int>() >= Count);
return _items.Length;
}
set
{
if (value < _size)
{
throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity);
}
Contract.Ensures(Capacity >= 0);
Contract.EndContractBlock();
// We don't want to update the version number when we change the capacity.
// Some existing applications have dependency on this.
if (value != _items.Length)
{
if (value > 0)
{
Object[] newItems = new Object[value];
if (_size > 0)
{
Array.Copy(_items, 0, newItems, 0, _size);
}
_items = newItems;
}
else
{
_items = new Object[_defaultCapacity];
}
}
}
}
// Read-only property describing how many elements are in the List.
public virtual int Count
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
return _size;
}
}
public virtual bool IsFixedSize
{
get { return false; }
}
// Is this ArrayList read-only?
public virtual bool IsReadOnly
{
get { return false; }
}
// Is this ArrayList synchronized (thread-safe)?
public virtual bool IsSynchronized
{
get { return false; }
}
// Synchronization root for this object.
public virtual Object SyncRoot
{
get
{
if (_syncRoot == null)
{
System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
}
return _syncRoot;
}
}
// Sets or Gets the element at the given index.
//
public virtual Object this[int index]
{
get
{
if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
Contract.EndContractBlock();
return _items[index];
}
set
{
if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
Contract.EndContractBlock();
_items[index] = value;
_version++;
}
}
// Creates a ArrayList wrapper for a particular IList. This does not
// copy the contents of the IList, but only wraps the IList. So any
// changes to the underlying list will affect the ArrayList. This would
// be useful if you want to Reverse a subrange of an IList, or want to
// use a generic BinarySearch or Sort method without implementing one yourself.
// However, since these methods are generic, the performance may not be
// nearly as good for some operations as they would be on the IList itself.
//
public static ArrayList Adapter(IList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new IListWrapper(list);
}
// Adds the given object to the end of this list. The size of the list is
// increased by one. If required, the capacity of the list is doubled
// before adding the new element.
//
public virtual int Add(Object value)
{
Contract.Ensures(Contract.Result<int>() >= 0);
if (_size == _items.Length) EnsureCapacity(_size + 1);
_items[_size] = value;
_version++;
return _size++;
}
// Adds the elements of the given collection to the end of this list. If
// required, the capacity of the list is increased to twice the previous
// capacity or the new size, whichever is larger.
//
public virtual void AddRange(ICollection c)
{
InsertRange(_size, c);
}
// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.Ensures(Contract.Result<int>() < Count);
Contract.Ensures(Contract.Result<int>() < index + count);
Contract.EndContractBlock();
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
public virtual int BinarySearch(Object value)
{
Contract.Ensures(Contract.Result<int>() < Count);
return BinarySearch(0, Count, value, null);
}
public virtual int BinarySearch(Object value, IComparer comparer)
{
Contract.Ensures(Contract.Result<int>() < Count);
return BinarySearch(0, Count, value, comparer);
}
// Clears the contents of ArrayList.
public virtual void Clear()
{
if (_size > 0)
{
Array.Clear(_items, 0, _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.
_size = 0;
}
_version++;
}
// Clones this ArrayList, doing a shallow copy. (A copy is made of all
// Object references in the ArrayList, but the Objects pointed to
// are not cloned).
public virtual Object Clone()
{
Contract.Ensures(Contract.Result<Object>() != null);
ArrayList la = new ArrayList(_size);
la._size = _size;
la._version = _version;
Array.Copy(_items, 0, la._items, 0, _size);
return la;
}
// Contains returns true if the specified element is in the ArrayList.
// It does a linear, O(n) search. Equality is determined by calling
// item.Equals().
//
public virtual bool Contains(Object item)
{
if (item == null)
{
for (int i = 0; i < _size; i++)
if (_items[i] == null)
return true;
return false;
}
else
{
for (int i = 0; i < _size; i++)
if ((_items[i] != null) && (_items[i].Equals(item)))
return true;
return false;
}
}
// Copies this ArrayList into array, which must be of a
// compatible array type.
//
public virtual void CopyTo(Array array)
{
CopyTo(array, 0);
}
// Copies this ArrayList into array, which must be of a
// compatible array type.
//
public virtual void CopyTo(Array array, int arrayIndex)
{
if ((array != null) && (array.Rank != 1))
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
Contract.EndContractBlock();
// Delegate rest of error checking to Array.Copy.
Array.Copy(_items, 0, array, arrayIndex, _size);
}
// Copies a section of this list to the given array at the given index.
//
// The method uses the Array.Copy method to copy the elements.
//
public virtual void CopyTo(int index, Array array, int arrayIndex, int count)
{
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
if ((array != null) && (array.Rank != 1))
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
Contract.EndContractBlock();
// Delegate rest of error checking to Array.Copy.
Array.Copy(_items, index, array, arrayIndex, count);
}
// Ensures that the capacity of this list is at least the given minimum
// value. If the current capacity of the list is less than min, the
// capacity is increased to twice the current capacity or to min,
// whichever is larger.
private void EnsureCapacity(int min)
{
if (_items.Length < min)
{
int newCapacity = _items.Length == 0 ? _defaultCapacity : _items.Length * 2;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > MaxArrayLength) newCapacity = MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
// Returns a list wrapper that is fixed at the current size. Operations
// that add or remove items will fail, however, replacing items is allowed.
//
public static IList FixedSize(IList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<IList>() != null);
Contract.EndContractBlock();
return new FixedSizeList(list);
}
// Returns a list wrapper that is fixed at the current size. Operations
// that add or remove items will fail, however, replacing items is allowed.
//
public static ArrayList FixedSize(ArrayList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new FixedSizeArrayList(list);
}
// Returns an enumerator for this list with the given
// permission for removal of elements. If modifications made to the list
// while an enumeration is in progress, the MoveNext and
// GetObject methods of the enumerator will throw an exception.
//
public virtual IEnumerator GetEnumerator()
{
Contract.Ensures(Contract.Result<IEnumerator>() != null);
return new ArrayListEnumeratorSimple(this);
}
// Returns an enumerator for a section of this list with the given
// permission for removal of elements. If modifications made to the list
// while an enumeration is in progress, the MoveNext and
// GetObject methods of the enumerator will throw an exception.
//
public virtual IEnumerator GetEnumerator(int index, int count)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.Ensures(Contract.Result<IEnumerator>() != null);
Contract.EndContractBlock();
return new ArrayListEnumerator(this, index, count);
}
// Returns the index of the first occurrence of a given value in a range of
// this list. The list is searched forwards from beginning to end.
// The elements of the list are compared to the given value using the
// Object.Equals method.
//
// This method uses the Array.IndexOf method to perform the
// search.
//
public virtual int IndexOf(Object value)
{
Contract.Ensures(Contract.Result<int>() < Count);
return Array.IndexOf((Array)_items, value, 0, _size);
}
// Returns the index of the first occurrence of a given value in a range of
// this list. The list is searched forwards, starting at index
// startIndex and ending at count number of elements. The
// elements of the list are compared to the given value using the
// Object.Equals method.
//
// This method uses the Array.IndexOf method to perform the
// search.
//
public virtual int IndexOf(Object value, int startIndex)
{
if (startIndex > _size)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
Contract.Ensures(Contract.Result<int>() < Count);
Contract.EndContractBlock();
return Array.IndexOf((Array)_items, value, startIndex, _size - startIndex);
}
// Returns the index of the first occurrence of a given value in a range of
// this list. The list is searched forwards, starting at index
// startIndex and upto count number of elements. The
// elements of the list are compared to the given value using the
// Object.Equals method.
//
// This method uses the Array.IndexOf method to perform the
// search.
//
public virtual int IndexOf(Object value, int startIndex, int count)
{
if (startIndex > _size)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
if (count < 0 || startIndex > _size - count) throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_Count);
Contract.Ensures(Contract.Result<int>() < Count);
Contract.EndContractBlock();
return Array.IndexOf((Array)_items, value, startIndex, count);
}
// Inserts an element into this list at a given index. The size of the list
// is increased by one. If required, the capacity of the list is doubled
// before inserting the new element.
//
public virtual void Insert(int index, Object value)
{
// Note that insertions at the end are legal.
if (index < 0 || index > _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_ArrayListInsert);
//Contract.Ensures(Count == Contract.OldValue(Count) + 1);
Contract.EndContractBlock();
if (_size == _items.Length) EnsureCapacity(_size + 1);
if (index < _size)
{
Array.Copy(_items, index, _items, index + 1, _size - index);
}
_items[index] = value;
_size++;
_version++;
}
// Inserts the elements of the given collection at a given index. If
// required, the capacity of the list is increased to twice the previous
// capacity or the new size, whichever is larger. Ranges may be added
// to the end of the list by setting index to the ArrayList's size.
//
public virtual void InsertRange(int index, ICollection c)
{
if (c == null)
throw new ArgumentNullException(nameof(c), SR.ArgumentNull_Collection);
if (index < 0 || index > _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
//Contract.Ensures(Count == Contract.OldValue(Count) + c.Count);
Contract.EndContractBlock();
int count = c.Count;
if (count > 0)
{
EnsureCapacity(_size + count);
// shift existing items
if (index < _size)
{
Array.Copy(_items, index, _items, index + count, _size - index);
}
Object[] itemsToInsert = new Object[count];
c.CopyTo(itemsToInsert, 0);
itemsToInsert.CopyTo(_items, index);
_size += count;
_version++;
}
}
// Returns the index of the last occurrence of a given value in a range of
// this list. The list is searched backwards, starting at the end
// and ending at the first element in the list. The elements of the list
// are compared to the given value using the Object.Equals method.
//
// This method uses the Array.LastIndexOf method to perform the
// search.
//
public virtual int LastIndexOf(Object value)
{
Contract.Ensures(Contract.Result<int>() < _size);
return LastIndexOf(value, _size - 1, _size);
}
// Returns the index of the last occurrence of a given value in a range of
// this list. The list is searched backwards, starting at index
// startIndex and ending at the first element in the list. The
// elements of the list are compared to the given value using the
// Object.Equals method.
//
// This method uses the Array.LastIndexOf method to perform the
// search.
//
public virtual int LastIndexOf(Object value, int startIndex)
{
if (startIndex >= _size)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
Contract.Ensures(Contract.Result<int>() < Count);
Contract.EndContractBlock();
return LastIndexOf(value, startIndex, startIndex + 1);
}
// Returns the index of the last occurrence of a given value in a range of
// this list. The list is searched backwards, starting at index
// startIndex and upto count elements. The elements of
// the list are compared to the given value using the Object.Equals
// method.
//
// This method uses the Array.LastIndexOf method to perform the
// search.
//
public virtual int LastIndexOf(Object value, int startIndex, int count)
{
if (Count != 0 && (startIndex < 0 || count < 0))
throw new ArgumentOutOfRangeException(startIndex < 0 ? nameof(startIndex) : nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
Contract.Ensures(Contract.Result<int>() < Count);
Contract.EndContractBlock();
if (_size == 0) // Special case for an empty list
return -1;
if (startIndex >= _size || count > startIndex + 1)
throw new ArgumentOutOfRangeException(startIndex >= _size ? nameof(startIndex) : nameof(count), SR.ArgumentOutOfRange_BiggerThanCollection);
return Array.LastIndexOf((Array)_items, value, startIndex, count);
}
// Returns a read-only IList wrapper for the given IList.
//
#if FEATURE_NETCORE
[FriendAccessAllowed]
#endif
public static IList ReadOnly(IList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<IList>() != null);
Contract.EndContractBlock();
return new ReadOnlyList(list);
}
// Returns a read-only ArrayList wrapper for the given ArrayList.
//
public static ArrayList ReadOnly(ArrayList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new ReadOnlyArrayList(list);
}
// Removes the element at the given index. The size of the list is
// decreased by one.
//
public virtual void Remove(Object obj)
{
Contract.Ensures(Count >= 0);
int index = IndexOf(obj);
Debug.Assert(index >= 0 || !(obj is Int32), "You passed an Int32 to Remove that wasn't in the ArrayList." + Environment.NewLine + "Did you mean RemoveAt? int: " + obj + " Count: " + Count);
if (index >= 0)
RemoveAt(index);
}
// Removes the element at the given index. The size of the list is
// decreased by one.
//
public virtual void RemoveAt(int index)
{
if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
Contract.Ensures(Count >= 0);
//Contract.Ensures(Count == Contract.OldValue(Count) - 1);
Contract.EndContractBlock();
_size--;
if (index < _size)
{
Array.Copy(_items, index + 1, _items, index, _size - index);
}
_items[_size] = null;
_version++;
}
// Removes a range of elements from this list.
//
public virtual void RemoveRange(int index, int count)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.Ensures(Count >= 0);
//Contract.Ensures(Count == Contract.OldValue(Count) - count);
Contract.EndContractBlock();
if (count > 0)
{
int i = _size;
_size -= count;
if (index < _size)
{
Array.Copy(_items, index + count, _items, index, _size - index);
}
while (i > _size) _items[--i] = null;
_version++;
}
}
// Returns an IList that contains count copies of value.
//
public static ArrayList Repeat(Object value, int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
ArrayList list = new ArrayList((count > _defaultCapacity) ? count : _defaultCapacity);
for (int i = 0; i < count; i++)
list.Add(value);
return list;
}
// Reverses the elements in this list.
public virtual void Reverse()
{
Reverse(0, Count);
}
// Reverses the elements in a range of this list. Following a call to this
// method, an element in the range given by index and count
// which was previously located at index i will now be located at
// index index + (index + count - i - 1).
//
// This method uses the Array.Reverse method to reverse the
// elements.
//
public virtual void Reverse(int index, int count)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.EndContractBlock();
Array.Reverse(_items, index, count);
_version++;
}
// Sets the elements starting at the given index to the elements of the
// given collection.
//
public virtual void SetRange(int index, ICollection c)
{
if (c == null) throw new ArgumentNullException(nameof(c), SR.ArgumentNull_Collection);
Contract.EndContractBlock();
int count = c.Count;
if (index < 0 || index > _size - count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
if (count > 0)
{
c.CopyTo(_items, index);
_version++;
}
}
public virtual ArrayList GetRange(int index, int count)
{
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException(index < 0 ? nameof(index) : nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new Range(this, index, count);
}
// Sorts the elements in this list. Uses the default comparer and
// Array.Sort.
public virtual void Sort()
{
Sort(0, Count, Comparer.Default);
}
// Sorts the elements in this list. Uses Array.Sort with the
// provided comparer.
public virtual void Sort(IComparer comparer)
{
Sort(0, Count, comparer);
}
// Sorts the elements in a section of this list. The sort compares the
// elements to each other using the given IComparer interface. If
// comparer is null, the elements are compared to each other using
// the IComparable interface, which in that case must be implemented by all
// elements of the list.
//
// This method uses the Array.Sort method to sort the elements.
//
public virtual void Sort(int index, int count, IComparer comparer)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (_size - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.EndContractBlock();
Array.Sort(_items, index, count, comparer);
_version++;
}
// Returns a thread-safe wrapper around an IList.
//
public static IList Synchronized(IList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<IList>() != null);
Contract.EndContractBlock();
return new SyncIList(list);
}
// Returns a thread-safe wrapper around a ArrayList.
//
public static ArrayList Synchronized(ArrayList list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new SyncArrayList(list);
}
// ToArray returns a new Object array containing the contents of the ArrayList.
// This requires copying the ArrayList, which is an O(n) operation.
public virtual Object[] ToArray()
{
Contract.Ensures(Contract.Result<Object[]>() != null);
if (_size == 0)
return Array.Empty<Object>();
Object[] array = new Object[_size];
Array.Copy(_items, 0, array, 0, _size);
return array;
}
// ToArray returns a new array of a particular type containing the contents
// of the ArrayList. This requires copying the ArrayList and potentially
// downcasting all elements. This copy may fail and is an O(n) operation.
// Internally, this implementation calls Array.Copy.
//
[SecuritySafeCritical]
public virtual Array ToArray(Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
Contract.Ensures(Contract.Result<Array>() != null);
Contract.EndContractBlock();
Array array = Array.CreateInstance(type, _size);
Array.Copy(_items, 0, array, 0, _size);
return array;
}
// Sets the capacity of this list to the size of the list. This method can
// be used to minimize a list's memory overhead once it is known that no
// new elements will be added to the list. To completely clear a list and
// release all memory referenced by the list, execute the following
// statements:
//
// list.Clear();
// list.TrimToSize();
//
public virtual void TrimToSize()
{
Capacity = _size;
}
// This class wraps an IList, exposing it as a ArrayList
// Note this requires reimplementing half of ArrayList...
private class IListWrapper : ArrayList
{
private IList _list;
internal IListWrapper(IList list)
{
_list = list;
_version = 0; // list doesn't not contain a version number
}
public override int Capacity
{
get { return _list.Count; }
set
{
if (value < Count) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity);
Contract.EndContractBlock();
}
}
public override int Count
{
get { return _list.Count; }
}
public override bool IsReadOnly
{
get { return _list.IsReadOnly; }
}
public override bool IsFixedSize
{
get { return _list.IsFixedSize; }
}
public override bool IsSynchronized
{
get { return _list.IsSynchronized; }
}
public override Object this[int index]
{
get
{
return _list[index];
}
set
{
_list[index] = value;
_version++;
}
}
public override Object SyncRoot
{
get { return _list.SyncRoot; }
}
public override int Add(Object obj)
{
int i = _list.Add(obj);
_version++;
return i;
}
public override void AddRange(ICollection c)
{
InsertRange(Count, c);
}
// Other overloads with automatically work
public override int BinarySearch(int index, int count, Object value, IComparer comparer)
{
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException(index < 0 ? nameof(index) : nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (Count - index < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);
Contract.EndContractBlock();
if (comparer == null)
comparer = Comparer.Default;
int lo = index;
int hi = index + count - 1;
int mid;
while (lo <= hi)
{
mid = (lo + hi) / 2;
int r = comparer.Compare(value, _list[mid]);
if (r == 0)
return mid;
if (r < 0)
hi = mid - 1;
else
lo = mid + 1;
}
// return bitwise complement of the first element greater than value.
// Since hi is less than lo now, ~lo is the correct item.
return ~lo;
}
public override void Clear()
{
// If _list is an array, it will support Clear method.
// We shouldn't allow clear operation on a FixedSized ArrayList
if (_list.IsFixedSize)
{
throw new NotSupportedException(SR.NotSupported_FixedSizeCollection);
}
_list.Clear();
_version++;
}
public override Object Clone()
{
// This does not do a shallow copy of _list into a ArrayList!
// This clones the IListWrapper, creating another wrapper class!
return new IListWrapper(_list);
}
public override bool Contains(Object obj)
{
return _list.Contains(obj);
}
public override void CopyTo(Array array, int index)
{
_list.CopyTo(array, index);
}
public override void CopyTo(int index, Array array, int arrayIndex, int count)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
if (index < 0 || arrayIndex < 0)
throw new ArgumentOutOfRangeException(index < 0 ? nameof(index) : nameof(arrayIndex), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
if (array.Length - arrayIndex < count)
throw new ArgumentException(SR.Argument_InvalidOffLen);