Move copyToArrayImpl implementation to Common source set
This commit is contained in:
committed by
Space Team
parent
6fdfd4e8dd
commit
7010bf2c20
@@ -36,26 +36,10 @@ internal fun <T> copyToArray(collection: Collection<T>): Array<T> {
|
||||
}
|
||||
|
||||
@JsName("copyToArrayImpl")
|
||||
internal actual fun copyToArrayImpl(collection: Collection<*>): Array<Any?> {
|
||||
val array = emptyArray<Any?>()
|
||||
val iterator = collection.iterator()
|
||||
while (iterator.hasNext())
|
||||
array.asDynamic().push(iterator.next())
|
||||
return array
|
||||
}
|
||||
internal actual fun copyToArrayImpl(collection: Collection<*>): Array<Any?> = collectionToArrayCommonImpl(collection)
|
||||
|
||||
@JsName("copyToExistingArrayImpl")
|
||||
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
||||
if (array.size < collection.size)
|
||||
return copyToArrayImpl(collection).unsafeCast<Array<T>>()
|
||||
|
||||
val iterator = collection.iterator()
|
||||
var index = 0
|
||||
while (iterator.hasNext()) {
|
||||
array[index++] = iterator.next().unsafeCast<T>()
|
||||
}
|
||||
return terminateCollectionToArray(collection.size, array)
|
||||
}
|
||||
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> = collectionToArrayCommonImpl(collection, array)
|
||||
|
||||
internal actual fun <T> terminateCollectionToArray(collectionSize: Int, array: Array<T>): Array<T> = array
|
||||
|
||||
|
||||
@@ -84,14 +84,9 @@ internal fun <T> Array<out T>?.contentDeepHashCodeImpl(): Int {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
internal actual fun <T> arrayOfNulls(reference: Array<T>, size: Int): Array<T> = arrayOfNulls<Any>(size) as Array<T>
|
||||
|
||||
internal actual fun copyToArrayImpl(collection: Collection<*>): Array<Any?> {
|
||||
val array = arrayOfUninitializedElements<Any?>(collection.size)
|
||||
val iterator = collection.iterator()
|
||||
var index = 0
|
||||
while (iterator.hasNext())
|
||||
array[index++] = iterator.next()
|
||||
return array
|
||||
}
|
||||
internal actual fun copyToArrayImpl(collection: Collection<*>): Array<Any?> = collectionToArrayCommonImpl(collection)
|
||||
|
||||
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> = collectionToArrayCommonImpl(collection, array)
|
||||
|
||||
internal actual fun <T> terminateCollectionToArray(collectionSize: Int, array: Array<T>): Array<T> = array
|
||||
|
||||
|
||||
@@ -479,6 +479,40 @@ internal fun throwIndexOverflow() { throw ArithmeticException("Index overflow ha
|
||||
@SinceKotlin("1.3")
|
||||
internal fun throwCountOverflow() { throw ArithmeticException("Count overflow has happened.") }
|
||||
|
||||
|
||||
internal fun collectionToArrayCommonImpl(collection: Collection<*>): Array<Any?> {
|
||||
if (collection.isEmpty()) return emptyArray<Any?>()
|
||||
|
||||
val destination = arrayOfNulls<Any>(collection.size)
|
||||
|
||||
val iterator = collection.iterator()
|
||||
var index = 0
|
||||
while (iterator.hasNext()) {
|
||||
destination[index++] = iterator.next()
|
||||
}
|
||||
|
||||
return destination
|
||||
}
|
||||
|
||||
internal fun <T> collectionToArrayCommonImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
||||
if (collection.isEmpty()) return terminateCollectionToArray(0, array)
|
||||
|
||||
val destination = if (array.size < collection.size) {
|
||||
arrayOfNulls(array, collection.size)
|
||||
} else {
|
||||
array
|
||||
}
|
||||
|
||||
val iterator = collection.iterator()
|
||||
var index = 0
|
||||
while (iterator.hasNext()) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
destination[index++] = iterator.next() as T
|
||||
}
|
||||
|
||||
return terminateCollectionToArray(collection.size, destination)
|
||||
}
|
||||
|
||||
/**
|
||||
* In JVM if the size of [array] is bigger than [collectionSize], sets `array[collectionSize] = null`.
|
||||
* In other platforms does nothing.
|
||||
|
||||
@@ -22,17 +22,4 @@ internal fun <T> copyToArray(collection: Collection<T>): Array<T> =
|
||||
//TODO: Find more proper way to call abstract collection's toArray
|
||||
@Suppress("INVISIBLE_MEMBER") collection.toArray() as Array<T>
|
||||
else
|
||||
copyToArrayImpl(collection) as Array<T>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
||||
if (array.size < collection.size)
|
||||
return copyToArrayImpl(collection) as Array<T>
|
||||
|
||||
val iterator = collection.iterator()
|
||||
var index = 0
|
||||
while (iterator.hasNext()) {
|
||||
array[index++] = iterator.next() as T
|
||||
}
|
||||
return terminateCollectionToArray(collection.size, array)
|
||||
}
|
||||
copyToArrayImpl(collection) as Array<T>
|
||||
Reference in New Issue
Block a user