diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt new file mode 100644 index 00000000000..44b9403da98 --- /dev/null +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -0,0 +1,507 @@ +/* + * 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. + */ + +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("UArraysKt") + +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.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns an array of type [ByteArray], which is a view of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.asByteArray(): ByteArray { + return storage +} + +/** + * Returns an array of type [IntArray], which is a view of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.asIntArray(): IntArray { + return storage +} + +/** + * Returns an array of type [LongArray], which is a view of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.asLongArray(): LongArray { + return storage +} + +/** + * Returns an array of type [ShortArray], which is a view of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.asShortArray(): ShortArray { + return storage +} + +/** + * Returns an array of type [UByteArray], which is a view of this array where each element is an unsigned reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ByteArray.asUByteArray(): UByteArray { + return UByteArray(this) +} + +/** + * Returns an array of type [UIntArray], which is a view of this array where each element is an unsigned reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun IntArray.asUIntArray(): UIntArray { + return UIntArray(this) +} + +/** + * Returns an array of type [ULongArray], which is a view of this array where each element is an unsigned reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun LongArray.asULongArray(): ULongArray { + return ULongArray(this) +} + +/** + * Returns an array of type [UShortArray], which is a view of this array where each element is an unsigned reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ShortArray.asUShortArray(): UShortArray { + return UShortArray(this) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UIntArray.contentEquals(other: UIntArray): Boolean { + return storage.contentEquals(other.storage) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun ULongArray.contentEquals(other: ULongArray): Boolean { + return storage.contentEquals(other.storage) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UByteArray.contentEquals(other: UByteArray): Boolean { + return storage.contentEquals(other.storage) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UShortArray.contentEquals(other: UShortArray): Boolean { + return storage.contentEquals(other.storage) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.contentHashCode(): Int { + return storage.contentHashCode() +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.contentHashCode(): Int { + return storage.contentHashCode() +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.contentHashCode(): Int { + return storage.contentHashCode() +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.contentHashCode(): Int { + return storage.contentHashCode() +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.contentToString(): String { + return joinToString(", ", "[", "]") +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.contentToString(): String { + return joinToString(", ", "[", "]") +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.contentToString(): String { + return joinToString(", ", "[", "]") +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.contentToString(): String { + return joinToString(", ", "[", "]") +} + +/** + * Returns new array which is a copy of the original array. + * + * @sample samples.collections.Arrays.CopyOfOperations.copyOf + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.copyOf(): UIntArray { + return UIntArray(storage.copyOf()) +} + +/** + * Returns new array which is a copy of the original array. + * + * @sample samples.collections.Arrays.CopyOfOperations.copyOf + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.copyOf(): ULongArray { + return ULongArray(storage.copyOf()) +} + +/** + * Returns new array which is a copy of the original array. + * + * @sample samples.collections.Arrays.CopyOfOperations.copyOf + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.copyOf(): UByteArray { + return UByteArray(storage.copyOf()) +} + +/** + * Returns new array which is a copy of the original array. + * + * @sample samples.collections.Arrays.CopyOfOperations.copyOf + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.copyOf(): UShortArray { + return UShortArray(storage.copyOf()) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + * The copy is either truncated or padded at the end with zero values if necessary. + * + * - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize]. + * - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.copyOf(newSize: Int): UIntArray { + return UIntArray(storage.copyOf(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + * The copy is either truncated or padded at the end with zero values if necessary. + * + * - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize]. + * - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.copyOf(newSize: Int): ULongArray { + return ULongArray(storage.copyOf(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + * The copy is either truncated or padded at the end with zero values if necessary. + * + * - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize]. + * - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.copyOf(newSize: Int): UByteArray { + return UByteArray(storage.copyOf(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + * The copy is either truncated or padded at the end with zero values if necessary. + * + * - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize]. + * - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.copyOf(newSize: Int): UShortArray { + return UShortArray(storage.copyOf(newSize)) +} + +/** + * Returns a new array which is a copy of the specified range of the original array. + * + * @param fromIndex the start of the range (inclusive), must be in `0..array.size` + * @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size` + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.copyOfRange(fromIndex: Int, toIndex: Int): UIntArray { + return UIntArray(storage.copyOfRange(fromIndex, toIndex)) +} + +/** + * Returns a new array which is a copy of the specified range of the original array. + * + * @param fromIndex the start of the range (inclusive), must be in `0..array.size` + * @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size` + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.copyOfRange(fromIndex: Int, toIndex: Int): ULongArray { + return ULongArray(storage.copyOfRange(fromIndex, toIndex)) +} + +/** + * Returns a new array which is a copy of the specified range of the original array. + * + * @param fromIndex the start of the range (inclusive), must be in `0..array.size` + * @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size` + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.copyOfRange(fromIndex: Int, toIndex: Int): UByteArray { + return UByteArray(storage.copyOfRange(fromIndex, toIndex)) +} + +/** + * Returns a new array which is a copy of the specified range of the original array. + * + * @param fromIndex the start of the range (inclusive), must be in `0..array.size` + * @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size` + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.copyOfRange(fromIndex: Int, toIndex: Int): UShortArray { + return UShortArray(storage.copyOfRange(fromIndex, toIndex)) +} + +/** + * Returns an array of type [ByteArray], which is a copy of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.toByteArray(): ByteArray { + return storage.copyOf() +} + +/** + * Returns an array of type [IntArray], which is a copy of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.toIntArray(): IntArray { + return storage.copyOf() +} + +/** + * Returns an array of type [LongArray], which is a copy of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.toLongArray(): LongArray { + return storage.copyOf() +} + +/** + * Returns an array of type [ShortArray], which is a copy of this array where each element is a signed reinterpretation + * of the corresponding element of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.toShortArray(): ShortArray { + return storage.copyOf() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.toTypedArray(): Array { + return Array(size) { index -> this[index] } +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.toTypedArray(): Array { + return Array(size) { index -> this[index] } +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.toTypedArray(): Array { + return Array(size) { index -> this[index] } +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.toTypedArray(): Array { + return Array(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. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ByteArray.toUByteArray(): UByteArray { + return UByteArray(this.copyOf()) +} + +/** + * 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. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun IntArray.toUIntArray(): UIntArray { + return UIntArray(this.copyOf()) +} + +/** + * 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. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun LongArray.toULongArray(): ULongArray { + return ULongArray(this.copyOf()) +} + +/** + * 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. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ShortArray.toUShortArray(): UShortArray { + return UShortArray(this.copyOf()) +} + diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt index 22370d52ca0..2d257eb36e0 100644 --- a/libraries/stdlib/js/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -1126,6 +1126,6 @@ public actual fun BooleanArray.toTypedArray(): Array { * Returns a *typed* object array containing all of the elements of this primitive array. */ public actual fun CharArray.toTypedArray(): Array { - return Array(size, { i -> this[i] }) + return Array(size) { index -> this[index] } } diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index 9019d4eba6d..4de0dd11a4e 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -64,4 +64,158 @@ class UnsignedArraysTest { assertEquals(index.toULong(), initArray[index]) } } + + + @Test + fun contentHashCode() { + ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) } + uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) } + ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) } + ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) } + } + + @Test + fun contentToString() { + ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) } + uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) } + ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) } + ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) } + } + + @Test + fun contentEquals() { + uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { arr -> + assertTrue(arr contentEquals UIntArray(arr.size, arr::get)) + assertFalse(arr contentEquals UIntArray(arr.size - 1, arr::get)) + } + ulongArrayOf(1u, ULong.MAX_VALUE, ULong.MIN_VALUE).let { arr -> + assertTrue(arr contentEquals ULongArray(arr.size, arr::get)) + assertFalse(arr contentEquals ULongArray(arr.size - 1, arr::get)) + } + ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { arr -> + assertTrue(arr contentEquals UShortArray(arr.size, arr::get)) + assertFalse(arr contentEquals UShortArray(arr.size - 1, arr::get)) + } + ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { arr -> + assertTrue(arr contentEquals UByteArray(arr.size, arr::get)) + assertFalse(arr contentEquals UByteArray(arr.size - 1, arr::get)) + } + } + + @Test + fun asArray() { + val uintArray = uintArrayOf(1, UInt.MAX_VALUE) + val intArray = uintArray.asIntArray() + assertTrue(intArray contentEquals intArrayOf(1, -1)) + + intArray.reverse() + val uintArray2 = intArray.asUIntArray() + + assertTrue(uintArray contentEquals uintArray2) + assertTrue(uintArray contentEquals uintArrayOf(UInt.MAX_VALUE, 1)) + + + val ulongArray = ulongArrayOf(1, ULong.MAX_VALUE) + val longArray = ulongArray.asLongArray() + assertTrue(longArray contentEquals longArrayOf(1, -1)) + + longArray.reverse() + val ulongArray2 = longArray.asULongArray() + assertTrue(ulongArray contentEquals ulongArray2) + assertTrue(ulongArray contentEquals ulongArrayOf(ULong.MAX_VALUE, 1)) + } + + + @Test + fun toArray() { + val uintArray = uintArrayOf(UInt.MAX_VALUE) + val intArray = uintArray.toIntArray() + assertTrue(intArray contentEquals intArrayOf(-1)) + + intArray[0] = 0 + val uintArray2 = intArray.toUIntArray() + assertEquals(UInt.MAX_VALUE, uintArray[0]) + + intArray[0] = 1 + assertEquals(0u, uintArray2[0]) + + + val ulongArray = ulongArrayOf(ULong.MAX_VALUE) + val longArray = ulongArray.toLongArray() + assertTrue(longArray contentEquals longArrayOf(-1)) + + longArray[0] = 0 + val ulongArray2 = longArray.toULongArray() + assertEquals(ULong.MAX_VALUE, ulongArray[0]) + + longArray[0] = 1 + assertEquals(0u, ulongArray2[0]) + } + + @Test + fun toTypedArray() { + uintArrayOf(UInt.MIN_VALUE, UInt.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) } + ulongArrayOf(ULong.MIN_VALUE, ULong.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) } + ushortArrayOf(UShort.MIN_VALUE, UShort.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) } + ubyteArrayOf(UByte.MIN_VALUE, UByte.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) } + } + + @Test + fun copyOf() { + uintArrayOf(UInt.MAX_VALUE).let { arr -> + val copy = arr.copyOf() + assertTrue(arr contentEquals copy) + copy[0] = 1u + assertFalse(arr contentEquals copy) + arr[0] = 2u + assertFalse(arr contentEquals copy) + } + ulongArrayOf(ULong.MAX_VALUE).let { arr -> + val copy = arr.copyOf() + assertTrue(arr contentEquals copy) + copy[0] = 1u + assertFalse(arr contentEquals copy) + arr[0] = 2u + assertFalse(arr contentEquals copy) + } + } + + @Test + fun copyAndResize() { + assertTrue(uintArrayOf(1, 2) contentEquals uintArrayOf(1, 2, 3).copyOf(2)) + assertTrue(uintArrayOf(1, 2, 0) contentEquals uintArrayOf(1, 2).copyOf(3)) + + assertTrue(ulongArrayOf(1, 2) contentEquals ulongArrayOf(1, 2, 3).copyOf(2)) + assertTrue(ulongArrayOf(1, 2, 0) contentEquals ulongArrayOf(1, 2).copyOf(3)) + + + assertTrue(uintArrayOf() contentEquals uintArrayOf(1).copyOf(0)) + assertTrue(ulongArrayOf() contentEquals ulongArrayOf(1).copyOf(0)) + + // RuntimeException is the most specific common of JVM and JS implementations + assertFailsWith { uintArrayOf().copyOf(-1) } + assertFailsWith { ulongArrayOf().copyOf(-1) } + } + + + @Test + fun copyOfRange() { + assertTrue(ubyteArrayOf(0, 1, 2) contentEquals ubyteArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3)) + assertTrue(ushortArrayOf(0, 1, 2) contentEquals ushortArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3)) + assertTrue(uintArrayOf(0, 1, 2) contentEquals uintArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3)) + assertTrue(ulongArrayOf(0, 1, 2) contentEquals ulongArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3)) + + for (pos in 0..3) { + assertTrue(uintArrayOf() contentEquals uintArrayOf(1, 2, 3).copyOfRange(pos, pos)) + assertTrue(ulongArrayOf() contentEquals ULongArray(3) { it.toULong() }.copyOfRange(pos, pos)) + } + + for ((start, end) in listOf(-1 to 0, 0 to 2, 2 to 2, 1 to 0)) { + val bounds = "start: $start, end: $end" + val exClass = if (start > end) IllegalArgumentException::class else IndexOutOfBoundsException::class + assertFailsWith(exClass, bounds) { uintArrayOf(1).copyOfRange(start, end) } + assertFailsWith(exClass, bounds) { ulongArrayOf(1uL).copyOfRange(start, end) } + } + } + } \ No newline at end of file diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 8bb8f215e83..a6af68ae599 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -2458,6 +2458,25 @@ public abstract class kotlin/collections/ShortIterator : java/util/Iterator, kot public fun remove ()V } +public final class kotlin/collections/UArraysKt { + public static final fun contentEquals ([B[B)Z + public static final fun contentEquals ([I[I)Z + public static final fun contentEquals ([J[J)Z + public static final fun contentEquals ([S[S)Z + public static final fun contentHashCode ([B)I + public static final fun contentHashCode ([I)I + public static final fun contentHashCode ([J)I + public static final fun contentHashCode ([S)I + public static final fun contentToString ([B)Ljava/lang/String; + public static final fun contentToString ([I)Ljava/lang/String; + public static final fun contentToString ([J)Ljava/lang/String; + public static final fun contentToString ([S)Ljava/lang/String; + public static final fun toTypedArray ([B)[Lkotlin/UByte; + public static final fun toTypedArray ([I)[Lkotlin/UInt; + public static final fun toTypedArray ([J)[Lkotlin/ULong; + public static final fun toTypedArray ([S)[Lkotlin/UShort; +} + public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker { public fun ()V public final fun next ()B diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 9f720baf9f2..9c224ca35a3 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -9,6 +9,15 @@ import templates.Family.* object ArrayOps : TemplateGroupBase() { + init { + defaultBuilder { + specialFor(ArraysOfUnsigned) { + since("1.3") + annotation("@ExperimentalUnsignedTypes") + } + } + } + val f_isEmpty = fn("isEmpty()") { include(ArraysOfObjects, ArraysOfPrimitives) } builder { @@ -53,7 +62,7 @@ object ArrayOps : TemplateGroupBase() { } val f_contentEquals = fn("contentEquals(other: SELF)") { - include(ArraysOfObjects, ArraysOfPrimitives) + include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { since("1.1") infix(true) @@ -64,6 +73,10 @@ object ArrayOps : TemplateGroupBase() { """ } returns("Boolean") + if (family == ArraysOfUnsigned) { + body { "return storage.contentEquals(other.storage)" } + return@builder + } on(Platform.JVM) { inlineOnly() body { "return java.util.Arrays.equals(this, other)" } @@ -101,7 +114,7 @@ object ArrayOps : TemplateGroupBase() { } val f_contentToString = fn("contentToString()") { - include(ArraysOfObjects, ArraysOfPrimitives) + include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { since("1.1") doc { @@ -111,6 +124,10 @@ object ArrayOps : TemplateGroupBase() { } sample("samples.collections.Arrays.ContentOperations.contentToString") returns("String") + if (family == ArraysOfUnsigned) { + body { """return joinToString(", ", "[", "]")""" } + return@builder + } on(Platform.JVM) { inlineOnly() body { "return java.util.Arrays.toString(this)" } @@ -147,13 +164,17 @@ object ArrayOps : TemplateGroupBase() { } val f_contentHashCode = fn("contentHashCode()") { - include(ArraysOfObjects, ArraysOfPrimitives) + include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { since("1.1") doc { "Returns a hash code based on the contents of this array as if it is [List]." } returns("Int") + if (family == ArraysOfUnsigned) { + body { "return storage.contentHashCode()" } + return@builder + } on(Platform.JVM) { inlineOnly() body { "return java.util.Arrays.hashCode(this)" } @@ -221,6 +242,80 @@ object ArrayOps : TemplateGroupBase() { } } + val f_asSignedArray = fn("asSignedArray()") { + include(ArraysOfUnsigned) + } builder { + val arrayType = primitive!!.name.drop(1) + "Array" + signature("as$arrayType()") + returns(arrayType) + + doc { + """ + Returns an array of type [$arrayType], which is a view of this array where each element is a signed reinterpretation + of the corresponding element of this array. + """ + } + + inlineOnly() + body { """return storage""" } + } + + val f_toSignedArray = fn("toSignedArray()") { + include(ArraysOfUnsigned) + } builder { + val arrayType = primitive!!.name.drop(1) + "Array" + signature("to$arrayType()") + returns(arrayType) + + doc { + """ + Returns an array of type [$arrayType], which is a copy of this array where each element is a signed reinterpretation + of the corresponding element of this array. + """ + } + + inlineOnly() + body { """return storage.copyOf()""" } + } + + val f_asUnsignedArray = fn("asUnsignedArray()") { + include(ArraysOfUnsigned) + } builder { + val arrayType = primitive!!.name.drop(1) + "Array" + receiver(arrayType) + signature("asU$arrayType()") + returns("SELF") + + doc { + """ + Returns an array of type [U$arrayType], which is a view of this array where each element is an unsigned reinterpretation + of the corresponding element of this array. + """ + } + + inlineOnly() + body { """return U$arrayType(this)""" } + } + + val f_toUnsignedArray = fn("toUnsignedArray()") { + include(ArraysOfUnsigned) + } builder { + val arrayType = primitive!!.name.drop(1) + "Array" + receiver(arrayType) + signature("toU$arrayType()") + returns("SELF") + + doc { + """ + Returns an array of type [U$arrayType], which is a copy of this array where each element is an unsigned reinterpretation + of the corresponding element of this array. + """ + } + + inlineOnly() + body { """return U$arrayType(this.copyOf())""" } + } + val f_plusElement = fn("plusElement(element: T)") { include(InvariantArraysOfObjects) } builder { @@ -388,7 +483,7 @@ object ArrayOps : TemplateGroupBase() { } val f_copyOfRange = fn("copyOfRange(fromIndex: Int, toIndex: Int)") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) + include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builderWith { primitive -> doc { """ @@ -400,39 +495,45 @@ object ArrayOps : TemplateGroupBase() { } returns("SELF") - on(Platform.JS) { - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - suppress("ACTUAL_WITHOUT_EXPECT") // TODO: KT-21937 - returns("Array") - } - val rangeCheck = "AbstractList.checkRangeIndexes(fromIndex, toIndex, size)" - when (primitive) { - PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" } - else -> { - body { "return this.asDynamic().slice(fromIndex, toIndex)" } - } - } - body { rangeCheck + "\n" + body } - } - on(Platform.JVM) { - annotation("""@JvmName("copyOfRangeInline")""") + specialFor(ArraysOfUnsigned) { inlineOnly() - body { - """ - return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { - copyOfRangeImpl(fromIndex, toIndex) - } else { - if (toIndex > size) throw IndexOutOfBoundsException("toIndex: ${'$'}toIndex, size: ${'$'}size") - java.util.Arrays.copyOfRange(this, fromIndex, toIndex) - } - """ - } + body { "return SELF(storage.copyOfRange(fromIndex, toIndex))" } } - on(Platform.Common) { - specialFor(InvariantArraysOfObjects) { - suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 + specialFor(InvariantArraysOfObjects, ArraysOfPrimitives) { + on(Platform.JS) { + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + suppress("ACTUAL_WITHOUT_EXPECT") // TODO: KT-21937 + returns("Array") + } + val rangeCheck = "AbstractList.checkRangeIndexes(fromIndex, toIndex, size)" + when (primitive) { + PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" } + else -> { + body { "return this.asDynamic().slice(fromIndex, toIndex)" } + } + } + body { rangeCheck + "\n" + body } + } + on(Platform.JVM) { + annotation("""@JvmName("copyOfRangeInline")""") + inlineOnly() + body { + """ + return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { + copyOfRangeImpl(fromIndex, toIndex) + } else { + if (toIndex > size) throw IndexOutOfBoundsException("toIndex: ${'$'}toIndex, size: ${'$'}size") + java.util.Arrays.copyOfRange(this, fromIndex, toIndex) + } + """ + } + } + on(Platform.Common) { + specialFor(InvariantArraysOfObjects) { + suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 + } } } } @@ -440,43 +541,53 @@ object ArrayOps : TemplateGroupBase() { val f_copyOf = fn("copyOf()") { include(InvariantArraysOfObjects) include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) + include(ArraysOfUnsigned) } builder { doc { "Returns new array which is a copy of the original array." } sample("samples.collections.Arrays.CopyOfOperations.copyOf") returns("SELF") - on(Platform.JVM) { + + specialFor(ArraysOfUnsigned) { inlineOnly() - body { "return java.util.Arrays.copyOf(this, size)" } + body { "return SELF(storage.copyOf())" } } - on(Platform.JS) { - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - suppress("ACTUAL_WITHOUT_EXPECT") // TODO: KT-21937 + specialFor(InvariantArraysOfObjects, ArraysOfPrimitives) { + on(Platform.JVM) { + inlineOnly() + body { "return java.util.Arrays.copyOf(this, size)" } } - when (primitive) { - null -> { - inline(suppressWarning = true) - body { "return this.asDynamic().slice()" } + on(Platform.JS) { + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + suppress("ACTUAL_WITHOUT_EXPECT") // TODO: KT-21937 } - PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return withType(\"${primitive}Array\", this.asDynamic().slice())" } - else -> { - inline(suppressWarning = true) - body { "return this.asDynamic().slice()" } + when (primitive) { + null -> { + inline(suppressWarning = true) + body { "return this.asDynamic().slice()" } + } + PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return withType(\"${primitive}Array\", this.asDynamic().slice())" } + else -> { + inline(suppressWarning = true) + body { "return this.asDynamic().slice()" } + } } } - } - on(Platform.Common) { - specialFor(InvariantArraysOfObjects) { - suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 + on(Platform.Common) { + specialFor(InvariantArraysOfObjects) { + suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 + } } + } } val f_copyOf_newSize = fn("copyOf(newSize: Int)") { include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) include(InvariantArraysOfObjects) + include(ArraysOfUnsigned) } builder { doc { """ @@ -488,6 +599,11 @@ object ArrayOps : TemplateGroupBase() { """ } val newSizeCheck = """require(newSize >= 0) { "Invalid new array size: ${'$'}newSize." }""" + specialFor(ArraysOfUnsigned) { + inlineOnly() + returns("SELF") + body { "return SELF(storage.copyOf(newSize))" } + } specialFor(ArraysOfPrimitives) { sample("samples.collections.Arrays.CopyOfOperations.resizedPrimitiveCopyOf") returns("SELF") @@ -519,16 +635,16 @@ object ArrayOps : TemplateGroupBase() { """ } } - } - on(Platform.JVM) { - inlineOnly() - body { - "return java.util.Arrays.copyOf(this, newSize)" + on(Platform.Common) { + suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 } } - on(Platform.Common) { - specialFor(InvariantArraysOfObjects) { - suppress("NO_ACTUAL_FOR_EXPECT") // TODO: KT-21937 + specialFor(ArraysOfPrimitives, InvariantArraysOfObjects) { + on(Platform.JVM) { + inlineOnly() + body { + "return java.util.Arrays.copyOf(this, newSize)" + } } } } @@ -689,7 +805,7 @@ object ArrayOps : TemplateGroupBase() { } val f_toTypedArray = fn("toTypedArray()") { - include(ArraysOfPrimitives) + include(ArraysOfPrimitives, ArraysOfUnsigned) } builder { returns("Array") doc { @@ -697,27 +813,29 @@ object ArrayOps : TemplateGroupBase() { Returns a *typed* object array containing all of the elements of this primitive array. """ } - on(Platform.JVM) { - body { - """ + body { "return Array(size) { index -> this[index] }" } + specialFor(ArraysOfPrimitives) { + on(Platform.JVM) { + body { + """ val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] @Suppress("UNCHECKED_CAST") return result as Array """ + } } - } - on(Platform.JS) { - when (primitive) { - PrimitiveType.Char -> - body { "return Array(size, { i -> this[i] })" } - PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return copyOf().unsafeCast>()" } - else -> - body { "return js(\"[]\").slice.call(this)" } - } + on(Platform.JS) { + when (primitive) { + PrimitiveType.Char -> {} + PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return copyOf().unsafeCast>()" } + else -> + body { "return js(\"[]\").slice.call(this)" } + } + } } }