-
Notifications
You must be signed in to change notification settings - Fork 1
/
StructQueue.cs
487 lines (412 loc) · 16.1 KB
/
StructQueue.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Nucs.Extensions;
namespace Nucs.Collections.Structs {
// Decompiled with JetBrains decompiler
// Type: System.Collections.Generic.StructQueue`1
// Assembly: System.Collections, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 9A40C45A-BB02-45B9-9BD3-DCD356A94F97
// Assembly location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.7\System.Collections.dll
internal sealed class QueueDebugView<T> {
private readonly StructQueue<T> _queue;
public QueueDebugView(StructQueue<T> queue) =>
_queue = queue;
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public T[] Items => _queue.ToArray();
}
#nullable enable
[DebuggerTypeProxy(typeof(QueueDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
public struct StructQueue<T> : IEnumerable<T>, IEnumerable, ICollection, IReadOnlyCollection<T>, IDisposable {
#nullable disable
private T[] _array;
private int _head;
private int _tail;
private int _size;
public T[] InternalArray => _array;
public StructQueue(int capacity) {
_array = capacity >= 0 ? new T[capacity] : capacity == 0 ? Array.Empty<T>() : throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "SR.ArgumentOutOfRange_NeedNonNegNum");
_head = 0;
_tail = 0;
_size = 0;
}
#nullable enable
public StructQueue(IEnumerable<T> collection) {
_head = 0;
_array = collection != null ? ToArray<T>(collection, out _size) : throw new ArgumentNullException(nameof(collection));
if (_size == _array.Length) {
_tail = 0;
return;
}
_tail = _size;
}
public StructQueue(IList<T> collection) {
_head = 0;
_array = collection.ToArrayFast(collection.Count);
_size = collection.Count;
if (_size == _array.Length) {
_tail = 0;
return;
}
_tail = _size;
}
public StructQueue(List<T> collection) {
_head = 0;
_array = collection.ToArrayFast(collection.Count);
_size = collection.Count;
if (_size == _array.Length) {
_tail = 0;
return;
}
_tail = _size;
}
public StructQueue(T[] collection, int startIndex, int count) {
_head = startIndex;
_array = collection;
_size = count;
if (count == _array.Length) {
_tail = 0;
return;
}
_tail = count;
}
public StructQueue(T[] collection) : this(collection, 0, collection.Length) { }
public StructQueue(StructList<T> collection) : this((T[]) collection.InternalArray.Clone(), 0, collection.Length) { }
public readonly int Count => _size;
public readonly int Capacity => _array.Length;
public readonly bool IsEmpty => _size == 0;
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => null;
public ref T this[int index] {
get {
#if DEBUG
if (index >= _size)
throw new ArgumentOutOfRangeException(nameof(index));
#endif
var target = _head + index;
if (target < _tail) {
return ref _array[target];
} else {
return ref _array[target % _size]; //0-5, _head:3, index:3 -> 6 % 5 -> [1]
}
}
}
public void Clear() {
if (_size != 0) {
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) {
if (_head < _tail) {
Array.Clear(_array, _head, _size);
} else {
Array.Clear(_array, _head, _array.Length - _head);
Array.Clear(_array, 0, _tail);
}
}
_size = 0;
}
_head = 0;
_tail = 0;
}
public readonly void CopyTo(T[] array, int arrayIndex) {
if (array == null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex > array.Length)
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, "SR.ArgumentOutOfRange_Index");
if (array.Length - arrayIndex < _size)
throw new ArgumentException("SR.Argument_InvalidOffLen");
int size = _size;
if (size == 0)
return;
int length1 = Math.Min(_array.Length - _head, size);
Array.Copy(_array, _head, array, arrayIndex, length1);
int length2 = size - length1;
if (length2 <= 0)
return;
Array.Copy(_array, 0, array, arrayIndex + _array.Length - _head, length2);
}
#nullable disable
void ICollection.CopyTo(Array array, int index) {
if (array == null)
throw new ArgumentNullException(nameof(array));
if (array.Rank != 1)
throw new ArgumentException("SR.Arg_RankMultiDimNotSupported", nameof(array));
int num = array.GetLowerBound(0) == 0 ? array.Length : throw new ArgumentException("SR.Arg_NonZeroLowerBound", nameof(array));
if (index < 0 || index > num)
throw new ArgumentOutOfRangeException(nameof(index), index, "SR.ArgumentOutOfRange_Index");
if (num - index < _size)
throw new ArgumentException("SR.Argument_InvalidOffLen");
int size = _size;
if (size == 0)
return;
try {
int length1 = _array.Length - _head < size ? _array.Length - _head : size;
Array.Copy(_array, _head, array, index, length1);
int length2 = size - length1;
if (length2 <= 0)
return;
Array.Copy(_array, 0, array, index + _array.Length - _head, length2);
} catch (ArrayTypeMismatchException ex) {
throw new ArgumentException("SR.Argument_InvalidArrayType", nameof(array));
}
}
public void Enqueue(T item) {
if (_size == _array.Length) {
int capacity = (int) (_array.Length * 200L / 100L);
if (capacity < _array.Length + 4)
capacity = _array.Length + 4;
SetCapacity(capacity);
}
_array[_tail] = item;
MoveNext(ref _tail);
++_size;
}
public void EnqueueFirstOut(T item) {
if (_size == _array.Length) {
int capacity = (int) (_array.Length * 200L / 100L);
if (capacity < _array.Length + 4)
capacity = _array.Length + 4;
SetCapacity(capacity);
}
//if head is 0 then we got to try to go to end or enlarge
MoveBack(ref _head);
_array[_head] = item;
++_size;
}
public void Enqueue(ref T item) {
if (_size == _array.Length) {
int capacity = (int) (_array.Length * 200L / 100L);
if (capacity < _array.Length + 4)
capacity = _array.Length + 4;
SetCapacity(capacity);
}
_array[_tail] = item;
MoveNext(ref _tail);
++_size;
}
public Enumerator GetEnumerator() =>
new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() =>
new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() =>
new Enumerator(this);
#nullable enable
public T Dequeue() {
int head = _head;
T[] array = _array;
if (_size == 0)
throw new InvalidOperationException("SR.InvalidOperation_EmptyQueue");
T obj = array[head]!;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
array[head] = default!;
MoveNext(ref _head);
--_size;
return obj!;
}
public ref T PeakRef() {
if (_size == 0)
throw new InvalidOperationException("SR.InvalidOperation_EmptyQueue");
return ref _array[_head]!;
}
public bool TryDequeue([MaybeNullWhen(false)] out T result) {
int head = _head;
T[] array = _array;
if (_size == 0) {
result = default;
return false;
}
result = array[head];
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
array[head] = default!;
MoveNext(ref _head);
--_size;
return true;
}
public readonly T Peek() {
if (_size == 0)
throw new InvalidOperationException("SR.InvalidOperation_EmptyQueue");
return _array[_head];
}
public readonly bool TryPeek([MaybeNullWhen(false)] out T result) {
if (_size == 0) {
result = default;
return false;
}
result = _array[_head];
return true;
}
public readonly bool Contains(T item) {
if (_size == 0)
return false;
if (_head < _tail)
return Array.IndexOf<T>(_array, item, _head, _size) >= 0;
return Array.IndexOf<T>(_array, item, _head, _array.Length - _head) >= 0 || Array.IndexOf<T>(_array, item, 0, _tail) >= 0;
}
public readonly T[] ToArray() {
if (_size == 0)
return Array.Empty<T>();
T[] objArray = new T[_size];
if (_head < _tail) {
Array.Copy(_array, _head, objArray, 0, _size);
} else {
Array.Copy(_array, _head, objArray, 0, _array.Length - _head);
Array.Copy(_array, 0, objArray, _array.Length - _head, _tail);
}
return objArray;
}
public readonly void CopyTo(T[] objArray) {
if (_size == 0)
return;
if (_head < _tail) {
Array.Copy(_array, _head, objArray, 0, _size);
} else {
Array.Copy(_array, _head, objArray, 0, _array.Length - _head);
Array.Copy(_array, 0, objArray, _array.Length - _head, _tail);
}
}
public readonly void CopyTo(Span<T> objArray) {
if (_size == 0)
return;
if (_head < _tail) {
_array.AsSpan(_head).CopyTo(objArray.Slice(0, _size));
} else {
_array.AsSpan(_head).CopyTo(objArray.Slice(0, _array.Length - _head));
_array.AsSpan().CopyTo(objArray.Slice(_array.Length - _head, _tail));
}
}
public void EnsureCapacity(int capacity) {
if (capacity <= Capacity)
return;
SetCapacity(capacity);
}
public void SetCapacity(int capacity) {
T[] objArray = new T[capacity];
if (_size > 0) {
if (_head < _tail) {
Array.Copy(_array, _head, objArray, 0, _size);
} else {
Array.Copy(_array, _head, objArray, 0, _array.Length - _head);
Array.Copy(_array, 0, objArray, _array.Length - _head, _tail);
}
}
_array = objArray;
_head = 0;
_tail = _size == capacity ? 0 : _size;
}
#nullable disable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void MoveNext(ref int index) {
int num = index + 1;
if (num == _array.Length)
num = 0;
index = num;
}
#nullable disable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void MoveBack(ref int index) {
int num = index - 1;
if (num == -1)
num = _array.Length - 1;
index = num;
}
public void TrimExcess() {
if (_size >= (int) (_array.Length * 0.9))
return;
SetCapacity(_size);
}
#nullable enable
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator {
#nullable disable
private readonly StructQueue<T> _q;
private int _index;
private T _currentElement;
internal Enumerator(StructQueue<T> q) {
_q = q;
_index = -1;
_currentElement = default;
}
public void Dispose() {
_index = -2;
_currentElement = default;
}
public bool MoveNext() {
if (_index == -2)
return false;
++_index;
if (_index == _q._size) {
_index = -2;
_currentElement = default;
return false;
}
T[] array = _q._array;
int length = array.Length;
int index = _q._head + _index;
if (index >= length)
index -= length;
_currentElement = array[index];
return true;
}
#nullable enable
public T Current {
get {
if (_index < 0)
ThrowEnumerationNotStartedOrEnded();
return _currentElement;
}
}
private void ThrowEnumerationNotStartedOrEnded() =>
throw new InvalidOperationException(_index == -1 ? "SR.InvalidOperation_EnumNotStarted" : "SR.InvalidOperation_EnumEnded");
object? IEnumerator.Current => Current;
void IEnumerator.Reset() {
_index = -1;
_currentElement = default;
}
}
internal static T[] ToArray<T>(IEnumerable<T> source, out int length) {
if (source is ICollection<T> objs) {
int count = objs.Count;
if (count != 0) {
T[] array = new T[count];
objs.CopyTo(array, 0);
length = count;
return array;
}
} else {
using (IEnumerator<T> enumerator = source.GetEnumerator()) {
if (enumerator.MoveNext()) {
T[] array = new T[4] {
enumerator.Current,
default,
default,
default
};
int num = 1;
while (enumerator.MoveNext()) {
if (num == array.Length) {
int newSize = num << 1;
if ((uint) newSize > 2146435071U)
newSize = 2146435071 <= num ? num + 1 : 2146435071;
Array.Resize<T>(ref array, newSize);
}
array[num++] = enumerator.Current;
}
length = num;
return array;
}
}
}
length = 0;
return Array.Empty<T>();
}
#region IDisposable
public void Dispose() {
Array.Clear(_array, 0, _array.Length);
_array = Array.Empty<T>();
_head = _size = _tail = 0;
}
#endregion
}
}