Introduce operations on Arrays returning Arrays: reversedArray, sortedArray.

#KT-8711
This commit is contained in:
Ilya Gorbunov
2015-08-28 19:05:10 +03:00
parent 89df3925fa
commit faa26cdb25
7 changed files with 426 additions and 20 deletions
+258
View File
@@ -205,6 +205,120 @@ public fun String.reversed(): String {
return StringBuilder().append(this).reverse().toString()
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun <T, A: Array<out T>> A.reversedArray(): A {
if (isEmpty()) return this
val result = this.copyOf() as Array<T>
var i1 = 0
var i2 = lastIndex
while (i1 < i2) {
val tmp = result[i1]
result[i1] = result[i2]
result[i2] = tmp
i1 += 1
i2 -= 1
}
return result as A
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun BooleanArray.reversedArray(): BooleanArray {
if (isEmpty()) return this
val result = BooleanArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun ByteArray.reversedArray(): ByteArray {
if (isEmpty()) return this
val result = ByteArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun CharArray.reversedArray(): CharArray {
if (isEmpty()) return this
val result = CharArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun DoubleArray.reversedArray(): DoubleArray {
if (isEmpty()) return this
val result = DoubleArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun FloatArray.reversedArray(): FloatArray {
if (isEmpty()) return this
val result = FloatArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun IntArray.reversedArray(): IntArray {
if (isEmpty()) return this
val result = IntArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun LongArray.reversedArray(): LongArray {
if (isEmpty()) return this
val result = LongArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns an array with elements of this array in reversed order.
*/
public fun ShortArray.reversedArray(): ShortArray {
if (isEmpty()) return this
val result = ShortArray(size())
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result
}
/**
* Returns a sorted list of all elements.
*/
@@ -383,6 +497,150 @@ public fun <T : Comparable<T>> Sequence<T>.sorted(): Sequence<T> {
}
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun <T : Comparable<T>, A: Array<out T>> A.sortedArray(): A {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as A
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun ByteArray.sortedArray(): ByteArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as ByteArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun CharArray.sortedArray(): CharArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as CharArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun DoubleArray.sortedArray(): DoubleArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as DoubleArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun FloatArray.sortedArray(): FloatArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as FloatArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun IntArray.sortedArray(): IntArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as IntArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun LongArray.sortedArray(): LongArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as LongArray
}
/**
* Returns an array with all elements of this array sorted according to their natural sort order.
*/
public fun ShortArray.sortedArray(): ShortArray {
if (isEmpty()) return this
return this.copyOf().apply { sort() } as ShortArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun <T : Comparable<T>, A: Array<out T>> A.sortedArrayDescending(): A {
if (isEmpty()) return this
// TODO: Use reverseOrder<T>()
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) } as A
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun ByteArray.sortedArrayDescending(): ByteArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as ByteArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun CharArray.sortedArrayDescending(): CharArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as CharArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun DoubleArray.sortedArrayDescending(): DoubleArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as DoubleArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun FloatArray.sortedArrayDescending(): FloatArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as FloatArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun IntArray.sortedArrayDescending(): IntArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as IntArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun LongArray.sortedArrayDescending(): LongArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as LongArray
}
/**
* Returns an array with all elements of this array sorted descending according to their natural sort order.
*/
public fun ShortArray.sortedArrayDescending(): ShortArray {
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as ShortArray
}
/**
* Returns an array with all elements of this array sorted according the specified [comparator].
*/
public fun <T, A: Array<out T>> A.sortedArrayWith(comparator: Comparator<in T>): A {
if (isEmpty()) return this
return this.copyOf().apply { sortWith(comparator) } as A
}
/**
* Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function.
*/
@@ -885,6 +885,13 @@ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit {
Arrays.sort(this, fromIndex, toIndex)
}
/**
* Sorts array or range in array inplace.
*/
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size()): Unit {
Arrays.sort(this, fromIndex, toIndex, comparator)
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
+49 -20
View File
@@ -1,5 +1,6 @@
package test.collections
import java.util.*
import kotlin.test.*
import org.junit.Test as test
@@ -528,7 +529,7 @@ class ArraysTest {
} is UnsupportedOperationException)
}
test fun reverse() {
test fun reversed() {
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() }
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() }
expect(listOf<Short>(3, 2, 1)) { shortArrayOf(1, 2, 3).reversed() }
@@ -539,6 +540,17 @@ class ArraysTest {
expect(listOf(false, false, true)) { booleanArrayOf(true, false, false).reversed() }
}
test fun reversedArray() {
assertArrayNotSameButEquals(intArrayOf(3, 2, 1), intArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(byteArrayOf(3, 2, 1), byteArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(shortArrayOf(3, 2, 1), shortArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(longArrayOf(3, 2, 1), longArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(floatArrayOf(3F, 2F, 1F), floatArrayOf(1F, 2F, 3F).reversedArray())
assertArrayNotSameButEquals(doubleArrayOf(3.0, 2.0, 1.0), doubleArrayOf(1.0, 2.0, 3.0).reversedArray())
assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').reversedArray())
assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).reversedArray())
}
test fun drop() {
expect(listOf(1), { intArrayOf(1).drop(0) })
expect(listOf(), { intArrayOf().drop(1) })
@@ -786,34 +798,37 @@ class ArraysTest {
assertTrue(arrayOf<Long>().sorted().none())
assertEquals(listOf(1), arrayOf(1).sorted())
arrayOf("ac", "aD", "aba").let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
fun arrayData<A, T: Comparable<T>>(vararg values: T, toArray: Array<out T>.() -> A) = ArraySortedChecker<A, T>(values.toArray(), comparator { a, b -> a.compareTo(b) })
with (arrayData("ac", "aD", "aba") { toList().toTypedArray() }) {
checkSorted<List<String>>({ sorted() }, { sortedDescending() }, { iterator() })
checkSorted<Array<String>>({ sortedArray() }, { sortedArrayDescending()}, { iterator() } )
}
intArrayOf(3, 7, 1).let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
with (arrayData(3, 7, 1) { toIntArray() }) {
checkSorted<List<Int>>( { sorted() }, { sortedDescending() }, { iterator() })
checkSorted<IntArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
}
longArrayOf(1, Long.MIN_VALUE, Long.MAX_VALUE).let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
with (arrayData(1L, Long.MIN_VALUE, Long.MAX_VALUE) { toLongArray() }) {
checkSorted<List<Long>>( { sorted() }, { sortedDescending() }, { iterator() })
checkSorted<LongArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
}
charArrayOf('a', 'D', 'c').let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
with (arrayData('a', 'D', 'c') { toCharArray() }) {
checkSorted<List<Char>>( { sorted() }, { sortedDescending() }, { iterator() })
checkSorted<CharArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
}
byteArrayOf(1, Byte.MAX_VALUE, Byte.MIN_VALUE).let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
with (arrayData(1.toByte(), Byte.MAX_VALUE, Byte.MIN_VALUE) { toByteArray() }) {
checkSorted<List<Byte>>( { sorted() }, { sortedDescending() }, { iterator() })
checkSorted<ByteArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
}
doubleArrayOf(Double.POSITIVE_INFINITY, 1.0, Double.MAX_VALUE).let {
it.sorted().assertSorted { a, b -> a <= b }
it.sortedDescending().assertSorted { a, b -> a >= b }
with(arrayData(Double.POSITIVE_INFINITY, 1.0, Double.MAX_VALUE) { toDoubleArray() }) {
checkSorted<List<Double>>( { sorted() }, { sortedDescending() }, { iterator() })
checkSorted<DoubleArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
}
}
@@ -834,6 +849,20 @@ class ArraysTest {
}
test fun sortedWith() {
assertEquals(listOf(3, 0, 4, 1, 5, 2), intArrayOf(0, 1, 2, 3, 4, 5).sortedWith( compareBy { it: Int -> it % 3 }.thenByDescending { it } ))
val comparator = compareBy { it: Int -> it % 3 }.thenByDescending { it }
fun arrayData<A, T>(array: A, comparator: Comparator<T>) = ArraySortedChecker<A, T>(array, comparator)
arrayData(intArrayOf(0, 1, 2, 3, 4, 5), comparator)
.checkSorted<List<Int>>( { sortedWith(comparator) }, { sortedWith(comparator.reversed()) }, { iterator() })
arrayData(arrayOf(0, 1, 2, 3, 4, 5), comparator)
.checkSorted<Array<Int>>( { sortedArrayWith(comparator) }, { sortedArrayWith(comparator.reversed()) }, { iterator() })
}
}
private class ArraySortedChecker<A, T>(val array: A, val comparator: Comparator<in T>) {
public fun checkSorted<R>(sorted: A.() -> R, sortedDescending: A.() -> R, iterator: R.() -> Iterator<T>) {
array.sorted().iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
array.sortedDescending().iterator().assertSorted { a, b -> comparator.compare(a, b) >= 0 }
}
}