Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
ImmutableQueue.cs
Go to the documentation of this file.
4using System.Linq;
5
7
8public static class ImmutableQueue
9{
11 {
12 return ImmutableQueue<T>.Empty;
13 }
14
16 {
17 return ImmutableQueue<T>.Empty.Enqueue(item);
18 }
19
21 {
22 Requires.NotNull(items, "items");
23 if (items is T[] items2)
24 {
25 return Create(items2);
26 }
27 using IEnumerator<T> enumerator = items.GetEnumerator();
28 if (!enumerator.MoveNext())
29 {
30 return ImmutableQueue<T>.Empty;
31 }
34 while (enumerator.MoveNext())
35 {
37 }
39 }
40
41 public static ImmutableQueue<T> Create<T>(params T[] items)
42 {
43 Requires.NotNull(items, "items");
44 if (items.Length == 0)
45 {
46 return ImmutableQueue<T>.Empty;
47 }
49 for (int num = items.Length - 1; num >= 0; num--)
50 {
51 immutableStack = immutableStack.Push(items[num]);
52 }
54 }
55
57 {
58 Requires.NotNull(queue, "queue");
59 value = queue.Peek();
60 return queue.Dequeue();
61 }
62}
63[DebuggerDisplay("IsEmpty = {IsEmpty}")]
65public sealed class ImmutableQueue<T> : IImmutableQueue<T>, IEnumerable<T>, IEnumerable
66{
68 public struct Enumerator
69 {
71
73
75
76 public T Current
77 {
78 get
79 {
80 if (_remainingForwardsStack == null)
81 {
82 throw new InvalidOperationException();
83 }
84 if (!_remainingForwardsStack.IsEmpty)
85 {
86 return _remainingForwardsStack.Peek();
87 }
88 if (!_remainingBackwardsStack.IsEmpty)
89 {
90 return _remainingBackwardsStack.Peek();
91 }
92 throw new InvalidOperationException();
93 }
94 }
95
102
103 public bool MoveNext()
104 {
105 if (_remainingForwardsStack == null)
106 {
108 _remainingBackwardsStack = _originalQueue.BackwardsReversed;
109 }
110 else if (!_remainingForwardsStack.IsEmpty)
111 {
113 }
114 else if (!_remainingBackwardsStack.IsEmpty)
115 {
117 }
118 if (_remainingForwardsStack.IsEmpty)
119 {
120 return !_remainingBackwardsStack.IsEmpty;
121 }
122 return true;
123 }
124 }
125
127 {
129
131
133
134 private bool _disposed;
135
136 public T Current
137 {
138 get
139 {
141 if (_remainingForwardsStack == null)
142 {
143 throw new InvalidOperationException();
144 }
145 if (!_remainingForwardsStack.IsEmpty)
146 {
147 return _remainingForwardsStack.Peek();
148 }
149 if (!_remainingBackwardsStack.IsEmpty)
150 {
151 return _remainingBackwardsStack.Peek();
152 }
153 throw new InvalidOperationException();
154 }
155 }
156
157 object IEnumerator.Current => Current;
158
163
164 public bool MoveNext()
165 {
167 if (_remainingForwardsStack == null)
168 {
170 _remainingBackwardsStack = _originalQueue.BackwardsReversed;
171 }
172 else if (!_remainingForwardsStack.IsEmpty)
173 {
175 }
176 else if (!_remainingBackwardsStack.IsEmpty)
177 {
179 }
180 if (_remainingForwardsStack.IsEmpty)
181 {
182 return !_remainingBackwardsStack.IsEmpty;
183 }
184 return true;
185 }
186
187 public void Reset()
188 {
192 }
193
194 public void Dispose()
195 {
196 _disposed = true;
197 }
198
199 private void ThrowIfDisposed()
200 {
201 if (_disposed)
202 {
203 Requires.FailObjectDisposed(this);
204 }
205 }
206 }
207
209
211
212 private readonly ImmutableStack<T> _forwards;
213
215
216 public bool IsEmpty => _forwards.IsEmpty;
217
219
221 {
222 get
223 {
224 if (_backwardsReversed == null)
225 {
226 _backwardsReversed = _backwards.Reverse();
227 }
228 return _backwardsReversed;
229 }
230 }
231
237
239 {
240 return Empty;
241 }
242
247
248 public T Peek()
249 {
250 if (IsEmpty)
251 {
253 }
254 return _forwards.Peek();
255 }
256
257 public ref readonly T PeekRef()
258 {
259 if (IsEmpty)
260 {
262 }
263 return ref _forwards.PeekRef();
264 }
265
267 {
268 if (IsEmpty)
269 {
270 return new ImmutableQueue<T>(ImmutableStack.Create(value), ImmutableStack<T>.Empty);
271 }
272 return new ImmutableQueue<T>(_forwards, _backwards.Push(value));
273 }
274
276 {
277 return Enqueue(value);
278 }
279
281 {
282 if (IsEmpty)
283 {
285 }
287 if (!immutableStack.IsEmpty)
288 {
290 }
291 if (_backwards.IsEmpty)
292 {
293 return Empty;
294 }
296 }
297
299 {
300 value = Peek();
301 return Dequeue();
302 }
303
305 {
306 return Dequeue();
307 }
308
310 {
311 return new Enumerator(this);
312 }
313
315 {
316 if (!IsEmpty)
317 {
318 return new EnumeratorObject(this);
319 }
320 return Enumerable.Empty<T>().GetEnumerator();
321 }
322
324 {
325 return new EnumeratorObject(this);
326 }
327}
static IImmutableQueue< T > Dequeue< T >(this IImmutableQueue< T > queue, out T value)
ImmutableQueue(ImmutableStack< T > forwards, ImmutableStack< T > backwards)
static readonly ImmutableQueue< T > s_EmptyField
static ImmutableQueue< T > CreateRange< T >(IEnumerable< T > items)
readonly ImmutableStack< T > _backwards
static ImmutableQueue< T > Create< T >()
ImmutableQueue< T > Dequeue(out T value)
static string InvalidEmptyOperation
Definition SR.cs:30
Definition SR.cs:7