[fleet, util] move binary search function to util.core

GitOrigin-RevId: 72fd16b3c893c9858e307671df68b38ae7e6817e
This commit is contained in:
Alexander Zolotov
2025-07-31 13:59:36 +02:00
committed by intellij-monorepo-bot
parent c535c6dfd5
commit 4f8dec93d5
3 changed files with 48 additions and 22 deletions

View File

@@ -16,4 +16,47 @@ fun binarySearchInRange(from: Float, to: Float, predicate: (Float) -> Boolean):
}
}
return r
}
}
fun IntArray.binarySearch(element: Int): Int {
var l = 0
var r = size - 1
while (l <= r) {
val m = (l + r) / 2
val midElement = get(m)
if (midElement < element) {
l = m + 1
}
else if (midElement > element) {
r = m - 1
}
else {
return m
}
}
return -(l + 1)
}
fun LongArray.binarySearch(element: Long): Int {
var l = 0
var r = size - 1
while (l <= r) {
val m = (l + r) / 2
val midElement = get(m)
if (midElement < element) {
l = m + 1
}
else if (midElement > element) {
r = m - 1
}
else {
return m
}
}
return -(l + 1)
}

View File

@@ -1,6 +1,8 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package fleet.util.text
import fleet.util.binarySearch
private const val SEPARATORS = "`~!@#\$%^&*()-=+[{]}\\|;:'\",.<>/?"
private val maxSeparatorCode = SEPARATORS.maxBy { it.code }.code
private val separatorCodes = BooleanArray(maxSeparatorCode + 1).apply {
@@ -66,27 +68,6 @@ private fun isLuCategory(codepoint: Int): Boolean {
}
}
internal fun IntArray.binarySearch(element: Int): Int {
var l = 0
var r = size - 1
while (l <= r) {
val m = (l + r) / 2
val midElement = get(m)
if (midElement < element) {
l = m + 1
}
else if (midElement > element) {
r = m - 1
}
else {
return m
}
}
return -(l + 1)
}
// Code points are derived from:
// https://unicode.org/Public/UNIDATA/PropList.txt
private fun isOtherUppercase(codepoint: Int): Boolean {

View File

@@ -1,6 +1,8 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package fleet.util.text
import fleet.util.binarySearch
/**
* Ranges char codepoints which are both unicode identifier part and java identifier part (could be made two lists, but very long)
* Each pair of numbers in the list represent the start and exclusive end of a range.