Unify MutableList.reverse function template between JVM and JS

This commit is contained in:
Ilya Gorbunov
2016-12-07 05:18:27 +03:00
parent 085f476d22
commit e890cb137f
4 changed files with 17 additions and 16 deletions
-12
View File
@@ -75,15 +75,3 @@ public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
private fun <T> collectionsSort(list: MutableList<T>, comparator: Comparator<in T>): Unit = noImpl
/**
* Reverses elements in the list in-place.
*/
public fun <T> MutableList<T>.reverse(): Unit {
val size = this.size
for (i in 0..(size / 2) - 1) {
val i2 = size - i - 1
val tmp = this[i]
this[i] = this[i2]
this[i2] = tmp
}
}
@@ -780,6 +780,21 @@ public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>
return list
}
/**
* Reverses elements in the list in-place.
*/
public fun <T> MutableList<T>.reverse(): Unit {
val midPoint = (size / 2) - 1
if (midPoint < 0) return
var reverseIndex = lastIndex
for (index in 0..midPoint) {
val tmp = this[index]
this[index] = this[reverseIndex]
this[reverseIndex] = tmp
reverseIndex--
}
}
/**
* Returns a list with elements in reversed order.
*/
@@ -784,7 +784,6 @@ public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>
/**
* Reverses elements in the list in-place.
*/
@kotlin.jvm.JvmVersion
public fun <T> MutableList<T>.reverse(): Unit {
java.util.Collections.reverse(this)
}
@@ -8,10 +8,9 @@ fun ordering(): List<GenericFunction> {
templates add f("reverse()") {
doc { f -> "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." }
only(Lists, InvariantArraysOfObjects, ArraysOfPrimitives)
jvmOnly(Lists) { true }
customReceiver(Lists) { "MutableList<T>" }
returns { "Unit" }
body { f ->
body {
"""
val midPoint = (size / 2) - 1
if (midPoint < 0) return
@@ -24,7 +23,7 @@ fun ordering(): List<GenericFunction> {
}
"""
}
body(Lists) { """java.util.Collections.reverse(this)""" }
body(Platform.JVM, Lists) { """java.util.Collections.reverse(this)""" }
}
templates add f("reversed()") {