In-place reversing.

#KT-9034
This commit is contained in:
Ilya Gorbunov
2015-10-30 18:30:25 +03:00
parent 1400bdc09a
commit 4fc54a12b8
7 changed files with 200 additions and 27 deletions
@@ -106,6 +106,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
val buildPrimitives = LinkedHashSet(defaultPrimitives)
val buildFamilyPrimitives = FamilyProperty<Set<PrimitiveType>>()
val customReceiver = FamilyProperty<String>()
val customSignature = FamilyProperty<String>()
val deprecate = DeprecationProperty()
val doc = FamilyProperty<String>()
@@ -293,7 +294,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
}
val isAsteriskOrT = if (receiverAsterisk[f] == true) "*" else "T"
val receiver = when (f) {
val receiver = (customReceiver[f] ?: when (f) {
Iterables -> "Iterable<$isAsteriskOrT>"
Collections -> "Collection<$isAsteriskOrT>"
Lists -> "List<$isAsteriskOrT>"
@@ -310,7 +311,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
ProgressionsOfPrimitives -> primitive?.let { it.name + "Progression" } ?: throw IllegalArgumentException("Primitive progression should specify primitive type")
Primitives -> primitive?.let { it.name } ?: throw IllegalArgumentException("Primitive should specify primitive type")
Generic -> "T"
}.let { renderType(it, it) }
}).let { renderType(it, it) }
fun String.renderType(): String = renderType(this, receiver)
@@ -6,18 +6,28 @@ import templates.DocExtensions.collection
fun ordering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
// templates add f("reverse()") {
// deprecate("reverse will change its behavior soon. Use reversed() instead.")
// deprecateReplacement("reversed()")
// doc { "Returns a list with elements in reversed order." }
// returns { "List<T>" }
// body { """return reversed()""" }
//
// include(Strings)
// returns(Strings) { "SELF" }
//
// exclude(Sequences)
// }
templates add f("reverse()") {
doc { f -> "Reverses elements in the ${f.collection} in-place." }
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
customReceiver(Lists) { "MutableList<T>" }
returns { "Unit" }
body { f ->
val _this = if (f == ArraysOfObjects) "_this" else "this"
"""
val midPoint = (size / 2) - 1
if (midPoint < 0) return
${if (f == ArraysOfObjects) "val _this = this as Array<T>" else "" }
var reverseIndex = lastIndex
for (index in 0..midPoint) {
val tmp = $_this[index]
$_this[index] = $_this[reverseIndex]
$_this[reverseIndex] = tmp
reverseIndex--
}
"""
}
body(Lists) { """java.util.Collections.reverse(this)""" }
}
templates add f("reversed()") {
doc { "Returns a list with elements in reversed order." }