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())
}
}