using System.Diagnostics.Contracts; using System.Runtime.InteropServices; namespace System { /// /// A that contains extension methods for the /// public static class SpanExtensions { /// /// Creates a from a given 2D array /// /// The type of items in the input array /// The input 2D array /// A that maps all the items of the input array [Pure] public static Span AsSpan(this T[,] array) { ref T r0 = ref array[0, 0]; return MemoryMarshal.CreateSpan(ref r0, array.Length); } /// /// Creates a from a given 2D array /// /// The type of items in the input array /// The input 2D array /// The index of the initial row to map from the input array /// A that maps the desired items of the input array [Pure] public static Span AsSpan(this T[,] array, Index startIndex) { int rows = array.GetLength(0), offset = startIndex.GetOffset(rows), interval = rows - offset; ref T r0 = ref array[offset, 0]; return MemoryMarshal.CreateSpan(ref r0, interval * array.GetLength(1)); } /// /// Creates a from a given 2D array /// /// The type of items in the input array /// The input 2D array /// The index of the initial row to map from the input array /// A that maps the desired items of the input array [Pure] public static Span AsSpan(this T[,] array, int start) { int interval = array.GetLength(0) - start; ref T r0 = ref array[start, 0]; return MemoryMarshal.CreateSpan(ref r0, interval * array.GetLength(1)); } /// /// Creates a from a given 2D array /// /// The type of items in the input array /// The input 2D array /// The index of the initial row to map from the input array /// The number of rows to map from the input array /// A that maps the desired items of the input array [Pure] public static Span AsSpan(this T[,] array, int start, int length) { ref T r0 = ref array[start, 0]; return MemoryMarshal.CreateSpan(ref r0, length * array.GetLength(1)); } /// /// Creates a from a given 2D array /// /// The type of items in the input array /// The input 2D array /// The range of rows to map from the input array /// A that maps the desired items of the input array [Pure] public static Span AsSpan(this T[,] array, Range range) { var indices = range.GetOffsetAndLength(array.GetLength(0)); ref T r0 = ref array[indices.Offset, 0]; return MemoryMarshal.CreateSpan(ref r0, indices.Length * array.GetLength(1)); } } }