sort and binarySearch methods now have correct default toIndex parameter #KT-4963 Fixed

This commit is contained in:
Ilya Ryzhenkov
2014-05-07 17:11:54 +04:00
committed by Andrey Breslav
parent 9ec78a437d
commit 178ae83e8d
3 changed files with 26 additions and 18 deletions
+16 -16
View File
@@ -10,56 +10,56 @@ import java.util.*
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun <T> Array<out T>.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun <T> Array<out T>.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
/** /**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/ */
public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size - 1): Int { public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element) return Arrays.binarySearch(this, fromIndex, toIndex, element)
} }
@@ -372,56 +372,56 @@ public fun <T, C : MutableCollection<in R>, R : T> Stream<T>.filterIsInstanceTo(
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
/** /**
* Sorts array or range in array inplace * Sorts array or range in array inplace
*/ */
public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
Arrays.sort(this, fromIndex, toIndex) Arrays.sort(this, fromIndex, toIndex)
} }
@@ -5,6 +5,14 @@ import org.junit.Test as test
class ArraysJVMTest { class ArraysJVMTest {
test fun sort() {
var a = intArray(5, 2, 1, 4, 3)
var b = intArray(1, 2, 3, 4, 5)
a.sort()
for (i in a.indices)
expect(b[i]) { a[i] }
}
test fun copyOf() { test fun copyOf() {
checkContent(booleanArray(true, false, true, false, true, false).copyOf().iterator(), 6) { it % 2 == 0 } checkContent(booleanArray(true, false, true, false, true, false).copyOf().iterator(), 6) { it % 2 == 0 }
checkContent(byteArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toByte() } checkContent(byteArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toByte() }
@@ -53,7 +53,7 @@ fun specialJVM(): List<GenericFunction> {
} }
} }
templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size - 1)") { templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
only(ArraysOfObjects, ArraysOfPrimitives) only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted." } doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted." }
@@ -63,7 +63,7 @@ fun specialJVM(): List<GenericFunction> {
} }
} }
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size - 1)") { templates add f("sort(fromIndex: Int = 0, toIndex: Int = size)") {
only(ArraysOfObjects, ArraysOfPrimitives) only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
doc { "Sorts array or range in array inplace" } doc { "Sorts array or range in array inplace" }