small optimization of List.fill

This commit is contained in:
Sergey Chernov 2026-04-20 14:09:09 +03:00
parent 87060c7eb7
commit 4acee56de8

View File

@ -438,10 +438,9 @@ fun List<T>.sort(): void {
*/ */
static fun List<T>.fill(size: Int, capacity = -1, block: (Int)->T): List<T> { static fun List<T>.fill(size: Int, capacity = -1, block: (Int)->T): List<T> {
require(size >= 0, "size must not be negative") require(size >= 0, "size must not be negative")
val capacity = capacity < size ? size : capacity
List<T>().also { List<T>().also {
if( size > 0 ) { if( size > 0 ) {
it.ensureCapacity(capacity) it.ensureCapacity(capacity < size ? size : capacity)
for( i in 0..<size ) it += block(i) for( i in 0..<size ) it += block(i)
} }
} }