Add Array.Reverse<T> (#7132)
authorJustin Van Patten <jvp@justinvp.com>
Tue, 13 Sep 2016 17:25:13 +0000 (10:25 -0700)
committerJan Vorlicek <janvorli@microsoft.com>
Tue, 13 Sep 2016 17:25:13 +0000 (19:25 +0200)
* Add Array.Reverse<T>

Add generic `Reverse<T>` methods to `Array`.

* Change List<T>.Reverse to use Array.Reverse<T>

Implement `List<T>.Reverse` using `Array.Reverse<T>`.

src/mscorlib/model.xml
src/mscorlib/src/System/Array.cs
src/mscorlib/src/System/Collections/Generic/List.cs

index 7af08b7..41ff879 100644 (file)
       <Member Name="Resize&lt;T&gt;(T[]@,System.Int32)" />
       <Member Name="Reverse(System.Array)" />
       <Member Name="Reverse(System.Array,System.Int32,System.Int32)" />
+      <Member Name="Reverse&lt;T&gt;(T[])" />
+      <Member Name="Reverse&lt;T&gt;(T[],System.Int32,System.Int32)" />
       <Member Name="SetValue(System.Object,System.Int32)" />
       <Member Name="SetValue(System.Object,System.Int32,System.Int32)" />
       <Member Name="SetValue(System.Object,System.Int32,System.Int32,System.Int32)" />
index 0879049..d81f99c 100644 (file)
@@ -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>(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>(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.
index ae3356d..010c566 100644 (file)
@@ -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<T> becomes available, the below code
-            // can be deleted and replaced with a call to Array.Reverse<T>.
-            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++;
         }