using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections;
///
/// Expanding Collection based on doubling capacity allocations when adding elements.
///
///
/// Not implementing because it expects to return void,
/// and we don't need that here in Q&.
///
///
public abstract class Expander :
IEnumerable,
IGetSet,
IAdd
{
protected T[] Data;
public long Count { get; protected set; } = 0;
public long Capacity => Data.Length;
protected Expander(int capacity = 0x10)
{
Data = new T[capacity];
}
protected Expander(IEnumerable data)
{
Data = data.ToArray();
Count = Data.Length;
}
public T this[long index] {
get => Get(index);
set => Set(index, value);
}
public T Get(long index) => Data[index];
public void Set(long index, T value)
{
Prepare(index);
Data[index] = value;
}
public long Add(T value)
{
Prepare(Count);
Data[Count] = value;
return Count++;
}
protected void Prepare(long position)
{
while (position >= Data.Length) {
T[] grown = new T[Data.Length * 2];
Array.Copy(Data, grown, Count);
Data = grown;
}
}
public IEnumerator GetEnumerator()
{
foreach (T? item in Data) {
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}