Reverse range and sortDescending range #KT-36955

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-03 07:23:34 +03:00
parent 05cb0e8994
commit d6472aa700
12 changed files with 1091 additions and 85 deletions
@@ -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 <T> Array<T>.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 <T : Comparable<T>> Array<out T>.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 <T : Comparable<T>> Array<out T>.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 <T : Comparable<T>> Array<out T>.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 <T : Comparable<T>> Array<out T>.sort(): Unit
*/
public expect fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): 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 <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Unit
/**
* Returns an array of Boolean containing all of the elements of this generic array.
*/
@@ -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)
}
/**
@@ -1826,6 +1826,110 @@ public fun <T> Array<out T>.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 <T : Comparable<T>> Array<out T>.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<ByteArray>()
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<ShortArray>()
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<IntArray>()
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<Array<Long>>(), 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<FloatArray>()
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<DoubleArray>()
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<CharArray>()
subarray.sort()
}
/**
* Sorts the array in-place according to the order specified by the given [comparison] function.
*/
@@ -1891,6 +1995,18 @@ public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): 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 <T> Array<out T>.sortWith(comparator: Comparator<in T>, 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.
*/
@@ -1844,6 +1844,110 @@ public fun <T> Array<out T>.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 <T : Comparable<T>> Array<out T>.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<ByteArray>()
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<ShortArray>()
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<IntArray>()
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<Array<Long>>(), 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<FloatArray>()
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<DoubleArray>()
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<CharArray>()
subarray.sort()
}
/**
* Sorts the array in-place according to the order specified by the given [comparison] function.
*/
@@ -1909,6 +2013,18 @@ public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): 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 <T> Array<out T>.sortWith(comparator: Comparator<in T>, 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.
*/
@@ -22,6 +22,12 @@ internal fun <T> sortArrayWith(array: Array<out T>, comparator: Comparator<in T>
}
}
internal fun <T> sortArrayWith(array: Array<out T>, fromIndex: Int, toIndex: Int, comparator: Comparator<in T>) {
if (fromIndex < toIndex - 1) {
mergeSort(array.unsafeCast<Array<T>>(), fromIndex, toIndex - 1, comparator)
}
}
internal fun <T : Comparable<T>> sortArray(array: Array<out T>) {
if (getStableSortingIsSupported()) {
val comparison = { a: T, b: T -> a.compareTo(b) }
@@ -56,7 +62,7 @@ private fun <T> mergeSort(array: Array<T>, start: Int, endInclusive: Int, compar
val buffer = arrayOfNulls<Any?>(array.size).unsafeCast<Array<T>>()
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]
}
}
@@ -2278,6 +2278,89 @@ public fun <T> Array<out T>.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 <T : Comparable<T>> Array<out T>.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 <T> Array<out T>.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 <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.sort(this, fromIndex, toIndex, comparator)
}
@@ -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 {
@@ -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)
internal fun sortArray(array: ULongArray, fromIndex: Int, toIndex: Int) = quickSort(array, fromIndex, toIndex - 1)
@@ -1456,6 +1456,48 @@ class ArraysTest {
doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { reverse() }, snapshot = { toList() })
}
@Test
fun reverseRangeInPlace() {
fun <TArray, T> doTest(
build: Iterable<Int>.() -> TArray,
reverse: TArray.(fromIndex: Int, toIndex: Int) -> Unit,
snapshot: TArray.() -> List<T>
) {
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<IndexOutOfBoundsException> { array.reverse(-1, size) }
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
assertFailsWith<IllegalArgumentException> { 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<out String> }, 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 <TArray, T : Comparable<T>> doTest(
build: Iterable<Int>.() -> TArray,
sort: TArray.(fromIndex: Int, toIndex: Int) -> Unit,
snapshot: TArray.() -> List<T>
) {
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<IndexOutOfBoundsException> { array.sort(-1, size) }
assertFailsWith<IndexOutOfBoundsException> { array.sort(0, size + 1) }
assertFailsWith<IllegalArgumentException> { 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<out String> }, 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 <TArray, T : Comparable<T>> doTest(
build: Iterable<Int>.() -> TArray,
sortDescending: TArray.(fromIndex: Int, toIndex: Int) -> Unit,
snapshot: TArray.() -> List<T>
) {
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<IndexOutOfBoundsException> { array.sortDescending(-1, size) }
assertFailsWith<IndexOutOfBoundsException> { array.sortDescending(0, size + 1) }
assertFailsWith<IllegalArgumentException> { 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<out String> }, 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<Text> {
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<Long>().sorted().none())
assertEquals(listOf(1), arrayOf(1).sorted())