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
@@ -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() }