Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Util/CloneableStack.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.0 KB
Line 
1//
2// CloneableStack.cs
3//
4// Author:
5//       Mike Krüger <mkrueger@xamarin.com>
6//
7// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25// THE SOFTWARE.
26using System;
27using System.Collections.Generic;
28using System.Collections;
29
30namespace ICSharpCode.NRefactory.CSharp
31{
32  class CloneableStack<T> : IEnumerable<T>, ICollection<T>, ICloneable, IEquatable<CloneableStack<T>>
33  {
34    int count;
35    StackItem top;
36
37    #region IEnumerable[T] implementation
38    public IEnumerator<T> GetEnumerator ()
39    {
40      return new StackItemEnumerator (top);
41    }
42
43    IEnumerator IEnumerable.GetEnumerator ()
44    {
45      return new StackItemEnumerator (top);
46    }
47    #endregion
48
49    #region ICloneable implementation
50    public CloneableStack<T> Clone ()
51    {
52      CloneableStack<T> result = new CloneableStack<T> ();
53      result.count = count;
54      result.top   = top;
55      return result;
56    }
57
58    object ICloneable.Clone ()
59    {
60      return Clone();
61    }
62    #endregion
63
64    public void Clear ()
65    {
66      top = null;
67      count = 0;
68    }
69
70    public void Push (T item)
71    {
72      top = new StackItem (top, item);
73      count++;
74    }
75
76    public T Peek ()
77    {
78      return top.Item;
79    }
80
81    public T Pop ()
82    {
83      T result = top.Item;
84      top = top.Parent;
85      count--;
86      return result;
87    }
88
89    #region IEquatable[T] implementation
90    public bool Equals (CloneableStack<T> other)
91    {
92      return ReferenceEquals (top, other.top);
93    }
94    #endregion
95
96    #region ICollection[T] implementation
97    void ICollection<T>.Add (T item)
98    {
99      Push (item);
100    }
101
102    void ICollection<T>.Clear ()
103    {
104      top = null;
105      count = 0;
106    }
107
108    bool ICollection<T>.Contains (T item)
109    {
110      foreach (T t in this) {
111        if (t.Equals (item))
112          return true;
113      }
114      return false;
115    }
116
117    void ICollection<T>.CopyTo (T[] array, int arrayIndex)
118    {
119      int idx = arrayIndex;
120      foreach (T t in this) {
121        array[idx++] = t;
122      }
123    }
124
125    bool ICollection<T>.Remove (T item)
126    {
127      throw new NotImplementedException ();
128    }
129
130    public int Count {
131      get {
132        return count;
133      }
134    }
135
136    bool ICollection<T>.IsReadOnly {
137      get {
138        return false;
139      }
140    }
141    #endregion
142
143    class StackItem
144    {
145      public readonly StackItem Parent;
146      public readonly T Item;
147
148      public StackItem (StackItem parent, T item)
149      {
150        Parent = parent;
151        Item = item;
152      }
153    }
154
155    class StackItemEnumerator : IEnumerator<T>
156    {
157      StackItem cur, first;
158
159      public StackItemEnumerator (StackItem cur)
160      {
161        this.cur = first = new StackItem (cur, default(T));
162      }
163
164      #region IDisposable implementation
165      void IDisposable.Dispose ()
166      {
167        cur = first = null;
168      }
169      #endregion
170
171      #region IEnumerator implementation
172      bool IEnumerator.MoveNext ()
173      {
174        if (cur == null)
175          return false;
176        cur = cur.Parent;
177        return cur != null;
178      }
179
180      void IEnumerator.Reset ()
181      {
182        cur = first;
183      }
184
185      object IEnumerator.Current {
186        get {
187          return cur.Item;
188        }
189      }
190      #endregion
191
192      #region IEnumerator[T] implementation
193      T IEnumerator<T>.Current {
194        get {
195          return cur.Item;
196        }
197      }
198      #endregion
199    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.