From 1a4e1d7f55c2e8e8dd52b758f11938b1939e464a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 28 Dec 2018 04:19:02 +0300 Subject: [PATCH] Rewrite copyOfUninitializedElements with templates without using copyRangeTo --- .../main/kotlin/generated/_ArraysNative.kt | 243 ++++++++++++++++++ .../kotlin/kotlin/collections/ArrayUtil.kt | 138 +--------- 2 files changed, 244 insertions(+), 137 deletions(-) diff --git a/runtime/src/main/kotlin/generated/_ArraysNative.kt b/runtime/src/main/kotlin/generated/_ArraysNative.kt index f3417426554..a7d6aac116a 100644 --- a/runtime/src/main/kotlin/generated/_ArraysNative.kt +++ b/runtime/src/main/kotlin/generated/_ArraysNative.kt @@ -1051,6 +1051,249 @@ public actual inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): Ch return copyOfUninitializedElements(fromIndex, toIndex) } +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Array { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = arrayOfUninitializedElements(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = ByteArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = ShortArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = IntArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = LongArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = FloatArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): DoubleArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = DoubleArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun BooleanArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): BooleanArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = BooleanArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = CharArray(newSize) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) + return result +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun Array.copyOfUninitializedElements(newSize: Int): Array { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun ByteArray.copyOfUninitializedElements(newSize: Int): ByteArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun ShortArray.copyOfUninitializedElements(newSize: Int): ShortArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun LongArray.copyOfUninitializedElements(newSize: Int): LongArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun FloatArray.copyOfUninitializedElements(newSize: Int): FloatArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun DoubleArray.copyOfUninitializedElements(newSize: Int): DoubleArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun BooleanArray.copyOfUninitializedElements(newSize: Int): BooleanArray { + return copyOfUninitializedElements(0, newSize) +} + +/** + * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +@PublishedApi +internal fun CharArray.copyOfUninitializedElements(newSize: Int): CharArray { + return copyOfUninitializedElements(0, newSize) +} + /** * Returns an array containing all elements of the original array and then the given [element]. */ diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt index 357e408dbe7..0bdd6da3e81 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt @@ -19,37 +19,6 @@ internal fun arrayOfUninitializedElements(size: Int): Array { return Array(size) } -/** - * Returns new array which is a copy of the original array with new elements filled with **lateinit** _uninitialized_ values. - * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, - * either throwing exception or returning some kind of implementation-specific default value. - */ -@PublishedApi -internal fun Array.copyOfUninitializedElements(newSize: Int): Array = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun ByteArray.copyOfUninitializedElements(newSize: Int): ByteArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun ShortArray.copyOfUninitializedElements(newSize: Int): ShortArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun LongArray.copyOfUninitializedElements(newSize: Int): LongArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun CharArray.copyOfUninitializedElements(newSize: Int): CharArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun FloatArray.copyOfUninitializedElements(newSize: Int): FloatArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun DoubleArray.copyOfUninitializedElements(newSize: Int): DoubleArray = copyOfUninitializedElements(0, newSize) - -@PublishedApi -internal fun BooleanArray.copyOfUninitializedElements(newSize: Int): BooleanArray = copyOfUninitializedElements(0, newSize) /** * Returns a new array which is a copy of the original array with new elements filled with null values. @@ -64,7 +33,7 @@ internal fun Array.copyOfNulls(fromIndex: Int, toIndex: Int): Array { throw IllegalArgumentException("$fromIndex > $toIndex") } val result = @Suppress("TYPE_PARAMETER_AS_REIFIED") arrayOfNulls(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size)) return result } @@ -95,111 +64,6 @@ internal fun collectionToArray(collection: Collection, array: Array internal fun collectionToArray(collection: Collection): Array = collectionToArray(collection, arrayOfUninitializedElements(collection.size)) -/** - * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) - * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. - * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, - * either throwing exception or returning some kind of implementation-specific default value. - */ -@PublishedApi -internal fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Array { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = arrayOfUninitializedElements(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = ByteArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = ShortArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = IntArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = LongArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = CharArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = FloatArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): DoubleArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = DoubleArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - -@PublishedApi -internal fun BooleanArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): BooleanArray { - val newSize = toIndex - fromIndex - if (newSize < 0) { - throw IllegalArgumentException("$fromIndex > $toIndex") - } - val result = BooleanArray(newSize) - copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) - return result -} - /** * Resets an array element at a specified index to some implementation-specific _uninitialized_ value.