Make primitive array factory functions constructors

This commit is contained in:
Alexander Udalov
2016-01-20 23:14:06 +03:00
parent 4b03adaa57
commit 9e8b6571f4
7 changed files with 203 additions and 223 deletions
+48
View File
@@ -23,6 +23,12 @@ package kotlin
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class ByteArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Byte)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Byte
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -42,6 +48,12 @@ public class ByteArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class CharArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Char)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Char
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -61,6 +73,12 @@ public class CharArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class ShortArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Short)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Short
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -80,6 +98,12 @@ public class ShortArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class IntArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Int)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Int
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -99,6 +123,12 @@ public class IntArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class LongArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Long)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Long
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -118,6 +148,12 @@ public class LongArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class FloatArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Float)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Float
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -137,6 +173,12 @@ public class FloatArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
*/
public class DoubleArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Double)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Double
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -156,6 +198,12 @@ public class DoubleArray(size: Int) : Cloneable {
* @constructor Creates a new array of the specified [size], with all elements initialized to false.
*/
public class BooleanArray(size: Int) : Cloneable {
/**
* 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 constructor(size: Int, init: (Int) -> Boolean)
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): Boolean
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
@@ -29,102 +29,6 @@ public inline fun <reified @PureReifiable T> Array(size: Int, init: (Int) -> T):
return result as Array<T>
}
/**
* Returns an array of [Double] numbers with 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 fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = DoubleArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Float] numbers with 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 fun FloatArray(size: Int, init: (Int) -> Float): FloatArray {
val result = FloatArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Long] numbers with 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 fun LongArray(size: Int, init: (Int) -> Long): LongArray {
val result = LongArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Int] numbers with 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 fun IntArray(size: Int, init: (Int) -> Int): IntArray {
val result = IntArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Char] numbers with 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 fun CharArray(size: Int, init: (Int) -> Char): CharArray {
val result = CharArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Short] numbers with 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 fun ShortArray(size: Int, init: (Int) -> Short): ShortArray {
val result = ShortArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Byte] numbers with 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 fun ByteArray(size: Int, init: (Int) -> Byte): ByteArray {
val result = ByteArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Boolean] numbers with 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 fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
val result = BooleanArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an empty array of the specified type [T].
*/
@@ -0,0 +1,84 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jvm.internal
@Suppress("unused")
object IntrinsicArrayConstructors {
private inline fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = DoubleArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun FloatArray(size: Int, init: (Int) -> Float): FloatArray {
val result = FloatArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun LongArray(size: Int, init: (Int) -> Long): LongArray {
val result = LongArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun IntArray(size: Int, init: (Int) -> Int): IntArray {
val result = IntArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun CharArray(size: Int, init: (Int) -> Char): CharArray {
val result = CharArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun ShortArray(size: Int, init: (Int) -> Short): ShortArray {
val result = ShortArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun ByteArray(size: Int, init: (Int) -> Byte): ByteArray {
val result = ByteArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
private inline fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
val result = BooleanArray(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result
}
}