In-place reversing.
#KT-9034
This commit is contained in:
@@ -10,6 +10,7 @@ public object Collections {
|
||||
|
||||
public fun <T> sort(list: MutableList<T>, comparator: java.util.Comparator<in T>): Unit = java.util.sort(list, comparator)
|
||||
|
||||
@Deprecated("Use list.reverse() instead.", ReplaceWith("list.reverse()"))
|
||||
public fun <T> reverse(list: MutableList<T>): Unit {
|
||||
val size = list.size()
|
||||
for (i in 0..(size / 2) - 1) {
|
||||
|
||||
@@ -4143,6 +4143,142 @@ public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List<Shor
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun <T> Array<out T>.reverse(): Unit {
|
||||
val midPoint = (size / 2) - 1
|
||||
if (midPoint < 0) return
|
||||
val _this = this as Array<T>
|
||||
var reverseIndex = lastIndex
|
||||
for (index in 0..midPoint) {
|
||||
val tmp = _this[index]
|
||||
_this[index] = _this[reverseIndex]
|
||||
_this[reverseIndex] = tmp
|
||||
reverseIndex--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun BooleanArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun ByteArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun CharArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun DoubleArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun FloatArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun IntArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun LongArray.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--
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
public fun ShortArray.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.
|
||||
*/
|
||||
|
||||
@@ -789,6 +789,13 @@ public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the collection in-place.
|
||||
*/
|
||||
public fun <T> MutableList<T>.reverse(): Unit {
|
||||
java.util.Collections.reverse(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list with elements in reversed order.
|
||||
*/
|
||||
|
||||
@@ -546,6 +546,17 @@ class ArraysTest {
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
|
||||
@test fun reverseInPlace() {
|
||||
assertArrayNotSameButEquals(intArrayOf(3, 2, 1), intArrayOf(1, 2, 3).apply { reverse() })
|
||||
assertArrayNotSameButEquals(byteArrayOf(3, 2, 1), byteArrayOf(1, 2, 3).apply { reverse() })
|
||||
assertArrayNotSameButEquals(shortArrayOf(3, 2, 1), shortArrayOf(1, 2, 3).apply { reverse() })
|
||||
assertArrayNotSameButEquals(longArrayOf(3, 2, 1), longArrayOf(1, 2, 3).apply { reverse() })
|
||||
assertArrayNotSameButEquals(floatArrayOf(3F, 2F, 1F), floatArrayOf(1F, 2F, 3F).apply { reverse() })
|
||||
assertArrayNotSameButEquals(doubleArrayOf(3.0, 2.0, 1.0), doubleArrayOf(1.0, 2.0, 3.0).apply { reverse() })
|
||||
assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').apply { reverse() })
|
||||
assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).apply { reverse() })
|
||||
}
|
||||
|
||||
@test fun reversed() {
|
||||
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() }
|
||||
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() }
|
||||
|
||||
@@ -310,22 +310,29 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reverse() {
|
||||
@test fun reverseInPlace() {
|
||||
val data = arrayListOf<String>()
|
||||
data.reverse()
|
||||
assertTrue(data.isEmpty())
|
||||
|
||||
data.add("foo")
|
||||
data.reverse()
|
||||
assertEquals(listOf("foo"), data)
|
||||
|
||||
data.add("bar")
|
||||
data.reverse()
|
||||
assertEquals(listOf("bar", "foo"), data)
|
||||
|
||||
data.add("zoo")
|
||||
data.reverse()
|
||||
assertEquals(listOf("zoo", "foo", "bar"), data)
|
||||
}
|
||||
|
||||
@test fun reversed() {
|
||||
val data = listOf("foo", "bar")
|
||||
val rev = data.reversed()
|
||||
assertEquals(listOf("bar", "foo"), rev)
|
||||
}
|
||||
|
||||
@test fun reverseFunctionShouldReturnReversedCopyForList() {
|
||||
val list: List<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 3, 2)) { list.reversed() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
@test fun reverseFunctionShouldReturnReversedCopyForIterable() {
|
||||
val iterable: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 3, 2)) { iterable.reversed() }
|
||||
expect(listOf(2, 3, 1)) { iterable }
|
||||
assertNotEquals(data, rev)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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." }
|
||||
|
||||
Reference in New Issue
Block a user