-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReusableSpanQueue.cs
209 lines (174 loc) · 6.12 KB
/
ReusableSpanQueue.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Nucs.Collections.Structs {
[DebuggerTypeProxy(typeof(QueueDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
public ref struct ReusableSpanQueue<T> {
#nullable disable
private ReadOnlySpan<T> _array;
private int _head;
private int _size;
public ReadOnlySpan<T> InternalArray => _array;
#nullable enable
public ReusableSpanQueue(ReadOnlySpan<T> collection, int startIndex, int count) {
_head = startIndex;
_array = collection;
_size = count;
if (count == _array.Length) {
return;
}
}
public ReusableSpanQueue(ReadOnlySpan<T> collection) : this(collection, 0, collection.Length) { }
public void Reuse() {
this = new ReusableSpanQueue<T>(_array, 0, _array.Length);
}
public void Reuse(int startIndex, int count) {
this = new ReusableSpanQueue<T>(_array, startIndex, count);
}
public void CopyTo(Array array, int index) {
throw new NotImplementedException();
}
public readonly int Count => _size;
public void Clear() {
_size = 0;
_head = 0;
}
public Enumerator GetEnumerator() =>
new Enumerator(this);
#nullable enable
public T Dequeue() {
int head = _head;
if (_size == 0)
throw new InvalidOperationException("SR.InvalidOperation_EmptyQueue");
T obj = _array[head]!;
MoveNext(ref _head);
--_size;
return obj!;
}
public bool TryDequeue([MaybeNullWhen(false)] out T result) {
int head = _head;
if (_size == 0) {
result = default;
return false;
}
result = _array[head];
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;
}
#nullable disable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void MoveNext(ref int index) {
int num = index + 1;
if (num == _array.Length)
num = 0;
index = num;
}
#nullable enable
public ref struct Enumerator {
#nullable disable
private readonly ReusableSpanQueue<T> _q;
private int _index;
private T _currentElement;
internal Enumerator(ReusableSpanQueue<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;
}
ReadOnlySpan<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");
public void 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() {
_head = _size = 0;
}
#endregion
}
}