Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LargeArrayBuilder.cs
Go to the documentation of this file.
2
4
5internal struct LargeArrayBuilder<T>
6{
7 private readonly int _maxCapacity;
8
9 private T[] _first;
10
12
13 private T[] _current;
14
15 private int _index;
16
17 private int _count;
18
19 public int Count => _count;
20
22 : this(int.MaxValue)
23 {
24 }
25
27 {
28 this = default(LargeArrayBuilder<T>);
29 _first = (_current = Array.Empty<T>());
31 }
32
33 [MethodImpl(MethodImplOptions.AggressiveInlining)]
34 public void Add(T item)
35 {
36 int index = _index;
37 T[] current = _current;
38 if ((uint)index >= (uint)current.Length)
39 {
41 }
42 else
43 {
44 current[index] = item;
45 _index = index + 1;
46 }
47 _count++;
48 }
49
50 [MethodImpl(MethodImplOptions.NoInlining)]
52 {
54 _current[_index++] = item;
55 }
56
57 public void AddRange(IEnumerable<T> items)
58 {
59 using IEnumerator<T> enumerator = items.GetEnumerator();
61 int index = _index;
62 while (enumerator.MoveNext())
63 {
64 T current = enumerator.Current;
65 if ((uint)index >= (uint)destination.Length)
66 {
68 }
69 else
70 {
71 destination[index] = current;
72 }
73 index++;
74 }
75 _count += index - _index;
76 _index = index;
77 }
78
79 [MethodImpl(MethodImplOptions.NoInlining)]
81 {
82 _count += index - _index;
83 _index = index;
86 index = _index;
88 }
89
90 public void CopyTo(T[] array, int arrayIndex, int count)
91 {
92 int num = 0;
93 while (count > 0)
94 {
95 T[] buffer = GetBuffer(num);
96 int num2 = Math.Min(count, buffer.Length);
98 count -= num2;
100 num++;
101 }
102 }
103
104 public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
105 {
106 int num = position.Row;
107 int column = position.Column;
108 T[] buffer = GetBuffer(num);
110 if (count == 0)
111 {
112 return new CopyPosition(num, column + num2).Normalize(buffer.Length);
113 }
114 do
115 {
116 buffer = GetBuffer(++num);
117 num2 = CopyToCore(buffer, 0);
118 }
119 while (count > 0);
120 return new CopyPosition(num, num2).Normalize(buffer.Length);
122 {
123 int num3 = Math.Min(sourceBuffer.Length - sourceIndex, count);
125 arrayIndex += num3;
126 count -= num3;
127 return num3;
128 }
129 }
130
131 public T[] GetBuffer(int index)
132 {
133 if (index != 0)
134 {
135 if (index > _buffers.Count)
136 {
137 return _current;
138 }
139 return _buffers[index - 1];
140 }
141 return _first;
142 }
143
144 [MethodImpl(MethodImplOptions.NoInlining)]
145 public void SlowAdd(T item)
146 {
147 Add(item);
148 }
149
150 public T[] ToArray()
151 {
152 if (TryMove(out var array))
153 {
154 return array;
155 }
156 array = new T[_count];
157 CopyTo(array, 0, _count);
158 return array;
159 }
160
161 public bool TryMove(out T[] array)
162 {
163 array = _first;
164 return _count == _first.Length;
165 }
166
167 private void AllocateBuffer()
168 {
169 if ((uint)_count < 8u)
170 {
171 int num = Math.Min((_count == 0) ? 4 : (_count * 2), _maxCapacity);
172 _current = new T[num];
175 return;
176 }
177 int num2;
178 if (_count == 8)
179 {
180 num2 = 8;
181 }
182 else
183 {
184 _buffers.Add(_current);
186 }
187 _current = new T[num2];
188 _index = 0;
189 }
190}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static byte Min(byte val1, byte val2)
Definition Math.cs:912
CopyPosition Normalize(int endColumn)
System.Collections.Generic.ArrayBuilder< T[]> _buffers
void CopyTo(T[] array, int arrayIndex, int count)
CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
void AddWithBufferAllocation(T item, ref T[] destination, ref int index)