qamp/Qrakhen.Qamp.Core/Collections/PopStack.cs

44 lines
985 B
C#

using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections;
/// <summary>
/// Pop-Only stack with a fixed array that is gradually reduced.
/// </summary>
public class PopStack<T> :
IEnumerable<T>,
IPop<T>,
IPeekable<T>
{
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<T> 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<T> GetEnumerator()
{
foreach (var item in _data)
yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}