Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ImmutableStack.cs
Go to the documentation of this file.
4using System.Linq;
5
7
8public static class ImmutableStack
9{
11 {
12 return ImmutableStack<T>.Empty;
13 }
14
16 {
17 return ImmutableStack<T>.Empty.Push(item);
18 }
19
21 {
22 Requires.NotNull(items, "items");
24 foreach (T item in items)
25 {
27 }
28 return immutableStack;
29 }
30
31 public static ImmutableStack<T> Create<T>(params T[] items)
32 {
33 Requires.NotNull(items, "items");
35 foreach (T value in items)
36 {
38 }
39 return immutableStack;
40 }
41
43 {
44 Requires.NotNull(stack, "stack");
45 value = stack.Peek();
46 return stack.Pop();
47 }
48}
49[DebuggerDisplay("IsEmpty = {IsEmpty}; Top = {_head}")]
51public sealed class ImmutableStack<T> : IImmutableStack<T>, IEnumerable<T>, IEnumerable
52{
54 public struct Enumerator
55 {
57
59
60 public T Current
61 {
62 get
63 {
64 if (_remainingStack == null || _remainingStack.IsEmpty)
65 {
66 throw new InvalidOperationException();
67 }
68 return _remainingStack.Peek();
69 }
70 }
71
73 {
74 Requires.NotNull(stack, "stack");
75 _originalStack = stack;
76 _remainingStack = null;
77 }
78
79 public bool MoveNext()
80 {
81 if (_remainingStack == null)
82 {
84 }
85 else if (!_remainingStack.IsEmpty)
86 {
88 }
89 return !_remainingStack.IsEmpty;
90 }
91 }
92
94 {
96
98
99 private bool _disposed;
100
101 public T Current
102 {
103 get
104 {
106 if (_remainingStack == null || _remainingStack.IsEmpty)
107 {
108 throw new InvalidOperationException();
109 }
110 return _remainingStack.Peek();
111 }
112 }
113
114 object IEnumerator.Current => Current;
115
117 {
118 Requires.NotNull(stack, "stack");
119 _originalStack = stack;
120 }
121
122 public bool MoveNext()
123 {
125 if (_remainingStack == null)
126 {
128 }
129 else if (!_remainingStack.IsEmpty)
130 {
132 }
133 return !_remainingStack.IsEmpty;
134 }
135
136 public void Reset()
137 {
139 _remainingStack = null;
140 }
141
142 public void Dispose()
143 {
144 _disposed = true;
145 }
146
147 private void ThrowIfDisposed()
148 {
149 if (_disposed)
150 {
151 Requires.FailObjectDisposed(this);
152 }
153 }
154 }
155
156 private static readonly ImmutableStack<T> s_EmptyField = new ImmutableStack<T>();
157
158 private readonly T _head;
159
160 private readonly ImmutableStack<T> _tail;
161
163
164 public bool IsEmpty => _tail == null;
165
167 {
168 }
169
170 private ImmutableStack(T head, ImmutableStack<T> tail)
171 {
172 _head = head;
173 _tail = tail;
174 }
175
177 {
178 return Empty;
179 }
180
185
186 public T Peek()
187 {
188 if (IsEmpty)
189 {
191 }
192 return _head;
193 }
194
195 public ref readonly T PeekRef()
196 {
197 if (IsEmpty)
198 {
200 }
201 return ref _head;
202 }
203
205 {
206 return new ImmutableStack<T>(value, this);
207 }
208
210 {
211 return Push(value);
212 }
213
215 {
216 if (IsEmpty)
217 {
219 }
220 return _tail;
221 }
222
224 {
225 value = Peek();
226 return Pop();
227 }
228
230 {
231 return Pop();
232 }
233
235 {
236 return new Enumerator(this);
237 }
238
240 {
241 if (!IsEmpty)
242 {
243 return new EnumeratorObject(this);
244 }
245 return Enumerable.Empty<T>().GetEnumerator();
246 }
247
249 {
250 return new EnumeratorObject(this);
251 }
252
254 {
257 while (!immutableStack2.IsEmpty)
258 {
261 }
262 return immutableStack;
263 }
264}
ImmutableStack< T > Pop(out T value)
ImmutableStack(T head, ImmutableStack< T > tail)
static IImmutableStack< T > Pop< T >(this IImmutableStack< T > stack, out T value)
static ImmutableStack< T > CreateRange< T >(IEnumerable< T > items)
static ImmutableStack< T > Create< T >()
static readonly ImmutableStack< T > s_EmptyField
static string InvalidEmptyOperation
Definition SR.cs:30
Definition SR.cs:7