From: Justin Van Patten Date: Tue, 13 Sep 2016 17:25:13 +0000 (-0700) Subject: Add Array.Reverse (#7132) X-Git-Tag: accepted/tizen/base/20180629.140029~3588 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b33e7563569135c5040958fd97b74b96607db3d6;p=platform%2Fupstream%2Fcoreclr.git Add Array.Reverse (#7132) * Add Array.Reverse Add generic `Reverse` methods to `Array`. * Change List.Reverse to use Array.Reverse Implement `List.Reverse` using `Array.Reverse`. --- diff --git a/src/mscorlib/model.xml b/src/mscorlib/model.xml index 7af08b7..41ff879 100644 --- a/src/mscorlib/model.xml +++ b/src/mscorlib/model.xml @@ -286,6 +286,8 @@ + + diff --git a/src/mscorlib/src/System/Array.cs b/src/mscorlib/src/System/Array.cs index 0879049..d81f99c 100644 --- a/src/mscorlib/src/System/Array.cs +++ b/src/mscorlib/src/System/Array.cs @@ -1614,7 +1614,41 @@ namespace System { [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] private static extern bool TrySZReverse(Array array, int index, int count); - + + [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] + public static void Reverse(T[] array) + { + if (array == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + Contract.EndContractBlock(); + Reverse(array, 0, array.Length); + } + + [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] + public static void Reverse(T[] array, int index, int length) + { + if (array == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + if (index < 0) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + if (array.Length - index < length) + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); + Contract.EndContractBlock(); + + int i = index; + int j = index + length - 1; + while (i < j) + { + T temp = array[i]; + array[i] = array[j]; + array[j] = temp; + i++; + j--; + } + } + // Sorts the elements of an array. The sort compares the elements to each // other using the IComparable interface, which must be implemented // by all elements of the array. diff --git a/src/mscorlib/src/System/Collections/Generic/List.cs b/src/mscorlib/src/System/Collections/Generic/List.cs index ae3356d..010c566 100644 --- a/src/mscorlib/src/System/Collections/Generic/List.cs +++ b/src/mscorlib/src/System/Collections/Generic/List.cs @@ -930,22 +930,7 @@ namespace System.Collections.Generic { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); - // The non-generic Array.Reverse is not used because it does not perform - // well for non-primitive value types. - // If/when a generic Array.Reverse becomes available, the below code - // can be deleted and replaced with a call to Array.Reverse. - int i = index; - int j = index + count - 1; - T[] array = _items; - while (i < j) - { - T temp = array[i]; - array[i] = array[j]; - array[j] = temp; - i++; - j--; - } - + Array.Reverse(_items, index, count); _version++; }