From 7fba9793724804dfa2f9cde54dd48523127d4630 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 20 Apr 2015 23:15:21 +0300 Subject: [PATCH] Generate to[Primitive]Array() methods for generic arrays and collections. #KT-4180 Fixed --- .../src/generated/_ArraysToPrimitiveArrays.kt | 180 ++++++++++++++++++ .../stdlib/test/collections/ArraysTest.kt | 12 ++ .../src/generators/GenerateCollections.kt | 8 + .../kotlin-stdlib-gen/src/templates/Arrays.kt | 31 +++ 4 files changed, 231 insertions(+) create mode 100644 libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt diff --git a/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt b/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt new file mode 100644 index 00000000000..14175cea9e1 --- /dev/null +++ b/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt @@ -0,0 +1,180 @@ +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.platform.* +import java.util.* + +import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js + +/** + * Returns an array of Boolean containing all of the elements of this generic array. + */ +public fun Array.toBooleanArray(): BooleanArray { + val result = BooleanArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Boolean containing all of the elements of this collection. + */ +public fun Collection.toBooleanArray(): BooleanArray { + val result = BooleanArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Byte containing all of the elements of this generic array. + */ +public fun Array.toByteArray(): ByteArray { + val result = ByteArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Byte containing all of the elements of this collection. + */ +public fun Collection.toByteArray(): ByteArray { + val result = ByteArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Char containing all of the elements of this generic array. + */ +public fun Array.toCharArray(): CharArray { + val result = CharArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Char containing all of the elements of this collection. + */ +public fun Collection.toCharArray(): CharArray { + val result = CharArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Double containing all of the elements of this generic array. + */ +public fun Array.toDoubleArray(): DoubleArray { + val result = DoubleArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Double containing all of the elements of this collection. + */ +public fun Collection.toDoubleArray(): DoubleArray { + val result = DoubleArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Float containing all of the elements of this generic array. + */ +public fun Array.toFloatArray(): FloatArray { + val result = FloatArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Float containing all of the elements of this collection. + */ +public fun Collection.toFloatArray(): FloatArray { + val result = FloatArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Int containing all of the elements of this generic array. + */ +public fun Array.toIntArray(): IntArray { + val result = IntArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Int containing all of the elements of this collection. + */ +public fun Collection.toIntArray(): IntArray { + val result = IntArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Long containing all of the elements of this generic array. + */ +public fun Array.toLongArray(): LongArray { + val result = LongArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Long containing all of the elements of this collection. + */ +public fun Collection.toLongArray(): LongArray { + val result = LongArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Short containing all of the elements of this generic array. + */ +public fun Array.toShortArray(): ShortArray { + val result = ShortArray(size()) + for (index in indices) + result[index] = this[index] + return result +} + +/** + * Returns an array of Short containing all of the elements of this collection. + */ +public fun Collection.toShortArray(): ShortArray { + val result = ShortArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index f9a13f53784..9307f3b69a3 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -295,6 +295,18 @@ class ArraysTest { assertEquals(10, intsAsList[1], "Should reflect changes in original array") } + test fun toPrimitiveArray() { + val genericArray: Array = array(1, 2, 3) + val primitiveArray: IntArray = genericArray.toIntArray() + expect(3) { primitiveArray.size() } + assertEquals(genericArray.asList(), primitiveArray.asList()) + + + val charList = listOf('a', 'b') + val charArray: CharArray = charList.toCharArray() + assertEquals(charList, charArray.asList()) + } + /* TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index 0ec2a06ba48..85b7729fc19 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -21,6 +21,14 @@ fun generateCollectionsAPI(outDir: File) { specialJVM().writeTo(File(outDir, "_SpecialJVM.kt")) { build() } ranges().writeTo(File(outDir, "_Ranges.kt")) { build() } + toPrimitiveArrays().writeTo(File(outDir, "_ArraysToPrimitiveArrays.kt")) { + val builder = StringBuilder() + for (family in buildFamilies) + build(builder, family, buildPrimitives.single()) + + builder.toString() + } + numeric().writeTo(File(outDir, "_Numeric.kt")) { val builder = StringBuilder() for (numeric in numericPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 642e4f2761c..9f833057273 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -56,3 +56,34 @@ fun arrays(): List { return templates } + + + +fun toPrimitiveArrays(): List = + PrimitiveType.values().map { primitive -> + val arrayType = primitive.name + "Array" + f("to$arrayType()") { + only(ArraysOfObjects, Collections) + only(primitive) + doc(ArraysOfObjects) { "Returns an array of ${primitive.name} containing all of the elements of this generic array." } + doc(Collections) { "Returns an array of ${primitive.name} containing all of the elements of this collection." } + returns(arrayType) + body { + """ + val result = $arrayType(size()) + for (index in indices) + result[index] = this[index] + return result + """ + } + body(Collections) { + """ + val result = $arrayType(size()) + var index = 0 + for (element in this) + result[index++] = element + return result + """ + } + } + }