// to IntrospectiveSort was quirked. However since the phone builds always shipped with the new sort aka
// IntrospectiveSort and we would want to continue using this sort moving forward CoreCLR always uses the new sort.
- IntrospectiveSort(keys, index, length, comparer);
+ IntrospectiveSort(keys, index, length, comparer.Compare);
#else
if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
{
- IntrospectiveSort(keys, index, length, comparer);
+ IntrospectiveSort(keys, index, length, comparer.Compare);
}
else
{
- DepthLimitedQuickSort(keys, index, length + index - 1, comparer, IntrospectiveSortUtilities.QuickSortDepthThreshold);
+ DepthLimitedQuickSort(keys, index, length + index - 1, comparer.Compare, IntrospectiveSortUtilities.QuickSortDepthThreshold);
}
#endif
}
#endregion
+ internal static void Sort(T[] keys, int index, int length, Comparison<T> comparer)
+ {
+ Contract.Assert(keys != null, "Check the arguments in the caller!");
+ Contract.Assert(index >= 0 && length >= 0 && (keys.Length - index >= length), "Check the arguments in the caller!");
+ Contract.Assert(comparer != null, "Check the arguments in the caller!");
+
+ // Add a try block here to detect bogus comparisons
+ try
+ {
+#if FEATURE_CORECLR
+ // Since QuickSort and IntrospectiveSort produce different sorting sequence for equal keys the upgrade
+ // to IntrospectiveSort was quirked. However since the phone builds always shipped with the new sort aka
+ // IntrospectiveSort and we would want to continue using this sort moving forward CoreCLR always uses the new sort.
+
+ IntrospectiveSort(keys, index, length, comparer);
+#else
+ if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
+ {
+ IntrospectiveSort(keys, index, length, comparer);
+ }
+ else
+ {
+ DepthLimitedQuickSort(keys, index, length + index - 1, comparer, IntrospectiveSortUtilities.QuickSortDepthThreshold);
+ }
+#endif
+ }
+ catch (IndexOutOfRangeException)
+ {
+ IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(comparer);
+ }
+ catch (Exception e)
+ {
+ throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
+ }
+ }
+
internal static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
{
Contract.Requires(array != null, "Check the arguments in the caller!");
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];
private static void Swap(T[] a, int i, int j)
{
- if(i != j)
+ if (i != j)
{
T t = a[i];
a[i] = a[j];
}
}
- internal static void DepthLimitedQuickSort(T[] keys, int left, int right, IComparer<T> comparer, int depthLimit)
+#if !FEATURE_CORECLR
+ internal static void DepthLimitedQuickSort(T[] keys, int left, int right, Comparison<T> comparer, int depthLimit)
{
do
{
T x = keys[middle];
do
{
- while (comparer.Compare(keys[i], x) < 0) i++;
- while (comparer.Compare(x, keys[j]) < 0) j--;
+ while (comparer(keys[i], x) < 0) i++;
+ while (comparer(x, keys[j]) < 0) j--;
Contract.Assert(i >= left && j <= right, "(i>=left && j<=right) Sort failed - Is your IComparer bogus?");
if (i > j) break;
if (i < j)
}
} while (left < right);
}
+#endif
- 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)
{
Contract.Requires(keys != null);
Contract.Requires(comparer != null);
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)
{
Contract.Requires(keys != null);
Contract.Requires(comparer != null);
}
}
- 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)
{
Contract.Requires(keys != null);
Contract.Requires(comparer != null);
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;
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)
{
Contract.Requires(keys != null);
Contract.Requires(comparer != null);
}
}
- 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)
{
Contract.Requires(keys != null);
Contract.Requires(comparer != null);
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)
{
Contract.Requires(keys != null);
Contract.Requires(lo >= 0);
{
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--;
// to IntrospectiveSort was quirked. However since the phone builds always shipped with the new sort aka
// IntrospectiveSort and we would want to continue using this sort moving forward CoreCLR always uses the new sort.
- ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer);
+ ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer.Compare);
#else
if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
{
- ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer);
+ ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer.Compare);
}
else
{
- ArraySortHelper<T>.DepthLimitedQuickSort(keys, index, length + index - 1, comparer, IntrospectiveSortUtilities.QuickSortDepthThreshold);
+ ArraySortHelper<T>.DepthLimitedQuickSort(keys, index, length + index - 1, comparer.Compare, IntrospectiveSortUtilities.QuickSortDepthThreshold);
}
#endif
}
}
}
+#if !FEATURE_CORECLR
private static void DepthLimitedQuickSort(T[] keys, int left, int right, int depthLimit)
{
Contract.Requires(keys != null);
}
} while (left < right);
}
+#endif
internal static void IntrospectiveSort(T[] keys, int left, int length)
{
}
}
- #endregion
+#endregion
#region ArraySortHelper for paired key and value arrays
}
}
+#if !FEATURE_CORECLR
internal static void DepthLimitedQuickSort(TKey[] keys, TValue[] values, int left, int right, IComparer<TKey> comparer, int depthLimit)
{
do
}
} while (left < right);
}
+#endif
internal static void IntrospectiveSort(TKey[] keys, TValue[] values, int left, int length, IComparer<TKey> comparer)
{
}
}
+#if !FEATURE_CORECLR
private static void DepthLimitedQuickSort(TKey[] keys, TValue[] values, int left, int right, int depthLimit)
{
// The code in this function looks very similar to QuickSort in ArraySortHelper<T> class.
}
} while (left < right);
}
+#endif
internal static void IntrospectiveSort(TKey[] keys, TValue[] values, int left, int length)
{
#endregion
}
-
-