Skip to content

Optimize takeLast() for arrays #5425

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
45 changes: 9 additions & 36 deletions libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5005,10 +5005,7 @@ public fun <T> Array<out T>.takeLast(n: Int): List<T> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<T>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5024,10 +5021,7 @@ public fun ByteArray.takeLast(n: Int): List<Byte> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Byte>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5043,10 +5037,7 @@ public fun ShortArray.takeLast(n: Int): List<Short> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Short>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5062,10 +5053,7 @@ public fun IntArray.takeLast(n: Int): List<Int> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Int>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5081,10 +5069,7 @@ public fun LongArray.takeLast(n: Int): List<Long> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Long>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5100,10 +5085,7 @@ public fun FloatArray.takeLast(n: Int): List<Float> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Float>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5119,10 +5101,7 @@ public fun DoubleArray.takeLast(n: Int): List<Double> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Double>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5138,10 +5117,7 @@ public fun BooleanArray.takeLast(n: Int): List<Boolean> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Boolean>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -5157,10 +5133,7 @@ public fun CharArray.takeLast(n: Int): List<Char> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Char>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand Down
20 changes: 4 additions & 16 deletions libraries/stdlib/common/src/generated/_UArrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2482,10 +2482,7 @@ public fun UIntArray.takeLast(n: Int): List<UInt> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UInt>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -2503,10 +2500,7 @@ public fun ULongArray.takeLast(n: Int): List<ULong> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<ULong>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -2524,10 +2518,7 @@ public fun UByteArray.takeLast(n: Int): List<UByte> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UByte>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand All @@ -2545,10 +2536,7 @@ public fun UShortArray.takeLast(n: Int): List<UShort> {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UShort>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,7 @@ object Filtering : TemplateGroupBase() {
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])

val list = ArrayList<T>(n)
for (index in size - n until size)
list.add(this[index])
return list
return copyOfRange(size - n, size).asList()
"""
}
body(Lists) {
Expand Down