using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace ImageSharp.Formats
{
///
/// Like corefxlab Span, but with an AddOffset() method for efficiency.
/// TODO: When Span will be official, consider replacing this class!
///
///
///
internal struct MutableSpan
{
public T[] Data;
public int Offset;
public int TotalCount => Data.Length - Offset;
public MutableSpan(int size, int offset = 0)
{
Data = new T[size];
Offset = offset;
}
public MutableSpan(T[] data, int offset = 0)
{
Data = data;
Offset = offset;
}
public T this[int idx]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)] get { return Data[idx + Offset]; }
[MethodImpl(MethodImplOptions.AggressiveInlining)] set { Data[idx + Offset] = value; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MutableSpan Slice(int offset)
{
return new MutableSpan(Data, Offset + offset);
}
public static implicit operator MutableSpan(T[] data) => new MutableSpan(data, 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddOffset(int offset)
{
Offset += offset;
}
}
internal static class MutableSpanExtensions
{
public static MutableSpan Slice(this T[] array, int offset) => new MutableSpan(array, offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SaveTo(this MutableSpan data, ref Vector4 v)
{
v.X = data[0];
v.Y = data[1];
v.Z = data[2];
v.W = data[3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SaveTo(this MutableSpan data, ref Vector4 v)
{
v.X = data[0];
v.Y = data[1];
v.Z = data[2];
v.W = data[3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LoadFrom(this MutableSpan data, ref Vector4 v)
{
data[0] = v.X;
data[1] = v.Y;
data[2] = v.Z;
data[3] = v.W;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LoadFrom(this MutableSpan data, ref Vector4 v)
{
data[0] = (int)v.X;
data[1] = (int)v.Y;
data[2] = (int)v.Z;
data[3] = (int)v.W;
}
}
}