Implement sorting extension functions for UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-02-22 18:29:39 +03:00
committed by Ilya Gorbunov
parent c42dbb34ca
commit cb587893c0
6 changed files with 590 additions and 41 deletions
@@ -2511,6 +2511,214 @@ public inline fun UShortArray.reversedArray(): UShortArray {
return UShortArray(storage.reversedArray())
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sorted(): List<UInt> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sorted(): List<ULong> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sorted(): List<UByte> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sorted(): List<UShort> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedArray(): UIntArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedArray(): ULongArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedArray(): UByteArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedArray(): UShortArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedArrayDescending(): UIntArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedArrayDescending(): ULongArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedArrayDescending(): UByteArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedArrayDescending(): UShortArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns a list of all elements 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.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedDescending(): List<UInt> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements 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.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedDescending(): List<ULong> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements 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.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedDescending(): List<UByte> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements 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.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedDescending(): List<UShort> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns an array of type [ByteArray], which is a view of this array where each element is a signed reinterpretation
* of the corresponding element of this array.
@@ -3191,6 +3399,42 @@ public inline operator fun UShortArray.plus(elements: UShortArray): UShortArray
return UShortArray(storage + elements.storage)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Returns an array of type [ByteArray], which is a copy of this array where each element is a signed reinterpretation
* of the corresponding element of this array.
@@ -0,0 +1,152 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.collections
// UByteArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UByteArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UByteArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// UShortArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UShortArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UShortArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// UIntArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UIntArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UIntArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// ULongArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: ULongArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: ULongArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// Interfaces =============================================================================
/**
* Sorts the given array using qsort algorithm.
*/
@ExperimentalUnsignedTypes
internal fun sortArray(array: UByteArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: UShortArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: UIntArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: ULongArray) = quickSort(array, 0, array.size - 1)
@@ -923,4 +923,118 @@ class UnsignedArraysTest {
expect(listOf(2uL)) { ulongArrayOf(2, 3).filterNot { it > 2 } }
}
@Test
fun sort() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.apply { sort() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.apply { sort() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.apply { sort() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.apply { sort() })
}
@Test
fun sortDescending() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.apply { sortDescending() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.apply { sortDescending() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.apply { sortDescending() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.apply { sortDescending() })
}
@Test
fun sorted() {
assertTrue(uintArrayOf().sorted().none())
assertEquals(listOf(1u), uintArrayOf(1).sorted())
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.sorted())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.sorted())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.sorted())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.sorted())
}
@Test
fun sortedDescending() {
assertTrue(uintArrayOf().sortedDescending().none())
assertEquals(listOf(1u), uintArrayOf(1).sortedDescending())
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.sortedDescending())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.sortedDescending())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.sortedDescending())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.sortedDescending())
}
@Test
fun sortedBy() {
assertTrue(uintArrayOf().sortedBy { it.toString() }.none())
assertEquals(listOf(1u), uintArrayOf(1).sortedBy { it.toString() })
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(250, UByte.MAX_VALUE, 0, 1, 2, 5, 9, 80), ubyteArray.sortedBy { it.toByte() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(65501, UShort.MAX_VALUE, 0, 1, 2, 5, 9, 80), ushortArray.sortedBy { it.toShort() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(4294967200, UInt.MAX_VALUE, 0, 1, 2, 5, 9, 80), uintArray.sortedBy { it.toInt() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(ULong.MAX_VALUE - 123, ULong.MAX_VALUE, 0, 1, 2, 5, 9, 80), ulongArray.sortedBy { it.toLong() })
}
@Test
fun sortedArray() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.sortedArray())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.sortedArray())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.sortedArray())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.sortedArray())
}
@Test
fun sortedArrayDescending() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.sortedArrayDescending())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.sortedArrayDescending())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.sortedArrayDescending())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.sortedArrayDescending())
}
}
@@ -2395,6 +2395,30 @@ public final class kotlin/collections/UArraysKt {
public static final fun sliceArray-ojwP5H8 ([SLjava/util/Collection;)[S
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-GBYM_sE ([B)V
public static final fun sort-QwZRm1k ([J)V
public static final fun sort-rL5Bavg ([S)V
public static final fun sortDescending--ajY-9A ([I)V
public static final fun sortDescending-GBYM_sE ([B)V
public static final fun sortDescending-QwZRm1k ([J)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;
public static final fun sorted-QwZRm1k ([J)Ljava/util/List;
public static final fun sorted-rL5Bavg ([S)Ljava/util/List;
public static final fun sortedArray--ajY-9A ([I)[I
public static final fun sortedArray-GBYM_sE ([B)[B
public static final fun sortedArray-QwZRm1k ([J)[J
public static final fun sortedArray-rL5Bavg ([S)[S
public static final fun sortedArrayDescending--ajY-9A ([I)[I
public static final fun sortedArrayDescending-GBYM_sE ([B)[B
public static final fun sortedArrayDescending-QwZRm1k ([J)[J
public static final fun sortedArrayDescending-rL5Bavg ([S)[S
public static final fun sortedDescending--ajY-9A ([I)Ljava/util/List;
public static final fun sortedDescending-GBYM_sE ([B)Ljava/util/List;
public static final fun sortedDescending-QwZRm1k ([J)Ljava/util/List;
public static final fun sortedDescending-rL5Bavg ([S)Ljava/util/List;
public static final fun sumOfUByte ([Lkotlin/UByte;)I
public static final fun sumOfUInt ([Lkotlin/UInt;)I
public static final fun sumOfULong ([Lkotlin/ULong;)J
@@ -1010,54 +1010,62 @@ object ArrayOps : TemplateGroupBase() {
val f_sort = fn("sort()") {
include(ArraysOfPrimitives, PrimitiveType.numericPrimitives + PrimitiveType.Char)
include(ArraysOfUnsigned)
include(ArraysOfObjects)
} builder {
typeParam("T : Comparable<T>")
doc { "Sorts the array in-place according to the natural order of its elements." }
appendStableSortNote()
specialFor(ArraysOfPrimitives) {
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) {
doc { "Sorts the array in-place." }
}
returns("Unit")
on(Platform.JS) {
body {
"""if (size > 1) sortArray(this)"""
body(ArraysOfUnsigned) {
"""if (size > 1) sortArray(this)"""
}
specialFor(ArraysOfPrimitives, ArraysOfObjects) {
on(Platform.JS) {
body {
"""if (size > 1) sortArray(this)"""
}
specialFor(ArraysOfPrimitives) {
if (primitive != PrimitiveType.Long) {
on(Backend.Legacy) {
annotation("""@library("primitiveArraySort")""")
body { "definedExternally" }
}
on(Backend.IR) {
body { "this.asDynamic().sort()" }
}
} else {
body {
"""if (size > 1) sort { a: T, b: T -> a.compareTo(b) }"""
}
}
}
}
specialFor(ArraysOfPrimitives) {
if (primitive != PrimitiveType.Long) {
on(Backend.Legacy) {
annotation("""@library("primitiveArraySort")""")
body { "definedExternally" }
}
on(Backend.IR) {
body { "this.asDynamic().sort()" }
}
} else {
on(Platform.JVM) {
specialFor(ArraysOfObjects) {
inlineOnly()
body {
"""if (size > 1) sort { a: T, b: T -> a.compareTo(b) }"""
"""
@Suppress("UNCHECKED_CAST")
(this as Array<Any?>).sort()
"""
}
}
specialFor(ArraysOfPrimitives) {
body {
"if (size > 1) java.util.Arrays.sort(this)"
}
}
}
}
on(Platform.JVM) {
specialFor(ArraysOfObjects) {
inlineOnly()
body {
"""
@Suppress("UNCHECKED_CAST")
(this as Array<Any?>).sort()
"""
}
on(Platform.Native) {
body { """if (size > 1) sortArray(this)""" }
}
specialFor(ArraysOfPrimitives) {
body {
"if (size > 1) java.util.Arrays.sort(this)"
}
}
}
on(Platform.Native) {
body { """if (size > 1) sortArray(this)""" }
}
}
@@ -133,6 +133,7 @@ object Ordering : TemplateGroupBase() {
val f_sorted = fn("sorted()") {
includeDefault()
exclude(PrimitiveType.Boolean)
include(ArraysOfUnsigned)
} builder {
doc {
@@ -140,7 +141,7 @@ object Ordering : TemplateGroupBase() {
Returns a list of all elements sorted according to their natural sort order.
"""
}
if (f != ArraysOfPrimitives) {
if (f != ArraysOfPrimitives && f != ArraysOfUnsigned) {
appendStableSortNote()
}
returns("List<T>")
@@ -160,6 +161,11 @@ object Ordering : TemplateGroupBase() {
return toTypedArray().apply { sort() }.asList()
"""
}
body(ArraysOfUnsigned) {
"""
return copyOf().apply { sort() }.asList()
"""
}
body(ArraysOfObjects) {
"""
return sortedArray().asList()
@@ -188,7 +194,7 @@ object Ordering : TemplateGroupBase() {
}
val f_sortedArray = fn("sortedArray()") {
include(InvariantArraysOfObjects, ArraysOfPrimitives)
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
exclude(PrimitiveType.Boolean)
} builder {
doc {
@@ -208,11 +214,11 @@ object Ordering : TemplateGroupBase() {
}
val f_sortDescending = fn("sortDescending()") {
include(Lists, ArraysOfObjects, ArraysOfPrimitives)
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
exclude(PrimitiveType.Boolean)
} builder {
doc { """Sorts elements in the ${f.collection} in-place descending according to their natural sort order.""" }
if (f != ArraysOfPrimitives) {
if (f != ArraysOfPrimitives && f != ArraysOfUnsigned) {
appendStableSortNote()
}
returns("Unit")
@@ -222,7 +228,7 @@ object Ordering : TemplateGroupBase() {
}
body { """sortWith(reverseOrder())""" }
body(ArraysOfPrimitives) {
body(ArraysOfPrimitives, ArraysOfUnsigned) {
"""
if (size > 1) {
sort()
@@ -235,6 +241,7 @@ object Ordering : TemplateGroupBase() {
val f_sortedDescending = fn("sortedDescending()") {
includeDefault()
exclude(PrimitiveType.Boolean)
include(ArraysOfUnsigned)
} builder {
doc {
@@ -252,7 +259,7 @@ object Ordering : TemplateGroupBase() {
return sortedWith(reverseOrder())
"""
}
body(ArraysOfPrimitives) {
body(ArraysOfPrimitives, ArraysOfUnsigned) {
"""
return copyOf().apply { sort() }.reversed()
"""
@@ -269,7 +276,7 @@ object Ordering : TemplateGroupBase() {
}
val f_sortedArrayDescending = fn("sortedArrayDescending()") {
include(InvariantArraysOfObjects, ArraysOfPrimitives)
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
exclude(PrimitiveType.Boolean)
} builder {
doc {
@@ -286,7 +293,7 @@ object Ordering : TemplateGroupBase() {
return this.copyOf().apply { sortWith(reverseOrder()) }
"""
}
body(ArraysOfPrimitives) {
body(ArraysOfPrimitives, ArraysOfUnsigned) {
"""
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }