Unify Array.sort and sortWith function templates between JVM and JS.
Introduce new inline-only overload Array<out T: Comparable<T>>.sort().
This commit is contained in:
@@ -12850,6 +12850,78 @@ public inline fun CharArray.asList(): List<Char> {
|
||||
return this.unsafeCast<Array<Char>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun IntArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun LongArray.sort(): Unit {
|
||||
if (size > 1)
|
||||
sort { a: Long, b: Long -> a.compareTo(b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun ByteArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun ShortArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun DoubleArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun FloatArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun CharArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the natural order of its elements.
|
||||
*/
|
||||
public fun <T: Comparable<T>> Array<out T>.sort(): Unit {
|
||||
if (size > 1)
|
||||
sort { a: T, b: T -> a.compareTo(b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the order specified by the given [comparator].
|
||||
*/
|
||||
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
if (size > 1)
|
||||
sort { a, b -> comparator.compare(a, b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@@ -13328,70 +13400,6 @@ public inline fun <T> Array<out T>.plusElement(element: T): Array<T> {
|
||||
return this.asDynamic().concat(arrayOf(element))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun ByteArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun ShortArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun IntArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun FloatArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun DoubleArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@library("primitiveArraySort")
|
||||
public fun CharArray.sort(): Unit {
|
||||
noImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun <T: Comparable<T>> Array<out T>.sort(): Unit {
|
||||
if (size > 1)
|
||||
sort { a: T, b: T -> a.compareTo(b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun LongArray.sort(): Unit {
|
||||
if (size > 1)
|
||||
sort { a: Long, b: Long -> a.compareTo(b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the order specified by the given [comparison] function.
|
||||
*/
|
||||
@@ -13448,11 +13456,3 @@ public inline fun CharArray.sort(noinline comparison: (Char, Char) -> Int): Unit
|
||||
asDynamic().sort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the order specified by the given [comparator] object.
|
||||
*/
|
||||
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
if (size > 1)
|
||||
sort { a, b -> comparator.compare(a, b) }
|
||||
}
|
||||
|
||||
|
||||
@@ -12989,6 +12989,70 @@ public fun CharArray.asList(): List<Char> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun IntArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun LongArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun ByteArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun ShortArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun DoubleArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun FloatArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
public fun CharArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the natural order of its elements.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T: Comparable<T>> Array<out T>.sort(): Unit {
|
||||
(this as Array<Any?>).sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place according to the order specified by the given [comparator].
|
||||
*/
|
||||
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this, comparator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@@ -13870,69 +13934,15 @@ public inline fun <T> Array<T>.plusElement(element: T): Array<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
* Sorts the array in-place according to the natural order of its elements.
|
||||
*
|
||||
* @throws ClassCastException if any element of the array is not [Comparable].
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun <T> Array<out T>.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun ByteArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun ShortArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun IntArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun LongArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun FloatArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun DoubleArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place.
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun CharArray.sort(): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a range in the array in-place.
|
||||
*/
|
||||
@@ -13997,14 +14007,6 @@ public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
java.util.Arrays.sort(this, fromIndex, toIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in-place with the given [comparator].
|
||||
*/
|
||||
@kotlin.jvm.JvmVersion
|
||||
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
if (size > 1) java.util.Arrays.sort(this, comparator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a range in the array in-place with the given [comparator].
|
||||
*/
|
||||
|
||||
@@ -144,43 +144,5 @@ fun specialJS(): List<GenericFunction> {
|
||||
body { "asDynamic().sort(comparison)" }
|
||||
}
|
||||
|
||||
templates add f("sortWith(comparator: Comparator<in T>)") {
|
||||
only(ArraysOfObjects)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
returns("Unit")
|
||||
doc { "Sorts the array in-place according to the order specified by the given [comparator] object." }
|
||||
body {
|
||||
"""
|
||||
if (size > 1)
|
||||
sort { a, b -> comparator.compare(a, b) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sort()") {
|
||||
only(ArraysOfPrimitives)
|
||||
only(numericPrimitives + PrimitiveType.Char)
|
||||
exclude(PrimitiveType.Long)
|
||||
returns("Unit")
|
||||
doc { "Sorts the array in-place." }
|
||||
annotations("""@library("primitiveArraySort")""")
|
||||
body { "noImpl" }
|
||||
}
|
||||
|
||||
templates add f("sort()") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
only(PrimitiveType.Long)
|
||||
typeParam("T: Comparable<T>")
|
||||
returns("Unit")
|
||||
doc { "Sorts the array in-place." }
|
||||
body {
|
||||
"""
|
||||
if (size > 1)
|
||||
sort { a: T, b: T -> a.compareTo(b) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return templates
|
||||
}
|
||||
@@ -172,9 +172,15 @@ fun specialJVM(): List<GenericFunction> {
|
||||
|
||||
|
||||
templates add f("sort()") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
doc { "Sorts the array in-place." }
|
||||
// left with more generic signature for JVM only
|
||||
only(ArraysOfObjects)
|
||||
doc {
|
||||
"""
|
||||
Sorts the array in-place according to the natural order of its elements.
|
||||
|
||||
@throws ClassCastException if any element of the array is not [Comparable].
|
||||
"""
|
||||
}
|
||||
returns("Unit")
|
||||
body {
|
||||
"if (size > 1) java.util.Arrays.sort(this)"
|
||||
@@ -191,15 +197,6 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortWith(comparator: Comparator<in T>)") {
|
||||
only(ArraysOfObjects)
|
||||
doc { "Sorts the array in-place with the given [comparator]." }
|
||||
returns("Unit")
|
||||
body {
|
||||
"if (size > 1) java.util.Arrays.sort(this, comparator)"
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||
only(ArraysOfObjects)
|
||||
doc { "Sorts a range in the array in-place with the given [comparator]." }
|
||||
@@ -253,6 +250,68 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
object CommonArrays {
|
||||
fun f_sortPrimitives() =
|
||||
(PrimitiveType.numericPrimitives + PrimitiveType.Char).map { primitive ->
|
||||
f("sort()") {
|
||||
only(ArraysOfPrimitives)
|
||||
only(primitive)
|
||||
doc { "Sorts the array in-place." }
|
||||
returns("Unit")
|
||||
body(Platform.JVM) {
|
||||
"if (size > 1) java.util.Arrays.sort(this)"
|
||||
}
|
||||
if (primitive != PrimitiveType.Long) {
|
||||
annotations(Platform.JS, """@library("primitiveArraySort")""")
|
||||
body(Platform.JS) { "noImpl" }
|
||||
}
|
||||
else {
|
||||
body(Platform.JS) {
|
||||
"""
|
||||
if (size > 1)
|
||||
sort { a: T, b: T -> a.compareTo(b) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun f_sort() = f("sort()") {
|
||||
only(ArraysOfObjects)
|
||||
typeParam("T: Comparable<T>")
|
||||
doc {
|
||||
"""
|
||||
Sorts the array in-place according to the natural order of its elements.
|
||||
"""
|
||||
}
|
||||
returns("Unit")
|
||||
inline(Platform.JVM, Inline.Only)
|
||||
body(Platform.JVM) {
|
||||
"(this as Array<Any?>).sort()"
|
||||
}
|
||||
body(Platform.JS) {
|
||||
"""
|
||||
if (size > 1)
|
||||
sort { a: T, b: T -> a.compareTo(b) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
fun f_sortWith() = f("sortWith(comparator: Comparator<in T>)") {
|
||||
only(ArraysOfObjects)
|
||||
buildFamilyPrimitives(Platform.JS, buildFamilyPrimitives.default!! - PrimitiveType.Boolean)
|
||||
doc { "Sorts the array in-place according to the order specified by the given [comparator]." }
|
||||
returns("Unit")
|
||||
body(Platform.JVM) {
|
||||
"if (size > 1) java.util.Arrays.sort(this, comparator)"
|
||||
}
|
||||
body(Platform.JS) {
|
||||
"""
|
||||
if (size > 1)
|
||||
sort { a, b -> comparator.compare(a, b) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
fun f_asList() = f("asList()") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns a [List] that wraps the original array." }
|
||||
@@ -313,5 +372,5 @@ object CommonArrays {
|
||||
}
|
||||
|
||||
// TODO: use reflection later to get all functions of matching type
|
||||
fun templates() = listOf(this.f_asList(), this.f_toTypedArray())
|
||||
fun templates() = f_sortPrimitives() + listOf(f_sort(), f_sortWith(), f_asList(), f_toTypedArray())
|
||||
}
|
||||
Reference in New Issue
Block a user