Implement sorting extension functions for UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-02-22 18:29:39 +03:00
committed by Ilya Gorbunov
parent c42dbb34ca
commit cb587893c0
6 changed files with 590 additions and 41 deletions
@@ -2511,6 +2511,214 @@ public inline fun UShortArray.reversedArray(): UShortArray {
return UShortArray(storage.reversedArray())
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortDescending(): Unit {
if (size > 1) {
sort()
reverse()
}
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sorted(): List<UInt> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sorted(): List<ULong> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sorted(): List<UByte> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns a list of all elements sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sorted(): List<UShort> {
return copyOf().apply { sort() }.asList()
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedArray(): UIntArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedArray(): ULongArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedArray(): UByteArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedArray(): UShortArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedArrayDescending(): UIntArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedArrayDescending(): ULongArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedArrayDescending(): UByteArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedArrayDescending(): UShortArray {
if (isEmpty()) return this
return this.copyOf().apply { sortDescending() }
}
/**
* Returns a list of all elements sorted descending according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sortedDescending(): List<UInt> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements sorted descending according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sortedDescending(): List<ULong> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements sorted descending according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sortedDescending(): List<UByte> {
return copyOf().apply { sort() }.reversed()
}
/**
* Returns a list of all elements sorted descending according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sortedDescending(): List<UShort> {
return copyOf().apply { sort() }.reversed()
}
/**
* 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.
@@ -3191,6 +3399,42 @@ public inline operator fun UShortArray.plus(elements: UShortArray): UShortArray
return UShortArray(storage + elements.storage)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.sort(): Unit {
if (size > 1) sortArray(this)
}
/**
* 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.
@@ -0,0 +1,152 @@
/*
* Copyright 2010-2019 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.
*/
package kotlin.collections
// UByteArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UByteArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UByteArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// UShortArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UShortArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UShortArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// UIntArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: UIntArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: UIntArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// ULongArray =============================================================================
@ExperimentalUnsignedTypes
private fun partition(
array: ULongArray, left: Int, right: Int): Int {
var i = left
var j = right
val pivot = array[(left + right) / 2]
while (i <= j) {
while (array[i] < pivot)
i++
while (array[j] > pivot)
j--
if (i <= j) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
}
return i
}
@ExperimentalUnsignedTypes
private fun quickSort(
array: ULongArray, left: Int, right: Int) {
val index = partition(array, left, right)
if (left < index - 1)
quickSort(array, left, index - 1)
if (index < right)
quickSort(array, index, right)
}
// Interfaces =============================================================================
/**
* Sorts the given array using qsort algorithm.
*/
@ExperimentalUnsignedTypes
internal fun sortArray(array: UByteArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: UShortArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: UIntArray) = quickSort(array, 0, array.size - 1)
@ExperimentalUnsignedTypes
internal fun sortArray(array: ULongArray) = quickSort(array, 0, array.size - 1)
@@ -923,4 +923,118 @@ class UnsignedArraysTest {
expect(listOf(2uL)) { ulongArrayOf(2, 3).filterNot { it > 2 } }
}
@Test
fun sort() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.apply { sort() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.apply { sort() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.apply { sort() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.apply { sort() })
}
@Test
fun sortDescending() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.apply { sortDescending() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.apply { sortDescending() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.apply { sortDescending() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.apply { sortDescending() })
}
@Test
fun sorted() {
assertTrue(uintArrayOf().sorted().none())
assertEquals(listOf(1u), uintArrayOf(1).sorted())
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.sorted())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.sorted())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.sorted())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.sorted())
}
@Test
fun sortedDescending() {
assertTrue(uintArrayOf().sortedDescending().none())
assertEquals(listOf(1u), uintArrayOf(1).sortedDescending())
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.sortedDescending())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.sortedDescending())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.sortedDescending())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.sortedDescending())
}
@Test
fun sortedBy() {
assertTrue(uintArrayOf().sortedBy { it.toString() }.none())
assertEquals(listOf(1u), uintArrayOf(1).sortedBy { it.toString() })
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertEquals(listOf<UByte>(250, UByte.MAX_VALUE, 0, 1, 2, 5, 9, 80), ubyteArray.sortedBy { it.toByte() })
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertEquals(listOf<UShort>(65501, UShort.MAX_VALUE, 0, 1, 2, 5, 9, 80), ushortArray.sortedBy { it.toShort() })
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertEquals(listOf<UInt>(4294967200, UInt.MAX_VALUE, 0, 1, 2, 5, 9, 80), uintArray.sortedBy { it.toInt() })
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertEquals(listOf<ULong>(ULong.MAX_VALUE - 123, ULong.MAX_VALUE, 0, 1, 2, 5, 9, 80), ulongArray.sortedBy { it.toLong() })
}
@Test
fun sortedArray() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(0, 1, 2, 5, 9, 80, 250, UByte.MAX_VALUE), ubyteArray.sortedArray())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(0, 1, 2, 5, 9, 80, 65501, UShort.MAX_VALUE), ushortArray.sortedArray())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(0, 1, 2, 5, 9, 80, 4294967200, UInt.MAX_VALUE), uintArray.sortedArray())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(0, 1, 2, 5, 9, 80, ULong.MAX_VALUE - 123, ULong.MAX_VALUE), ulongArray.sortedArray())
}
@Test
fun sortedArrayDescending() {
val ubyteArray = ubyteArrayOf(5, 2, 1, 9, 80, 0, UByte.MAX_VALUE, 250)
assertArrayContentEquals(ubyteArrayOf(UByte.MAX_VALUE, 250, 80, 9, 5, 2, 1, 0), ubyteArray.sortedArrayDescending())
val ushortArray = ushortArrayOf(5, 2, 1, 9, 80, 0, UShort.MAX_VALUE, 65501)
assertArrayContentEquals(ushortArrayOf(UShort.MAX_VALUE, 65501, 80, 9, 5, 2, 1, 0), ushortArray.sortedArrayDescending())
val uintArray = uintArrayOf(5, 2, 1, 9, 80, 0, UInt.MAX_VALUE, 4294967200)
assertArrayContentEquals(uintArrayOf(UInt.MAX_VALUE, 4294967200, 80, 9, 5, 2, 1, 0), uintArray.sortedArrayDescending())
val ulongArray = ulongArrayOf(5, 2, 1, 9, 80, 0, ULong.MAX_VALUE, ULong.MAX_VALUE - 123)
assertArrayContentEquals(ulongArrayOf(ULong.MAX_VALUE, ULong.MAX_VALUE - 123, 80, 9, 5, 2, 1, 0), ulongArray.sortedArrayDescending())
}
}