1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
21
using System.Diagnostics;
24
namespace ICSharpCode.NRefactory.Utils
27
/// An immutable stack.
29
/// Using 'foreach' on the stack will return the items from top to bottom (in the order they would be popped).
31
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
32
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
34
public sealed class ImmutableStack<T> : IEnumerable<T>
37
/// Gets the empty stack instance.
39
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "ImmutableStack is immutable")]
40
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
41
public static readonly ImmutableStack<T> Empty = new ImmutableStack<T>();
44
readonly ImmutableStack<T> next;
46
private ImmutableStack()
50
private ImmutableStack(T value, ImmutableStack<T> next)
57
/// Pushes an item on the stack. This does not modify the stack itself, but returns a new
58
/// one with the value pushed.
60
public ImmutableStack<T> Push(T item)
62
return new ImmutableStack<T>(item, this);
66
/// Gets the item on the top of the stack.
68
/// <exception cref="InvalidOperationException">The stack is empty.</exception>
72
throw new InvalidOperationException("Operation not valid on empty stack.");
77
/// Gets the item on the top of the stack.
78
/// Returns <c>default(T)</c> if the stack is empty.
80
public T PeekOrDefault()
86
/// Gets the stack with the top item removed.
88
/// <exception cref="InvalidOperationException">The stack is empty.</exception>
89
public ImmutableStack<T> Pop()
92
throw new InvalidOperationException("Operation not valid on empty stack.");
97
/// Gets if this stack is empty.
100
get { return next == null; }
104
/// Gets an enumerator that iterates through the stack top-to-bottom.
106
public IEnumerator<T> GetEnumerator()
108
ImmutableStack<T> t = this;
110
yield return t.value;
115
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
117
return this.GetEnumerator();
121
public override string ToString()
123
StringBuilder b = new StringBuilder("[Stack");
124
foreach (T val in this) {