using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BF { public class BFStack : IEnumerable { private BFStackPool nodePool; private BFNode top; private int count; public int Count { get { return count; } } private EqualityComparer comparer; private bool valueType = false; public BFStack(EqualityComparer comp = null) { comparer = comp ?? EqualityComparer.Default; nodePool = new BFStackPool(); count = 0; valueType = typeof(T).IsValueType; } public BFStack(int capacity) { comparer = EqualityComparer.Default; nodePool = new BFStackPool(capacity); count = 0; valueType = typeof(T).IsValueType; } public BFStack(IEnumerable collection) { nodePool = new BFStackPool(); count = 0; foreach (var item in collection) { Push(item); } } public void Push(T item) { var node = nodePool.Alloc(item); node.next = top; top = node; ++count; } public T Pop() { //BFLog.LogAssert(count != 0, "BFStack Can't Pop! Count : " + count.ToString()); var temp = top; top = top.next; var item = temp.item; nodePool.Free(temp); --count; return item; } public T Peek() { BFLog.LogAssert(count != 0, "BFStack Can't Peek! Count : " + count.ToString()); return top.item; } public bool Contains(T t) { var cur = top; if (!valueType && t == null) { while (null != cur) { if (null == t) { if (null == cur.item) return true; } } } else { while (null != cur) { if (comparer.Equals(t, cur.item)) return true; cur = cur.next; } } return false; } public Enumerator GetEnumerator() { return new Enumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); } public struct Enumerator : IEnumerator { public T Current { get { return cur; } } object IEnumerator.Current { get { return cur; } } public bool MoveNext() { if (null == top) return false; cur = top.item; top = top.next; return true; } public void Dispose() { stack = null; top = null; cur = default(T); } public void Reset() { top = stack.top; cur = default(T); } private BFStack stack; private T cur; private BFNode top; public Enumerator(BFStack s) { stack = s; top = s.top; cur = default(T); } } } // public class Stack : IEnumerable, IEnumerable, IReadOnlyCollection, ICollection // { // public Stack(); // public Stack(IEnumerable collection); // public Stack(int capacity); // public int Count { get; } // public void Clear(); // public bool Contains(T item); // public void CopyTo(T[] array, int arrayIndex); // public Enumerator GetEnumerator(); // public T Peek(); // public T Pop(); // public void Push(T item); // public T[] ToArray(); // public void TrimExcess(); // public struct Enumerator : IEnumerator, IEnumerator, IDisposable // { // public T Current { get; } // public void Dispose(); // public bool MoveNext(); // } // } }