// - edit Vector.tt to modify
#nullable enable
namespace Brutal.Numerics;
///
/// Implements a 2D vector with sbyte components.
///
[DebuggerTypeProxy(typeof(VectorDebugView2))]
public partial struct sbyte2 : IVector2
, ISignedIntegerVector
{
///
public static int ComponentSize => sizeof(sbyte);
/// Gets the number of components.
public static int ComponentCount => 2;
///
[XmlAttribute]
public sbyte X;
///
[XmlAttribute]
public sbyte Y;
sbyte IVector2.X { readonly get => X; set => X = value; }
sbyte IVector2.Y { readonly get => Y; set => Y = value; }
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte2(sbyte x, sbyte y)
{
X = x;
Y = y;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte2(sbyte 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 sbyte2(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 sbyte2 IComponentTuple.Create(ReadOnlySpan span) => new(span);
///
public static sbyte2 UnitX => new(1, 0);
///
public static sbyte2 UnitY => new(0, 1);
///
public sbyte 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 sbyte x, out sbyte 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);
}
}