Implement binarySearch method for UArrays (KT-27262)
This commit is contained in:
committed by
Ilya Gorbunov
parent
6cd9858147
commit
550c74bb6b
@@ -1218,7 +1218,7 @@ object ArrayOps : TemplateGroupBase() {
|
||||
|
||||
val f_binarySearch = fn("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||
platforms(Platform.JVM)
|
||||
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||
include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
} builder {
|
||||
doc {
|
||||
@@ -1238,6 +1238,55 @@ object ArrayOps : TemplateGroupBase() {
|
||||
body {
|
||||
"return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element)"
|
||||
}
|
||||
|
||||
specialFor(ArraysOfUnsigned) {
|
||||
val elementConversion: String
|
||||
val midValConversion: String
|
||||
val compareFunction: String
|
||||
when (primitive!!) {
|
||||
PrimitiveType.UByte, PrimitiveType.UShort -> {
|
||||
elementConversion = ".toInt()"
|
||||
midValConversion = ".toInt()"
|
||||
compareFunction = "uintCompare"
|
||||
}
|
||||
PrimitiveType.UInt -> {
|
||||
elementConversion = ".toInt()"
|
||||
midValConversion = ""
|
||||
compareFunction = "uintCompare"
|
||||
}
|
||||
PrimitiveType.ULong -> {
|
||||
elementConversion = ".toLong()"
|
||||
midValConversion = ""
|
||||
compareFunction = "ulongCompare"
|
||||
}
|
||||
else -> error(primitive!!)
|
||||
}
|
||||
|
||||
|
||||
body {
|
||||
"""
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
|
||||
val signedElement = element$elementConversion
|
||||
var low = fromIndex
|
||||
var high = toIndex - 1
|
||||
|
||||
while (low <= high) {
|
||||
val mid = (low + high).ushr(1) // safe from overflows
|
||||
val midVal = storage[mid]
|
||||
val cmp = $compareFunction(midVal$midValConversion, signedElement)
|
||||
|
||||
if (cmp < 0)
|
||||
low = mid + 1
|
||||
else if (cmp > 0)
|
||||
high = mid - 1
|
||||
else
|
||||
return mid // key found
|
||||
}
|
||||
return -(low + 1) // key not found
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_binarySearch_comparator = fn("binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||
|
||||
Reference in New Issue
Block a user