dotty.tools.dotc.util.Util.scala Maven / Gradle / Ivy
The newest version!
package dotty.tools.dotc.util
import reflect.ClassTag
object Util {
/** The index `i` in `candidates.indices` such that `candidates(i) <= x` and
* `candidates(i)` is closest to `x`, determined by binary search, or -1
* if `x < candidates(0)`.
* @param hint If between 0 and `candidates.length` use this
* as the first search point, otherwise use
* `candidates.length/2`.
* @pre candidates is sorted
*/
def bestFit(candidates: Array[Int], length: Int, x: Int, hint: Int = -1): Int = {
def recur(lo: Int, hi: Int, mid: Int): Int =
if (x < candidates(mid))
recur(lo, mid - 1, (lo + mid - 1) / 2)
else if (mid + 1 < length && x >= candidates(mid + 1))
recur(mid + 1, hi, (mid + 1 + hi) / 2)
else mid
val initMid = if (0 <= hint && hint < length) hint else length / 2
if (length == 0 || x < candidates(0)) -1
else recur(0, length, initMid)
}
/** An array twice the size of given array, with existing elements copied over */
def dble[T: ClassTag](arr: Array[T]): Array[T] = {
val arr1 = new Array[T](arr.length * 2)
System.arraycopy(arr, 0, arr1, 0, arr.length)
arr1
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy