2013-10-27 07:54:00 +00:00
|
|
|
|
namespace BizHawk.Common
|
2011-01-11 02:55:51 +00:00
|
|
|
|
{
|
2011-06-19 23:39:25 +00:00
|
|
|
|
public class MruStack<T>
|
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
private readonly T[] _store;
|
|
|
|
|
private int _count;
|
|
|
|
|
private int _head;
|
2011-06-19 23:39:25 +00:00
|
|
|
|
|
|
|
|
|
public MruStack(int capacity)
|
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
_store = new T[capacity];
|
2011-06-19 23:39:25 +00:00
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 18:57:58 +00:00
|
|
|
|
public int Count => _count;
|
2014-02-04 21:15:33 +00:00
|
|
|
|
|
2017-04-15 20:37:30 +00:00
|
|
|
|
public void Clear()
|
2011-06-19 23:39:25 +00:00
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
_head = 0;
|
|
|
|
|
_count = 0;
|
|
|
|
|
for (int i = 0; i < _store.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
_store[i] = default(T);
|
|
|
|
|
}
|
2011-06-19 23:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Push(T value)
|
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
_store[_head] = value;
|
|
|
|
|
_head = (_head + 1) % _store.Length;
|
2011-06-19 23:39:25 +00:00
|
|
|
|
|
2014-02-04 21:15:33 +00:00
|
|
|
|
if (_count < _store.Length)
|
|
|
|
|
{
|
|
|
|
|
_count++;
|
|
|
|
|
}
|
2011-06-19 23:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Pop()
|
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
if (_count == 0)
|
|
|
|
|
{
|
2011-06-19 23:39:25 +00:00
|
|
|
|
return default(T);
|
2014-02-04 21:15:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_head--;
|
|
|
|
|
if (_head < 0)
|
|
|
|
|
{
|
|
|
|
|
_head = _store.Length - 1;
|
|
|
|
|
}
|
2011-06-19 23:39:25 +00:00
|
|
|
|
|
2014-02-04 21:15:33 +00:00
|
|
|
|
_count--;
|
|
|
|
|
T value = _store[_head];
|
|
|
|
|
_store[_head] = default(T);
|
2011-06-19 23:39:25 +00:00
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasElements()
|
|
|
|
|
{
|
2014-02-04 21:15:33 +00:00
|
|
|
|
return _count > 0;
|
2011-06-19 23:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|