Implement reversing extension functions for UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-01-24 17:38:15 +03:00
committed by Ilya Gorbunov
parent 92cd84682c
commit 114736c09b
5 changed files with 182 additions and 5 deletions
@@ -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<UInt> {
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<ULong> {
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<UByte> {
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<UShort> {
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.
@@ -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<out String> }, 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() })
}
@@ -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<UByte>(3u, 2u, 1u)) { ubyteArrayOf(1u, 2u, 3u).reversed() }
expect(listOf<UShort>(3u, 2u, 1u)) { ushortArrayOf(1u, 2u, 3u).reversed() }
expect(listOf<ULong>(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())
}
}
@@ -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;
@@ -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<T>")
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<T>")
@@ -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 =