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
@@ -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]
}
}