Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
DynamicArray.cs
浏览该文件的文档.
1// ReSharper disable SuspiciousTypeConversion.Global
2
3namespace Engine {
4 public class DynamicArray<T> : IList<T> {
5 public struct Enumerator : IEnumerator<T> {
7
8 int m_index;
9
10 public T Current => m_array.Array[m_index];
11
12 object IEnumerator.Current => m_array.Array[m_index];
13
15 m_array = array;
16 m_index = -1;
17 }
18
19 public void Dispose() { }
20
21 public bool MoveNext() {
22 m_index++;
23 return m_index < m_array.Count;
24 }
25
26 public void Reset() {
27 m_index = -1;
28 }
29 }
30
31 struct Comparer : IComparer<T> {
33
34 public Comparer(Comparison<T> comparison) {
35 if (comparison == null) {
36 throw new ArgumentNullException(nameof(comparison));
37 }
38 Comparison = comparison;
39 }
40
41 int IComparer<T>.Compare(T x, T y) => Comparison(x, y);
42 }
43
44 const int MinCapacity = 4;
45
47
49
50 static T[] m_emptyArray = [];
51
52 public int Capacity {
53 get => m_array.Length;
54 set {
55 if (value != Capacity) {
56 if (value < m_count) {
57 throw new InvalidOperationException("Capacity cannot be made smaller than number of elements.");
58 }
59 Reallocate(value);
60 }
61 }
62 }
63
64 public int Count {
65 get => m_count;
66 set {
67 if (value > Capacity) {
68 Reallocate(value);
69 }
70 else if (value < 0) {
71 throw new InvalidOperationException("Count cannot be negative.");
72 }
73 m_count = value;
74 }
75 }
76
77 public T this[int index] {
78 get => index >= m_count ? throw new IndexOutOfRangeException() : m_array[index];
79 set {
80 if (index >= m_count) {
81 throw new IndexOutOfRangeException();
82 }
83 m_array[index] = value;
84 }
85 }
86
87 public T[] Array => m_array;
88
89 public bool IsReadOnly => false;
90
91 public DynamicArray() { }
92
93 public DynamicArray(int capacity) => Capacity = capacity;
94
95 public DynamicArray(IEnumerable<T> items) {
96 AddRange(items);
97 }
98
99 public DynamicArray(IReadOnlyCollection<T> items) {
100 AddRange(items);
101 }
102
103 public DynamicArray(IReadOnlyList<T> items) {
104 AddRange(items);
105 }
106
108 AddRange(items);
109 }
110
111 public int IndexOf(T item) {
112 EqualityComparer<T> @default = EqualityComparer<T>.Default;
113 for (int i = 0; i < m_count; i++) {
114 if (@default.Equals(item, m_array[i])) {
115 return i;
116 }
117 }
118 return -1;
119 }
120
121 public void Add(T item) {
123 m_array[m_count] = item;
124 m_count++;
125 }
126
127 public void AddRange(IEnumerable<T> items) {
128 if (items is DynamicArray<T> items2) {
129 AddRangeTyped(items2);
130 return;
131 }
132 if (items is IReadOnlyList<T> items3) {
133 AddRangeTyped(items3);
134 return;
135 }
136 if (items is IList<T> items4) {
137 AddRangeTyped(items4);
138 return;
139 }
140 if (items is IReadOnlyCollection<T> items5) {
141 AddRangeTyped(items5);
142 return;
143 }
144 if (items == null) {
145 throw new ArgumentNullException(nameof(items));
146 }
147 AddRangeTyped(items);
148 }
149
150 public void AddRange(IReadOnlyCollection<T> items) {
151 if (items is DynamicArray<T> items2) {
152 AddRangeTyped(items2);
153 return;
154 }
155 if (items is IReadOnlyList<T> items3) {
156 AddRangeTyped(items3);
157 return;
158 }
159 if (items is IList<T> items4) {
160 AddRangeTyped(items4);
161 return;
162 }
163 if (items == null) {
164 throw new ArgumentNullException(nameof(items));
165 }
166 AddRangeTyped(items);
167 }
168
169 public void AddRange(IList<T> items) {
170 if (items is DynamicArray<T> items2) {
171 AddRangeTyped(items2);
172 return;
173 }
174 if (items == null) {
175 throw new ArgumentNullException(nameof(items));
176 }
177 AddRangeTyped(items);
178 }
179
180 public void AddRange(IReadOnlyList<T> items) {
181 if (items is DynamicArray<T> items2) {
182 AddRangeTyped(items2);
183 return;
184 }
185 if (items == null) {
186 throw new ArgumentNullException(nameof(items));
187 }
188 AddRangeTyped(items);
189 }
190
191 public void AddRange(DynamicArray<T> items) {
192 if (items == null) {
193 throw new ArgumentNullException(nameof(items));
194 }
195 AddRangeTyped(items);
196 }
197
198 public bool Remove(T item) {
199 int num = IndexOf(item);
200 if (num >= 0) {
201 RemoveAt(num);
202 return true;
203 }
204 return false;
205 }
206
207 public void RemoveAt(int index) {
208 if (index < m_count) {
209 m_count--;
210 if (index < m_count) {
211 System.Array.Copy(m_array, index + 1, m_array, index, m_count - index);
212 }
213 m_array[m_count] = default;
214 return;
215 }
216 throw new IndexOutOfRangeException();
217 }
218
219 public void RemoveAtEnd() {
220 if (m_count > 0) {
221 m_count--;
222 m_array[m_count] = default;
223 return;
224 }
225 throw new IndexOutOfRangeException();
226 }
227
228 public int RemoveAll(Predicate<T> match) {
229 ArgumentNullException.ThrowIfNull(match);
230 int i;
231 for (i = 0; i < m_count && !match(m_array[i]); i++) { }
232 if (i >= m_count) {
233 return 0;
234 }
235 int j = i + 1;
236 while (j < m_count) {
237 for (; j < m_count && match(m_array[j]); j++) { }
238 if (j < m_count) {
239 m_array[i++] = m_array[j++];
240 }
241 }
242 System.Array.Clear(m_array, i, m_count - i);
243 int result = m_count - i;
244 m_count = i;
245 return result;
246 }
247
248 public void RemoveRange(int index, int count) {
249 if (index < 0
250 || count < 0
251 || m_count - index < count) {
252 throw new IndexOutOfRangeException();
253 }
254 if (count > 0) {
255 m_count -= count;
256 if (index < m_count) {
257 System.Array.Copy(m_array, index + count, m_array, index, m_count - index);
258 }
259 System.Array.Clear(m_array, m_count, count);
260 }
261 }
262
263 public void RemoveRange(IEnumerable<T> items) {
264 foreach (T t in items) {
265 Remove(t);
266 }
267 }
268
269 public void Insert(int index, T item) {
270 if (index <= m_count) {
272 if (index < m_count) {
273 System.Array.Copy(m_array, index, m_array, index + 1, m_count - index);
274 }
275 m_array[index] = item;
276 m_count++;
277 return;
278 }
279 throw new IndexOutOfRangeException();
280 }
281
282 public void Clear() {
283 System.Array.Clear(m_array, 0, m_count);
284 m_count = 0;
285 }
286
287 public void Reverse() {
288 int num = 0;
289 int num2 = m_count - 1;
290 while (num < num2) {
291 T val = m_array[num];
292 m_array[num] = m_array[num2];
293 m_array[num2] = val;
294 num++;
295 num2--;
296 }
297 }
298
299 public void Sort() {
300 System.Array.Sort(m_array, 0, m_count);
301 }
302
303 public void Sort(Comparison<T> comparison) {
304 System.Array.Sort(m_array, 0, m_count, new Comparer(comparison));
305 }
306
307 public void Sort(int index, int count) {
308 if (index < 0
309 || count < 0
310 || index + count > m_count) {
311 throw new ArgumentOutOfRangeException();
312 }
313 System.Array.Sort(m_array, index, count);
314 }
315
316 public void Sort(int index, int count, Comparison<T> comparison) {
317 if (index < 0
318 || count < 0
319 || index + count > m_count) {
320 throw new ArgumentOutOfRangeException();
321 }
322 System.Array.Sort(m_array, index, count, new Comparer(comparison));
323 }
324
325 public Enumerator GetEnumerator() => new(this);
326
327 IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
328
329 IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
330
331 public bool Contains(T item) {
332 EqualityComparer<T> @default = EqualityComparer<T>.Default;
333 for (int i = 0; i < m_count; i++) {
334 if (@default.Equals(item, m_array[i])) {
335 return true;
336 }
337 }
338 return false;
339 }
340
341 public void CopyTo(T[] array, int arrayIndex) {
342 System.Array.Copy(m_array, 0, array, arrayIndex, m_count);
343 }
344
345 protected virtual T[] Allocate(int capacity) => new T[capacity];
346
347 protected virtual void Free(T[] array) { }
348
349 void Reallocate(int capacity) {
350 if (capacity > 0) {
351 ReallocateNonZero(capacity);
352 }
353 else if (m_array != m_emptyArray) {
354 Free(m_array);
356 }
357 }
358
359 void ReallocateNonZero(int capacity) {
360 T[] array = Allocate(capacity);
361 if (m_array != m_emptyArray) {
362 System.Array.Copy(m_array, 0, array, 0, m_count);
363 Free(m_array);
364 }
365 m_array = array;
366 }
367
373
374 void EnsureCapacityExact(int capacity) {
375 if (capacity > Capacity) {
376 ReallocateNonZero(capacity);
377 }
378 }
379
380 void AddRangeTyped(IEnumerable<T> items) {
381 foreach (T item in items) {
382 Add(item);
383 }
384 }
385
386 void AddRangeTyped(IReadOnlyCollection<T> items) {
387 EnsureCapacityExact(Count + items.Count);
388 foreach (T item in items) {
389 m_array[m_count] = item;
390 m_count++;
391 }
392 }
393
394 void AddRangeTyped(IReadOnlyList<T> items) {
395 EnsureCapacityExact(Count + items.Count);
396 for (int i = 0; i < items.Count; i++) {
397 m_array[m_count] = items[i];
398 m_count++;
399 }
400 }
401
402 void AddRangeTyped(IList<T> items) {
403 EnsureCapacityExact(Count + items.Count);
404 for (int i = 0; i < items.Count; i++) {
405 m_array[m_count] = items[i];
406 m_count++;
407 }
408 }
409
412 System.Array.Copy(items.Array, 0, m_array, Count, items.Count);
413 m_count += items.Count;
414 }
415 }
416}
void Insert(int index, T item)
void RemoveRange(IEnumerable< T > items)
void AddRangeTyped(IReadOnlyList< T > items)
Enumerator(DynamicArray< T > array)
void AddRangeTyped(IList< T > items)
Comparer(Comparison< T > comparison)
void AddRangeTyped(DynamicArray< T > items)
void ReallocateNonZero(int capacity)
int RemoveAll(Predicate< T > match)
void CopyTo(T[] array, int arrayIndex)
void AddRange(IEnumerable< T > items)
void AddRange(IReadOnlyCollection< T > items)
void AddRange(IReadOnlyList< T > items)
void RemoveRange(int index, int count)
void AddRangeTyped(IReadOnlyCollection< T > items)
DynamicArray(IReadOnlyList< T > items)
void Sort(int index, int count)
virtual T[] Allocate(int capacity)
DynamicArray< T > m_array
void Sort(int index, int count, Comparison< T > comparison)
DynamicArray(IReadOnlyCollection< T > items)
void AddRangeTyped(IEnumerable< T > items)
void Sort(Comparison< T > comparison)
void AddRange(IList< T > items)
void AddRange(DynamicArray< T > items)
void Reallocate(int capacity)
DynamicArray(IEnumerable< T > items)
DynamicArray(DynamicArray< T > items)
void EnsureCapacityExact(int capacity)
virtual void Free(T[] array)
static int Max(int x1, int x2)