Generate to[Primitive]Array() methods for generic arrays and collections.

#KT-4180 Fixed
This commit is contained in:
Ilya Gorbunov
2015-04-20 23:15:21 +03:00
parent d18b086113
commit 7fba979372
4 changed files with 231 additions and 0 deletions
@@ -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<out Boolean>.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<Boolean>.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<out Byte>.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<Byte>.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<out Char>.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<Char>.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<out Double>.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<Double>.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<out Float>.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<Float>.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<out Int>.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<Int>.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<out Long>.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<Long>.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<out Short>.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<Short>.toShortArray(): ShortArray {
val result = ShortArray(size())
var index = 0
for (element in this)
result[index++] = element
return result
}
@@ -295,6 +295,18 @@ class ArraysTest {
assertEquals(10, intsAsList[1], "Should reflect changes in original array")
}
test fun toPrimitiveArray() {
val genericArray: Array<Int> = 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
@@ -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)
@@ -56,3 +56,34 @@ fun arrays(): List<GenericFunction> {
return templates
}
fun toPrimitiveArrays(): List<GenericFunction> =
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
"""
}
}
}