Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,23 +1987,7 @@ public static void Sort<T>(T[] array, Comparison<T> comparison)
throw new ArgumentNullException(nameof(comparison));
}

IComparer<T> comparer = new FunctorComparer<T>(comparison);
Array.Sort(array, comparer);
}

internal sealed class FunctorComparer<T> : IComparer<T>
{
private Comparison<T> _comparison;

public FunctorComparer(Comparison<T> comparison)
{
_comparison = comparison;
}

public int Compare(T x, T y)
{
return _comparison(x, y);
}
ArraySortHelper<T>.Sort(array, 0, array.Length, comparison);
}

public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void Sort(T[] keys, int index, int length, IComparer<T> comparer)
comparer = Comparer<T>.Default;
}

IntrospectiveSort(keys, index, length, comparer);
IntrospectiveSort(keys, index, length, comparer.Compare);
}
catch (IndexOutOfRangeException)
{
Expand Down Expand Up @@ -84,6 +84,27 @@ public static int BinarySearch(T[] array, int index, int length, T value, ICompa

#endregion

internal static void Sort(T[] keys, int index, int length, Comparison<T> comparer)
{
Debug.Assert(keys != null, "Check the arguments in the caller!");
Debug.Assert(index >= 0 && length >= 0 && (keys.Length - index >= length), "Check the arguments in the caller!");
Debug.Assert(comparer != null, "Check the arguments in the caller!");

// Add a try block here to detect bogus comparisons
try
{
IntrospectiveSort(keys, index, length, comparer);
}
catch (IndexOutOfRangeException)
{
IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(comparer);
}
catch (Exception e)
{
throw new InvalidOperationException(SR.InvalidOperation_IComparerFailed, e);
}
}

internal static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
{
Debug.Assert(array != null, "Check the arguments in the caller!");
Expand All @@ -110,11 +131,11 @@ internal static int InternalBinarySearch(T[] array, int index, int length, T val
return ~lo;
}

private static void SwapIfGreater(T[] keys, IComparer<T> comparer, int a, int b)
private static void SwapIfGreater(T[] keys, Comparison<T> comparer, int a, int b)
{
if (a != b)
{
if (comparer.Compare(keys[a], keys[b]) > 0)
if (comparer(keys[a], keys[b]) > 0)
{
T key = keys[a];
keys[a] = keys[b];
Expand All @@ -133,7 +154,7 @@ private static void Swap(T[] a, int i, int j)
}
}

internal static void IntrospectiveSort(T[] keys, int left, int length, IComparer<T> comparer)
internal static void IntrospectiveSort(T[] keys, int left, int length, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(comparer != null);
Expand All @@ -148,7 +169,7 @@ internal static void IntrospectiveSort(T[] keys, int left, int length, IComparer
IntroSort(keys, left, length + left - 1, 2 * IntrospectiveSortUtilities.FloorLog2(keys.Length), comparer);
}

private static void IntroSort(T[] keys, int lo, int hi, int depthLimit, IComparer<T> comparer)
private static void IntroSort(T[] keys, int lo, int hi, int depthLimit, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(comparer != null);
Expand Down Expand Up @@ -195,7 +216,7 @@ private static void IntroSort(T[] keys, int lo, int hi, int depthLimit, ICompare
}
}

private static int PickPivotAndPartition(T[] keys, int lo, int hi, IComparer<T> comparer)
private static int PickPivotAndPartition(T[] keys, int lo, int hi, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(comparer != null);
Expand All @@ -218,8 +239,8 @@ private static int PickPivotAndPartition(T[] keys, int lo, int hi, IComparer<T>

while (left < right)
{
while (comparer.Compare(keys[++left], pivot) < 0) ;
while (comparer.Compare(pivot, keys[--right]) < 0) ;
while (comparer(keys[++left], pivot) < 0) ;
while (comparer(pivot, keys[--right]) < 0) ;

if (left >= right)
break;
Expand All @@ -232,7 +253,7 @@ private static int PickPivotAndPartition(T[] keys, int lo, int hi, IComparer<T>
return left;
}

private static void Heapsort(T[] keys, int lo, int hi, IComparer<T> comparer)
private static void Heapsort(T[] keys, int lo, int hi, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(comparer != null);
Expand All @@ -252,7 +273,7 @@ private static void Heapsort(T[] keys, int lo, int hi, IComparer<T> comparer)
}
}

private static void DownHeap(T[] keys, int i, int n, int lo, IComparer<T> comparer)
private static void DownHeap(T[] keys, int i, int n, int lo, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(comparer != null);
Expand All @@ -264,19 +285,19 @@ private static void DownHeap(T[] keys, int i, int n, int lo, IComparer<T> compar
while (i <= n / 2)
{
child = 2 * i;
if (child < n && comparer.Compare(keys[lo + child - 1], keys[lo + child]) < 0)
if (child < n && comparer(keys[lo + child - 1], keys[lo + child]) < 0)
{
child++;
}
if (!(comparer.Compare(d, keys[lo + child - 1]) < 0))
if (!(comparer(d, keys[lo + child - 1]) < 0))
break;
keys[lo + i - 1] = keys[lo + child - 1];
i = child;
}
keys[lo + i - 1] = d;
}

private static void InsertionSort(T[] keys, int lo, int hi, IComparer<T> comparer)
private static void InsertionSort(T[] keys, int lo, int hi, Comparison<T> comparer)
{
Debug.Assert(keys != null);
Debug.Assert(lo >= 0);
Expand All @@ -289,7 +310,7 @@ private static void InsertionSort(T[] keys, int lo, int hi, IComparer<T> compare
{
j = i;
t = keys[i + 1];
while (j >= lo && comparer.Compare(t, keys[j]) < 0)
while (j >= lo && comparer(t, keys[j]) < 0)
{
keys[j + 1] = keys[j];
j--;
Expand Down Expand Up @@ -582,5 +603,3 @@ private static void InsertionSort(TKey[] keys, TValue[] values, int lo, int hi,
}
#endregion
}


Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,7 @@ public void Sort(Comparison<T> comparison)

if (_size > 0)
{
IComparer<T> comparer = Comparer<T>.Create(comparison);
Array.Sort(_items, 0, _size, comparer);
ArraySortHelper<T>.Sort(_items, 0, _size, comparison);
}
}

Expand Down