Simplify unsigned array constructor functions

This commit is contained in:
Ilya Gorbunov
2018-06-29 16:27:00 +03:00
parent 615f57f2fc
commit 7a208c3e01
5 changed files with 36 additions and 35 deletions
+8 -7
View File
@@ -260,9 +260,11 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
val storageElementType = type.asSigned.capitalized val storageElementType = type.asSigned.capitalized
val storageArrayType = storageElementType + "Array" val storageArrayType = storageElementType + "Array"
override fun generateBody() { override fun generateBody() {
out.println("@Suppress(\"NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS\")")
out.println("@SinceKotlin(\"1.3\")") out.println("@SinceKotlin(\"1.3\")")
out.println("public inline class $arrayType internal constructor(private val storage: $storageArrayType) : Collection<$elementType> {") out.println("public inline class $arrayType")
out.println("@Suppress(\"NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS\")")
out.println("@PublishedApi")
out.println("internal constructor(private val storage: $storageArrayType) : Collection<$elementType> {")
out.println( out.println(
""" """
/** Returns the array element at the given [index]. This method can be called using the index operator. */ /** Returns the array element at the given [index]. This method can be called using the index operator. */
@@ -294,17 +296,16 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
out.println("}") out.println("}")
// TODO: Make inline constructor, like in ByteArray
out.println() out.println()
out.println("@SinceKotlin(\"1.3\")") out.println("@SinceKotlin(\"1.3\")")
out.println("""public /*inline*/ fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType { out.println("""public inline fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType {
return $arrayType($storageArrayType(size) { index -> init(index).to$storageElementType() }) return $arrayType($storageArrayType(size) { index -> init(index).to$storageElementType() })
} }
@Suppress("FORBIDDEN_VARARG_PARAMETER_TYPE")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun $arrayTypeOf(vararg elements: $elementType): $arrayType { // TODO: @kotlin.internal.InlineOnly
return $arrayType(elements.size) { index -> elements[index] } public inline fun $arrayTypeOf(vararg elements: $elementType): $arrayType = elements"""
}"""
) )
} }
} }
@@ -7,9 +7,11 @@
package kotlin package kotlin
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline class UByteArray internal constructor(private val storage: ByteArray) : Collection<UByte> { public inline class UByteArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
internal constructor(private val storage: ByteArray) : Collection<UByte> {
/** Returns the array element at the given [index]. This method can be called using the index operator. */ /** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UByte = storage[index].toUByte() public operator fun get(index: Int): UByte = storage[index].toUByte()
@@ -39,12 +41,10 @@ public inline class UByteArray internal constructor(private val storage: ByteArr
} }
@SinceKotlin("1.3") @SinceKotlin("1.3")
public /*inline*/ fun UByteArray(size: Int, init: (Int) -> UByte): UByteArray { public inline fun UByteArray(size: Int, init: (Int) -> UByte): UByteArray {
return UByteArray(ByteArray(size) { index -> init(index).toByte() }) return UByteArray(ByteArray(size) { index -> init(index).toByte() })
} }
@Suppress("FORBIDDEN_VARARG_PARAMETER_TYPE")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun ubyteArrayOf(vararg elements: UByte): UByteArray { // TODO: @kotlin.internal.InlineOnly
return UByteArray(elements.size) { index -> elements[index] } public inline fun ubyteArrayOf(vararg elements: UByte): UByteArray = elements
}
@@ -7,9 +7,11 @@
package kotlin package kotlin
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline class UIntArray internal constructor(private val storage: IntArray) : Collection<UInt> { public inline class UIntArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
internal constructor(private val storage: IntArray) : Collection<UInt> {
/** Returns the array element at the given [index]. This method can be called using the index operator. */ /** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UInt = storage[index].toUInt() public operator fun get(index: Int): UInt = storage[index].toUInt()
@@ -39,12 +41,10 @@ public inline class UIntArray internal constructor(private val storage: IntArray
} }
@SinceKotlin("1.3") @SinceKotlin("1.3")
public /*inline*/ fun UIntArray(size: Int, init: (Int) -> UInt): UIntArray { public inline fun UIntArray(size: Int, init: (Int) -> UInt): UIntArray {
return UIntArray(IntArray(size) { index -> init(index).toInt() }) return UIntArray(IntArray(size) { index -> init(index).toInt() })
} }
@Suppress("FORBIDDEN_VARARG_PARAMETER_TYPE")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun uintArrayOf(vararg elements: UInt): UIntArray { // TODO: @kotlin.internal.InlineOnly
return UIntArray(elements.size) { index -> elements[index] } public inline fun uintArrayOf(vararg elements: UInt): UIntArray = elements
}
@@ -7,9 +7,11 @@
package kotlin package kotlin
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline class ULongArray internal constructor(private val storage: LongArray) : Collection<ULong> { public inline class ULongArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
internal constructor(private val storage: LongArray) : Collection<ULong> {
/** Returns the array element at the given [index]. This method can be called using the index operator. */ /** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): ULong = storage[index].toULong() public operator fun get(index: Int): ULong = storage[index].toULong()
@@ -39,12 +41,10 @@ public inline class ULongArray internal constructor(private val storage: LongArr
} }
@SinceKotlin("1.3") @SinceKotlin("1.3")
public /*inline*/ fun ULongArray(size: Int, init: (Int) -> ULong): ULongArray { public inline fun ULongArray(size: Int, init: (Int) -> ULong): ULongArray {
return ULongArray(LongArray(size) { index -> init(index).toLong() }) return ULongArray(LongArray(size) { index -> init(index).toLong() })
} }
@Suppress("FORBIDDEN_VARARG_PARAMETER_TYPE")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun ulongArrayOf(vararg elements: ULong): ULongArray { // TODO: @kotlin.internal.InlineOnly
return ULongArray(elements.size) { index -> elements[index] } public inline fun ulongArrayOf(vararg elements: ULong): ULongArray = elements
}
@@ -7,9 +7,11 @@
package kotlin package kotlin
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline class UShortArray internal constructor(private val storage: ShortArray) : Collection<UShort> { public inline class UShortArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
internal constructor(private val storage: ShortArray) : Collection<UShort> {
/** Returns the array element at the given [index]. This method can be called using the index operator. */ /** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UShort = storage[index].toUShort() public operator fun get(index: Int): UShort = storage[index].toUShort()
@@ -39,12 +41,10 @@ public inline class UShortArray internal constructor(private val storage: ShortA
} }
@SinceKotlin("1.3") @SinceKotlin("1.3")
public /*inline*/ fun UShortArray(size: Int, init: (Int) -> UShort): UShortArray { public inline fun UShortArray(size: Int, init: (Int) -> UShort): UShortArray {
return UShortArray(ShortArray(size) { index -> init(index).toShort() }) return UShortArray(ShortArray(size) { index -> init(index).toShort() })
} }
@Suppress("FORBIDDEN_VARARG_PARAMETER_TYPE")
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun ushortArrayOf(vararg elements: UShort): UShortArray { // TODO: @kotlin.internal.InlineOnly
return UShortArray(elements.size) { index -> elements[index] } public inline fun ushortArrayOf(vararg elements: UShort): UShortArray = elements
}