forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayList.cs
826 lines (678 loc) · 25.5 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
using System;
using System.Linq;
using System.Collections.Generic;
namespace DataStructures.Lists
{
/// <summary>
/// The Array-Based List Data Structure.
/// </summary>
public class ArrayList<T> : IEnumerable<T>
{
/// <summary>
/// Instance variables.
/// </summary>
// This sets the default maximum array length to refer to MAXIMUM_ARRAY_LENGTH_x64
// Set the flag IsMaximumCapacityReached to false
bool DefaultMaxCapacityIsX64 = true;
bool IsMaximumCapacityReached = false;
// The C# Maximum Array Length (before encountering overflow)
// Reference: http://referencesource.microsoft.com/#mscorlib/system/array.cs,2d2b551eabe74985
public const int MAXIMUM_ARRAY_LENGTH_x64 = 0X7FEFFFFF; //x64
public const int MAXIMUM_ARRAY_LENGTH_x86 = 0x8000000; //x86
// This is used as a default empty list initialization.
private readonly T[] _emptyArray = new T[0];
// The default capacity to resize to, when a minimum is lower than 5.
private const int _defaultCapacity = 8;
// The internal array of elements.
// NOT A PROPERTY.
private T[] _collection;
// This keeps track of the number of elements added to the array.
// Serves as an index of last item + 1.
private int _size { get; set; }
/// <summary>
/// CONSTRUCTORS
/// </summary>
public ArrayList() : this(capacity: 0) { }
public ArrayList(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException();
}
if (capacity == 0)
{
_collection = _emptyArray;
}
else
{
_collection = new T[capacity];
}
// Zerofiy the _size;
_size = 0;
}
/// <summary>
/// Ensures the capacity.
/// </summary>
/// <param name="minCapacity">Minimum capacity.</param>
private void _ensureCapacity(int minCapacity)
{
// If the length of the inner collection is less than the minCapacity
// ... and if the maximum capacity wasn't reached yet,
// ... then maximize the inner collection.
if (_collection.Length < minCapacity && IsMaximumCapacityReached == false)
{
int newCapacity = (_collection.Length == 0 ? _defaultCapacity : _collection.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
int maxCapacity = (DefaultMaxCapacityIsX64 == true ? MAXIMUM_ARRAY_LENGTH_x64 : MAXIMUM_ARRAY_LENGTH_x86);
if (newCapacity < minCapacity)
newCapacity = minCapacity;
if (newCapacity >= maxCapacity)
{
newCapacity = maxCapacity - 1;
IsMaximumCapacityReached = true;
}
this._resizeCapacity(newCapacity);
}
}
/// <summary>
/// Resizes the collection to a new maximum number of capacity.
/// </summary>
/// <param name="newCapacity">New capacity.</param>
private void _resizeCapacity(int newCapacity)
{
if (newCapacity != _collection.Length && newCapacity > _size)
{
try
{
Array.Resize<T>(ref _collection, newCapacity);
}
catch (OutOfMemoryException)
{
if (DefaultMaxCapacityIsX64 == true)
{
DefaultMaxCapacityIsX64 = false;
_ensureCapacity(newCapacity);
}
throw;
}
}
}
/// <summary>
/// Gets the the number of elements in list.
/// </summary>
/// <value>Int.</value>
public int Count
{
get
{
return _size;
}
}
/// <summary>
/// Returns the capacity of list, which is the total number of slots.
/// </summary>
public int Capacity
{
get { return _collection.Length; }
}
/// <summary>
/// Determines whether this list is empty.
/// </summary>
/// <returns><c>true</c> if list is empty; otherwise, <c>false</c>.</returns>
public bool IsEmpty
{
get
{
return (Count == 0);
}
}
/// <summary>
/// Gets the first element in the list.
/// </summary>
/// <value>The first.</value>
public T First
{
get
{
if (Count == 0)
{
throw new IndexOutOfRangeException("List is empty.");
}
return _collection[0];
}
}
/// <summary>
/// Gets the last element in the list.
/// </summary>
/// <value>The last.</value>
public T Last
{
get
{
if (IsEmpty)
{
throw new IndexOutOfRangeException("List is empty.");
}
return _collection[Count - 1];
}
}
/// <summary>
/// Gets or sets the item at the specified index.
/// example: var a = list[0];
/// example: list[0] = 1;
/// </summary>
/// <param name="index">Index.</param>
public T this[int index]
{
get
{
if (index < 0 || index >= _size)
{
throw new IndexOutOfRangeException();
}
return _collection[index];
}
set
{
if (index < 0 || index >= _size)
{
throw new IndexOutOfRangeException();
}
_collection[index] = value;
}
}
/// <summary>
/// Add the specified dataItem to list.
/// </summary>
/// <param name="dataItem">Data item.</param>
public void Add(T dataItem)
{
if (_size == _collection.Length)
{
_ensureCapacity(_size + 1);
}
_collection[_size++] = dataItem;
}
/// <summary>
/// Adds an enumerable collection of items to list.
/// </summary>
/// <param name="elements"></param>
public void AddRange(IEnumerable<T> elements)
{
if (elements == null)
throw new ArgumentNullException();
// make sure the size won't overflow by adding the range
if (((uint)_size + elements.Count()) > MAXIMUM_ARRAY_LENGTH_x64)
throw new OverflowException();
// grow the internal collection once to avoid doing multiple redundant grows
if (elements.Any())
{
_ensureCapacity(_size + elements.Count());
foreach (var element in elements)
this.Add(element);
}
}
/// <summary>
/// Adds an element to list repeatedly for a specified count.
/// </summary>
public void AddRepeatedly(T value, int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException();
if (((uint)_size + count) > MAXIMUM_ARRAY_LENGTH_x64)
throw new OverflowException();
// grow the internal collection once to avoid doing multiple redundant grows
if (count > 0)
{
_ensureCapacity(_size + count);
for (int i = 0; i < count; i++)
this.Add(value);
}
}
/// <summary>
/// Inserts a new element at an index. Doesn't override the cell at index.
/// </summary>
/// <param name="dataItem">Data item to insert.</param>
/// <param name="index">Index of insertion.</param>
public void InsertAt(T dataItem, int index)
{
if (index < 0 || index > _size)
{
throw new IndexOutOfRangeException("Please provide a valid index.");
}
// If the inner array is full and there are no extra spaces,
// ... then maximize it's capacity to a minimum of _size + 1.
if (_size == _collection.Length)
{
_ensureCapacity(_size + 1);
}
// If the index is not "at the end", then copy the elements of the array
// ... between the specified index and the last index to the new range (index + 1, _size);
// The cell at "index" will become available.
if (index < _size)
{
Array.Copy(_collection, index, _collection, index + 1, (_size - index));
}
// Write the dataItem to the available cell.
_collection[index] = dataItem;
// Increase the size.
_size++;
}
/// <summary>
/// Removes the specified dataItem from list.
/// </summary>
/// <returns>>True if removed successfully, false otherwise.</returns>
/// <param name="dataItem">Data item.</param>
public bool Remove(T dataItem)
{
int index = IndexOf(dataItem);
if (index >= 0)
{
RemoveAt(index);
return true;
}
return false;
}
/// <summary>
/// Removes the list element at the specified index.
/// </summary>
/// <param name="index">Index of element.</param>
public void RemoveAt(int index)
{
if (index < 0 || index >= _size)
{
throw new IndexOutOfRangeException("Please pass a valid index.");
}
// Decrease the size by 1, to avoid doing Array.Copy if the element is to be removed from the tail of list.
_size--;
// If the index is still less than size, perform an Array.Copy to override the cell at index.
// This operation is O(N), where N = size - index.
if (index < _size)
{
Array.Copy(_collection, index + 1, _collection, index, (_size - index));
}
// Reset the writable cell to the default value of type T.
_collection[_size] = default(T);
}
/// <summary>
/// Clear this instance.
/// </summary>
public void Clear()
{
if (_size > 0)
{
_size = 0;
Array.Clear(_collection, 0, _size);
_collection = _emptyArray;
}
}
/// <summary>
/// Resize the List to a new size.
/// </summary>
public void Resize(int newSize)
{
Resize(newSize, default(T));
}
/// <summary>
/// Resize the list to a new size.
/// </summary>
public void Resize(int newSize, T defaultValue)
{
int currentSize = this.Count;
if (newSize < currentSize)
{
this._ensureCapacity(newSize);
}
else if (newSize > currentSize)
{
// Optimisation step.
// This is just to avoid multiple automatic capacity changes.
if (newSize > this._collection.Length)
this._ensureCapacity(newSize + 1);
this.AddRange(Enumerable.Repeat<T>(defaultValue, newSize - currentSize));
}
}
/// <summary>
/// Reverses this list.
/// </summary>
public void Reverse()
{
Reverse(0, _size);
}
/// <summary>
/// Reverses the order of a number of elements. Starting a specific index.
/// </summary>
/// <param name="startIndex">Start index.</param>
/// <param name="count">Count of elements to reverse.</param>
public void Reverse(int startIndex, int count)
{
// Handle the bounds of startIndex
if (startIndex < 0 || startIndex >= _size)
{
throw new IndexOutOfRangeException("Please pass a valid starting index.");
}
// Handle the bounds of count and startIndex with respect to _size.
if (count < 0 || startIndex > (_size - count))
{
throw new ArgumentOutOfRangeException();
}
// Use Array.Reverse
// Running complexity is better than O(N). But unknown.
// Array.Reverse uses the closed-source function TrySZReverse.
Array.Reverse(_collection, startIndex, count);
}
/// <summary>
/// For each element in list, apply the specified action to it.
/// </summary>
/// <param name="action">Typed Action.</param>
public void ForEach(Action<T> action)
{
// Null actions are not allowed.
if (action == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < _size; ++i)
{
action(_collection[i]);
}
}
/// <summary>
/// Checks whether the list contains the specified dataItem.
/// </summary>
/// <returns>True if list contains the dataItem, false otherwise.</returns>
/// <param name="dataItem">Data item.</param>
public bool Contains(T dataItem)
{
// Null-value check
if ((Object)dataItem == null)
{
for (int i = 0; i < _size; ++i)
{
if ((Object)_collection[i] == null) return true;
}
}
else
{
// Construct a default equality comparer for this Type T
// Use it to get the equal match for the dataItem
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < _size; ++i)
{
if (comparer.Equals(_collection[i], dataItem)) return true;
}
}
return false;
}
/// <summary>
/// Checks whether the list contains the specified dataItem.
/// </summary>
/// <returns>True if list contains the dataItem, false otherwise.</returns>
/// <param name="dataItem">Data item.</param>
/// <param name="comparer">The Equality Comparer object.</param>
public bool Contains(T dataItem, IEqualityComparer<T> comparer)
{
// Null comparers are not allowed.
if (comparer == null)
{
throw new ArgumentNullException();
}
// Null-value check
if ((Object)dataItem == null)
{
for (int i = 0; i < _size; ++i)
{
if ((Object)_collection[i] == null) return true;
}
}
else
{
for (int i = 0; i < _size; ++i)
{
if (comparer.Equals(_collection[i], dataItem)) return true;
}
}
return false;
}
/// <summary>
/// Checks whether an item specified via a Predicate<T> function exists exists in list.
/// </summary>
/// <param name="searchMatch">Match predicate.</param>
public bool Exists(Predicate<T> searchMatch)
{
// Use the FindIndex to look through the collection
// If the returned index != -1 then it does exist, otherwise it doesn't.
return (FindIndex(searchMatch) != -1);
}
/// <summary>
/// Finds the index of the element that matches the predicate.
/// </summary>
/// <returns>The index of element if found, -1 otherwise.</returns>
/// <param name="searchMatch">Match predicate.</param>
public int FindIndex(Predicate<T> searchMatch)
{
return FindIndex(0, _size, searchMatch);
}
/// <summary>
/// Finds the index of the element that matches the predicate.
/// </summary>
/// <returns>The index of the element if found, -1 otherwise.</returns>
/// <param name="startIndex">Starting index to search from.</param>
/// <param name="searchMatch">Match predicate.</param>
public int FindIndex(int startIndex, Predicate<T> searchMatch)
{
return FindIndex(startIndex, (_size - startIndex), searchMatch);
}
/// <summary>
/// Finds the index of the first element that matches the given predicate function.
/// </summary>
/// <returns>The index of element if found, -1 if not found.</returns>
/// <param name="startIndex">Starting index of search.</param>
/// <param name="count">Count of elements to search through.</param>
/// <param name="searchMatch">Match predicate function.</param>
public int FindIndex(int startIndex, int count, Predicate<T> searchMatch)
{
// Check bound of startIndex
if (startIndex < 0 || startIndex > _size)
{
throw new IndexOutOfRangeException("Please pass a valid starting index.");
}
// CHeck the bounds of count and startIndex with respect to _size
if (count < 0 || startIndex > (_size - count))
{
throw new ArgumentOutOfRangeException();
}
// Null match-predicates are not allowed
if (searchMatch == null)
{
throw new ArgumentNullException();
}
// Start the search
int endIndex = startIndex + count;
for (int index = startIndex; index < endIndex; ++index)
{
if (searchMatch(_collection[index]) == true) return index;
}
// Not found, return -1
return -1;
}
/// <summary>
/// Returns the index of a given dataItem.
/// </summary>
/// <returns>Index of element in list.</returns>
/// <param name="dataItem">Data item.</param>
public int IndexOf(T dataItem)
{
return IndexOf(dataItem, 0, _size);
}
/// <summary>
/// Returns the index of a given dataItem.
/// </summary>
/// <returns>Index of element in list.</returns>
/// <param name="dataItem">Data item.</param>
/// <param name="startIndex">The starting index to search from.</param>
public int IndexOf(T dataItem, int startIndex)
{
return IndexOf(dataItem, startIndex, _size);
}
/// <summary>
/// Returns the index of a given dataItem.
/// </summary>
/// <returns>Index of element in list.</returns>
/// <param name="dataItem">Data item.</param>
/// <param name="startIndex">The starting index to search from.</param>
/// <param name="count">Count of elements to search through.</param>
public int IndexOf(T dataItem, int startIndex, int count)
{
// Check the bound of the starting index.
if (startIndex < 0 || (uint)startIndex > (uint)_size)
{
throw new IndexOutOfRangeException("Please pass a valid starting index.");
}
// Check the bounds of count and starting index with respect to _size.
if (count < 0 || startIndex > (_size - count))
{
throw new ArgumentOutOfRangeException();
}
// Everything is cool, start looking for the index
// Use the Array.IndexOf
// Array.IndexOf has a O(n) running time complexity, where: "n = count - size".
// Array.IndexOf uses EqualityComparer<T>.Default to return the index of element which loops
// ... over all the elements in the range [startIndex,count) in the array.
return Array.IndexOf(_collection, dataItem, startIndex, count);
}
/// <summary>
/// Find the specified element that matches the Search Predication.
/// </summary>
/// <param name="searchMatch">Match predicate.</param>
public T Find(Predicate<T> searchMatch)
{
// Null Predicate functions are not allowed.
if (searchMatch == null)
{
throw new ArgumentNullException();
}
// Begin searching, and return the matched element
for (int i = 0; i < _size; ++i)
{
if (searchMatch(_collection[i]))
{
return _collection[i];
}
}
// Not found, return the default value of the type T.
return default(T);
}
/// <summary>
/// Finds all the elements that match the typed Search Predicate.
/// </summary>
/// <returns>ArrayList<T> of matched elements. Empty list is returned if not element was found.</returns>
/// <param name="searchMatch">Match predicate.</param>
public ArrayList<T> FindAll(Predicate<T> searchMatch)
{
// Null Predicate functions are not allowed.
if (searchMatch == null)
{
throw new ArgumentNullException();
}
ArrayList<T> matchedElements = new ArrayList<T>();
// Begin searching, and add the matched elements to the new list.
for (int i = 0; i < _size; ++i)
{
if (searchMatch(_collection[i]))
{
matchedElements.Add(_collection[i]);
}
}
// Return the new list of elements.
return matchedElements;
}
/// <summary>
/// Get a range of elements, starting from an index..
/// </summary>
/// <returns>The range as ArrayList<T>.</returns>
/// <param name="startIndex">Start index to get range from.</param>
/// <param name="count">Count of elements.</param>
public ArrayList<T> GetRange(int startIndex, int count)
{
// Handle the bound errors of startIndex
if (startIndex < 0 || (uint)startIndex > (uint)_size)
{
throw new IndexOutOfRangeException("Please provide a valid starting index.");
}
// Handle the bound errors of count and startIndex with respect to _size
if (count < 0 || startIndex > (_size - count))
{
throw new ArgumentOutOfRangeException();
}
var newArrayList = new ArrayList<T>(count);
// Use Array.Copy to quickly copy the contents from this array to the new list's inner array.
Array.Copy(_collection, startIndex, newArrayList._collection, 0, count);
// Assign count to the new list's inner _size counter.
newArrayList._size = count;
return newArrayList;
}
/// <summary>
/// Return an array version of this list.
/// </summary>
/// <returns>Array.</returns>
public T[] ToArray()
{
T[] newArray = new T[Count];
if (Count > 0)
{
Array.Copy(_collection, 0, newArray, 0, Count);
}
return newArray;
}
/// <summary>
/// Return an array version of this list.
/// </summary>
/// <returns>Array.</returns>
public List<T> ToList()
{
var newList = new List<T>(this.Count);
if (this.Count > 0)
{
for (int i = 0; i < this.Count; ++i)
{
newList.Add(_collection[i]);
}
}
return newList;
}
/// <summary>
/// Return a human readable, multi-line, print-out (string) of this list.
/// </summary>
/// <returns>The human readable string.</returns>
/// <param name="addHeader">If set to <c>true</c> a header with count and Type is added; otherwise, only elements are printed.</param>
public string ToHumanReadable(bool addHeader = false)
{
int i = 0;
string listAsString = string.Empty;
string preLineIndent = (addHeader == false ? "" : "\t");
for (i = 0; i < Count; ++i)
{
listAsString = String.Format("{0}{1}[{2}] => {3}\r\n", listAsString, preLineIndent, i, _collection[i]);
}
if (addHeader == true)
{
listAsString = String.Format("ArrayList of count: {0}.\r\n(\r\n{1})", Count, listAsString);
}
return listAsString;
}
/********************************************************************************/
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return _collection[i];
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}