diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 1bb1506a27c..e971178f6e3 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -5249,6 +5249,159 @@ public fun CharArray.reverse(): Unit { } } +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun Array.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun ByteArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun ShortArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun IntArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun LongArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun FloatArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun DoubleArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun BooleanArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +public fun CharArray.reverse(fromIndex: Int, toIndex: Int): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + /** * Returns a list with elements in reversed order. */ @@ -7751,6 +7904,153 @@ public expect fun CharArray.sort(): Unit */ public expect fun > Array.sort(): Unit +/** + * 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") +public expect fun > Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@SinceKotlin("1.4") +public expect fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + * + * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + */ +@SinceKotlin("1.4") +public fun > Array.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sortWith(reverseOrder(), fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun ByteArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun ShortArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun IntArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun LongArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun FloatArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun DoubleArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +public fun CharArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + /** * Sorts the array in-place according to the order specified by the given [comparator]. * @@ -7758,6 +8058,13 @@ public expect fun > Array.sort(): Unit */ public expect fun Array.sortWith(comparator: Comparator): Unit +/** + * Sorts a range in the array in-place with the given [comparator]. + * + * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + */ +public expect fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit + /** * Returns an array of Boolean containing all of the elements of this generic array. */ diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 1a82b386fef..2336ddb5787 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -2614,6 +2614,46 @@ public inline fun UShortArray.reverse(): Unit { storage.reverse() } +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.reverse(fromIndex: Int, toIndex: Int): Unit { + storage.reverse(fromIndex, toIndex) +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.reverse(fromIndex: Int, toIndex: Int): Unit { + storage.reverse(fromIndex, toIndex) +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.reverse(fromIndex: Int, toIndex: Int): Unit { + storage.reverse(fromIndex, toIndex) +} + +/** + * Reverses elements of the array in the specified range in-place. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.reverse(fromIndex: Int, toIndex: Int): Unit { + storage.reverse(fromIndex, toIndex) +} + /** * Returns a list with elements in reversed order. */ @@ -3890,7 +3930,7 @@ public inline operator fun UShortArray.plus(elements: UShortArray): UShortArray @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UIntArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -3901,7 +3941,7 @@ public fun UIntArray.sort(): Unit { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun ULongArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -3912,7 +3952,7 @@ public fun ULongArray.sort(): Unit { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UByteArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) } /** @@ -3923,7 +3963,91 @@ public fun UByteArray.sort(): Unit { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UShortArray.sort(): Unit { - if (size > 1) sortArray(this) + if (size > 1) sortArray(this, 0, size) +} + +/** + * Sorts a range in the array in-place. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UIntArray.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. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun ULongArray.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. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UByteArray.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. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UIntArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun ULongArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UByteArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) +} + +/** + * Sorts elements of the array in the specified range in-place. + * The elements are sorted descending according to their natural sort order. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UShortArray.sortDescending(fromIndex: Int, toIndex: Int): Unit { + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) } /** diff --git a/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt b/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt index dc764b0d850..ebd48231e99 100644 --- a/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt @@ -1826,6 +1826,110 @@ public fun Array.sort(comparison: (a: T, b: T) -> Int): Unit { if (size > 1) sortArrayWith(this, comparison) } +/** + * 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) + sortArrayWith(this, fromIndex, toIndex, naturalOrder()) +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + sortArrayWith(this.unsafeCast>(), fromIndex, toIndex, naturalOrder()) +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ @@ -1891,6 +1995,18 @@ public actual fun Array.sortWith(comparator: Comparator): Unit if (size > 1) sortArrayWith(this, comparator) } +/** + * Sorts a range in the array in-place with the given [comparator]. + * + * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + */ +@SinceKotlin("1.4") +@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) +} + /** * Returns a *typed* object array containing all of the elements of this primitive array. */ diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt index 78219c43130..df2f1a01cdd 100644 --- a/libraries/stdlib/js/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -1844,6 +1844,110 @@ public fun Array.sort(comparison: (a: T, b: T) -> Int): Unit { if (size > 1) sortArrayWith(this, comparison) } +/** + * 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) + sortArrayWith(this, fromIndex, toIndex, naturalOrder()) +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + sortArrayWith(this.unsafeCast>(), fromIndex, toIndex, naturalOrder()) +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + +/** + * 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) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() +} + /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ @@ -1909,6 +2013,18 @@ public actual fun Array.sortWith(comparator: Comparator): Unit if (size > 1) sortArrayWith(this, comparator) } +/** + * Sorts a range in the array in-place with the given [comparator]. + * + * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + */ +@SinceKotlin("1.4") +@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) +} + /** * Returns a *typed* object array containing all of the elements of this primitive array. */ diff --git a/libraries/stdlib/js/src/kotlin/collections/ArraySorting.kt b/libraries/stdlib/js/src/kotlin/collections/ArraySorting.kt index 9bb1c2a99db..a5ae28688db 100644 --- a/libraries/stdlib/js/src/kotlin/collections/ArraySorting.kt +++ b/libraries/stdlib/js/src/kotlin/collections/ArraySorting.kt @@ -22,6 +22,12 @@ internal fun sortArrayWith(array: Array, comparator: Comparator } } +internal fun sortArrayWith(array: Array, fromIndex: Int, toIndex: Int, comparator: Comparator) { + if (fromIndex < toIndex - 1) { + mergeSort(array.unsafeCast>(), fromIndex, toIndex - 1, comparator) + } +} + internal fun > sortArray(array: Array) { if (getStableSortingIsSupported()) { val comparison = { a: T, b: T -> a.compareTo(b) } @@ -56,7 +62,7 @@ private fun mergeSort(array: Array, start: Int, endInclusive: Int, compar val buffer = arrayOfNulls(array.size).unsafeCast>() val result = mergeSort(array, buffer, start, endInclusive, comparator) if (result !== array) { - result.forEachIndexed { i, v -> array[i] = v } + for (i in start..endInclusive) array[i] = result[i] } } diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index 3e8cc0404cd..711f3fe9138 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -2278,6 +2278,89 @@ public fun Array.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) } +/** + * 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 { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfArray + */ +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + /** * Sorts a range in the array in-place. * @@ -2289,69 +2372,6 @@ public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit java.util.Arrays.sort(this, fromIndex, toIndex) } -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - * - * @sample samples.collections.Arrays.Sorting.sortRangeOfArray - */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - /** * Sorts the array in-place according to the order specified by the given [comparator]. * @@ -2366,7 +2386,8 @@ 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 { java.util.Arrays.sort(this, fromIndex, toIndex, comparator) } diff --git a/libraries/stdlib/jvm/test/collections/ArraysJVMTest.kt b/libraries/stdlib/jvm/test/collections/ArraysJVMTest.kt index 8b3506cfd7b..642d07344e2 100644 --- a/libraries/stdlib/jvm/test/collections/ArraysJVMTest.kt +++ b/libraries/stdlib/jvm/test/collections/ArraysJVMTest.kt @@ -5,11 +5,8 @@ package test.collections -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertTrue import java.util.Collections +import kotlin.test.* class ArraysJVMTest { diff --git a/libraries/stdlib/src/kotlin/collections/UArraySorting.kt b/libraries/stdlib/src/kotlin/collections/UArraySorting.kt index 78332e26c1f..0dedaec8460 100644 --- a/libraries/stdlib/src/kotlin/collections/UArraySorting.kt +++ b/libraries/stdlib/src/kotlin/collections/UArraySorting.kt @@ -143,10 +143,10 @@ private fun quickSort( * Sorts the given array using qsort algorithm. */ @ExperimentalUnsignedTypes -internal fun sortArray(array: UByteArray) = quickSort(array, 0, array.size - 1) +internal fun sortArray(array: UByteArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) @ExperimentalUnsignedTypes -internal fun sortArray(array: UShortArray) = quickSort(array, 0, array.size - 1) +internal fun sortArray(array: UShortArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) @ExperimentalUnsignedTypes -internal fun sortArray(array: UIntArray) = quickSort(array, 0, array.size - 1) +internal fun sortArray(array: UIntArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) @ExperimentalUnsignedTypes -internal fun sortArray(array: ULongArray) = quickSort(array, 0, array.size - 1) \ No newline at end of file +internal fun sortArray(array: ULongArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1) \ No newline at end of file diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index be2d9aac933..0dd4d25f2e1 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -1456,6 +1456,48 @@ class ArraysTest { doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { reverse() }, snapshot = { toList() }) } + @Test + fun reverseRangeInPlace() { + + fun doTest( + build: Iterable.() -> TArray, + reverse: TArray.(fromIndex: Int, toIndex: Int) -> Unit, + snapshot: TArray.() -> List + ) { + val arrays = (0..7).map { n -> n to (0 until n).build() } + for ((size, array) in arrays) { + for (fromIndex in 0 until size) { + for (toIndex in fromIndex..size) { + val original = array.snapshot().toMutableList() + array.reverse(fromIndex, toIndex) + val reversed = array.snapshot() + assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed) + } + } + + assertFailsWith { array.reverse(-1, size) } + assertFailsWith { array.reverse(0, size + 1) } + assertFailsWith { array.reverse(0, -1) } + } + } + + doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toString()}.toTypedArray() as Array }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + + doTest(build = { map {it}.toIntArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toLong()}.toLongArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toByte()}.toByteArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toShort()}.toShortArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toFloat()}.toFloatArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toDouble()}.toDoubleArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {'a' + it}.toCharArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUInt()}.toUIntArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toULong()}.toULongArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUByte()}.toUByteArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() }) + } + @Test fun reversed() { expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() } @@ -1820,6 +1862,120 @@ class ArraysTest { assertArrayNotSameButEquals(arrayOf("all", "Foo", "9", "80"), strArr) } + @Test fun sortRange() { + + fun > doTest( + build: Iterable.() -> TArray, + sort: TArray.(fromIndex: Int, toIndex: Int) -> Unit, + snapshot: TArray.() -> List + ) { + val arrays = (0..7).map { n -> n to (-2 until n - 2).shuffled().build() } + for ((size, array) in arrays) { + for (fromIndex in 0 until size) { + for (toIndex in fromIndex..size) { + val original = array.snapshot().toMutableList() + array.sort(fromIndex, toIndex) + val sorted = array.snapshot() + assertEquals(original.apply { subList(fromIndex, toIndex).sort() }, sorted) + } + } + + assertFailsWith { array.sort(-1, size) } + assertFailsWith { array.sort(0, size + 1) } + assertFailsWith { array.sort(0, -1) } + } + } + + doTest(build = { map {it.toString()}.toTypedArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toString()}.toTypedArray() as Array }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + + doTest(build = { map {it}.toIntArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toLong()}.toLongArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toByte()}.toByteArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toShort()}.toShortArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toFloat()}.toFloatArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toDouble()}.toDoubleArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {'a' + it}.toCharArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUInt()}.toUIntArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toULong()}.toULongArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUByte()}.toUByteArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUShort()}.toUShortArray() }, sort = { from, to -> sort(from, to) }, snapshot = { toList() }) + } + + @Test + fun sortDescendingRangeInPlace() { + + fun > doTest( + build: Iterable.() -> TArray, + sortDescending: TArray.(fromIndex: Int, toIndex: Int) -> Unit, + snapshot: TArray.() -> List + ) { + val arrays = (0..7).map { n -> n to (-2 until n - 2).build() } + for ((size, array) in arrays) { + for (fromIndex in 0 until size) { + for (toIndex in fromIndex..size) { + val original = array.snapshot().toMutableList() + array.sortDescending(fromIndex, toIndex) + val reversed = array.snapshot() + assertEquals(original.apply { subList(fromIndex, toIndex).sortDescending() }, reversed) + } + } + + assertFailsWith { array.sortDescending(-1, size) } + assertFailsWith { array.sortDescending(0, size + 1) } + assertFailsWith { array.sortDescending(1, 0) } + } + } + + doTest(build = { map {it.toString()}.toTypedArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toString()}.toTypedArray() as Array }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + + doTest(build = { map {it}.toIntArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toLong()}.toLongArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toByte()}.toByteArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toShort()}.toShortArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toFloat()}.toFloatArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toDouble()}.toDoubleArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {'a' + it}.toCharArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUInt()}.toUIntArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toULong()}.toULongArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUByte()}.toUByteArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + doTest(build = { map {it.toUShort()}.toUShortArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() }) + } + + @Test + fun sortDescendingRangeInPlace_Objects() { + data class Text(val data: String) : Comparable { + override fun compareTo(other: Text): Int = data.compareTo(other.data) + } + + val first1 = Text("first") + val first2 = Text(first1.data) + val first3 = Text(first1.data) + val second1 = Text("second") + val second2 = Text(second1.data) + val third1 = Text("third") + + assertEquals(first1, first2) + assertEquals(first1, first3) + assertNotSame(first1, first2) + assertNotSame(first1, first3) + assertNotSame(first2, first3) + + assertEquals(second1, second2) + assertNotSame(second1, second2) + + val original = arrayOf(first3, third1, second2, first2, first1, second1) + original.copyOf().apply { sortDescending(1, 5) }.forEachIndexed { i, e -> + assertSame(original[i], e) + } + + val sorted = arrayOf(third1, second2, second1, first3, first2, first1) + original.apply { sortDescending(0, 6) }.forEachIndexed { i, e -> + assertSame(sorted[i], e) + } + } + @Test fun sortedTests() { assertTrue(arrayOf().sorted().none()) assertEquals(listOf(1), arrayOf(1).sorted()) diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 8debda1ead8..c009c7a40d6 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -1556,14 +1556,23 @@ public final class kotlin/collections/ArraysKt { public static final fun reduceRightOrNull ([ZLkotlin/jvm/functions/Function2;)Ljava/lang/Boolean; public static final fun requireNoNulls ([Ljava/lang/Object;)[Ljava/lang/Object; public static final fun reverse ([B)V + public static final fun reverse ([BII)V public static final fun reverse ([C)V + public static final fun reverse ([CII)V public static final fun reverse ([D)V + public static final fun reverse ([DII)V public static final fun reverse ([F)V + public static final fun reverse ([FII)V public static final fun reverse ([I)V + public static final fun reverse ([III)V public static final fun reverse ([J)V + public static final fun reverse ([JII)V public static final fun reverse ([Ljava/lang/Object;)V + public static final fun reverse ([Ljava/lang/Object;II)V public static final fun reverse ([S)V + public static final fun reverse ([SII)V public static final fun reverse ([Z)V + public static final fun reverse ([ZII)V public static final fun reversed ([B)Ljava/util/List; public static final fun reversed ([C)Ljava/util/List; public static final fun reversed ([D)Ljava/util/List; @@ -1692,6 +1701,7 @@ public final class kotlin/collections/ArraysKt { public static final fun sort ([III)V public static final fun sort ([J)V public static final fun sort ([JII)V + public static final fun sort ([Ljava/lang/Comparable;II)V public static final fun sort ([Ljava/lang/Object;)V public static final fun sort ([Ljava/lang/Object;II)V public static final fun sort ([S)V @@ -1702,18 +1712,27 @@ public final class kotlin/collections/ArraysKt { public static synthetic fun sort$default ([FIIILjava/lang/Object;)V public static synthetic fun sort$default ([IIIILjava/lang/Object;)V public static synthetic fun sort$default ([JIIILjava/lang/Object;)V + public static synthetic fun sort$default ([Ljava/lang/Comparable;IIILjava/lang/Object;)V public static synthetic fun sort$default ([Ljava/lang/Object;IIILjava/lang/Object;)V public static synthetic fun sort$default ([SIIILjava/lang/Object;)V public static final fun sortBy ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V public static final fun sortByDescending ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V public static final fun sortDescending ([B)V + public static final fun sortDescending ([BII)V public static final fun sortDescending ([C)V + public static final fun sortDescending ([CII)V public static final fun sortDescending ([D)V + public static final fun sortDescending ([DII)V public static final fun sortDescending ([F)V + public static final fun sortDescending ([FII)V public static final fun sortDescending ([I)V + public static final fun sortDescending ([III)V public static final fun sortDescending ([J)V + public static final fun sortDescending ([JII)V public static final fun sortDescending ([Ljava/lang/Comparable;)V + public static final fun sortDescending ([Ljava/lang/Comparable;II)V public static final fun sortDescending ([S)V + public static final fun sortDescending ([SII)V public static final fun sortWith ([Ljava/lang/Object;Ljava/util/Comparator;)V public static final fun sortWith ([Ljava/lang/Object;Ljava/util/Comparator;II)V public static synthetic fun sortWith$default ([Ljava/lang/Object;Ljava/util/Comparator;IIILjava/lang/Object;)V @@ -2619,12 +2638,24 @@ public final class kotlin/collections/unsigned/UArraysKt { public static final fun sliceArray-tAntMlw ([ILkotlin/ranges/IntRange;)[I public static final fun sliceArray-xo_DsdI ([BLjava/util/Collection;)[B public static final fun sort--ajY-9A ([I)V + public static final fun sort--nroSd4 ([JII)V + public static synthetic fun sort--nroSd4$default ([JIIILjava/lang/Object;)V + public static final fun sort-4UcCI2c ([BII)V + public static synthetic fun sort-4UcCI2c$default ([BIIILjava/lang/Object;)V + public static final fun sort-Aa5vz7o ([SII)V + public static synthetic fun sort-Aa5vz7o$default ([SIIILjava/lang/Object;)V public static final fun sort-GBYM_sE ([B)V public static final fun sort-QwZRm1k ([J)V + public static final fun sort-oBK06Vg ([III)V + public static synthetic fun sort-oBK06Vg$default ([IIIILjava/lang/Object;)V public static final fun sort-rL5Bavg ([S)V public static final fun sortDescending--ajY-9A ([I)V + public static final fun sortDescending--nroSd4 ([JII)V + public static final fun sortDescending-4UcCI2c ([BII)V + public static final fun sortDescending-Aa5vz7o ([SII)V public static final fun sortDescending-GBYM_sE ([B)V public static final fun sortDescending-QwZRm1k ([J)V + public static final fun sortDescending-oBK06Vg ([III)V public static final fun sortDescending-rL5Bavg ([S)V public static final fun sorted--ajY-9A ([I)Ljava/util/List; public static final fun sorted-GBYM_sE ([B)Ljava/util/List; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 225ae229d23..9b9aa36319e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -1180,7 +1180,7 @@ object ArrayOps : TemplateGroupBase() { returns("Unit") body(ArraysOfUnsigned) { - """if (size > 1) sortArray(this)""" + """if (size > 1) sortArray(this, 0, size)""" } specialFor(ArraysOfPrimitives, ArraysOfObjects) { @@ -1227,7 +1227,7 @@ object ArrayOps : TemplateGroupBase() { } } on(Platform.Native) { - body { """if (size > 1) sortArray(this)""" } + body { """if (size > 1) sortArray(this, 0, size)""" } } } } @@ -1291,11 +1291,24 @@ object ArrayOps : TemplateGroupBase() { } } - val f_sort_range = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") { + val f_sort_range_jvm = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") { platforms(Platform.JVM) - include(ArraysOfObjects, ArraysOfPrimitives) + include(ArraysOfObjects) + } builder { + doc { "Sorts a range in the array in-place." } + appendStableSortNote() + sample("samples.collections.Arrays.Sorting.sortRangeOfArrayOfComparable") + returns("Unit") + body { "java.util.Arrays.sort(this, fromIndex, toIndex)" } + } + + val f_sort_range = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") { + include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) exclude(PrimitiveType.Boolean) } builder { + on(Platform.Common) { since("1.4") } + + typeParam("T : Comparable") doc { "Sorts a range in the array in-place." } specialFor(ArraysOfObjects) { appendStableSortNote() @@ -1305,30 +1318,123 @@ object ArrayOps : TemplateGroupBase() { sample("samples.collections.Arrays.Sorting.sortRangeOfArray") } returns("Unit") - body { - "java.util.Arrays.sort(this, fromIndex, toIndex)" + + if (f == ArraysOfUnsigned) { + since("1.4") + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) + """ + } + return@builder + } + + on(Platform.JVM) { + if (f == ArraysOfObjects) since("1.4") + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + body { + "java.util.Arrays.sort(this, fromIndex, toIndex)" + } + } + on(Platform.Native) { + since("1.4") + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArray(this, fromIndex, toIndex) + """ + } + } + on(Platform.JS) { + since("1.4") + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArrayWith(this, fromIndex, toIndex, naturalOrder()) + """ + } + specialFor(ArraysOfPrimitives) { + if (primitive != PrimitiveType.Long) { + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val subarray = this.asDynamic().subarray(fromIndex, toIndex).unsafeCast() + subarray.sort() + """ + } + } else { + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArrayWith(this.unsafeCast>(), fromIndex, toIndex, naturalOrder()) + """ + } + } + } } } val f_sortWith_range = fn("sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM, Platform.Native) include(ArraysOfObjects) } builder { doc { "Sorts a range in the array in-place with the given [comparator]." } appendStableSortNote() returns("Unit") on(Platform.JVM) { + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") body { "java.util.Arrays.sort(this, fromIndex, toIndex, comparator)" } } on(Platform.Native) { + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") body { - "sortArrayWith(this, fromIndex, toIndex, comparator)" + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArrayWith(this, fromIndex, toIndex, comparator) + """ + } + } + on(Platform.JS) { + since("1.4") + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + sortArrayWith(this, fromIndex, toIndex, comparator) + """ } } } + val f_sortDescending_range = fn("sortDescending(fromIndex: Int, toIndex: Int)") { + include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) + exclude(PrimitiveType.Boolean) + } builder { + since("1.4") + doc { + """ + Sorts elements of the ${f.collection} in the specified range in-place. + The elements are sorted descending according to their natural sort order. + """ + } + returns("Unit") + typeParam("T : Comparable") + + specialFor(ArraysOfObjects) { + appendStableSortNote() + body { """sortWith(reverseOrder(), fromIndex, toIndex)""" } + } + body(ArraysOfPrimitives, ArraysOfUnsigned) { + """ + sort(fromIndex, toIndex) + reverse(fromIndex, toIndex) + """ + } + } val f_asList = fn("asList()") { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 2b5c49c548c..47ab64d95b4 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -53,6 +53,32 @@ object Ordering : TemplateGroupBase() { } } + val f_reverse_range = fn("reverse(fromIndex: Int, toIndex: Int)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) + } builder { + since("1.4") + doc { "Reverses elements of the ${f.collection} in the specified range in-place." } + returns("Unit") + body { + """ + AbstractList.checkRangeIndexes(fromIndex, toIndex, size) + val midPoint = (fromIndex + toIndex) / 2 + if (fromIndex == midPoint) return + var reverseIndex = toIndex - 1 + for (index in fromIndex until midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } + """ + } + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { """storage.reverse(fromIndex, toIndex)""" } + } + } + val f_reversed = fn("reversed()") { include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings) } builder {