mscorlib(4.0.0.0) API with additions
ArrayBuilder.cs
2 {
3  internal struct ArrayBuilder<T>
4  {
5  private const int DefaultCapacity = 4;
6 
7  private const int MaxCoreClrArrayLength = 2146435071;
8 
9  private T[] _array;
10 
11  private int _count;
12 
13  public int Capacity
14  {
15  get
16  {
17  T[] array = _array;
18  if (array == null)
19  {
20  return 0;
21  }
22  return array.Length;
23  }
24  }
25 
26  public int Count => _count;
27 
28  public T this[int index]
29  {
30  get
31  {
32  return _array[index];
33  }
34  set
35  {
36  _array[index] = value;
37  }
38  }
39 
40  public ArrayBuilder(int capacity)
41  {
42  this = default(ArrayBuilder<T>);
43  if (capacity > 0)
44  {
45  _array = new T[capacity];
46  }
47  }
48 
49  public void Add(T item)
50  {
51  if (_count == Capacity)
52  {
53  EnsureCapacity(_count + 1);
54  }
55  UncheckedAdd(item);
56  }
57 
58  public T First()
59  {
60  return _array[0];
61  }
62 
63  public T Last()
64  {
65  return _array[_count - 1];
66  }
67 
68  public T[] ToArray()
69  {
70  if (_count == 0)
71  {
72  return new T[0];
73  }
74  T[] array = _array;
75  if (_count < array.Length)
76  {
77  array = new T[_count];
78  Array.Copy(_array, 0, array, 0, _count);
79  }
80  return array;
81  }
82 
83  public void UncheckedAdd(T item)
84  {
85  _array[_count++] = item;
86  }
87 
88  private void EnsureCapacity(int minimum)
89  {
90  int capacity = Capacity;
91  int num = (capacity == 0) ? 4 : (2 * capacity);
92  if ((uint)num > 2146435071u)
93  {
94  num = Math.Max(capacity + 1, 2146435071);
95  }
96  num = Math.Max(num, minimum);
97  T[] array = new T[num];
98  if (_count > 0)
99  {
100  Array.Copy(_array, 0, array, 0, _count);
101  }
102  _array = array;
103  }
104  }
105 }
static sbyte Max(sbyte val1, sbyte val2)
Returns the larger of two 8-bit signed integers.
Definition: Math.cs:581
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
The Add key (the addition key on the numeric keypad).
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Provides constants and static methods for trigonometric, logarithmic, and other common mathematical f...
Definition: Math.cs:10