From 114736c09b71e9b898f3204d82c63d7ddd11311e Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 24 Jan 2019 17:38:15 +0300 Subject: [PATCH] Implement reversing extension functions for UArrays --- .../stdlib/common/src/generated/_UArrays.kt | 128 ++++++++++++++++++ .../stdlib/test/collections/ArraysTest.kt | 4 + .../test/collections/UnsignedArraysTest.kt | 18 ++- .../kotlin-stdlib-runtime-merged.txt | 4 + .../src/templates/Ordering.kt | 33 ++++- 5 files changed, 182 insertions(+), 5 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 617103af758..acae6d049f6 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -1383,6 +1383,134 @@ public inline fun UShortArray.singleOrNull(predicate: (UShort) -> Boolean): USho return single } +/** + * Reverses elements in the array in-place. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.reverse(): Unit { + storage.reverse() +} + +/** + * Reverses elements in the array in-place. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.reverse(): Unit { + storage.reverse() +} + +/** + * Reverses elements in the array in-place. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.reverse(): Unit { + storage.reverse() +} + +/** + * Reverses elements in the array in-place. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.reverse(): Unit { + storage.reverse() +} + +/** + * Returns a list with elements in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} + +/** + * Returns a list with elements in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} + +/** + * Returns a list with elements in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} + +/** + * Returns a list with elements in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} + +/** + * Returns an array with elements of this array in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.reversedArray(): UIntArray { + return UIntArray(storage.reversedArray()) +} + +/** + * Returns an array with elements of this array in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.reversedArray(): ULongArray { + return ULongArray(storage.reversedArray()) +} + +/** + * Returns an array with elements of this array in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.reversedArray(): UByteArray { + return UByteArray(storage.reversedArray()) +} + +/** + * Returns an array with elements of this array in reversed order. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.reversedArray(): UShortArray { + return UShortArray(storage.reversedArray()) +} + /** * 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. diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 0c4b649fd17..918a0a48df4 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -1044,6 +1044,10 @@ class ArraysTest { doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { reverse() }, snapshot = { toList() }) doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { reverse() }, snapshot = { toList() }) doTest(build = { map {it.toString()}.toTypedArray() as Array }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toUInt()}.toUIntArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toULong()}.toULongArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toUByte()}.toUByteArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { reverse() }, snapshot = { toList() }) } diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index 8ee462e674c..c501adffea2 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -9,7 +9,10 @@ package test.collections import test.collections.behaviors.collectionBehavior import kotlin.test.* -fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String = "") { assertTrue(expected contentEquals actual, message) } +fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String = "") { assertTrue(expected contentEquals actual, message) } +fun assertArrayContentEquals(expected: ULongArray, actual: ULongArray, message: String = "") { assertTrue(expected contentEquals actual, message) } +fun assertArrayContentEquals(expected: UShortArray, actual: UShortArray, message: String = "") { assertTrue(expected contentEquals actual, message) } +fun assertArrayContentEquals(expected: UByteArray, actual: UByteArray, message: String = "") { assertTrue(expected contentEquals actual, message) } class UnsignedArraysTest { @@ -348,4 +351,17 @@ class UnsignedArraysTest { assertEquals(genericArray.toList(), ulongArray.toList()) } + @Test fun reversed() { + expect(listOf(3u, 2u, 1u)) { uintArrayOf(1u, 2u, 3u).reversed() } + expect(listOf(3u, 2u, 1u)) { ubyteArrayOf(1u, 2u, 3u).reversed() } + expect(listOf(3u, 2u, 1u)) { ushortArrayOf(1u, 2u, 3u).reversed() } + expect(listOf(3u, 2u, 1u)) { ulongArrayOf(1u, 2u, 3u).reversed() } + } + + @Test fun reversedArray() { + assertArrayContentEquals(uintArrayOf(3u, 2u, 1u), uintArrayOf(1u, 2u, 3u).reversedArray()) + assertArrayContentEquals(ubyteArrayOf(3u, 2u, 1u), ubyteArrayOf(1u, 2u, 3u).reversedArray()) + assertArrayContentEquals(ushortArrayOf(3u, 2u, 1u), ushortArrayOf(1u, 2u, 3u).reversedArray()) + assertArrayContentEquals(ulongArrayOf(3u, 2u, 1u), ulongArrayOf(1u, 2u, 3u).reversedArray()) + } } \ 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 f90b6579e04..b557ac43416 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 @@ -2335,6 +2335,10 @@ public final class kotlin/collections/UArraysKt { public static final fun random-JzugnMA ([JLkotlin/random/Random;)J public static final fun random-oSF2wD8 ([BLkotlin/random/Random;)B public static final fun random-s5X_as8 ([SLkotlin/random/Random;)S + public static final fun reversed--ajY-9A ([I)Ljava/util/List; + public static final fun reversed-GBYM_sE ([B)Ljava/util/List; + public static final fun reversed-QwZRm1k ([J)Ljava/util/List; + public static final fun reversed-rL5Bavg ([S)Ljava/util/List; public static final fun singleOrNull--ajY-9A ([I)Lkotlin/UInt; public static final fun singleOrNull-GBYM_sE ([B)Lkotlin/UByte; public static final fun singleOrNull-QwZRm1k ([J)Lkotlin/ULong; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index ee1246e0286..8bf30c1173d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -10,8 +10,17 @@ import templates.SequenceClass.* object Ordering : TemplateGroupBase() { + init { + defaultBuilder { + specialFor(ArraysOfUnsigned) { + since("1.3") + annotation("@ExperimentalUnsignedTypes") + } + } + } + val f_reverse = fn("reverse()") { - include(Lists, InvariantArraysOfObjects, ArraysOfPrimitives) + include(Lists, InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { doc { "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." } returns("Unit") @@ -28,6 +37,14 @@ object Ordering : TemplateGroupBase() { } """ } + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { + """ + storage.reverse() + """ + } + } specialFor(Lists) { receiver("MutableList") on(Platform.JVM) { @@ -37,7 +54,7 @@ object Ordering : TemplateGroupBase() { } val f_reversed = fn("reversed()") { - include(Iterables, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings) + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings) } builder { doc { "Returns a list with elements in reversed order." } returns("List") @@ -50,7 +67,7 @@ object Ordering : TemplateGroupBase() { """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ if (isEmpty()) return emptyList() val list = toMutableList() @@ -70,7 +87,7 @@ object Ordering : TemplateGroupBase() { } val f_reversedArray = fn("reversedArray()") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) + include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { doc { "Returns an array with elements of this array in reversed order." } returns("SELF") @@ -94,6 +111,14 @@ object Ordering : TemplateGroupBase() { return result """ } + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { + """ + return SELF(storage.reversedArray()) + """ + } + } } val stableSortNote =