servicepoint-tanks/DisplayCommands/ByteGrid.cs

43 lines
1.3 KiB
C#
Raw Normal View History

2024-04-12 14:29:26 +02:00
using System.Diagnostics;
namespace DisplayCommands;
public sealed class ByteGrid(ushort width, ushort height) : IEquatable<ByteGrid>
2024-04-12 14:29:26 +02:00
{
public ushort Height { get; } = height;
2024-04-12 14:29:26 +02:00
public ushort Width { get; } = width;
2024-04-12 14:29:26 +02:00
internal Memory<byte> Data { get; } = new byte[width * height].AsMemory();
public byte this[ushort x, ushort y]
{
get => Data.Span[GetIndex(x, y)];
2024-04-12 14:29:26 +02:00
set => Data.Span[GetIndex(x, y)] = value;
}
2024-04-13 14:08:51 +02:00
public bool Equals(ByteGrid? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Height == other.Height && Width == other.Width && Data.Span.SequenceEqual(other.Data.Span);
}
2024-04-12 14:29:26 +02:00
private int GetIndex(ushort x, ushort y)
{
Debug.Assert(x < Width);
Debug.Assert(y < Height);
return x + y * Width;
}
public void Clear() => Data.Span.Clear();
public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is ByteGrid other && Equals(other));
2024-04-13 14:08:51 +02:00
public override int GetHashCode() => HashCode.Combine(Height, Width, Data);
2024-04-13 14:08:51 +02:00
public static bool operator ==(ByteGrid? left, ByteGrid? right) => Equals(left, right);
2024-04-13 14:08:51 +02:00
public static bool operator !=(ByteGrid? left, ByteGrid? right) => !Equals(left, right);
2024-04-13 14:08:51 +02:00
}