// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if !NETSTANDARD2_1_OR_GREATER
using CommunityToolkit.HighPerformance.Helpers;
#endif
using CommunityToolkit.HighPerformance.Memory.Internals;
using CommunityToolkit.HighPerformance.Memory.Views;
#if !NETSTANDARD2_1_OR_GREATER
using RuntimeHelpers = CommunityToolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
#endif
#pragma warning disable CS0809, CA1065
namespace CommunityToolkit.HighPerformance;
///
/// A readonly version of .
///
/// The type of items in the current instance.
[DebuggerTypeProxy(typeof(MemoryDebugView2D<>))]
[DebuggerDisplay("{ToString(),raw}")]
public readonly ref partial struct ReadOnlySpan2D
{
#if NETSTANDARD2_1_OR_GREATER
///
/// The instance pointing to the first item in the target memory area.
///
private readonly ReadOnlySpan span;
#else
///
/// The target instance, if present.
///
private readonly object? instance;
///
/// The initial offset within .
///
private readonly IntPtr offset;
///
/// The height of the specified 2D region.
///
private readonly int height;
#endif
///
/// The width of the specified 2D region.
///
private readonly int width;
///
/// The stride of the specified 2D region.
///
private readonly int stride;
#if NETSTANDARD2_1_OR_GREATER
///
/// Initializes a new instance of the struct with the specified parameters.
///
/// The reference to the first item to map.
/// The height of the 2D memory area to map.
/// The width of the 2D memory area to map.
/// The pitch of the 2D memory area to map (the distance between each row).
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ReadOnlySpan2D(in T value, int height, int width, int pitch)
{
this.span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(value), height);
this.width = width;
this.stride = width + pitch;
}
#endif
///
/// Initializes a new instance of the struct with the specified parameters.
///
/// The pointer to the first item to map.
/// The height of the 2D memory area to map.
/// The width of the 2D memory area to map.
/// The pitch of the 2D memory area to map (the distance between each row).
/// Thrown when one of the parameters are negative.
public unsafe ReadOnlySpan2D(void* pointer, int height, int width, int pitch)
{
if (RuntimeHelpers.IsReferenceOrContainsReferences())
{
ThrowHelper.ThrowArgumentExceptionForManagedType();
}
if (width < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
if (height < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if (pitch < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}
OverflowHelper.EnsureIsInNativeIntRange(height, width, pitch);
#if NETSTANDARD2_1_OR_GREATER
this.span = new ReadOnlySpan(pointer, height);
#else
this.instance = null;
this.offset = (IntPtr)pointer;
this.height = height;
#endif
this.width = width;
this.stride = width + pitch;
}
#if !NETSTANDARD2_1_OR_GREATER
///
/// Initializes a new instance of the struct with the specified parameters.
///
/// The target instance.
/// The initial offset within the target instance.
/// The height of the 2D memory area to map.
/// The width of the 2D memory area to map.
/// The pitch of the 2D memory area to map.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ReadOnlySpan2D(object? instance, IntPtr offset, int height, int width, int pitch)
{
this.instance = instance;
this.offset = offset;
this.height = height;
this.width = width;
this.stride = width + pitch;
}
#endif
///
/// Initializes a new instance of the struct.
///
/// The target array to wrap.
/// The height of the resulting 2D area.
/// The width of each row in the resulting 2D area.
///
/// Thrown when either or are invalid.
///
/// The total area must match the length of .
public ReadOnlySpan2D(T[] array, int height, int width)
: this(array, 0, height, width, 0)
{
}
///
/// Initializes a new instance of the struct.
///
/// The target array to wrap.
/// The initial offset within .
/// The height of the resulting 2D area.
/// The width of each row in the resulting 2D area.
/// The pitch in the resulting 2D area.
///
/// Thrown when one of the input parameters is out of range.
///
///
/// Thrown when the requested area is outside of bounds for .
///
public ReadOnlySpan2D(T[] array, int offset, int height, int width, int pitch)
{
if ((uint)offset > (uint)array.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForOffset();
}
if (height < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if (width < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
if (pitch < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}
if (width == 0 || height == 0)
{
this = default;
return;
}
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = array.Length - offset;
if (area > remaining)
{
ThrowHelper.ThrowArgumentException();
}
#if NETSTANDARD2_1_OR_GREATER
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(offset), height);
#else
this.instance = array;
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(offset));
this.height = height;
#endif
this.width = width;
this.stride = width + pitch;
}
///
/// Initializes a new instance of the struct wrapping a 2D array.
///
/// The given 2D array to wrap.
public ReadOnlySpan2D(T[,]? array)
{
if (array is null)
{
this = default;
return;
}
#if NETSTANDARD2_1_OR_GREATER
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReference(), array.GetLength(0));
#else
this.instance = array;
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(0, 0));
this.height = array.GetLength(0);
#endif
this.width = this.stride = array.GetLength(1);
}
///
/// Initializes a new instance of the struct wrapping a 2D array.
///
/// The given 2D array to wrap.
/// The target row to map within .
/// The target column to map within .
/// The height to map within .
/// The width to map within .
///
/// Thrown when either , or
/// are negative or not within the bounds that are valid for .
///
public ReadOnlySpan2D(T[,]? array, int row, int column, int height, int width)
{
if (array is null)
{
if (row != 0 || column != 0 || height != 0 || width != 0)
{
ThrowHelper.ThrowArgumentException();
}
this = default;
return;
}
int rows = array.GetLength(0);
int columns = array.GetLength(1);
if ((uint)row >= (uint)rows)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow();
}
if ((uint)column >= (uint)columns)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn();
}
if ((uint)height > (uint)(rows - row))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if ((uint)width > (uint)(columns - column))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
#if NETSTANDARD2_1_OR_GREATER
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(row, column), height);
#else
this.instance = array;
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(row, column));
this.height = height;
#endif
this.width = width;
this.stride = columns;
}
///
/// Initializes a new instance of the struct wrapping a layer in a 3D array.
///
/// The given 3D array to wrap.
/// The target layer to map within .
/// Thrown when a parameter is invalid.
public ReadOnlySpan2D(T[,,] array, int depth)
{
if ((uint)depth >= (uint)array.GetLength(0))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForDepth();
}
#if NETSTANDARD2_1_OR_GREATER
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(depth, 0, 0), array.GetLength(1));
#else
this.instance = array;
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, 0, 0));
this.height = array.GetLength(1);
#endif
this.width = this.stride = array.GetLength(2);
}
///
/// Initializes a new instance of the struct wrapping a layer in a 3D array.
///
/// The given 3D array to wrap.
/// The target layer to map within .
/// The target row to map within .
/// The target column to map within .
/// The height to map within .
/// The width to map within .
/// Thrown when a parameter is invalid.
public ReadOnlySpan2D(T[,,] array, int depth, int row, int column, int height, int width)
{
if ((uint)depth >= (uint)array.GetLength(0))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForDepth();
}
int rows = array.GetLength(1);
int columns = array.GetLength(2);
if ((uint)row >= (uint)rows)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow();
}
if ((uint)column >= (uint)columns)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn();
}
if ((uint)height > (uint)(rows - row))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if ((uint)width > (uint)(columns - column))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
#if NETSTANDARD2_1_OR_GREATER
this.span = MemoryMarshal.CreateReadOnlySpan(ref array.DangerousGetReferenceAt(depth, row, column), height);
#else
this.instance = array;
this.offset = ObjectMarshal.DangerousGetObjectDataByteOffset(array, ref array.DangerousGetReferenceAt(depth, row, column));
this.height = height;
#endif
this.width = width;
this.stride = columns;
}
#if NETSTANDARD2_1_OR_GREATER
///
/// Initializes a new instance of the struct.
///
/// The target to wrap.
/// The height of the resulting 2D area.
/// The width of each row in the resulting 2D area.
///
/// Thrown when either or are invalid.
///
/// The total area must match the length of .
internal ReadOnlySpan2D(ReadOnlySpan span, int height, int width)
: this(span, 0, height, width, 0)
{
}
///
/// Initializes a new instance of the struct.
///
/// The target to wrap.
/// The initial offset within .
/// The height of the resulting 2D area.
/// The width of each row in the resulting 2D area.
/// The pitch in the resulting 2D area.
///
/// Thrown when one of the input parameters is out of range.
///
///
/// Thrown when the requested area is outside of bounds for .
///
internal ReadOnlySpan2D(ReadOnlySpan span, int offset, int height, int width, int pitch)
{
if ((uint)offset > (uint)span.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForOffset();
}
if (height < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if (width < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
if (pitch < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}
if (width == 0 || height == 0)
{
this = default;
return;
}
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = span.Length - offset;
if (area > remaining)
{
ThrowHelper.ThrowArgumentException();
}
this.span = MemoryMarshal.CreateSpan(ref span.DangerousGetReferenceAt(offset), height);
this.width = width;
this.stride = width + pitch;
}
///
/// Creates a new instance of the struct with the specified parameters.
///
/// The reference to the first item to map.
/// The height of the 2D memory area to map.
/// The width of the 2D memory area to map.
/// The pitch of the 2D memory area to map (the distance between each row).
/// A instance with the specified parameters.
/// Thrown when one of the parameters are negative.
[Pure]
public static ReadOnlySpan2D DangerousCreate(in T value, int height, int width, int pitch)
{
if (width < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
if (height < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if (pitch < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}
OverflowHelper.EnsureIsInNativeIntRange(height, width, pitch);
return new ReadOnlySpan2D(in value, height, width, pitch);
}
#endif
///
/// Gets an empty instance.
///
public static ReadOnlySpan2D Empty => default;
///
/// Gets a value indicating whether the current instance is empty.
///
public bool IsEmpty
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Height == 0 || this.width == 0;
}
///
/// Gets the length of the current instance.
///
public nint Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (nint)(uint)Height * (nint)(uint)this.width;
}
///
/// Gets the height of the underlying 2D memory area.
///
public int Height
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if NETSTANDARD2_1_OR_GREATER
return this.span.Length;
#else
return this.height;
#endif
}
}
///
/// Gets the width of the underlying 2D memory area.
///
public int Width
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.width;
}
///
/// Gets the element at the specified zero-based indices.
///
/// The target row to get the element from.
/// The target column to get the element from.
/// A reference to the element at the specified indices.
///
/// Thrown when either or are invalid.
///
public ref readonly T this[int row, int column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)row >= (uint)Height ||
(uint)column >= (uint)Width)
{
ThrowHelper.ThrowIndexOutOfRangeException();
}
return ref DangerousGetReferenceAt(row, column);
}
}
#if NETSTANDARD2_1_OR_GREATER
///
/// Gets the element at the specified zero-based indices.
///
/// The target row to get the element from.
/// The target column to get the element from.
/// A reference to the element at the specified indices.
///
/// Thrown when either or are invalid.
///
public ref readonly T this[Index row, Index column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref this[row.GetOffset(Height), column.GetOffset(this.width)];
}
///
/// Slices the current instance with the specified parameters.
///
/// The target range of rows to select.
/// The target range of columns to select.
///
/// Thrown when either or are invalid.
///
/// A new instance representing a slice of the current one.
public ReadOnlySpan2D this[Range rows, Range columns]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
(int row, int height) = rows.GetOffsetAndLength(Height);
(int column, int width) = columns.GetOffsetAndLength(this.width);
return Slice(row, column, height, width);
}
}
#endif
///
/// Copies the contents of this into a destination instance.
///
/// The destination instance.
///
/// Thrown when is shorter than the source instance.
///
public void CopyTo(Span destination)
{
if (IsEmpty)
{
return;
}
if (TryGetSpan(out ReadOnlySpan span))
{
span.CopyTo(destination);
}
else
{
if (Length > destination.Length)
{
ThrowHelper.ThrowArgumentExceptionForDestinationTooShort();
}
// Copy each row individually
#if NETSTANDARD2_1_OR_GREATER
for (int i = 0, j = 0; i < Height; i++, j += this.width)
{
GetRowSpan(i).CopyTo(destination.Slice(j));
}
#else
int height = Height;
nint width = (nint)(uint)this.width;
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < height; i++)
{
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
{
destinationRef = sourceStart;
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
}
}
#endif
}
}
///
/// Copies the contents of this into a destination instance.
/// For this API to succeed, the target has to have the same shape as the current one.
///
/// The destination instance.
///
/// Thrown when does not have the same shape as the source instance.
///
public void CopyTo(Span2D destination)
{
if (destination.Height != Height ||
destination.Width != Width)
{
ThrowHelper.ThrowArgumentExceptionForDestinationWithNotSameShape();
}
if (IsEmpty)
{
return;
}
if (destination.TryGetSpan(out Span span))
{
CopyTo(span);
}
else
{
// Copy each row individually
#if NETSTANDARD2_1_OR_GREATER
for (int i = 0; i < Height; i++)
{
GetRowSpan(i).CopyTo(destination.GetRowSpan(i));
}
#else
int height = Height;
nint width = (nint)(uint)this.width;
for (int i = 0; i < height; i++)
{
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
ref T destinationRef = ref destination.DangerousGetReferenceAt(i, 0);
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
{
destinationRef = sourceStart;
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
}
}
#endif
}
}
///
/// Attempts to copy the current instance to a destination .
///
/// The target of the copy operation.
/// Whether or not the operation was successful.
public bool TryCopyTo(Span destination)
{
if (destination.Length >= Length)
{
CopyTo(destination);
return true;
}
return false;
}
///
/// Attempts to copy the current instance to a destination .
///
/// The target of the copy operation.
/// Whether or not the operation was successful.
public bool TryCopyTo(Span2D destination)
{
if (destination.Height == Height &&
destination.Width == Width)
{
CopyTo(destination);
return true;
}
return false;
}
///
/// Returns a reference to the 0th element of the instance. If the current
/// instance is empty, returns a reference. It can be used for pinning
/// and is required to support the use of span within a fixed statement.
///
/// A reference to the 0th element, or a reference.
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[EditorBrowsable(EditorBrowsableState.Never)]
public unsafe ref T GetPinnableReference()
{
ref T r0 = ref Unsafe.AsRef(null);
if (Length != 0)
{
#if NETSTANDARD2_1_OR_GREATER
r0 = ref MemoryMarshal.GetReference(this.span);
#else
r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference(this.instance, this.offset);
#endif
}
return ref r0;
}
///
/// Returns a reference to the first element within the current instance, with no bounds check.
///
/// A reference to the first element within the current instance.
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T DangerousGetReference()
{
#if NETSTANDARD2_1_OR_GREATER
return ref MemoryMarshal.GetReference(this.span);
#else
return ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference(this.instance, this.offset);
#endif
}
///
/// Returns a reference to a specified element within the current instance, with no bounds check.
///
/// The target row to get the element from.
/// The target column to get the element from.
/// A reference to the element at the specified indices.
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T DangerousGetReferenceAt(int i, int j)
{
#if NETSTANDARD2_1_OR_GREATER
ref T r0 = ref MemoryMarshal.GetReference(this.span);
#else
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference(this.instance, this.offset);
#endif
nint index = ((nint)(uint)i * (nint)(uint)this.stride) + (nint)(uint)j;
return ref Unsafe.Add(ref r0, index);
}
///
/// Slices the current instance with the specified parameters.
///
/// The target row to map within the current instance.
/// The target column to map within the current instance.
/// The height to map within the current instance.
/// The width to map within the current instance.
///
/// Thrown when either , or
/// are negative or not within the bounds that are valid for the current instance.
///
/// A new instance representing a slice of the current one.
[Pure]
public ReadOnlySpan2D Slice(int row, int column, int height, int width)
{
if ((uint)row >= Height)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow();
}
if ((uint)column >= this.width)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn();
}
if ((uint)height > (Height - row))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight();
}
if ((uint)width > (this.width - column))
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth();
}
nint shift = ((nint)(uint)this.stride * (nint)(uint)row) + (nint)(uint)column;
int pitch = this.stride - width;
#if NETSTANDARD2_1_OR_GREATER
ref T r0 = ref this.span.DangerousGetReferenceAt(shift);
return new ReadOnlySpan2D(in r0, height, width, pitch);
#else
IntPtr offset = this.offset + (shift * (nint)(uint)Unsafe.SizeOf());
return new ReadOnlySpan2D(this.instance, offset, height, width, pitch);
#endif
}
#if NETSTANDARD2_1_OR_GREATER
///
/// Gets a for a specified row.
///
/// The index of the target row to retrieve.
/// Throw when is out of range.
/// The resulting row .
[Pure]
public ReadOnlySpan GetRowSpan(int row)
{
if ((uint)row >= (uint)Height)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow();
}
ref T r0 = ref DangerousGetReferenceAt(row, 0);
return MemoryMarshal.CreateReadOnlySpan(ref r0, this.width);
}
#endif
///
/// Tries to get a instance, if the underlying buffer is contiguous and small enough.
///
/// The resulting , in case of success.
/// Whether or not was correctly assigned.
public bool TryGetSpan(out ReadOnlySpan span)
{
// We can only create a Span if the buffer is contiguous
if (this.stride == this.width &&
Length <= int.MaxValue)
{
#if NETSTANDARD2_1_OR_GREATER
span = MemoryMarshal.CreateReadOnlySpan(ref MemoryMarshal.GetReference(this.span), (int)Length);
return true;
#else
// An empty Span2D is still valid
if (IsEmpty)
{
span = default;
return true;
}
// Pinned ReadOnlySpan2D
if (this.instance is null)
{
unsafe
{
span = new Span((void*)this.offset, (int)Length);
}
return true;
}
// Without Span runtime support, we can only get a Span from a T[] instance
if (this.instance.GetType() == typeof(T[]))
{
T[] array = Unsafe.As(this.instance)!;
int index = array.AsSpan().IndexOf(ref ObjectMarshal.DangerousGetObjectDataReferenceAt(array, this.offset));
span = array.AsSpan(index, (int)Length);
return true;
}
#endif
}
span = default;
return false;
}
///
/// Copies the contents of the current instance into a new 2D array.
///
/// A 2D array containing the data in the current instance.
[Pure]
public T[,] ToArray()
{
T[,] array = new T[Height, this.width];
#if NETSTANDARD2_1_OR_GREATER
CopyTo(array.AsSpan());
#else
// Skip the initialization if the array is empty
if (Length > 0)
{
int height = Height;
nint width = (nint)(uint)this.width;
ref T destinationRef = ref array.DangerousGetReference();
for (int i = 0; i < height; i++)
{
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
{
destinationRef = sourceStart;
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
}
}
}
#endif
return array;
}
///
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Equals() on Span will always throw an exception. Use == instead.")]
public override bool Equals(object? obj)
{
throw new NotSupportedException("CommunityToolkit.HighPerformance.ReadOnlySpan2D.Equals(object) is not supported.");
}
///
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("GetHashCode() on Span will always throw an exception.")]
public override int GetHashCode()
{
throw new NotSupportedException("CommunityToolkit.HighPerformance.ReadOnlySpan2D.GetHashCode() is not supported.");
}
///
public override string ToString()
{
return $"CommunityToolkit.HighPerformance.ReadOnlySpan2D<{typeof(T)}>[{Height}, {this.width}]";
}
///
/// Checks whether two instances are equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// Whether or not and are equal.
public static bool operator ==(ReadOnlySpan2D left, ReadOnlySpan2D right)
{
return
#if NETSTANDARD2_1_OR_GREATER
left.span == right.span &&
#else
ReferenceEquals(
left.instance, right.instance) &&
left.offset == right.offset &&
left.height == right.height &&
#endif
left.width == right.width &&
left.stride == right.stride;
}
///
/// Checks whether two instances are not equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// Whether or not and are not equal.
public static bool operator !=(ReadOnlySpan2D left, ReadOnlySpan2D right)
{
return !(left == right);
}
///
/// Implicily converts a given 2D array into a instance.
///
/// The input 2D array to convert.
public static implicit operator ReadOnlySpan2D(T[,]? array) => new(array);
///
/// Implicily converts a given into a instance.
///
/// The input to convert.
public static implicit operator ReadOnlySpan2D(Span2D span)
{
#if NETSTANDARD2_1_OR_GREATER
return new ReadOnlySpan2D(in span.DangerousGetReference(), span.Height, span.Width, span.Stride - span.Width);
#else
return new ReadOnlySpan2D(span.Instance!, span.Offset, span.Height, span.Width, span.Stride - span.Width);
#endif
}
}