STDLIB: support for vararg of primitive types

- *Array.copyRangeTo for Byte,Short,Char,Long,Float,Double
- bunch of *ArrayOf(vararg elements:*) = elements and inlined
- small inhancement in  c++ part of Kotlin_*Array_copyImpl(...) refactored
This commit is contained in:
Vasily Levchenko
2017-02-10 16:02:51 +03:00
committed by vvlevchenko
parent be75e1bce9
commit c8ed56b24a
3 changed files with 102 additions and 82 deletions
+47 -9
View File
@@ -7,6 +7,21 @@
#include "Natives.h" #include "Natives.h"
#include "Types.h" #include "Types.h"
template<typename T>
static inline void copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
const ArrayHeader* array = thiz->array();
ArrayHeader* destinationArray = destination->array();
if (fromIndex < 0 || fromIndex + count > array->count_ ||
toIndex < 0 || toIndex + count > destinationArray->count_) {
ThrowArrayIndexOutOfBoundsException();
}
memmove(PrimitiveArrayAddressOfElementAt<T>(destinationArray, toIndex),
PrimitiveArrayAddressOfElementAt<T>(array, fromIndex),
count * sizeof(T));
}
extern "C" { extern "C" {
// TODO: those must be compiler intrinsics afterwards. // TODO: those must be compiler intrinsics afterwards.
@@ -226,19 +241,42 @@ void Kotlin_IntArray_fillImpl(KRef thiz, KInt fromIndex, KInt toIndex, KInt valu
} }
} }
void Kotlin_ByteArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KByte>(thiz, fromIndex, destination, toIndex, count);
}
void Kotlin_ShortArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KShort>(thiz, fromIndex, destination, toIndex, count);
}
void Kotlin_CharArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KChar>(thiz, fromIndex, destination, toIndex, count);
}
void Kotlin_IntArray_copyImpl(KConstRef thiz, KInt fromIndex, void Kotlin_IntArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) { KRef destination, KInt toIndex, KInt count) {
const ArrayHeader* array = thiz->array(); copyImpl<KInt>(thiz, fromIndex, destination, toIndex, count);
ArrayHeader* destinationArray = destination->array();
if (fromIndex < 0 || fromIndex + count > array->count_ ||
toIndex < 0 || toIndex + count > destinationArray->count_) {
ThrowArrayIndexOutOfBoundsException();
}
memmove(PrimitiveArrayAddressOfElementAt<KInt>(destinationArray, toIndex),
PrimitiveArrayAddressOfElementAt<KInt>(array, fromIndex),
count * sizeof(KInt));
} }
void Kotlin_LongArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KLong>(thiz, fromIndex, destination, toIndex, count);
}
void Kotlin_FloatArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KFloat>(thiz, fromIndex, destination, toIndex, count);
}
void Kotlin_DoubleArray_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) {
copyImpl<KDouble>(thiz, fromIndex, destination, toIndex, count);
}
KLong Kotlin_LongArray_get(KConstRef thiz, KInt index) { KLong Kotlin_LongArray_get(KConstRef thiz, KInt index) {
const ArrayHeader* array = thiz->array(); const ArrayHeader* array = thiz->array();
if (static_cast<uint32_t>(index) >= array->count_) { if (static_cast<uint32_t>(index) >= array->count_) {
+8 -73
View File
@@ -898,108 +898,43 @@ public inline fun <reified @PureReifiable T> arrayOfNulls(size: Int): Array<T?>
*/ */
public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T): Array<T> = elements as Array<T> public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T): Array<T> = elements as Array<T>
// TODO: optimize those operations.
/** /**
* Returns an array containing the specified [Double] numbers. * Returns an array containing the specified [Double] numbers.
*/ */
public fun doubleArrayOf(vararg elements: Double): DoubleArray { public inline fun doubleArrayOf(vararg elements: Double) = elements
val result = DoubleArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified [Float] numbers. * Returns an array containing the specified [Float] numbers.
*/ */
public fun floatArrayOf(vararg elements: Float): FloatArray { public inline fun floatArrayOf(vararg elements: Float) = elements
val result = FloatArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified [Long] numbers. * Returns an array containing the specified [Long] numbers.
*/ */
public fun longArrayOf(vararg elements: Long): LongArray { public inline fun longArrayOf(vararg elements: Long) = elements
val result = LongArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified [Int] numbers. * Returns an array containing the specified [Int] numbers.
*/ */
public fun intArrayOf(vararg elements: Int): IntArray { public inline fun intArrayOf(vararg elements: Int) = elements
val result = IntArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified characters. * Returns an array containing the specified characters.
*/ */
public fun charArrayOf(vararg elements: Char): CharArray { public inline fun charArrayOf(vararg elements: Char) = elements
val result = CharArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified [Short] numbers. * Returns an array containing the specified [Short] numbers.
*/ */
public fun shortArrayOf(vararg elements: Short): ShortArray { public inline fun shortArrayOf(vararg elements: Short) = elements
val result = ShortArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified [Byte] numbers. * Returns an array containing the specified [Byte] numbers.
*/ */
public fun byteArrayOf(vararg elements: Byte): ByteArray { public inline fun byteArrayOf(vararg elements: Byte) = elements
val result = ByteArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
/** /**
* Returns an array containing the specified boolean values. * Returns an array containing the specified boolean values.
*/ */
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray { public inline fun booleanArrayOf(vararg elements: Boolean) = elements
val result = BooleanArray(elements.size)
var index = 0
while (index < elements.size) {
result[index] = elements[index]
index++
}
return result
}
@@ -62,9 +62,33 @@ internal fun IntArray.fill(fromIndex: Int, toIndex: Int, value: Int) {
external private fun copyImpl(array: Array<Any>, fromIndex: Int, external private fun copyImpl(array: Array<Any>, fromIndex: Int,
destination: Array<Any>, toIndex: Int, count: Int) destination: Array<Any>, toIndex: Int, count: Int)
@SymbolName("Kotlin_ByteArray_copyImpl")
external private fun copyImpl(array: ByteArray, fromIndex: Int,
destination: ByteArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_ShortArray_copyImpl")
external private fun copyImpl(array: ShortArray, fromIndex: Int,
destination: ShortArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_CharArray_copyImpl")
external private fun copyImpl(array: CharArray, fromIndex: Int,
destination: CharArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_IntArray_copyImpl") @SymbolName("Kotlin_IntArray_copyImpl")
external private fun copyImpl(array: IntArray, fromIndex: Int, external private fun copyImpl(array: IntArray, fromIndex: Int,
destination: IntArray, toIndex: Int, count: Int) destination: IntArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_LongArray_copyImpl")
external private fun copyImpl(array: LongArray, fromIndex: Int,
destination: LongArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_FloatArray_copyImpl")
external private fun copyImpl(array: FloatArray, fromIndex: Int,
destination: FloatArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_DoubleArray_copyImpl")
external private fun copyImpl(array: DoubleArray, fromIndex: Int,
destination: DoubleArray, toIndex: Int, count: Int)
/** /**
* Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices * Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices
@@ -76,10 +100,33 @@ fun <E> Array<E>.copyRangeTo(destination: Array<E>, fromIndex: Int, toIndex: Int
destinationIndex, toIndex - fromIndex) destinationIndex, toIndex - fromIndex)
} }
fun ByteArray.copyRangeTo(destination: ByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
fun ShortArray.copyRangeTo(destination: ShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
fun CharArray.copyRangeTo(destination: CharArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
} }
fun LongArray.copyRangeTo(destination: LongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
fun FloatArray.copyRangeTo(destination: FloatArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
fun DoubleArray.copyRangeTo(destination: DoubleArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
/** /**
* Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices * Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices
* to another part of this array starting at [destinationIndex]. * to another part of this array starting at [destinationIndex].