Optimize K/N ArrayList.toArray #KT-42720

This commit is contained in:
Abduqodiri Qurbonzoda
2021-03-02 10:45:00 +03:00
parent 421cb6971d
commit 1c1492da3d
@@ -164,6 +164,26 @@ actual class ArrayList<E> private constructor(
return array.subarrayContentToString(offset, length)
}
@Suppress("UNCHECKED_CAST")
override fun <T> toArray(destination: Array<T>): Array<T> {
if (destination.size < length) {
return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array<T>
}
(array as Array<T>).copyInto(destination, 0, startIndex = offset, endIndex = offset + length)
if (destination.size > length) {
destination[length] = null as T // null-terminate
}
return destination
}
override fun toArray(): Array<Any?> {
@Suppress("UNCHECKED_CAST")
return array.copyOfRange(fromIndex = offset, toIndex = offset + length) as Array<Any?>
}
// ---------------------------- private ----------------------------
private fun checkElementIndex(index: Int) {