Stack<TValue>
using System;
using System.Collections.Generic;
using System.Linq;
namespace Stashbox.Utils.Data
{
internal class Stack<TValue> : ExpandableArray<TValue>
{
public new static Stack<TValue> FromEnumerable(IEnumerable<TValue> enumerable)
{
return new Stack<TValue>(enumerable);
}
public Stack()
{
}
public Stack(IEnumerable<TValue> initial)
: base(EnumerableExtensions.CastToArray<TValue>(initial))
{
}
public TValue Pop()
{
if (Length == 0)
return default(TValue);
TValue result = Repository[Length - 1];
Repository[--Length] = default(TValue);
return result;
}
public void PushBack(TValue item)
{
if (Length == 0) {
Length = 1;
Repository = new TValue[1] {
item
};
}
EnsureSize(1);
TValue[] array = new TValue[Length];
array[0] = item;
Array.Copy(Repository, 0, array, 1, Length - 1);
Repository = array;
}
public TValue PeekBack()
{
if (Length == 0)
return default(TValue);
return Repository[0];
}
public void ReplaceBack(TValue value)
{
Repository[0] = value;
}
}
}