CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.04k stars 298 forks source link

Span2D Constructor overload #929

Open sjMagstim opened 2 months ago

sjMagstim commented 2 months ago

Overview

A constructor for the Span2D<T> to create a 2D view over a Span<T>. Currently only arrays and pointers but I believe a Span would also be suitable. In fact, I think I can see an implementation already there, it's just marked as internal.

API breakdown

public readonly ref partial struct Span2D<T>
{
// ...
    public Span2D(Span<T> span, int height, int width) : this (span, 0, height, width, 0) {...}

    public Span2D(Span<T> span, int offset, int height, int width, int pitch) {...}
// ...
}

Usage example

Span<int> p = stackalloc int[9];
Span2D<int> span = new Span2D<int>(p, 3, 3);

Breaking change?

No

Alternatives

Span<int> span = stackalloc int[6];
Span2D<int> span2d = Span2D<int>.DangerousCreate(ref span[0], 2, 3, 0);
int* p = stackalloc int[6];
Span2D<int> span2d = new Span2D<int>(p, 2, 3, 0);

Additional context

No response

Help us help you

Yes, but only if others can assist

winstxnhdw commented 2 weeks ago

Need this as well.