Andrea Giuliano
12/09/2020, 1:06 PM.toList()
method no problem but then when I check for efficiency the blame graph tells me that tolist() is not that efficient. Anything I’m missing? Any suggestion?Nicolas Chaduc
12/09/2020, 1:17 PM.toList()
create new list object with all of your int references each call.
.toList()
core :
/**
* Returns a [List] containing all elements.
*/
public fun <T> Iterable<T>.toList(): List<T> {
if (this is Collection) {
return when (size) {
0 -> emptyList()
1 -> listOf(if (this is List) get(0) else iterator().next())
else -> this.toMutableList()
}
}
return this.toMutableList().optimizeReadOnlyList()
}
toMutableList()
core :
/**
* Returns a new [MutableList] filled with all elements of this collection.
*/
public fun <T> Iterable<T>.toMutableList(): MutableList<T> {
if (this is Collection<T>)
return this.toMutableList()
return toCollection(ArrayList<T>())
}
IntArray is not collection and it’s `toCollection()`call :
/**
* Appends all elements to the given [destination] collection.
*/
public fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(destination: C): C {
for (item in this) {
destination.add(item)
}
return destination
}
Andrea Giuliano
12/09/2020, 1:18 PMNicolas Chaduc
12/09/2020, 1:20 PMfor
loop you need to use IntArray iterator directly I think and not convert itAdam Powell
12/09/2020, 2:19 PMephemient
12/09/2020, 2:30 PM.toList()
creates a copy, while .asList()
creates a wrapper (if that makes a difference to you)Andrea Giuliano
12/09/2020, 3:20 PMasad.awadia
12/09/2020, 4:53 PMelizarov
12/09/2020, 4:53 PMasad.awadia
12/09/2020, 4:54 PMnanodeath
12/09/2020, 5:15 PMAndrea Giuliano
12/12/2020, 5:22 PM