diff --git a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt index 8982f688630..89cde43e0f9 100644 --- a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt @@ -17,9 +17,9 @@ package kotlin.collections /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -49,9 +49,9 @@ public fun UIntArray.binarySearch(element: UInt, fromIndex: Int = 0, toIndex: In /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -81,9 +81,9 @@ public fun ULongArray.binarySearch(element: ULong, fromIndex: Int = 0, toIndex: /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -113,9 +113,9 @@ public fun UByteArray.binarySearch(element: UByte, fromIndex: Int = 0, toIndex: /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -142,3 +142,38 @@ public fun UShortArray.binarySearch(element: UShort, fromIndex: Int = 0, toIndex return -(low + 1) // key not found } +/** + * Fills original array with the provided value. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.fill(element: UInt, fromIndex: Int = 0, toIndex: Int = size): Unit { + storage.fill(element.toInt(), fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.fill(element: ULong, fromIndex: Int = 0, toIndex: Int = size): Unit { + storage.fill(element.toLong(), fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.fill(element: UByte, fromIndex: Int = 0, toIndex: Int = size): Unit { + storage.fill(element.toByte(), fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.fill(element: UShort, fromIndex: Int = 0, toIndex: Int = size): Unit { + storage.fill(element.toShort(), fromIndex, toIndex) +} \ No newline at end of file diff --git a/libraries/stdlib/jvm/test/collections/UArrayJVMTest.kt b/libraries/stdlib/jvm/test/collections/UArrayJVMTest.kt index 8e6dba314e7..29133aca189 100644 --- a/libraries/stdlib/jvm/test/collections/UArrayJVMTest.kt +++ b/libraries/stdlib/jvm/test/collections/UArrayJVMTest.kt @@ -6,6 +6,7 @@ package test.collections import kotlin.test.Test +import kotlin.test.assertTrue import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -66,6 +67,55 @@ class UArrayJVMTest { test(array, UIntArray::binarySearch, operations, UInt::toUInt) test(array.toULongArray(), ULongArray::binarySearch, operations, UInt::toULong) } + + @Test + fun fill() { + fun testFailures(array: A, fill: A.(E, Int, Int) -> Unit, element: E, arraySize: Int) { + assertFailsWith { + array.fill(element, -1, arraySize) + } + assertFailsWith { + array.fill(element, 0, arraySize + 1) + } + assertFailsWith { + array.fill(element, 1, 0) + } + } + + testFailures(UByteArray(5) { it.toUByte() }, UByteArray::fill, 0u, 5) + testFailures(UShortArray(5) { it.toUShort() }, UShortArray::fill, 0u, 5) + testFailures(UIntArray(5) { it.toUInt() }, UIntArray::fill, 0u, 5) + testFailures(ULongArray(5) { it.toULong() }, ULongArray::fill, 0u, 5) + + fun test( + array: UIntArray, + fill: A.(E, Int, Int) -> Unit, + operations: List>, + arrayTransform: UIntArray.() -> A, + elementTransform: UInt.() -> E, + contentEquals: A.(A) -> Boolean + ) { + for (o in operations) { + val result = array.arrayTransform() + result.fill(o.element.elementTransform(), o.fromIndex, o.toIndex) + assertTrue(o.expectedResult.arrayTransform().contentEquals(result)) + } + } + + val array = UIntArray(5) { it.toUInt() } + + val operations = listOf( + OperationOnRange(5u, 1, 4, uintArrayOf(0u, 5u, 5u, 5u, 4u)), + OperationOnRange(1u, 0, 5, uintArrayOf(1u, 1u, 1u, 1u, 1u)), + OperationOnRange(2u, 0, 3, uintArrayOf(2u, 2u, 2u, 3u, 4u)), + OperationOnRange(3u, 2, 5, uintArrayOf(0u, 1u, 3u, 3u, 3u)) + ) + + test(array, UByteArray::fill, operations, UIntArray::toUByteArray, UInt::toUByte, UByteArray::contentEquals) + test(array, UShortArray::fill, operations, UIntArray::toUShortArray, UInt::toUShort, UShortArray::contentEquals) + test(array, UIntArray::fill, operations, UIntArray::copyOf, UInt::toUInt, UIntArray::contentEquals) + test(array, ULongArray::fill, operations, UIntArray::toULongArray, UInt::toULong, ULongArray::contentEquals) + } } 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 ce60bc5ef3e..bca46db0125 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 @@ -2307,6 +2307,14 @@ public final class kotlin/collections/UArraysKt { public static final fun contentToString-GBYM_sE ([B)Ljava/lang/String; public static final fun contentToString-QwZRm1k ([J)Ljava/lang/String; public static final fun contentToString-rL5Bavg ([S)Ljava/lang/String; + public static final fun fill-2fe2U9s ([IIII)V + public static synthetic fun fill-2fe2U9s$default ([IIIIILjava/lang/Object;)V + public static final fun fill-EtDCXyQ ([SSII)V + public static synthetic fun fill-EtDCXyQ$default ([SSIIILjava/lang/Object;)V + public static final fun fill-K6DWlUc ([JJII)V + public static synthetic fun fill-K6DWlUc$default ([JJIIILjava/lang/Object;)V + public static final fun fill-WpHrYlw ([BBII)V + public static synthetic fun fill-WpHrYlw$default ([BBIIILjava/lang/Object;)V public static final fun plus-CFIt9YE ([ILjava/util/Collection;)[I public static final fun plus-kzHmqpY ([JLjava/util/Collection;)[J public static final fun plus-ojwP5H8 ([SLjava/util/Collection;)[S diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 46ae9416daa..f17eb6516ef 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -1205,7 +1205,7 @@ object ArrayOps : TemplateGroupBase() { val f_fill = fn("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") { platforms(Platform.JVM) - include(InvariantArraysOfObjects, ArraysOfPrimitives) + include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { doc { "Fills original array with the provided value." } returns("Unit") @@ -1214,6 +1214,13 @@ object ArrayOps : TemplateGroupBase() { java.util.Arrays.fill(this, fromIndex, toIndex, element) """ } + + specialFor(ArraysOfUnsigned) { + val signedPrimitiveName = primitive!!.name.drop(1) + body { + "storage.fill(element.to$signedPrimitiveName(), fromIndex, toIndex)" + } + } } val f_binarySearch = fn("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {