copyInto: copying elements between two arrays

#KT-25874 Fixed
This commit is contained in:
Ilya Gorbunov
2018-08-21 08:56:17 +03:00
parent 58e6f910bc
commit ea37a65178
8 changed files with 900 additions and 0 deletions
@@ -998,6 +998,24 @@ public final class kotlin/collections/ArraysKt {
public static final fun contains ([Ljava/lang/Object;Ljava/lang/Object;)Z
public static final fun contains ([SS)Z
public static final fun contains ([ZZ)Z
public static final fun copyInto ([B[BIII)[B
public static final fun copyInto ([C[CIII)[C
public static final fun copyInto ([D[DIII)[D
public static final fun copyInto ([F[FIII)[F
public static final fun copyInto ([I[IIII)[I
public static final fun copyInto ([J[JIII)[J
public static final fun copyInto ([Ljava/lang/Object;[Ljava/lang/Object;III)[Ljava/lang/Object;
public static final fun copyInto ([S[SIII)[S
public static final fun copyInto ([Z[ZIII)[Z
public static synthetic fun copyInto$default ([B[BIIIILjava/lang/Object;)[B
public static synthetic fun copyInto$default ([C[CIIIILjava/lang/Object;)[C
public static synthetic fun copyInto$default ([D[DIIIILjava/lang/Object;)[D
public static synthetic fun copyInto$default ([F[FIIIILjava/lang/Object;)[F
public static synthetic fun copyInto$default ([I[IIIIILjava/lang/Object;)[I
public static synthetic fun copyInto$default ([J[JIIIILjava/lang/Object;)[J
public static synthetic fun copyInto$default ([Ljava/lang/Object;[Ljava/lang/Object;IIIILjava/lang/Object;)[Ljava/lang/Object;
public static synthetic fun copyInto$default ([S[SIIIILjava/lang/Object;)[S
public static synthetic fun copyInto$default ([Z[ZIIIILjava/lang/Object;)[Z
public static final fun copyOfRange ([BII)[B
public static final fun copyOfRange ([CII)[C
public static final fun copyOfRange ([DII)[D
@@ -465,6 +465,69 @@ object ArrayOps : TemplateGroupBase() {
}
}
// TODO: Remove -1 from common signature
val f_copyInto = fn("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1)") {
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
} builder {
since("1.3")
returns("SELF")
doc {
"""
Copies this array or its subrange into the [destination] array and returns that array.
It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range.
@param destination the array to copy to.
@param destinationOffset the position in the [destination] array to copy to, 0 by default.
@param startIndex the beginning (inclusive) of the subrange to copy, 0 by default.
@param endIndex the end (exclusive) of the subrange to copy, size of this array by default.
@throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`.
@throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex],
or when that index is out of the [destination] array indices range.
@return the [destination] array.
"""
}
specialFor(ArraysOfUnsigned) {
inlineOnly()
body {
"return SELF(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex))"
}
}
specialFor(ArraysOfPrimitives, InvariantArraysOfObjects) {
specialFor(InvariantArraysOfObjects) {
receiver("Array<out T>")
}
on(Platform.JVM) {
suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
signature("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)")
body {
"""
@Suppress("NAME_SHADOWING")
val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed
System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
return destination
"""
}
}
on(Platform.JS) {
suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
signature("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)")
inlineOnly()
body {
val cast = ".unsafeCast<Array<$primitive>>()".takeIf { family == ArraysOfPrimitives } ?: ""
"""
arrayCopy(this$cast, destination$cast, destinationOffset, startIndex, endIndex)
return destination
"""
}
}
}
}
val f_copyOfRangeJvmImpl = fn("copyOfRangeImpl(fromIndex: Int, toIndex: Int)") {
include(InvariantArraysOfObjects, ArraysOfPrimitives)
platforms(Platform.JVM)