// // CloneableStack.cs // // Author: // Mike Krüger // // Copyright (c) 2014 Xamarin Inc. (http://xamarin.com) // // 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.Collections; namespace ICSharpCode.NRefactory.CSharp { class CloneableStack : IEnumerable, ICollection, ICloneable, IEquatable> { int count; StackItem top; #region IEnumerable[T] implementation public IEnumerator GetEnumerator () { return new StackItemEnumerator (top); } IEnumerator IEnumerable.GetEnumerator () { return new StackItemEnumerator (top); } #endregion #region ICloneable implementation public CloneableStack Clone () { CloneableStack result = new CloneableStack (); result.count = count; result.top = top; return result; } object ICloneable.Clone () { return Clone(); } #endregion public void Clear () { top = null; count = 0; } public void Push (T item) { top = new StackItem (top, item); count++; } public T Peek () { return top.Item; } public T Pop () { T result = top.Item; top = top.Parent; count--; return result; } #region IEquatable[T] implementation public bool Equals (CloneableStack other) { return ReferenceEquals (top, other.top); } #endregion #region ICollection[T] implementation void ICollection.Add (T item) { Push (item); } void ICollection.Clear () { top = null; count = 0; } bool ICollection.Contains (T item) { foreach (T t in this) { if (t.Equals (item)) return true; } return false; } void ICollection.CopyTo (T[] array, int arrayIndex) { int idx = arrayIndex; foreach (T t in this) { array[idx++] = t; } } bool ICollection.Remove (T item) { throw new NotImplementedException (); } public int Count { get { return count; } } bool ICollection.IsReadOnly { get { return false; } } #endregion class StackItem { public readonly StackItem Parent; public readonly T Item; public StackItem (StackItem parent, T item) { Parent = parent; Item = item; } } class StackItemEnumerator : IEnumerator { StackItem cur, first; public StackItemEnumerator (StackItem cur) { this.cur = first = new StackItem (cur, default(T)); } #region IDisposable implementation void IDisposable.Dispose () { cur = first = null; } #endregion #region IEnumerator implementation bool IEnumerator.MoveNext () { if (cur == null) return false; cur = cur.Parent; return cur != null; } void IEnumerator.Reset () { cur = first; } object IEnumerator.Current { get { return cur.Item; } } #endregion #region IEnumerator[T] implementation T IEnumerator.Current { get { return cur.Item; } } #endregion } } }