Sorting optimizations: do not make excessive copies, introduce methods to sort the whole array.

#KT-9904 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-05 19:12:32 +03:00
parent 3e81cdfc5d
commit 8bdd1e3246
7 changed files with 199 additions and 65 deletions
+23 -20
View File
@@ -489,7 +489,7 @@ public inline fun ShortArray.plus(element: Short): ShortArray {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun ByteArray.sort(): Unit { public fun ByteArray.sort(): Unit {
@@ -497,7 +497,7 @@ public fun ByteArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun CharArray.sort(): Unit { public fun CharArray.sort(): Unit {
@@ -505,7 +505,7 @@ public fun CharArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun DoubleArray.sort(): Unit { public fun DoubleArray.sort(): Unit {
@@ -513,7 +513,7 @@ public fun DoubleArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun FloatArray.sort(): Unit { public fun FloatArray.sort(): Unit {
@@ -521,7 +521,7 @@ public fun FloatArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun IntArray.sort(): Unit { public fun IntArray.sort(): Unit {
@@ -529,7 +529,7 @@ public fun IntArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
@library("primitiveArraySort") @library("primitiveArraySort")
public fun ShortArray.sort(): Unit { public fun ShortArray.sort(): Unit {
@@ -537,21 +537,23 @@ public fun ShortArray.sort(): Unit {
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
public fun <T: Comparable<T>> Array<out T>.sort(): Unit { public fun <T: Comparable<T>> Array<out T>.sort(): Unit {
sort { a: T, b: T -> a.compareTo(b) } if (size > 1)
sort { a: T, b: T -> a.compareTo(b) }
} }
/** /**
* Sorts the array inplace. * Sorts the array in-place.
*/ */
public fun LongArray.sort(): Unit { public fun LongArray.sort(): Unit {
sort { a: Long, b: Long -> a.compareTo(b) } if (size > 1)
sort { a: Long, b: Long -> a.compareTo(b) }
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun <T> Array<out T>.sort(comparison: (T, T) -> Int): Unit { public fun <T> Array<out T>.sort(comparison: (T, T) -> Int): Unit {
@@ -559,7 +561,7 @@ public fun <T> Array<out T>.sort(comparison: (T, T) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit { public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit {
@@ -567,7 +569,7 @@ public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit { public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit {
@@ -575,7 +577,7 @@ public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit { public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit {
@@ -583,7 +585,7 @@ public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit { public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit {
@@ -591,7 +593,7 @@ public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun IntArray.sort(comparison: (Int, Int) -> Int): Unit { public fun IntArray.sort(comparison: (Int, Int) -> Int): Unit {
@@ -599,7 +601,7 @@ public fun IntArray.sort(comparison: (Int, Int) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit { public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit {
@@ -607,7 +609,7 @@ public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparison] function. * Sorts the array in-place according to the order specified by the given [comparison] function.
*/ */
@native @native
public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit { public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit {
@@ -615,10 +617,11 @@ public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit {
} }
/** /**
* Sorts the array inplace according to the order specified by the given [comparator] object. * 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 { public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
sort { a, b -> comparator.compare(a, b) } if (size > 1)
sort { a, b -> comparator.compare(a, b) }
} }
/** /**
+7 -9
View File
@@ -999,17 +999,15 @@
mutableList.sort(boundComparator); mutableList.sort(boundComparator);
} }
//TODO: should be deleted when List will be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) if (mutableList.size > 1) {
var array = []; //TODO: should be deleted when List will be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects)
var it = mutableList.iterator(); var array = Kotlin.copyToArray(mutableList);
while (it.hasNext()) {
array.push(it.next());
}
array.sort(boundComparator); array.sort(boundComparator);
for (var i = 0, n = array.length; i < n; i++) { for (var i = 0, n = array.length; i < n; i++) {
mutableList.set_vux3hl$(i, array[i]); mutableList.set_vux3hl$(i, array[i]);
}
} }
}; };
+98 -26
View File
@@ -4725,56 +4725,56 @@ public fun ShortArray.sortDescending(): Unit {
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun <T : Comparable<T>> Array<out T>.sorted(): List<T> { public fun <T : Comparable<T>> Array<out T>.sorted(): List<T> {
return toArrayList().apply { sort() } return sortedArray().asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun ByteArray.sorted(): List<Byte> { public fun ByteArray.sorted(): List<Byte> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun CharArray.sorted(): List<Char> { public fun CharArray.sorted(): List<Char> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun DoubleArray.sorted(): List<Double> { public fun DoubleArray.sorted(): List<Double> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun FloatArray.sorted(): List<Float> { public fun FloatArray.sorted(): List<Float> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun IntArray.sorted(): List<Int> { public fun IntArray.sorted(): List<Int> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun LongArray.sorted(): List<Long> { public fun LongArray.sorted(): List<Long> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun ShortArray.sorted(): List<Short> { public fun ShortArray.sorted(): List<Short> {
return toArrayList().apply { sort() } return toTypedArray().apply { sort() }.asList()
} }
/** /**
@@ -5099,63 +5099,63 @@ public fun ShortArray.sortedDescending(): List<Short> {
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun <T> Array<out T>.sortedWith(comparator: Comparator<in T>): List<T> { public fun <T> Array<out T>.sortedWith(comparator: Comparator<in T>): List<T> {
return toArrayList().apply { sortWith(comparator) } return sortedArrayWith(comparator).asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun BooleanArray.sortedWith(comparator: Comparator<in Boolean>): List<Boolean> { public fun BooleanArray.sortedWith(comparator: Comparator<in Boolean>): List<Boolean> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun ByteArray.sortedWith(comparator: Comparator<in Byte>): List<Byte> { public fun ByteArray.sortedWith(comparator: Comparator<in Byte>): List<Byte> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun CharArray.sortedWith(comparator: Comparator<in Char>): List<Char> { public fun CharArray.sortedWith(comparator: Comparator<in Char>): List<Char> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun DoubleArray.sortedWith(comparator: Comparator<in Double>): List<Double> { public fun DoubleArray.sortedWith(comparator: Comparator<in Double>): List<Double> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun FloatArray.sortedWith(comparator: Comparator<in Float>): List<Float> { public fun FloatArray.sortedWith(comparator: Comparator<in Float>): List<Float> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun IntArray.sortedWith(comparator: Comparator<in Int>): List<Int> { public fun IntArray.sortedWith(comparator: Comparator<in Int>): List<Int> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun LongArray.sortedWith(comparator: Comparator<in Long>): List<Long> { public fun LongArray.sortedWith(comparator: Comparator<in Long>): List<Long> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun ShortArray.sortedWith(comparator: Comparator<in Short>): List<Short> { public fun ShortArray.sortedWith(comparator: Comparator<in Short>): List<Short> {
return toArrayList().apply { sortWith(comparator) } return toTypedArray().apply { sortWith(comparator) }.asList()
} }
/** /**
@@ -11447,7 +11447,71 @@ public operator fun <T> Array<T>.plus(element: T): Array<T> {
} }
/** /**
* Sorts array or range in array inplace. * Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun <T> Array<out T>.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun ByteArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun CharArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun DoubleArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun FloatArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun IntArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun LongArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts the array in-place.
*/
@kotlin.jvm.JvmVersion
public fun ShortArray.sort(): Unit {
if (size > 1) Arrays.sort(this)
}
/**
* Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11455,7 +11519,7 @@ public fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size()): Uni
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11463,7 +11527,7 @@ public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11471,7 +11535,7 @@ public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11479,7 +11543,7 @@ public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11487,7 +11551,7 @@ public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11495,7 +11559,7 @@ public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11503,7 +11567,7 @@ public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * Sorts a range in the array in-place.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -11511,7 +11575,15 @@ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
} }
/** /**
* Sorts array or range in array inplace. * 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) Arrays.sort(this, comparator)
}
/**
* Sorts a range in the array in-place with the given [comparator].
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size()): Unit { public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size()): Unit {
@@ -848,6 +848,10 @@ public fun <T : Comparable<T>> MutableList<T>.sortDescending(): Unit {
* Returns a list of all elements sorted according to their natural sort order. * Returns a list of all elements sorted according to their natural sort order.
*/ */
public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> { public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
return toArrayList().apply { sort() } return toArrayList().apply { sort() }
} }
@@ -876,6 +880,10 @@ public fun <T : Comparable<T>> Iterable<T>.sortedDescending(): List<T> {
* Returns a list of all elements sorted according to the specified [comparator]. * Returns a list of all elements sorted according to the specified [comparator].
*/ */
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> { public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toArrayList().apply { sortWith(comparator) } return toArrayList().apply { sortWith(comparator) }
} }
@@ -99,7 +99,21 @@ fun ordering(): List<GenericFunction> {
typeParam("T : Comparable<T>") typeParam("T : Comparable<T>")
body { body {
""" """
return toArrayList().apply { sort() } if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
return toArrayList().apply { sort() }
"""
}
body(ArraysOfPrimitives) {
"""
return toTypedArray().apply { sort() }.asList()
"""
}
body(ArraysOfObjects) {
"""
return sortedArray().asList()
""" """
} }
@@ -214,9 +228,23 @@ fun ordering(): List<GenericFunction> {
} }
body { body {
""" """
if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toArrayList().apply { sortWith(comparator) } return toArrayList().apply { sortWith(comparator) }
""" """
} }
body(ArraysOfPrimitives) {
"""
return toTypedArray().apply { sortWith(comparator) }.asList()
"""
}
body(ArraysOfObjects) {
"""
return sortedArrayWith(comparator).asList()
"""
}
returns("SELF", Sequences) returns("SELF", Sequences)
doc(Sequences) { doc(Sequences) {
@@ -136,7 +136,7 @@ fun specialJS(): List<GenericFunction> {
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
annotations("@native") annotations("@native")
returns("Unit") returns("Unit")
doc { "Sorts the array inplace according to the order specified by the given [comparison] function." } doc { "Sorts the array in-place according to the order specified by the given [comparison] function." }
body { "return noImpl" } body { "return noImpl" }
} }
@@ -144,8 +144,13 @@ fun specialJS(): List<GenericFunction> {
only(ArraysOfObjects) only(ArraysOfObjects)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
returns("Unit") returns("Unit")
doc { "Sorts the array inplace according to the order specified by the given [comparator] object." } doc { "Sorts the array in-place according to the order specified by the given [comparator] object." }
body { "sort { a, b -> comparator.compare(a, b) }" } body {
"""
if (size > 1)
sort { a, b -> comparator.compare(a, b) }
"""
}
} }
templates add f("sort()") { templates add f("sort()") {
@@ -153,7 +158,7 @@ fun specialJS(): List<GenericFunction> {
only(numericPrimitives + PrimitiveType.Char) only(numericPrimitives + PrimitiveType.Char)
exclude(PrimitiveType.Long) exclude(PrimitiveType.Long)
returns("Unit") returns("Unit")
doc { "Sorts the array inplace." } doc { "Sorts the array in-place." }
annotations("""@library("primitiveArraySort")""") annotations("""@library("primitiveArraySort")""")
body { "return noImpl" } body { "return noImpl" }
} }
@@ -163,10 +168,11 @@ fun specialJS(): List<GenericFunction> {
only(PrimitiveType.Long) only(PrimitiveType.Long)
typeParam("T: Comparable<T>") typeParam("T: Comparable<T>")
returns("Unit") returns("Unit")
doc { "Sorts the array inplace." } doc { "Sorts the array in-place." }
body { body {
""" """
sort { a: T, b: T -> a.compareTo(b) } if (size > 1)
sort { a: T, b: T -> a.compareTo(b) }
""" """
} }
} }
@@ -119,24 +119,43 @@ fun specialJVM(): List<GenericFunction> {
} }
} }
templates add f("sort()") {
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc { "Sorts the array in-place." }
returns("Unit")
body {
"if (size > 1) Arrays.sort(this)"
}
}
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") { templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") {
only(ArraysOfObjects, ArraysOfPrimitives) only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
doc { "Sorts array or range in array inplace." } doc { "Sorts a range in the array in-place." }
returns("Unit") returns("Unit")
body { body {
"Arrays.sort(this, fromIndex, toIndex)" "Arrays.sort(this, fromIndex, toIndex)"
} }
} }
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) Arrays.sort(this, comparator)"
}
}
templates add f("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size())") { templates add f("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size())") {
only(ArraysOfObjects) only(ArraysOfObjects)
doc { "Sorts array or range in array inplace." } doc { "Sorts a range in the array in-place with the given [comparator]." }
returns("Unit") returns("Unit")
body { body {
"Arrays.sort(this, fromIndex, toIndex, comparator)" "Arrays.sort(this, fromIndex, toIndex, comparator)"
} }
} }
templates add f("filterIsInstanceTo(destination: C, klass: Class<R>)") { templates add f("filterIsInstanceTo(destination: C, klass: Class<R>)") {