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