Provide binarySearch with comparator for arrays.

This commit is contained in:
Ilya Gorbunov
2015-07-22 00:09:35 +03:00
parent 34afb450d8
commit aeb7666578
2 changed files with 16 additions and 0 deletions
@@ -137,6 +137,13 @@ public fun ShortArray.asList(): List<Short> {
}
}
/**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted according to the specified [comparator].
*/
public fun <T> Array<out T>.binarySearch(element: T, comparator: Comparator<T>, fromIndex: Int = 0, toIndex: Int = size()): Int {
return Arrays.binarySearch(this, fromIndex, toIndex, element, comparator)
}
/**
* Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted.
*/
@@ -104,6 +104,15 @@ fun specialJVM(): List<GenericFunction> {
}
}
templates add f("binarySearch(element: T, comparator: Comparator<T>, fromIndex: Int = 0, toIndex: Int = size())") {
only(ArraysOfObjects)
doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted according to the specified [comparator]." }
returns("Int")
body {
"return Arrays.binarySearch(this, fromIndex, toIndex, element, comparator)"
}
}
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") {
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)