Skip to content

Optimize reversed() for arrays #5423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5771,89 +5771,71 @@ public fun CharArray.reverse(fromIndex: Int, toIndex: Int): Unit {
*/
public fun <T> Array<out T>.reversed(): List<T> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun ByteArray.reversed(): List<Byte> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun ShortArray.reversed(): List<Short> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun IntArray.reversed(): List<Int> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun LongArray.reversed(): List<Long> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun FloatArray.reversed(): List<Float> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun DoubleArray.reversed(): List<Double> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun BooleanArray.reversed(): List<Boolean> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
* Returns a list with elements in reversed order.
*/
public fun CharArray.reversed(): List<Char> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
Expand Down
16 changes: 4 additions & 12 deletions libraries/stdlib/common/src/generated/_UArrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2802,9 +2802,7 @@ public inline fun UShortArray.reverse(fromIndex: Int, toIndex: Int): Unit {
@ExperimentalUnsignedTypes
public fun UIntArray.reversed(): List<UInt> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
Expand All @@ -2814,9 +2812,7 @@ public fun UIntArray.reversed(): List<UInt> {
@ExperimentalUnsignedTypes
public fun ULongArray.reversed(): List<ULong> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
Expand All @@ -2826,9 +2822,7 @@ public fun ULongArray.reversed(): List<ULong> {
@ExperimentalUnsignedTypes
public fun UByteArray.reversed(): List<UByte> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
Expand All @@ -2838,9 +2832,7 @@ public fun UByteArray.reversed(): List<UByte> {
@ExperimentalUnsignedTypes
public fun UShortArray.reversed(): List<UShort> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ object Ordering : TemplateGroupBase() {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
return copyOf().apply { reverse() }.asList()
"""
}

Expand Down