Implement conversion from typed array and collection to UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-01-25 15:40:12 +03:00
committed by Ilya Gorbunov
parent bbaabb90e4
commit 299fac8e2d
8 changed files with 155 additions and 38 deletions
@@ -127,6 +127,7 @@ val reducedRuntimeSources = fullRuntimeSources - listOfKtFilesFrom(
"libraries/stdlib/common/src/generated/_Strings.kt",
"libraries/stdlib/common/src/generated/_UArrays.kt",
"libraries/stdlib/common/src/generated/_URanges.kt",
"libraries/stdlib/common/src/generated/_UCollections.kt",
"libraries/stdlib/common/src/kotlin/SequencesH.kt",
"libraries/stdlib/common/src/kotlin/TextH.kt",
"libraries/stdlib/common/src/kotlin/collections/",
@@ -6740,80 +6740,56 @@ public expect fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit
* Returns an array of Boolean containing all of the elements of this generic array.
*/
public fun Array<out Boolean>.toBooleanArray(): BooleanArray {
val result = BooleanArray(size)
for (index in indices)
result[index] = this[index]
return result
return BooleanArray(size) { index -> this[index] }
}
/**
* Returns an array of Byte containing all of the elements of this generic array.
*/
public fun Array<out Byte>.toByteArray(): ByteArray {
val result = ByteArray(size)
for (index in indices)
result[index] = this[index]
return result
return ByteArray(size) { index -> this[index] }
}
/**
* Returns an array of Char containing all of the elements of this generic array.
*/
public fun Array<out Char>.toCharArray(): CharArray {
val result = CharArray(size)
for (index in indices)
result[index] = this[index]
return result
return CharArray(size) { index -> this[index] }
}
/**
* Returns an array of Double containing all of the elements of this generic array.
*/
public fun Array<out Double>.toDoubleArray(): DoubleArray {
val result = DoubleArray(size)
for (index in indices)
result[index] = this[index]
return result
return DoubleArray(size) { index -> this[index] }
}
/**
* Returns an array of Float containing all of the elements of this generic array.
*/
public fun Array<out Float>.toFloatArray(): FloatArray {
val result = FloatArray(size)
for (index in indices)
result[index] = this[index]
return result
return FloatArray(size) { index -> this[index] }
}
/**
* Returns an array of Int containing all of the elements of this generic array.
*/
public fun Array<out Int>.toIntArray(): IntArray {
val result = IntArray(size)
for (index in indices)
result[index] = this[index]
return result
return IntArray(size) { index -> this[index] }
}
/**
* Returns an array of Long containing all of the elements of this generic array.
*/
public fun Array<out Long>.toLongArray(): LongArray {
val result = LongArray(size)
for (index in indices)
result[index] = this[index]
return result
return LongArray(size) { index -> this[index] }
}
/**
* Returns an array of Short containing all of the elements of this generic array.
*/
public fun Array<out Short>.toShortArray(): ShortArray {
val result = ShortArray(size)
for (index in indices)
result[index] = this[index]
return result
return ShortArray(size) { index -> this[index] }
}
/**
@@ -1215,6 +1215,15 @@ public fun UShortArray.toTypedArray(): Array<UShort> {
return Array(size) { index -> this[index] }
}
/**
* Returns an array of UByte containing all of the elements of this generic array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Array<out UByte>.toUByteArray(): UByteArray {
return UByteArray(size) { index -> this[index] }
}
/**
* Returns an array of type [UByteArray], which is a copy of this array where each element is an unsigned reinterpretation
* of the corresponding element of this array.
@@ -1226,6 +1235,15 @@ public inline fun ByteArray.toUByteArray(): UByteArray {
return UByteArray(this.copyOf())
}
/**
* Returns an array of UInt containing all of the elements of this generic array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Array<out UInt>.toUIntArray(): UIntArray {
return UIntArray(size) { index -> this[index] }
}
/**
* Returns an array of type [UIntArray], which is a copy of this array where each element is an unsigned reinterpretation
* of the corresponding element of this array.
@@ -1237,6 +1255,15 @@ public inline fun IntArray.toUIntArray(): UIntArray {
return UIntArray(this.copyOf())
}
/**
* Returns an array of ULong containing all of the elements of this generic array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Array<out ULong>.toULongArray(): ULongArray {
return ULongArray(size) { index -> this[index] }
}
/**
* Returns an array of type [ULongArray], which is a copy of this array where each element is an unsigned reinterpretation
* of the corresponding element of this array.
@@ -1248,6 +1275,15 @@ public inline fun LongArray.toULongArray(): ULongArray {
return ULongArray(this.copyOf())
}
/**
* Returns an array of UShort containing all of the elements of this generic array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Array<out UShort>.toUShortArray(): UShortArray {
return UShortArray(size) { index -> this[index] }
}
/**
* Returns an array of type [UShortArray], which is a copy of this array where each element is an unsigned reinterpretation
* of the corresponding element of this array.
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2019 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.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("UCollectionsKt")
package kotlin.collections
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.random.*
/**
* Returns an array of UByte containing all of the elements of this collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Collection<UByte>.toUByteArray(): UByteArray {
val result = UByteArray(size)
var index = 0
for (element in this)
result[index++] = element
return result
}
/**
* Returns an array of UInt containing all of the elements of this collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Collection<UInt>.toUIntArray(): UIntArray {
val result = UIntArray(size)
var index = 0
for (element in this)
result[index++] = element
return result
}
/**
* Returns an array of ULong containing all of the elements of this collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Collection<ULong>.toULongArray(): ULongArray {
val result = ULongArray(size)
var index = 0
for (element in this)
result[index++] = element
return result
}
/**
* Returns an array of UShort containing all of the elements of this collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Collection<UShort>.toUShortArray(): UShortArray {
val result = UShortArray(size)
var index = 0
for (element in this)
result[index++] = element
return result
}
@@ -334,4 +334,18 @@ class UnsignedArraysTest {
assertEquals(0.0, uintArrayOf(0, 2, 4).sumByDouble { (it % 2u).toInt().toDouble() })
assertEquals(6.0, ulongArrayOf(2, 3, 4).sumByDouble { (it - 1u).toInt().toDouble() })
}
@Test
fun toUnsignedArray() {
val uintList = listOf(1u, 2u, 3u)
val uintArray: UIntArray = uintList.toUIntArray()
expect(3) { uintArray.size }
assertEquals(uintList, uintArray.toList())
val genericArray: Array<ULong> = arrayOf<ULong>(1, 2, 3)
val ulongArray: ULongArray = genericArray.toULongArray()
expect(3) { ulongArray.size }
assertEquals(genericArray.toList(), ulongArray.toList())
}
}
@@ -2327,6 +2327,10 @@ public final class kotlin/collections/UArraysKt {
public static final fun toTypedArray-GBYM_sE ([B)[Lkotlin/UByte;
public static final fun toTypedArray-QwZRm1k ([J)[Lkotlin/ULong;
public static final fun toTypedArray-rL5Bavg ([S)[Lkotlin/UShort;
public static final fun toUByteArray ([Lkotlin/UByte;)[B
public static final fun toUIntArray ([Lkotlin/UInt;)[I
public static final fun toULongArray ([Lkotlin/ULong;)[J
public static final fun toUShortArray ([Lkotlin/UShort;)[S
}
public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker {
@@ -2337,6 +2341,13 @@ public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kot
public fun remove ()V
}
public final class kotlin/collections/UCollectionsKt {
public static final fun toUByteArray (Ljava/util/Collection;)[B
public static final fun toUIntArray (Ljava/util/Collection;)[I
public static final fun toULongArray (Ljava/util/Collection;)[J
public static final fun toUShortArray (Ljava/util/Collection;)[S
}
public abstract class kotlin/collections/UIntIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> ()V
public synthetic fun next ()Ljava/lang/Object;
@@ -322,26 +322,35 @@ object ArrayOps : TemplateGroupBase() {
}
val f_toPrimitiveArray = fn("toPrimitiveArray()") {
include(ArraysOfObjects, PrimitiveType.defaultPrimitives)
include(Collections, PrimitiveType.defaultPrimitives)
include(ArraysOfObjects, PrimitiveType.values().toSet())
include(Collections, PrimitiveType.values().toSet())
} builder {
val primitive = checkNotNull(primitive)
val arrayType = primitive.name + "Array"
signature("to$arrayType()")
returns(arrayType)
if (primitive in PrimitiveType.unsignedPrimitives) {
since("1.3")
annotation("@ExperimentalUnsignedTypes")
}
// TODO: Use different implementations for JS
specialFor(ArraysOfObjects) {
if (primitive in PrimitiveType.unsignedPrimitives) {
sourceFile(SourceFile.UArrays)
}
doc { "Returns an array of ${primitive.name} containing all of the elements of this generic array." }
body {
"""
val result = $arrayType(size)
for (index in indices)
result[index] = this[index]
return result
return $arrayType(size) { index -> this[index] }
"""
}
}
specialFor(Collections) {
if (primitive in PrimitiveType.unsignedPrimitives) {
sourceFile(SourceFile.UCollections)
}
doc { "Returns an array of ${primitive.name} containing all of the elements of this collection." }
body {
"""
@@ -10,6 +10,7 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru
Arrays(packageName = "kotlin.collections"),
UArrays(packageName = "kotlin.collections"),
Collections(packageName = "kotlin.collections"),
UCollections(packageName = "kotlin.collections"),
Sets(packageName = "kotlin.collections"),
Maps(packageName = "kotlin.collections"),
Sequences(packageName = "kotlin.sequences"),