using Brutal.Attributes;
using Brutal.Memory;
using Brutal.Pointers;
namespace Brutal.Collections;
///
/// A native-memory backed list with a fixed stride
///
public class NativeStrideList : INativeList
{
~NativeStrideList()
{
Dispose(false);
}
private readonly NativeList _nativeList;
///
/// The size of each element in bytes
///
public ByteSize Stride { get; }
public bool IsEmpty => Count == 0;
///
/// A raw pointer to the underlying memory
///
public Ptr Ptr => _nativeList.Ptr;
ElementCount ISpanLike.Length => Count;
///
/// The number of elements in the list
///
public ElementCount Count
{
get => _nativeList.Size.ToElementCount(Stride);
set => _nativeList.Size = value * Stride;
}
///
/// The number of elements that can be stored in the list without resizing
///
public ElementCount Capacity
{
get => _nativeList.Capacity.ToElementCount(Stride);
set => _nativeList.Capacity = value * Stride;
}
// ReSharper disable once UnusedMember.Local
private NativeStrideList() => throw new InvalidOperationException("NativeStrideList cannot be default constructed");
///
/// Constructs a new NativeStrideList with the given stride and initial capacity
///
public NativeStrideList(ByteSize stride, ElementCount capacity = default)
{
Stride = stride;
_nativeList = new NativeList();
if (stride < 1)
throw new ArgumentOutOfRangeException(nameof(stride), stride, "stride must be greater than 0");
_nativeList.EnsureCapacity(Stride * capacity);
}
public static NativeStrideList FromSpan(ByteSize stride, TSpanLike span)
where TSpanLike : ISpanLike, allows ref struct
{
var outList = new NativeStrideList(stride);
outList.AddRange(span.AsSpan());
return outList;
}
public static NativeStrideList FromSpan(TSpanLike span)
where TSpanLike : ISpanLike, allows ref struct
{
return FromSpan(span.Stride, span);
}
public static NativeStrideList FromSpan(ByteSize stride, ReadOnlySpan span)
where TValueType : unmanaged
{
var outList = new NativeStrideList(stride);
outList.AddRange(span);
return outList;
}
public static NativeStrideList FromSpan(ReadOnlySpan span)
where TValueType : unmanaged
{
return FromSpan(ByteSize.Of(), span);
}
// This class does not track if it's been disposed of before, because
// _nativeList.Dispose() is idempotent. Should that or out implementation
// change, then you may need to implement the Dispose pattern in this class.
///
/// Disposes the native memory.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Checks if the native list is null
///
public bool IsNull()
{
return _nativeList.IsNull();
}
///
/// Clears the list, setting count to 0 but preserving capacity
///
public void Clear()
{
_nativeList.Clear();
}
///
/// Trim the capacity to the actual number of elements in the list
///
public void TrimExcess()
{
_nativeList.TrimExcess();
}
///
/// Ensure the list can hold at least capacity elements without resizing
///
public void EnsureCapacity(ElementCount capacity)
{
_nativeList.EnsureCapacity(capacity * Stride);
}
///
/// Frees the native memory and resets the list
///
public void Free()
{
_nativeList.Free();
}
public NativeStrideSpan AsNativeSpan()
{
return new NativeStrideSpan(Ptr, Count, Stride);
}
public void AddRange(Span items)
{
AddRange((ReadOnlySpan)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(ReadOnlySpan items)
{
if (ByteSize.Of(items) % Stride != 0)
throw new ArgumentException("Items length must be a multiple of stride", nameof(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, allows ref struct
{
if (ByteSize.Of(items) % Stride != 0)
throw new ArgumentException("Items length must be a multiple of stride", nameof(items));
_nativeList.AddRange(items);
}
///
/// Adds an item to the end of the list
///
public void Add(T item)
where T : unmanaged
{
CheckStride();
_nativeList.Add(item);
}
public void AddRange(Span items)
where T : unmanaged
{
AddRange((ReadOnlySpan)items);
}
///
/// Adds a range of items to the end of the list
///
public void AddRange(ReadOnlySpan items)
where T : unmanaged
{
CheckStride();
_nativeList.AddRange(items);
}
///
/// Inserts an item at the given index, shifting all later items up by one
///
public void Insert(ElementCount index, T item)
where T : unmanaged
{
CheckStride();
_nativeList.Insert(Stride * index, item);
}
///
/// Removes the item at the given index, shifting all later items down by one
///
public void RemoveAt(ElementCount index)
where T : unmanaged
{
CheckStride();
_nativeList.RemoveAt(Stride * index, Stride);
}
private void CheckStride()
where T : unmanaged
{
CheckStride(ByteSize.Of());
}
private void CheckStride(ByteSize stride)
{
if (Stride != stride)
throw new ArgumentException($"NativeStrideList expected stride: {Stride}, but got: {stride}");
}
[return: Fixed]
public Span AsSpan()
where T : unmanaged
{
return _nativeList.AsSpan();
}
protected virtual void Dispose(bool isDisposing)
{
if (!isDisposing)
return;
_nativeList.Dispose();
}
}