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
@@ -99,7 +99,21 @@ fun ordering(): List<GenericFunction> {
typeParam("T : Comparable<T>")
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 {
"""
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) }
"""
}
body(ArraysOfPrimitives) {
"""
return toTypedArray().apply { sortWith(comparator) }.asList()
"""
}
body(ArraysOfObjects) {
"""
return sortedArrayWith(comparator).asList()
"""
}
returns("SELF", Sequences)
doc(Sequences) {
@@ -136,7 +136,7 @@ fun specialJS(): List<GenericFunction> {
exclude(PrimitiveType.Boolean)
annotations("@native")
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" }
}
@@ -144,8 +144,13 @@ fun specialJS(): List<GenericFunction> {
only(ArraysOfObjects)
exclude(PrimitiveType.Boolean)
returns("Unit")
doc { "Sorts the array inplace according to the order specified by the given [comparator] object." }
body { "sort { a, b -> comparator.compare(a, b) }" }
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()") {
@@ -153,7 +158,7 @@ fun specialJS(): List<GenericFunction> {
only(numericPrimitives + PrimitiveType.Char)
exclude(PrimitiveType.Long)
returns("Unit")
doc { "Sorts the array inplace." }
doc { "Sorts the array in-place." }
annotations("""@library("primitiveArraySort")""")
body { "return noImpl" }
}
@@ -163,10 +168,11 @@ fun specialJS(): List<GenericFunction> {
only(PrimitiveType.Long)
typeParam("T: Comparable<T>")
returns("Unit")
doc { "Sorts the array inplace." }
doc { "Sorts the array in-place." }
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())") {
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc { "Sorts array or range in array inplace." }
doc { "Sorts a range in the array in-place." }
returns("Unit")
body {
"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())") {
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")
body {
"Arrays.sort(this, fromIndex, toIndex, comparator)"
}
}
templates add f("filterIsInstanceTo(destination: C, klass: Class<R>)") {