Handle special cases of drop, take, takeLast when it is known in advance that the result is a single-item list. #KT-9990
This commit is contained in:
@@ -2518,15 +2518,7 @@ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? {
|
||||
*/
|
||||
public fun <T> Array<out T>.drop(n: Int): List<T> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<T>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2534,15 +2526,7 @@ public fun <T> Array<out T>.drop(n: Int): List<T> {
|
||||
*/
|
||||
public fun ByteArray.drop(n: Int): List<Byte> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Byte>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2550,15 +2534,7 @@ public fun ByteArray.drop(n: Int): List<Byte> {
|
||||
*/
|
||||
public fun ShortArray.drop(n: Int): List<Short> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Short>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2566,15 +2542,7 @@ public fun ShortArray.drop(n: Int): List<Short> {
|
||||
*/
|
||||
public fun IntArray.drop(n: Int): List<Int> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Int>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2582,15 +2550,7 @@ public fun IntArray.drop(n: Int): List<Int> {
|
||||
*/
|
||||
public fun LongArray.drop(n: Int): List<Long> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Long>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2598,15 +2558,7 @@ public fun LongArray.drop(n: Int): List<Long> {
|
||||
*/
|
||||
public fun FloatArray.drop(n: Int): List<Float> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Float>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2614,15 +2566,7 @@ public fun FloatArray.drop(n: Int): List<Float> {
|
||||
*/
|
||||
public fun DoubleArray.drop(n: Int): List<Double> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Double>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2630,15 +2574,7 @@ public fun DoubleArray.drop(n: Int): List<Double> {
|
||||
*/
|
||||
public fun BooleanArray.drop(n: Int): List<Boolean> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Boolean>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2646,15 +2582,7 @@ public fun BooleanArray.drop(n: Int): List<Boolean> {
|
||||
*/
|
||||
public fun CharArray.drop(n: Int): List<Char> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
return emptyList()
|
||||
val list = ArrayList<Char>(size - n)
|
||||
for (index in n..size - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return takeLast((size - n).coerceAtLeast(0))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3846,6 +3774,7 @@ public fun <T> Array<out T>.take(n: Int): List<T> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<T>(n)
|
||||
for (item in this) {
|
||||
@@ -3853,7 +3782,7 @@ public fun <T> Array<out T>.take(n: Int): List<T> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3863,6 +3792,7 @@ public fun ByteArray.take(n: Int): List<Byte> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Byte>(n)
|
||||
for (item in this) {
|
||||
@@ -3870,7 +3800,7 @@ public fun ByteArray.take(n: Int): List<Byte> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3880,6 +3810,7 @@ public fun ShortArray.take(n: Int): List<Short> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Short>(n)
|
||||
for (item in this) {
|
||||
@@ -3887,7 +3818,7 @@ public fun ShortArray.take(n: Int): List<Short> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3897,6 +3828,7 @@ public fun IntArray.take(n: Int): List<Int> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Int>(n)
|
||||
for (item in this) {
|
||||
@@ -3904,7 +3836,7 @@ public fun IntArray.take(n: Int): List<Int> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3914,6 +3846,7 @@ public fun LongArray.take(n: Int): List<Long> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Long>(n)
|
||||
for (item in this) {
|
||||
@@ -3921,7 +3854,7 @@ public fun LongArray.take(n: Int): List<Long> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3931,6 +3864,7 @@ public fun FloatArray.take(n: Int): List<Float> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Float>(n)
|
||||
for (item in this) {
|
||||
@@ -3938,7 +3872,7 @@ public fun FloatArray.take(n: Int): List<Float> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3948,6 +3882,7 @@ public fun DoubleArray.take(n: Int): List<Double> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Double>(n)
|
||||
for (item in this) {
|
||||
@@ -3955,7 +3890,7 @@ public fun DoubleArray.take(n: Int): List<Double> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3965,6 +3900,7 @@ public fun BooleanArray.take(n: Int): List<Boolean> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Boolean>(n)
|
||||
for (item in this) {
|
||||
@@ -3972,7 +3908,7 @@ public fun BooleanArray.take(n: Int): List<Boolean> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3982,6 +3918,7 @@ public fun CharArray.take(n: Int): List<Char> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(this[0])
|
||||
var count = 0
|
||||
val list = ArrayList<Char>(n)
|
||||
for (item in this) {
|
||||
@@ -3989,7 +3926,7 @@ public fun CharArray.take(n: Int): List<Char> {
|
||||
break
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4000,10 +3937,11 @@ public fun <T> Array<out T>.takeLast(n: Int): List<T> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4014,10 +3952,11 @@ public fun ByteArray.takeLast(n: Int): List<Byte> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4028,10 +3967,11 @@ public fun ShortArray.takeLast(n: Int): List<Short> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4042,10 +3982,11 @@ public fun IntArray.takeLast(n: Int): List<Int> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4056,10 +3997,11 @@ public fun LongArray.takeLast(n: Int): List<Long> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4070,10 +4012,11 @@ public fun FloatArray.takeLast(n: Int): List<Float> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4084,10 +4027,11 @@ public fun DoubleArray.takeLast(n: Int): List<Double> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4098,10 +4042,11 @@ public fun BooleanArray.takeLast(n: Int): List<Boolean> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4112,10 +4057,11 @@ public fun CharArray.takeLast(n: Int): List<Char> {
|
||||
if (n == 0) return emptyList()
|
||||
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 .. size - 1)
|
||||
list.add(this[index])
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -544,6 +544,8 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
|
||||
val resultSize = size - n
|
||||
if (resultSize <= 0)
|
||||
return emptyList()
|
||||
if (resultSize == 1)
|
||||
return listOf(last())
|
||||
list = ArrayList<T>(resultSize)
|
||||
if (this is List<T>) {
|
||||
if (this is RandomAccess) {
|
||||
@@ -553,7 +555,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
|
||||
for (item in listIterator(n))
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -713,7 +715,10 @@ public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
|
||||
public fun <T> Iterable<T>.take(n: Int): List<T> {
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (this is Collection<T> && n >= size) return toList()
|
||||
if (this is Collection<T>) {
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(first())
|
||||
}
|
||||
var count = 0
|
||||
val list = ArrayList<T>(n)
|
||||
for (item in this) {
|
||||
@@ -732,6 +737,7 @@ public fun <T> List<T>.takeLast(n: Int): List<T> {
|
||||
if (n == 0) return emptyList()
|
||||
val size = size
|
||||
if (n >= size) return toList()
|
||||
if (n == 1) return listOf(last())
|
||||
val list = ArrayList<T>(n)
|
||||
if (this is RandomAccess) {
|
||||
for (index in size - n .. size - 1)
|
||||
@@ -740,7 +746,7 @@ public fun <T> List<T>.takeLast(n: Int): List<T> {
|
||||
for (item in listIterator(n))
|
||||
list.add(item)
|
||||
}
|
||||
return list.optimizeReadOnlyList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user