Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# BigStack Class Not Limited to 2GB
[Serializable]
public class BigStack<T> : IEnumerable<T>
{
private const ulong DEFAULTSIZE = 32;
public BigArray<T> _array;
public BigStack() : this(DEFAULTSIZE)
{
}
public BigStack(ulong capacity)
{
_array = new BigArray<T>(capacity);
Count = 0;
ResizeCount = 0;
}
public BigStack(ICollection col)
{
if (col != null)
{
_array = new BigArray<T>((ulong) col.Count);
Count = 0;
ResizeCount = 0;
var en = col.GetEnumerator();
while (en.MoveNext()) Push((T) en.Current);
}
}
public BigStack(IEnumerable<T> col)
{
if (col != null)
{
_array = new BigArray<T>(DEFAULTSIZE);
Count = 0;
ResizeCount = 0;
using (var en = col.GetEnumerator())
{
while (en.MoveNext()) Push(en.Current);
}
}
}
public ulong Count { get; private set; }
public ulong MaxCount { get; private set; }
public ulong ResizeCount { get; private set; }
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
public void Clear()
{
_array = new BigArray<T>(Count);
Count = 0;
ResizeCount = 0;
}
public bool Contains(T item)
{
var size = Count;
var equalityComparer = EqualityComparer<T>.Default;
while (size-- > 0)
if (item == null)
{
if (_array[size] == null)
return true;
}
else if (_array[size] != null && equalityComparer.Equals(_array[size], item))
{
return true;
}
return false;
}
public void Trim()
{
if (Count == _array.Length)
return;
var TArray = new BigArray<T>(Count);
for (ulong i = 0; i < _array.Count; ++i)
TArray[i] = _array[i];
_array = TArray;
}
public T Peek()
{
return _array[Count - 1];
}
public T Pop()
{
Count--;
var TVal = _array[Count];
_array[Count] = default(T);
return TVal;
}
public void Push(T item)
{
if (Count == _array.Length)
{
ResizeCount++;
var newSize = _array.Length == 0 ? DEFAULTSIZE : 2 * _array.Length;
var TArray = new BigArray<T>(newSize);
for (ulong i = 0; i < _array.Count; ++i)
TArray[i] = _array[i];
_array = TArray;
}
_array[Count] = item;
Count++;
if (Count > MaxCount)
MaxCount = Count;
}
public void PushMany(IEnumerable<T> collection)
{
if (collection is ICollection<T> objs)
{
Count = 0;
_array = new BigArray<T>((ulong) objs.Count);
foreach (var obj in collection)
Push(obj);
}
}
[Serializable]
public struct Enumerator : IEnumerator<T>
{
private readonly BigStack<T> _stack;
private ulong _index;
public T Current { get; private set; }
object IEnumerator.Current => Current;
internal Enumerator(BigStack<T> stack)
{
_stack = stack;
_index = ulong.MaxValue;
Current = default(T);
}
public void Dispose()
{
_index = ulong.MaxValue - 1;
}
public bool MoveNext()
{
if (_index == ulong.MaxValue)
{
_index = _stack.Count - 1;
if (_index < ulong.MaxValue - 1)
Current = _stack._array[_index];
return _index < ulong.MaxValue - 1;
}
if (_index == ulong.MaxValue - 1)
return false;
_index--;
Current = !(_index < ulong.MaxValue - 1) ? default(T) : _stack._array[_index];
return _index < ulong.MaxValue - 1;
}
void IEnumerator.Reset()
{
_index = ulong.MaxValue;
Current = default(T);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment