From 12e3542bce14bf6a0bcdead921cd840fbe02b1e9 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 16 Jul 2015 17:33:53 +0300 Subject: [PATCH] Provide toTypedArray method for primitive arrays. --- js/js.libraries/src/core/kotlin_special.kt | 56 +++++++++++++ libraries/stdlib/src/generated/_SpecialJVM.kt | 80 +++++++++++++++++++ .../stdlib/test/collections/ArraysTest.kt | 7 ++ .../src/templates/SpecialJS.kt | 16 ++++ .../src/templates/SpecialJVM.kt | 18 +++++ 5 files changed, 177 insertions(+) diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index a8e8ea898eb..a26fc5bd68d 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -489,3 +489,59 @@ public inline fun ShortArray.plus(element: Short): ShortArray { return (this: dynamic).concat(arrayOf(element)) } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + return copyOf() as Array +} + diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index 004e2e79538..260d2271b8b 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -903,3 +903,83 @@ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index c87e21297e6..4b1647b741d 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -457,6 +457,13 @@ class ArraysTest { assertEquals(charList, charArray.asList()) } + test fun toTypedArray() { + val primitiveArray: LongArray = longArrayOf(1, 2, Long.MAX_VALUE) + val genericArray: Array = primitiveArray.toTypedArray() + expect(3) { genericArray.size() } + assertEquals(primitiveArray.asList(), genericArray.asList()) + } + test fun copyOf() { booleanArrayOf(true, false, true).let { assertArrayNotSameButEquals(it, it.copyOf()) } byteArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index 829ec53a024..401dc8a65e8 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -21,6 +21,22 @@ fun specialJS(): List { body(ArraysOfPrimitives) {"""return (this as Array).asList()"""} } + + templates add f("toTypedArray()") { + only(ArraysOfPrimitives) + returns("Array") + doc { + """ + Returns a *typed* object array containing all of the elements of this primitive array. + """ + } + body { + """ + return copyOf() as Array + """ + } + } + templates add f("copyOfRange(from: Int, to: Int)") { // TODO: Arguments checking as in java? only(ArraysOfObjects, ArraysOfPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index fc7c95dfe33..829947d49b7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -216,5 +216,23 @@ fun specialJVM(): List { } } + templates add f("toTypedArray()") { + only(ArraysOfPrimitives) + returns("Array") + doc { + """ + Returns a *typed* object array containing all of the elements of this primitive array. + """ + } + body { + """ + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array + """ + } + } + return templates } \ No newline at end of file