From a2b760d338989009823b63db765c3a9e0f592946 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Mon, 20 Apr 2020 22:03:01 +0300 Subject: [PATCH] Reverse range and sortDescending range #KT-36955 (#4101) (cherry picked from commit 82e8bc922ea350d34bb2936172e65635b00cf2cc) --- .../main/kotlin/generated/_ArraysNative.kt | 118 ++++++++++++++++-- .../kotlin/kotlin/collections/ArraySorting.kt | 24 ++-- 2 files changed, 121 insertions(+), 21 deletions(-) diff --git a/runtime/src/main/kotlin/generated/_ArraysNative.kt b/runtime/src/main/kotlin/generated/_ArraysNative.kt index 10a21cc433f..fbbe58b789d 100644 --- a/runtime/src/main/kotlin/generated/_ArraysNative.kt +++ b/runtime/src/main/kotlin/generated/_ArraysNative.kt @@ -2129,7 +2129,7 @@ public actual inline fun Array.plusElement(element: T): Array { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun IntArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2138,7 +2138,7 @@ public actual fun IntArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun LongArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2147,7 +2147,7 @@ public actual fun LongArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ByteArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2156,7 +2156,7 @@ public actual fun ByteArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ShortArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2165,7 +2165,7 @@ public actual fun ShortArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun DoubleArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2174,7 +2174,7 @@ public actual fun DoubleArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun FloatArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2183,7 +2183,7 @@ public actual fun FloatArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun CharArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -2194,7 +2194,105 @@ public actual fun CharArray.sort(): Unit { * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable */ public actual fun > Array.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) +} + +/** + * Sorts a range in the array in-place. + * + * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArrayOfComparable + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun > Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) } /** @@ -2211,7 +2309,9 @@ public actual fun Array.sortWith(comparator: Comparator): Unit * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. */ -public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) sortArrayWith(this, fromIndex, toIndex, comparator) } diff --git a/runtime/src/main/kotlin/kotlin/collections/ArraySorting.kt b/runtime/src/main/kotlin/kotlin/collections/ArraySorting.kt index 3b92da34a39..52dacac9648 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArraySorting.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArraySorting.kt @@ -362,7 +362,7 @@ private fun quickSort( // Interfaces ============================================================================= /** * Sorts the subarray specified by [fromIndex] (inclusive) and [toIndex] (exclusive) parameters - * using the qsort algorithm with the given [comparator]. + * using the merge sort algorithm with the given [comparator]. */ internal fun sortArrayWith( array: Array, fromIndex: Int = 0, toIndex: Int = array.size, comparator: Comparator) { @@ -372,21 +372,21 @@ internal fun sortArrayWith( /** * Sorts a subarray of [Comparable] elements specified by [fromIndex] (inclusive) and - * [toIndex] (exclusive) parameters using the qsort algorithm. + * [toIndex] (exclusive) parameters using the merge sort algorithm. */ -internal fun > sortArray(array: Array) { +internal fun > sortArray(array: Array, fromIndex: Int, toIndex: Int) { @Suppress("UNCHECKED_CAST") - mergeSort(array as Array, 0, array.size - 1) + mergeSort(array as Array, fromIndex, toIndex - 1) } /** * Sorts the given array using qsort algorithm. */ -internal fun sortArray(array: ByteArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: ShortArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: IntArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: LongArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: CharArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: FloatArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: DoubleArray) = quickSort(array, 0, array.size - 1) -internal fun sortArray(array: BooleanArray) = quickSort(array, 0, array.size - 1) \ No newline at end of file +internal fun sortArray(array: ByteArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: ShortArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: IntArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: LongArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: CharArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: FloatArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: DoubleArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) +internal fun sortArray(array: BooleanArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) \ No newline at end of file