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)
}
/**