Introduce operations on Arrays returning Arrays: reversedArray, sortedArray.

#KT-8711
This commit is contained in:
Ilya Gorbunov
2015-08-28 19:05:10 +03:00
parent 89df3925fa
commit faa26cdb25
7 changed files with 426 additions and 20 deletions
@@ -51,6 +51,38 @@ fun ordering(): List<GenericFunction> {
exclude(Sequences)
}
templates add f("reversedArray()") {
doc { "Returns an array with elements of this array in reversed order." }
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
returns("SELF")
body(ArraysOfObjectsSubtype) {
"""
if (isEmpty()) return this
val result = this.copyOf() as Array<T>
var i1 = 0
var i2 = lastIndex
while (i1 < i2) {
val tmp = result[i1]
result[i1] = result[i2]
result[i2] = tmp
i1 += 1
i2 -= 1
}
return result as A
"""
}
body(ArraysOfPrimitives) {
"""
if (isEmpty()) return this
val result = SELF(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
"""
}
}
templates add f("sorted()") {
exclude(Strings)
exclude(PrimitiveType.Boolean)
@@ -87,6 +119,22 @@ fun ordering(): List<GenericFunction> {
}
}
templates add f("sortedArray()") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc {
"Returns an array with all elements of this array sorted according to their natural sort order."
}
typeParam("T : Comparable<T>")
returns("SELF")
body() {
"""
if (isEmpty()) return this
return this.copyOf().apply { sort() } as SELF
"""
}
}
templates add f("sortedDescending()") {
exclude(Strings)
exclude(PrimitiveType.Boolean)
@@ -120,6 +168,31 @@ fun ordering(): List<GenericFunction> {
}
}
templates add f("sortedArrayDescending()") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc {
"Returns an array with all elements of this array sorted descending according to their natural sort order."
}
typeParam("T : Comparable<T>")
returns("SELF")
body(ArraysOfObjectsSubtype) {
"""
if (isEmpty()) return this
// TODO: Use reverseOrder<T>()
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) } as SELF
"""
}
body() {
"""
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as SELF
"""
}
}
templates add f("sortedWith(comparator: Comparator<in T>)") {
exclude(Strings)
returns("List<T>")
@@ -153,6 +226,20 @@ fun ordering(): List<GenericFunction> {
}
}
templates add f("sortedArrayWith(comparator: Comparator<in T>)") {
only(ArraysOfObjectsSubtype)
doc {
"Returns an array with all elements of this array sorted according the specified [comparator]."
}
returns("SELF")
body() {
"""
if (isEmpty()) return this
return this.copyOf().apply { sortWith(comparator) } as SELF
"""
}
}
templates add f("sortedBy(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) selector: (T) -> R?)") {
exclude(Strings)
inline(true)
@@ -140,6 +140,14 @@ fun specialJS(): List<GenericFunction> {
body { "return noImpl" }
}
templates add f("sortWith(comparator: Comparator<in T>)") {
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) }" }
}
templates add f("sort()") {
only(ArraysOfPrimitives)
only(numericPrimitives + PrimitiveType.Char)
@@ -127,6 +127,16 @@ fun specialJVM(): List<GenericFunction> {
}
}
templates add f("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size())") {
only(ArraysOfObjects)
doc { "Sorts array or range in array inplace." }
returns("Unit")
body {
"Arrays.sort(this, fromIndex, toIndex, comparator)"
}
}
templates add f("filterIsInstanceTo(destination: C, klass: Class<R>)") {
doc { "Appends all elements that are instances of specified class to the given [destination]." }
receiverAsterisk(true)