Provide toTypedArray method for primitive arrays.
This commit is contained in:
@@ -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<Boolean> {
|
||||
return copyOf() as Array<Boolean>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ByteArray.toTypedArray(): Array<Byte> {
|
||||
return copyOf() as Array<Byte>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun CharArray.toTypedArray(): Array<Char> {
|
||||
return copyOf() as Array<Char>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun DoubleArray.toTypedArray(): Array<Double> {
|
||||
return copyOf() as Array<Double>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun FloatArray.toTypedArray(): Array<Float> {
|
||||
return copyOf() as Array<Float>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun IntArray.toTypedArray(): Array<Int> {
|
||||
return copyOf() as Array<Int>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun LongArray.toTypedArray(): Array<Long> {
|
||||
return copyOf() as Array<Long>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ShortArray.toTypedArray(): Array<Short> {
|
||||
return copyOf() as Array<Short>
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Boolean> {
|
||||
val result = arrayOfNulls<Boolean>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Boolean>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ByteArray.toTypedArray(): Array<Byte> {
|
||||
val result = arrayOfNulls<Byte>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Byte>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun CharArray.toTypedArray(): Array<Char> {
|
||||
val result = arrayOfNulls<Char>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Char>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun DoubleArray.toTypedArray(): Array<Double> {
|
||||
val result = arrayOfNulls<Double>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Double>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun FloatArray.toTypedArray(): Array<Float> {
|
||||
val result = arrayOfNulls<Float>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Float>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun IntArray.toTypedArray(): Array<Int> {
|
||||
val result = arrayOfNulls<Int>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Int>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun LongArray.toTypedArray(): Array<Long> {
|
||||
val result = arrayOfNulls<Long>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Long>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ShortArray.toTypedArray(): Array<Short> {
|
||||
val result = arrayOfNulls<Short>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<Short>
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Long> = 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()) }
|
||||
|
||||
@@ -21,6 +21,22 @@ fun specialJS(): List<GenericFunction> {
|
||||
body(ArraysOfPrimitives) {"""return (this as Array<T>).asList()"""}
|
||||
}
|
||||
|
||||
|
||||
templates add f("toTypedArray()") {
|
||||
only(ArraysOfPrimitives)
|
||||
returns("Array<T>")
|
||||
doc {
|
||||
"""
|
||||
Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
"""
|
||||
}
|
||||
body {
|
||||
"""
|
||||
return copyOf() as Array<T>
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("copyOfRange(from: Int, to: Int)") {
|
||||
// TODO: Arguments checking as in java?
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
|
||||
@@ -216,5 +216,23 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("toTypedArray()") {
|
||||
only(ArraysOfPrimitives)
|
||||
returns("Array<T>")
|
||||
doc {
|
||||
"""
|
||||
Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
"""
|
||||
}
|
||||
body {
|
||||
"""
|
||||
val result = arrayOfNulls<T>(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result as Array<T>
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user