// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Mono.TextEditor.Utils
{
///
/// An immutable stack.
///
/// Using 'foreach' on the stack will return the items from top to bottom (in the order they would be popped).
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
[Serializable]
public sealed class ImmutableStack : IEnumerable
{
///
/// Gets the empty stack instance.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "ImmutableStack is immutable")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
public static readonly ImmutableStack Empty = new ImmutableStack();
readonly T value;
readonly ImmutableStack next;
private ImmutableStack()
{
}
private ImmutableStack(T value, ImmutableStack next)
{
this.value = value;
this.next = next;
}
///
/// Pushes an item on the stack. This does not modify the stack itself, but returns a new
/// one with the value pushed.
///
public ImmutableStack Push(T item)
{
return new ImmutableStack(item, this);
}
///
/// Gets the item on the top of the stack.
///
/// The stack is empty.
public T Peek()
{
if (IsEmpty)
throw new InvalidOperationException("Operation not valid on empty stack.");
return value;
}
///
/// Gets the item on the top of the stack.
/// Returns default(T) if the stack is empty.
///
public T PeekOrDefault()
{
return value;
}
///
/// Gets the stack with the top item removed.
///
/// The stack is empty.
public ImmutableStack Pop()
{
if (IsEmpty)
throw new InvalidOperationException("Operation not valid on empty stack.");
return next;
}
///
/// Gets if this stack is empty.
///
public bool IsEmpty {
get { return next == null; }
}
///
/// Gets an enumerator that iterates through the stack top-to-bottom.
///
public IEnumerator GetEnumerator()
{
ImmutableStack t = this;
while (!t.IsEmpty) {
yield return t.value;
t = t.next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
///
public override string ToString()
{
StringBuilder b = new StringBuilder("[Stack");
foreach (T val in this) {
b.Append(' ');
b.Append(val);
}
b.Append(']');
return b.ToString();
}
}
}