2023-04-03 11:04:31 +08:00

170 lines
4.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BF
{
public class BFStack<T> : IEnumerable<T>
{
private BFStackPool<T> nodePool;
private BFNode<T> top;
private int count;
public int Count { get { return count; } }
private EqualityComparer<T> comparer;
private bool valueType = false;
public BFStack(EqualityComparer<T> comp = null)
{
comparer = comp ?? EqualityComparer<T>.Default;
nodePool = new BFStackPool<T>();
count = 0;
valueType = typeof(T).IsValueType;
}
public BFStack(int capacity)
{
comparer = EqualityComparer<T>.Default;
nodePool = new BFStackPool<T>(capacity);
count = 0;
valueType = typeof(T).IsValueType;
}
public BFStack(IEnumerable<T> collection)
{
nodePool = new BFStackPool<T>();
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<T> IEnumerable<T>.GetEnumerator() { return new Enumerator(this); }
IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); }
public struct Enumerator : IEnumerator<T>
{
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<T> stack;
private T cur;
private BFNode<T> top;
public Enumerator(BFStack<T> s)
{
stack = s;
top = s.top;
cur = default(T);
}
}
}
// public class Stack<T> : IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, ICollection
// {
// public Stack();
// public Stack(IEnumerable<T> 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<T>, IEnumerator, IDisposable
// {
// public T Current { get; }
// public void Dispose();
// public bool MoveNext();
// }
// }
}