This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit b578433e37
2 changed files with 784842 additions and 0 deletions

View file

@ -0,0 +1,96 @@
using Brutal.Attributes;
using Brutal.Memory;
using Brutal.Pointers;
using Brutal.Pointers.Extensions;
namespace Brutal.Collections;
/// <summary>
/// An untyped span to unsafe memory, however with a stride.
/// </summary>
public readonly ref struct NativeStrideSpan(Ptr inData, ElementCount inLength, ByteSize inStride) : ISpanLike
{
/// <summary>
/// The pointer to the first element of the span.
/// </summary>
public Ptr Ptr => inData;
/// <summary>
/// The length of the span.
/// </summary>
public ElementCount Length => inLength;
/// <summary>
/// The stride of each element.
/// </summary>
public ByteSize Stride => inStride;
public bool IsEmpty => Length == 0;
/// <summary>
/// The total size of the span in bytes.
/// </summary>
public ByteSize ByteSize => Length * Stride;
/// <summary>
/// Slice the span into a new raw span.
/// </summary>
public NativeStrideSpan Slice(int start, int length)
{
return new NativeStrideSpan(Ptr.Offset(start * Stride), length, Stride);
}
/// <summary>
/// Create a new raw span with a different stride.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public NativeStrideSpan WithStride(ByteSize stride)
{
if (stride <= 0)
throw new ArgumentOutOfRangeException(nameof(stride));
var prevSize = ByteSize;
return new NativeStrideSpan(Ptr, prevSize.ToElementCount(stride), stride);
}
/// <inheritdoc />
public bool IsNull() => Ptr.IsNull();
/// <summary>
/// Create a new raw span from a managed span.
/// </summary>
public static unsafe NativeStrideSpan FromSpan<T>([Fixed] ReadOnlySpan<T> span)
where T : unmanaged
{
return new NativeStrideSpan(span.AsPointer(), span.Length, ByteSize.Of<T>());
}
/// <summary>
/// Create a new raw span from a managed span.
/// </summary>
public static unsafe NativeStrideSpan FromSpan<T>([Fixed] Span<T> span)
where T : unmanaged
{
return new NativeStrideSpan(span.AsPointer(), span.Length, ByteSize.Of<T>());
}
/// <summary>
/// Slice the span into a new raw span.
/// </summary>
public NativeStrideSpan this[int start, int end] => Slice(start, end - start);
[return: Fixed]
public unsafe Span<T> AsSpan<T>()
where T : unmanaged
{
if (typeof(T) == typeof(byte))
return new Span<T>(Ptr.Cast<T>(), ElementCount.Of(Length * Stride));
var size = ByteSize.Of<T>();
if (size != Stride)
throw new InvalidOperationException("Stride must be equal to the size of the type, or type must be byte.");
return new Span<T>(Ptr.Cast<T>(), Length);
}
public NativeStrideSpan this[Range start] => Slice(start.Start.Value, start.End.Value - start.Start.Value);
}

View file

@ -0,0 +1,53 @@
using System.Buffers;
using System.Runtime.CompilerServices;
namespace Brutal.Collections.Experimental;
/// <summary>
/// Stackalloc-or-rent scratch buffer.
/// <see cref="Span"/> is backed by the stack buffer if it's big enough,
/// else by an array rented from <see cref="ArrayPool{T}"/>.
/// <see cref="Dispose"/> returns any rented array to the pool.
/// </summary>
/// <remarks>Usage:<code>
/// using (var scratch = new ScratchSpan&lt;T&gt;(requiredLength, stackalloc T[256]))
/// {
/// var buf = scratch.Span; // length == required, backed by stack or pool
/// // ...
/// }
/// </code></remarks>
public readonly ref struct ScratchSpan<T>
{
private readonly T[]? _rented;
/// <summary>The scratch span of exactly <c>requiredLength</c> elements.</summary>
public Span<T> Span { get; }
public static implicit operator Span<T>(ScratchSpan<T> scratch) => scratch.Span;
/// <summary>Construct a ScratchSpan, get the result in <see cref="Span"/>.</summary>
/// <param name="requiredLength">Number of elements the caller needs.</param>
/// <param name="stackBuffer">Stack-allocated tentative buffer to use if large enough, otherwise an array is rented.</param>
public ScratchSpan(int requiredLength, Span<T> stackBuffer)
{
Span = requiredLength <= stackBuffer.Length ? stackBuffer : _rented = ArrayPool<T>.Shared.Rent(requiredLength);
Span = Span[..requiredLength];
}
/// <summary>Alternative form where <see cref="Span"/> is output directly to <paramref name="span"/>.</summary>
/// <param name="requiredLength">Number of elements the caller needs.</param>
/// <param name="stackBuffer">Stack-allocated tentative buffer to use if large enough, otherwise an array is rented.</param>
/// <param name="span">Output span of exactly <paramref name="requiredLength"/> elements, backed by either <paramref name="stackBuffer"/> or a rented array.</param>
public ScratchSpan(int requiredLength, Span<T> stackBuffer, out Span<T> span)
: this(requiredLength, stackBuffer)
{
span = Span;
}
/// Returns any rented array to the pool. No-op if the stack buffer was used.
public void Dispose()
{
if (_rented != null)
ArrayPool<T>.Shared.Return(_rented, clearArray: RuntimeHelpers.IsReferenceOrContainsReferences<T>());
}
}