61 lines
1.2 KiB
C#
61 lines
1.2 KiB
C#
using Qrakhen.Qamp.Core.Collections.Abstractions;
|
|
|
|
namespace Qrakhen.Qamp.Core.Collections;
|
|
|
|
/// <summary>
|
|
/// Push-only Stack that is based on <see cref="Expander{T}"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class PushStack<T> :
|
|
Expander<T>,
|
|
IPush<long, T>,
|
|
IToArray<T>
|
|
{
|
|
public PushStack(int initialCapacity = 0x10)
|
|
{
|
|
Data = new T[initialCapacity];
|
|
}
|
|
|
|
public T? Find(Func<T, bool> callback)
|
|
{
|
|
for (int i = 0; i < Count; i++)
|
|
if (callback(Data[i]))
|
|
return Data[i];
|
|
return default;
|
|
}
|
|
|
|
public void Push(T[] array)
|
|
{
|
|
foreach (T value in array) {
|
|
Add(value);
|
|
}
|
|
}
|
|
|
|
public void Push(IEnumerable<T> array)
|
|
{
|
|
foreach (T value in array) {
|
|
Add(value);
|
|
}
|
|
}
|
|
|
|
public long Push(T value) => Add(value);
|
|
|
|
public bool TryGet(long index, out T value)
|
|
{
|
|
value = default;
|
|
if (index < 0 || index >= Count)
|
|
return false;
|
|
value = Data[index];
|
|
return true;
|
|
}
|
|
|
|
public T[] ToArray() => Data.Subset(0, Count);
|
|
|
|
public T[] Subset(long from, int length)
|
|
{
|
|
T[] result = new T[length];
|
|
Array.Copy(Data, from, result, 0, length);
|
|
return result;
|
|
}
|
|
}
|