Update ListStack.cs

This commit is contained in:
_nebula 2023-05-07 18:23:40 +01:00
parent 20fe2c4fe7
commit 83a35d833f

View File

@ -6,7 +6,16 @@ namespace QSB.Utility;
public class ListStack<T> : IEnumerable<T>
{
private readonly List<T> _items = new();
private List<T> _items = new();
public int Count => _items.Count;
public ListStack() { }
public ListStack(int capacity)
{
_items = new List<T>(capacity);
}
public void Clear()
=> _items.Clear();
@ -33,6 +42,26 @@ public class ListStack<T> : IEnumerable<T>
return default;
}
public T RemoveFirstElementAndShift()
{
if (_items.Count == 0)
{
return default;
}
var firstElement = _items[0];
if (_items.Count == 0)
{
return firstElement;
}
// shift list left
_items = _items.GetRange(1, _items.Count - 1);
return firstElement;
}
public T Peek() => _items.Count > 0
? _items[_items.Count - 1]
: default;