From 01401cf7997cd45ecb33679aa907894b85c0eff8 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 10 Apr 2017 16:06:15 +0700 Subject: [PATCH] stdlib: Add array constructors with initializers --- runtime/src/main/kotlin/kotlin/Arrays.kt | 80 ++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 92f8450ef92..f857c8dfac1 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -31,6 +31,16 @@ public final class ByteArray { // Constructors are handled with compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Byte): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -71,6 +81,16 @@ public final class CharArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Char): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -113,6 +133,16 @@ public final class ShortArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Short): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -152,6 +182,16 @@ public final class IntArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Int): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -191,6 +231,16 @@ public final class LongArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Long): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -230,6 +280,16 @@ public final class FloatArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Float): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -265,6 +325,16 @@ public final class DoubleArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Double): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength() @@ -300,6 +370,16 @@ public final class BooleanArray { // Constructors are handled with the compiler magic. public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {} + /** + * Creates a new array of the specified [size], where each element is calculated by calling the specified + * [init] function. The [init] function returns an array element given its index. + */ + public inline constructor(size: Int, init: (Int) -> Boolean): this(size) { + for (i in 0..size) { + this[i] = init(i) + } + } + public val size: Int get() = getArrayLength()