using System.Collections;
using Brutal.Attributes;
using Brutal.Memory;
using Brutal.Pointers;
namespace Brutal.Collections;
///
/// A native-memory backed list with a fixed type
///
/// Element type
public class NativeList : INativeList
where TValueType : unmanaged
{
~NativeList()
{
Dispose();
}
private readonly NativeStrideList _nativeList;
public NativeList(ElementCount capacity = default)
{
_nativeList = new NativeStrideList(Stride, capacity);
}
public NativeList(ReadOnlySpan nativeList)
{
_nativeList = NativeStrideList.FromSpan(Stride, nativeList);
}
public void Dispose()
{
_nativeList.Dispose();
GC.SuppressFinalize(this);
}
public bool IsNull()
{
return _nativeList.IsNull();
}
public ByteSize Stride => ByteSize.Of();
public bool IsEmpty => Count == 0;
public ref TValueType this[Index index]
{
get
{
var actualIndex = index.GetOffset(Count);
return ref _nativeList.AsSpan()[actualIndex];
}
}
public void Add(TValueType item)
{
_nativeList.Add(item);
}
public void AddRange(Span items)
{
_nativeList.AddRange(items);
}
public void AddRange(ReadOnlySpan items)
{
_nativeList.AddRange(items);
}
///
/// Adds a range of bytes to the end of the list. The length of items must be a multiple of stride
///
public void AddRange(T items)
where T : ISpanLike
{
_nativeList.AddRange(items.AsSpan());
}
public void Insert(ElementCount index, TValueType item)
{
_nativeList.Insert(index, item);
}
public void RemoveAt(ElementCount index)
{
_nativeList.RemoveAt(index);
}
public Ptr Ptr => _nativeList.Ptr.Cast();
Ptr ISpanLike.Ptr => Ptr;
ElementCount ISpanLike.Length => Count;
public ElementCount Count
{
get => _nativeList.Count;
set => _nativeList.Count = value;
}
public ElementCount Capacity
{
get => _nativeList.Capacity;
set => _nativeList.Capacity = value;
}
public void Clear()
{
_nativeList.Clear();
}
public void TrimExcess()
{
_nativeList.TrimExcess();
}
public void EnsureCapacity(ElementCount capacity)
{
_nativeList.EnsureCapacity(capacity);
}
public void Free()
{
_nativeList.Free();
}
public IEnumerator GetEnumerator()
{
for (var i = 0; i < Count; ++i)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Span Slice(ElementCount start, ElementCount length)
{
return AsSpan().Slice(start, length);
}
public Span Slice(ElementCount start)
{
return AsSpan()[(int)start..];
}
[Fixed]
public Span this[Range range] => AsSpan()[range];
[return: Fixed]
public Span AsSpan()
{
return _nativeList.AsSpan();
}
Span ISpanLike.AsSpan()
{
return _nativeList.AsSpan();
}
public NativeStrideSpan AsNativeSpan()
{
return new NativeStrideSpan(Ptr, Count, Stride);
}
protected virtual void Dispose(bool isDisposing)
{
if (!isDisposing)
return;
_nativeList.Dispose();
}
public ref TValueType GetPinnableReference()
{
return ref this[0];
}
}