Skip to content

Commit 5ec29c6

Browse files
committed
Span2D ref structs
1 parent c6a1240 commit 5ec29c6

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace AssetRipper.TextureDecoder;
2+
3+
public readonly ref struct ReadOnlySpan2D<T>
4+
{
5+
public int Width { get; }
6+
public int Height { get; }
7+
public ReadOnlySpan<T> Data { get; }
8+
9+
public ReadOnlySpan2D(ReadOnlySpan<T> data, int width, int height)
10+
{
11+
Validate(data, width, height);
12+
Data = data;
13+
Width = width;
14+
Height = height;
15+
16+
static void Validate(ReadOnlySpan<T> data, int width, int height)
17+
{
18+
ArgumentOutOfRangeException.ThrowIfNegative(width);
19+
ArgumentOutOfRangeException.ThrowIfNegative(height);
20+
if (data.Length != width * height)
21+
{
22+
throw new ArgumentException("Data length does not match width and height.", nameof(data));
23+
}
24+
}
25+
}
26+
27+
//public ReadOnlySpan<T> this[int y] => Data.Slice(y * Width, Width);
28+
29+
public T this[int x, int y] => Data[y * Width + x];
30+
31+
public static explicit operator ReadOnlySpan2D<T>(T[] array) => new(array, array.Length, 1);
32+
33+
public static explicit operator ReadOnlySpan2D<T>(ReadOnlySpan<T> span) => new(span, span.Length, 1);
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace AssetRipper.TextureDecoder;
2+
3+
public readonly ref struct Span2D<T>
4+
{
5+
public int Width { get; }
6+
public int Height { get; }
7+
public Span<T> Data { get; }
8+
9+
public Span2D(Span<T> data, int width, int height)
10+
{
11+
Validate(data, width, height);
12+
Data = data;
13+
Width = width;
14+
Height = height;
15+
16+
static void Validate(Span<T> data, int width, int height)
17+
{
18+
ArgumentOutOfRangeException.ThrowIfNegative(width);
19+
ArgumentOutOfRangeException.ThrowIfNegative(height);
20+
if (data.Length != width * height)
21+
{
22+
throw new ArgumentException("Data length does not match width and height.", nameof(data));
23+
}
24+
}
25+
}
26+
27+
//public Span<T> this[int y] => Data.Slice(y * Width, Width);
28+
29+
public T this[int x, int y]
30+
{
31+
get => Data[y * Width + x];
32+
set => Data[y * Width + x] = value;
33+
}
34+
35+
public static implicit operator ReadOnlySpan2D<T>(Span2D<T> span) => new(span.Data, span.Width, span.Height);
36+
37+
public static explicit operator Span2D<T>(T[] array) => new(array, array.Length, 1);
38+
39+
public static explicit operator Span2D<T>(Span<T> span) => new(span, span.Length, 1);
40+
}

0 commit comments

Comments
 (0)