In-place sorting.

#KT-9034
This commit is contained in:
Ilya Gorbunov
2015-10-30 18:40:30 +03:00
parent 5e9b7b9e60
commit 1a0c2e2cf6
7 changed files with 264 additions and 224 deletions
@@ -823,25 +823,34 @@ class ArraysTest {
val intArr = intArrayOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
intArr.sort()
assertArrayNotSameButEquals(intArrayOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE), intArr)
intArr.sortDescending()
assertArrayNotSameButEquals(intArrayOf(Int.MAX_VALUE, 80, 9, 5, 2, 1, Int.MIN_VALUE), intArr)
val longArr = longArrayOf(200, 2, 1, 4, 3, Long.MIN_VALUE, Long.MAX_VALUE)
longArr.sort()
assertArrayNotSameButEquals(longArrayOf(Long.MIN_VALUE, 1, 2, 3, 4, 200, Long.MAX_VALUE), longArr)
longArr.sortDescending()
assertArrayNotSameButEquals(longArrayOf(Long.MAX_VALUE, 200, 4, 3, 2, 1, Long.MIN_VALUE), longArr)
val charArr = charArrayOf('d', 'c', 'E', 'a', '\u0000', '\uFFFF')
charArr.sort()
assertArrayNotSameButEquals(charArrayOf('\u0000', 'E', 'a', 'c', 'd', '\uFFFF'), charArr)
charArr.sortDescending()
assertArrayNotSameButEquals(charArrayOf('\uFFFF', 'd', 'c', 'a', 'E', '\u0000'), charArr)
val strArr = arrayOf("9", "80", "all", "Foo")
strArr.sort()
assertArrayNotSameButEquals(arrayOf("80", "9", "Foo", "all"), strArr)
strArr.sortDescending()
assertArrayNotSameButEquals(arrayOf("all", "Foo", "9", "80"), strArr)
}
@test fun sorted() {
assertTrue(arrayOf<Long>().sorted().none())
assertEquals(listOf(1), arrayOf(1).sorted())
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) })
fun arrayData<A, T: Comparable<T>>(vararg values: T, toArray: Array<out T>.() -> A) = ArraySortedChecker<A, T>(values.toArray(), naturalOrder())
with (arrayData("ac", "aD", "aba") { toList().toTypedArray() }) {
checkSorted<List<String>>({ sorted() }, { sortedDescending() }, { iterator() })
@@ -875,6 +884,18 @@ class ArraysTest {
}
}
@test fun sortByInPlace() {
val data = arrayOf("aa" to 20, "ab" to 3, "aa" to 3)
data.sortBy { it.second }
assertArrayNotSameButEquals(arrayOf("ab" to 3, "aa" to 3, "aa" to 20), data)
data.sortBy { it.first }
assertArrayNotSameButEquals(arrayOf("aa" to 3, "aa" to 20, "ab" to 3), data)
data.sortByDescending { (it.first + it.second).length }
assertArrayNotSameButEquals(arrayOf("aa" to 20, "aa" to 3, "ab" to 3), data)
}
@test fun sortedBy() {
val values = arrayOf("ac", "aD", "aba")
val indices = values.indices.toList().toIntArray()
@@ -900,11 +921,16 @@ class ArraysTest {
arrayData(arrayOf(0, 1, 2, 3, 4, 5), comparator)
.checkSorted<Array<out Int>>( { sortedArrayWith(comparator) }, { sortedArrayWith(comparator.reversed()) }, { iterator() })
// in-place
val array = Array(6) { it }
array.sortWith(comparator)
array.iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
}
}
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>) {
public fun <R> checkSorted(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 }
}
@@ -563,16 +563,40 @@ class CollectionTest {
expect(listOf(1)) { listOf(1) take 10 }
}
@test fun sortInPlace() {
val data = listOf(11, 3, 7)
val asc = data.toArrayList()
asc.sort()
assertEquals(listOf(3, 7, 11), asc)
val desc = data.toArrayList()
desc.sortDescending()
assertEquals(listOf(11, 7, 3), desc)
}
@test fun sorted() {
val data = listOf(11, 3, 7)
assertEquals(listOf(3, 7, 11), data.sorted())
assertEquals(listOf(11, 7, 3), data.sortedDescending())
}
@test fun sortByInPlace() {
val data = arrayListOf("aa" to 20, "ab" to 3, "aa" to 3)
data.sortBy { it.second }
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
data.sortBy { it.first }
assertEquals(listOf("aa" to 3, "aa" to 20, "ab" to 3), data)
data.sortByDescending { (it.first + it.second).length }
assertEquals(listOf("aa" to 20, "aa" to 3, "ab" to 3), data)
}
@test fun sortedBy() {
assertEquals(listOf("two" to 2, "three" to 3), listOf("three" to 3, "two" to 2).sortedBy { it.second })
assertEquals(listOf("three" to 3, "two" to 2), listOf("three" to 3, "two" to 2).sortedBy { it.first })
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length() })
assertEquals(listOf("two" to 3, "three" to 20), listOf("three" to 20, "two" to 3).sortedBy { it.second })
assertEquals(listOf("three" to 20, "two" to 3), listOf("three" to 20, "two" to 3).sortedBy { it.first })
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length })
}
@test fun sortedNullableBy() {