Reverse range and sortDescending range #KT-36955
This commit is contained in:
@@ -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())
|
||||
|
||||
+31
@@ -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;
|
||||
|
||||
@@ -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<T>")
|
||||
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<SELF>()
|
||||
subarray.sort()
|
||||
"""
|
||||
}
|
||||
} else {
|
||||
body {
|
||||
"""
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
sortArrayWith(this.unsafeCast<Array<Long>>(), fromIndex, toIndex, naturalOrder())
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sortWith_range = fn("sortWith(comparator: Comparator<in T>, 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<T>")
|
||||
|
||||
specialFor(ArraysOfObjects) {
|
||||
appendStableSortNote()
|
||||
body { """sortWith(reverseOrder(), fromIndex, toIndex)""" }
|
||||
}
|
||||
body(ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
"""
|
||||
sort(fromIndex, toIndex)
|
||||
reverse(fromIndex, toIndex)
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val f_asList = fn("asList()") {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user