~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/Utils/ImmutableStack.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
 
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
3
 
 
4
 
using System;
5
 
using System.Collections.Generic;
6
 
using System.Diagnostics;
7
 
using System.Text;
8
 
 
9
 
namespace ICSharpCode.NRefactory.Utils
10
 
{
11
 
        /// <summary>
12
 
        /// An immutable stack.
13
 
        /// 
14
 
        /// Using 'foreach' on the stack will return the items from top to bottom (in the order they would be popped).
15
 
        /// </summary>
16
 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
17
 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
18
 
        [Serializable]
19
 
        public sealed class ImmutableStack<T> : IEnumerable<T>
20
 
        {
21
 
                /// <summary>
22
 
                /// Gets the empty stack instance.
23
 
                /// </summary>
24
 
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "ImmutableStack is immutable")]
25
 
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
26
 
                public static readonly ImmutableStack<T> Empty = new ImmutableStack<T>();
27
 
                
28
 
                readonly T value;
29
 
                readonly ImmutableStack<T> next;
30
 
                
31
 
                private ImmutableStack()
32
 
                {
33
 
                }
34
 
                
35
 
                private ImmutableStack(T value, ImmutableStack<T> next)
36
 
                {
37
 
                        this.value = value;
38
 
                        this.next = next;
39
 
                }
40
 
                
41
 
                /// <summary>
42
 
                /// Pushes an item on the stack. This does not modify the stack itself, but returns a new
43
 
                /// one with the value pushed.
44
 
                /// </summary>
45
 
                public ImmutableStack<T> Push(T item)
46
 
                {
47
 
                        return new ImmutableStack<T>(item, this);
48
 
                }
49
 
                
50
 
                /// <summary>
51
 
                /// Gets the item on the top of the stack.
52
 
                /// </summary>
53
 
                /// <exception cref="InvalidOperationException">The stack is empty.</exception>
54
 
                public T Peek()
55
 
                {
56
 
                        if (IsEmpty)
57
 
                                throw new InvalidOperationException("Operation not valid on empty stack.");
58
 
                        return value;
59
 
                }
60
 
                
61
 
                /// <summary>
62
 
                /// Gets the item on the top of the stack.
63
 
                /// Returns <c>default(T)</c> if the stack is empty.
64
 
                /// </summary>
65
 
                public T PeekOrDefault()
66
 
                {
67
 
                        return value;
68
 
                }
69
 
                
70
 
                /// <summary>
71
 
                /// Gets the stack with the top item removed.
72
 
                /// </summary>
73
 
                /// <exception cref="InvalidOperationException">The stack is empty.</exception>
74
 
                public ImmutableStack<T> Pop()
75
 
                {
76
 
                        if (IsEmpty)
77
 
                                throw new InvalidOperationException("Operation not valid on empty stack.");
78
 
                        return next;
79
 
                }
80
 
                
81
 
                /// <summary>
82
 
                /// Gets if this stack is empty.
83
 
                /// </summary>
84
 
                public bool IsEmpty {
85
 
                        get { return next == null; }
86
 
                }
87
 
                
88
 
                /// <summary>
89
 
                /// Gets an enumerator that iterates through the stack top-to-bottom.
90
 
                /// </summary>
91
 
                public IEnumerator<T> GetEnumerator()
92
 
                {
93
 
                        ImmutableStack<T> t = this;
94
 
                        while (!t.IsEmpty) {
95
 
                                yield return t.value;
96
 
                                t = t.next;
97
 
                        }
98
 
                }
99
 
                
100
 
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
101
 
                {
102
 
                        return this.GetEnumerator();
103
 
                }
104
 
                
105
 
                /// <inheritdoc/>
106
 
                public override string ToString()
107
 
                {
108
 
                        StringBuilder b = new StringBuilder("[Stack");
109
 
                        foreach (T val in this) {
110
 
                                b.Append(' ');
111
 
                                b.Append(val);
112
 
                        }
113
 
                        b.Append(']');
114
 
                        return b.ToString();
115
 
                }
116
 
        }
117
 
}