// - edit Vector.tt to modify
#nullable enable
namespace Brutal.Numerics;
///
/// Implements a 2D vector with byte components.
///
[DebuggerTypeProxy(typeof(VectorDebugView2))]
public partial struct byte2 : IVector2
, IIntegerVector
{
///
public static int ComponentSize => sizeof(byte);
/// Gets the number of components.
public static int ComponentCount => 2;
///
[XmlAttribute]
public byte X;
///
[XmlAttribute]
public byte Y;
byte IVector2.X { readonly get => X; set => X = value; }
byte IVector2.Y { readonly get => Y; set => Y = value; }
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte2(byte x, byte y)
{
X = x;
Y = y;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte2(byte xy)
: this(xy, xy) { }
/// Creates a vector from the leading 2 elements of a span.
/// The elements to read; must contain at least 2, and any beyond that are ignored.
/// holds fewer than 2 elements.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte2(ReadOnlySpan span)
{
if (span.Length < 2)
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 2.");
this = Unsafe.ReadUnaligned(ref Unsafe.As(ref MemoryMarshal.GetReference(span)));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static byte2 IComponentTuple.Create(ReadOnlySpan span) => new(span);
///
public static byte2 UnitX => new(1, 0);
///
public static byte2 UnitY => new(0, 1);
///
public byte this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
return Unsafe.Add(ref Unsafe.AsRef(in X), index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
Unsafe.Add(ref X, index) = value;
}
}
public readonly void Deconstruct(out byte x, out byte y)
{
x = X;
y = Y;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span AsSpan()
{
return MemoryMarshal.CreateSpan(ref Unsafe.As(ref this), 2);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan AsReadOnlySpan()
{
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in this)), 2);
}
}