diff --git a/generators/builtins/unsignedTypes.kt b/generators/builtins/unsignedTypes.kt index 8a347d86925..b5cbbd57009 100644 --- a/generators/builtins/unsignedTypes.kt +++ b/generators/builtins/unsignedTypes.kt @@ -19,6 +19,7 @@ fun generateUnsignedTypes( ) { for (type in UnsignedType.values()) { generate(File(targetDir, "kotlin/${type.capitalized}.kt")) { UnsignedTypeGenerator(type, it) } + generate(File(targetDir, "kotlin/${type.capitalized}Array.kt")) { UnsignedArrayGenerator(type, it) } } generate(File(targetDir, "kotlin/UIterators.kt"), ::UnsignedIteratorsGenerator) @@ -193,3 +194,54 @@ class UnsignedIteratorsGenerator(out: PrintWriter) : BuiltInsSourceGenerator(out } } } + +class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltInsSourceGenerator(out) { + val elementType = type.capitalized + val arrayType = elementType + "Array" + val arrayTypeOf = elementType.toLowerCase() + "ArrayOf" + val storageElementType = type.asSigned.capitalized + val storageArrayType = storageElementType + "Array" + override fun generateBody() { + out.println("public inline class $arrayType internal constructor(private val storage: $storageArrayType) : Collection<$elementType> {") + out.println( + """ + /** Returns the array element at the given [index]. This method can be called using the index operator. */ + public operator fun get(index: Int): $elementType = storage[index].to$elementType() + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + public operator fun set(index: Int, value: $elementType) { + storage[index] = value.to$storageElementType() + } + + /** Returns the number of elements in the array. */ + public override val size: Int get() = storage.size + + /** Creates an iterator over the elements of the array. */ + public override operator fun iterator(): ${elementType}Iterator = Iterator(storage) + + private class Iterator(private val array: $storageArrayType) : ${elementType}Iterator() { + private var index = 0 + override fun hasNext() = index < array.size + override fun next$elementType() = if (index < array.size) array[index++].to$elementType() else throw NoSuchElementException(index.toString()) + } + + override fun contains(element: $elementType): Boolean = storage.contains(element.to$storageElementType()) + + override fun containsAll(elements: Collection<$elementType>): Boolean = elements.all { storage.contains(it.to$storageElementType()) } + + override fun isEmpty(): Boolean = this.storage.size == 0""" + ) + + out.println("}") + + out.println(""" +public /*inline*/ fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType { + return $arrayType($storageArrayType(size) { index -> init(index).to$storageElementType() }) +} + +public fun $arrayTypeOf(vararg elements: $elementType): $arrayType { + return $arrayType(elements.size) { index -> elements[index] } +}""" + ) + } +} diff --git a/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt b/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt new file mode 100644 index 00000000000..f5b44712b52 --- /dev/null +++ b/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +// Auto-generated file. DO NOT EDIT! + +package kotlin + +public inline class UByteArray internal constructor(private val storage: ByteArray) : Collection { + + /** 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() + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + public operator fun set(index: Int, value: UByte) { + storage[index] = value.toByte() + } + + /** Returns the number of elements in the array. */ + public override val size: Int get() = storage.size + + /** Creates an iterator over the elements of the array. */ + public override operator fun iterator(): UByteIterator = Iterator(storage) + + private class Iterator(private val array: ByteArray) : UByteIterator() { + private var index = 0 + override fun hasNext() = index < array.size + override fun nextUByte() = if (index < array.size) array[index++].toUByte() else throw NoSuchElementException(index.toString()) + } + + override fun contains(element: UByte): Boolean = storage.contains(element.toByte()) + + override fun containsAll(elements: Collection): Boolean = elements.all { storage.contains(it.toByte()) } + + override fun isEmpty(): Boolean = this.storage.size == 0 +} + +public /*inline*/ fun UByteArray(size: Int, init: (Int) -> UByte): UByteArray { + return UByteArray(ByteArray(size) { index -> init(index).toByte() }) +} + +public fun ubyteArrayOf(vararg elements: UByte): UByteArray { + return UByteArray(elements.size) { index -> elements[index] } +} diff --git a/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt b/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt new file mode 100644 index 00000000000..b6afcd3d4be --- /dev/null +++ b/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +// Auto-generated file. DO NOT EDIT! + +package kotlin + +public inline class UIntArray internal constructor(private val storage: IntArray) : Collection { + + /** 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() + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + public operator fun set(index: Int, value: UInt) { + storage[index] = value.toInt() + } + + /** Returns the number of elements in the array. */ + public override val size: Int get() = storage.size + + /** Creates an iterator over the elements of the array. */ + public override operator fun iterator(): UIntIterator = Iterator(storage) + + private class Iterator(private val array: IntArray) : UIntIterator() { + private var index = 0 + override fun hasNext() = index < array.size + override fun nextUInt() = if (index < array.size) array[index++].toUInt() else throw NoSuchElementException(index.toString()) + } + + override fun contains(element: UInt): Boolean = storage.contains(element.toInt()) + + override fun containsAll(elements: Collection): Boolean = elements.all { storage.contains(it.toInt()) } + + override fun isEmpty(): Boolean = this.storage.size == 0 +} + +public /*inline*/ fun UIntArray(size: Int, init: (Int) -> UInt): UIntArray { + return UIntArray(IntArray(size) { index -> init(index).toInt() }) +} + +public fun uintArrayOf(vararg elements: UInt): UIntArray { + return UIntArray(elements.size) { index -> elements[index] } +} diff --git a/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt b/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt new file mode 100644 index 00000000000..6dadead7316 --- /dev/null +++ b/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +// Auto-generated file. DO NOT EDIT! + +package kotlin + +public inline class ULongArray internal constructor(private val storage: LongArray) : Collection { + + /** 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() + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + public operator fun set(index: Int, value: ULong) { + storage[index] = value.toLong() + } + + /** Returns the number of elements in the array. */ + public override val size: Int get() = storage.size + + /** Creates an iterator over the elements of the array. */ + public override operator fun iterator(): ULongIterator = Iterator(storage) + + private class Iterator(private val array: LongArray) : ULongIterator() { + private var index = 0 + override fun hasNext() = index < array.size + override fun nextULong() = if (index < array.size) array[index++].toULong() else throw NoSuchElementException(index.toString()) + } + + override fun contains(element: ULong): Boolean = storage.contains(element.toLong()) + + override fun containsAll(elements: Collection): Boolean = elements.all { storage.contains(it.toLong()) } + + override fun isEmpty(): Boolean = this.storage.size == 0 +} + +public /*inline*/ fun ULongArray(size: Int, init: (Int) -> ULong): ULongArray { + return ULongArray(LongArray(size) { index -> init(index).toLong() }) +} + +public fun ulongArrayOf(vararg elements: ULong): ULongArray { + return ULongArray(elements.size) { index -> elements[index] } +} diff --git a/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt b/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt new file mode 100644 index 00000000000..0dd3a113a37 --- /dev/null +++ b/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +// Auto-generated file. DO NOT EDIT! + +package kotlin + +public inline class UShortArray internal constructor(private val storage: ShortArray) : Collection { + + /** 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() + + /** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */ + public operator fun set(index: Int, value: UShort) { + storage[index] = value.toShort() + } + + /** Returns the number of elements in the array. */ + public override val size: Int get() = storage.size + + /** Creates an iterator over the elements of the array. */ + public override operator fun iterator(): UShortIterator = Iterator(storage) + + private class Iterator(private val array: ShortArray) : UShortIterator() { + private var index = 0 + override fun hasNext() = index < array.size + override fun nextUShort() = if (index < array.size) array[index++].toUShort() else throw NoSuchElementException(index.toString()) + } + + override fun contains(element: UShort): Boolean = storage.contains(element.toShort()) + + override fun containsAll(elements: Collection): Boolean = elements.all { storage.contains(it.toShort()) } + + override fun isEmpty(): Boolean = this.storage.size == 0 +} + +public /*inline*/ fun UShortArray(size: Int, init: (Int) -> UShort): UShortArray { + return UShortArray(ShortArray(size) { index -> init(index).toShort() }) +} + +public fun ushortArrayOf(vararg elements: UShort): UShortArray { + return UShortArray(elements.size) { index -> elements[index] } +}