This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
ImmutableStack_1.cs
261 lines (235 loc) · 8.23 KB
/
ImmutableStack_1.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
namespace System.Collections.Immutable
{
/// <summary>
/// An immutable stack.
/// </summary>
/// <typeparam name="T">The type of element stored by the stack.</typeparam>
[DebuggerDisplay("IsEmpty = {IsEmpty}; Top = {_head}")]
[DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))]
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Ignored")]
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "Ignored")]
public sealed partial class ImmutableStack<T> : IImmutableStack<T>
{
/// <summary>
/// The singleton empty stack.
/// </summary>
/// <remarks>
/// Additional instances representing the empty stack may exist on deserialized stacks.
/// </remarks>
private static readonly ImmutableStack<T> s_EmptyField = new ImmutableStack<T>();
/// <summary>
/// The element on the top of the stack.
/// </summary>
private readonly T _head;
/// <summary>
/// A stack that contains the rest of the elements (under the top element).
/// </summary>
private readonly ImmutableStack<T> _tail;
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableStack{T}"/> class
/// that acts as the empty stack.
/// </summary>
private ImmutableStack()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableStack{T}"/> class.
/// </summary>
/// <param name="head">The head element on the stack.</param>
/// <param name="tail">The rest of the elements on the stack.</param>
private ImmutableStack(T head, ImmutableStack<T> tail)
{
Debug.Assert(tail != null);
_head = head;
_tail = tail;
}
/// <summary>
/// Gets the empty stack, upon which all stacks are built.
/// </summary>
public static ImmutableStack<T> Empty
{
get
{
Debug.Assert(s_EmptyField.IsEmpty);
return s_EmptyField;
}
}
/// <summary>
/// Gets the empty stack, upon which all stacks are built.
/// </summary>
public ImmutableStack<T> Clear()
{
Debug.Assert(s_EmptyField.IsEmpty);
return Empty;
}
/// <summary>
/// Gets an empty stack.
/// </summary>
IImmutableStack<T> IImmutableStack<T>.Clear()
{
return this.Clear();
}
/// <summary>
/// Gets a value indicating whether this instance is empty.
/// </summary>
/// <value>
/// <c>true</c> if this instance is empty; otherwise, <c>false</c>.
/// </value>
public bool IsEmpty
{
get { return _tail == null; }
}
/// <summary>
/// Gets the element on the top of the stack.
/// </summary>
/// <returns>
/// The element on the top of the stack.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown when the stack is empty.</exception>
[Pure]
public T Peek()
{
if (this.IsEmpty)
{
throw new InvalidOperationException(SR.InvalidEmptyOperation);
}
return _head;
}
#if !NETSTANDARD1_0
/// <summary>
/// Gets a read-only reference to the element on the top of the stack.
/// </summary>
/// <returns>
/// A read-only reference to the element on the top of the stack.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown when the stack is empty.</exception>
[Pure]
public ref readonly T PeekRef()
{
if (this.IsEmpty)
{
throw new InvalidOperationException(SR.InvalidEmptyOperation);
}
return ref _head;
}
#endif
/// <summary>
/// Pushes an element onto a stack and returns the new stack.
/// </summary>
/// <param name="value">The element to push onto the stack.</param>
/// <returns>The new stack.</returns>
[Pure]
public ImmutableStack<T> Push(T value)
{
return new ImmutableStack<T>(value, this);
}
/// <summary>
/// Pushes an element onto a stack and returns the new stack.
/// </summary>
/// <param name="value">The element to push onto the stack.</param>
/// <returns>The new stack.</returns>
[Pure]
IImmutableStack<T> IImmutableStack<T>.Push(T value)
{
return this.Push(value);
}
/// <summary>
/// Returns a stack that lacks the top element on this stack.
/// </summary>
/// <returns>A stack; never <c>null</c></returns>
/// <exception cref="InvalidOperationException">Thrown when the stack is empty.</exception>
[Pure]
public ImmutableStack<T> Pop()
{
if (this.IsEmpty)
{
throw new InvalidOperationException(SR.InvalidEmptyOperation);
}
Debug.Assert(_tail != null);
return _tail;
}
/// <summary>
/// Pops the top element off the stack.
/// </summary>
/// <param name="value">The value that was removed from the stack.</param>
/// <returns>
/// A stack; never <c>null</c>
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "0#")]
[Pure]
public ImmutableStack<T> Pop(out T value)
{
value = this.Peek();
return this.Pop();
}
/// <summary>
/// Returns a stack that lacks the top element on this stack.
/// </summary>
/// <returns>A stack; never <c>null</c></returns>
/// <exception cref="InvalidOperationException">Thrown when the stack is empty.</exception>
[Pure]
IImmutableStack<T> IImmutableStack<T>.Pop()
{
return this.Pop();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// An <see cref="Enumerator"/> that can be used to iterate through the collection.
/// </returns>
[Pure]
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
/// </returns>
[Pure]
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this.IsEmpty ?
Enumerable.Empty<T>().GetEnumerator() :
new EnumeratorObject(this);
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
[Pure]
IEnumerator IEnumerable.GetEnumerator()
{
return new EnumeratorObject(this);
}
/// <summary>
/// Reverses the order of a stack.
/// </summary>
/// <returns>The reversed stack.</returns>
[Pure]
internal ImmutableStack<T> Reverse()
{
var r = this.Clear();
for (ImmutableStack<T> f = this; !f.IsEmpty; f = f.Pop())
{
r = r.Push(f.Peek());
}
Debug.Assert(r != null);
Debug.Assert(r.IsEmpty == IsEmpty);
return r;
}
}
}