diff options
author | Atsushi Kanamori <AtsushiKan@users.noreply.github.com> | 2018-02-28 21:33:24 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2018-03-03 04:11:24 +0300 |
commit | 5fc2707d659be67cd33d6fff9aae793e15038a15 (patch) | |
tree | dd1ac4f34671c26e93bd913ed84ae800a716f4c0 | |
parent | dec1b08fa3065d259f5e2dc3a8c4ac3cd757854d (diff) |
Add the array/arraysegment->Span/Memory overloads to CoreCLR. (#16658)
Part of https://github.com/dotnet/corefx/issues/26894
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
-rw-r--r-- | src/System.Private.CoreLib/shared/System/MemoryExtensions.cs | 142 |
1 files changed, 138 insertions, 4 deletions
diff --git a/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs b/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs index effdecf92..427f05c4c 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs @@ -707,7 +707,7 @@ namespace System } /// <summary> - /// Creates a new span over the portion of the target array. + /// Creates a new span over the target array. /// </summary> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this T[] array) @@ -716,12 +716,73 @@ namespace System } /// <summary> + /// Creates a new Span over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the Span.</param> + /// <param name="length">The number of items in the Span.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=Length). + /// </exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span<T> AsSpan<T>(this T[] array, int start, int length) + { + return new Span<T>(array, start, length); + } + + /// <summary> /// Creates a new span over the portion of the target array segment. /// </summary> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span<T> AsSpan<T>(this ArraySegment<T> arraySegment) + public static Span<T> AsSpan<T>(this ArraySegment<T> segment) { - return new Span<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count); + return new Span<T>(segment.Array, segment.Offset, segment.Count); + } + + /// <summary> + /// Creates a new Span over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the Span.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=segment.Count). + /// </exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span<T> AsSpan<T>(this ArraySegment<T> segment, int start) + { + if (((uint)start) > segment.Count) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); + + return new Span<T>(segment.Array, segment.Offset + start, segment.Count - start); + } + + /// <summary> + /// Creates a new Span over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the Span.</param> + /// <param name="length">The number of items in the Span.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=segment.Count). + /// </exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span<T> AsSpan<T>(this ArraySegment<T> segment, int start, int length) + { + if (((uint)start) > segment.Count) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); + if (((uint)length) > segment.Count - start) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); + + return new Span<T>(segment.Array, segment.Offset + start, length); } /// <summary> @@ -753,11 +814,84 @@ namespace System public static ReadOnlyMemory<T> AsReadOnlyMemory<T>(this Memory<T> memory) => memory; /// <summary> - /// Creates a new memory over the portion of the target array. + /// Creates a new memory over the target array. + /// </summary> + public static Memory<T> AsMemory<T>(this T[] array) => new Memory<T>(array); + + /// <summary> + /// Creates a new memory over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the memory.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=array.Length). + /// </exception> public static Memory<T> AsMemory<T>(this T[] array, int start) => new Memory<T>(array, start); /// <summary> + /// Creates a new memory over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the memory.</param> + /// <param name="length">The number of items in the memory.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=Length). + /// </exception> + public static Memory<T> AsMemory<T>(this T[] array, int start, int length) => new Memory<T>(array, start, length); + + /// <summary> + /// Creates a new memory over the portion of the target array. + /// </summary> + public static Memory<T> AsMemory<T>(this ArraySegment<T> segment) => new Memory<T>(segment.Array, segment.Offset, segment.Count); + + /// <summary> + /// Creates a new memory over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the memory.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=segment.Count). + /// </exception> + public static Memory<T> AsMemory<T>(this ArraySegment<T> segment, int start) + { + if (((uint)start) > segment.Count) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); + + return new Memory<T>(segment.Array, segment.Offset + start, segment.Count - start); + } + + /// <summary> + /// Creates a new memory over the portion of the target array beginning + /// at 'start' index and ending at 'end' index (exclusive). + /// </summary> + /// <param name="array">The target array.</param> + /// <param name="start">The index at which to begin the memory.</param> + /// <param name="length">The number of items in the memory.</param> + /// <remarks>Returns default when <paramref name="array"/> is null.</remarks> + /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception> + /// <exception cref="System.ArgumentOutOfRangeException"> + /// Thrown when the specified <paramref name="start"/> or end index is not in the range (<0 or >=segment.Count). + /// </exception> + public static Memory<T> AsMemory<T>(this ArraySegment<T> segment, int start, int length) + { + if (((uint)start) > segment.Count) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); + if (((uint)length) > segment.Count - start) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); + + return new Memory<T>(segment.Array, segment.Offset + start, length); + } + + /// <summary> /// Copies the contents of the array into the span. If the source /// and destinations overlap, this method behaves as if the original values in /// a temporary location before the destination is overwritten. |