using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections;
///
/// Pop-Only stack with a fixed array that is gradually reduced.
///
public class PopStack :
IEnumerable,
IPop,
IPeekable
{
private readonly T[] _data;
private int _position;
public int Position => _position;
public int Length => _data.Length;
public bool Done => _position >= _data.Length;
public PopStack(IEnumerable data)
{
_data = data.ToArray();
_position = 0;
}
public T Pop() => _data[_position++];
public T Peek(int delta = 0) => _data[_position + delta];
public bool CanPeek(int delta = 0)
=> _position + delta > -1 && _position + delta < _data.Length;
public IEnumerator GetEnumerator()
{
foreach (var item in _data)
yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}