ck/binsearch▲toc

Binary search

.

fun binsearch( ok : intstd/core/types/int: V, ng : intstd/core/types/int: V, p : (intstd/core/types/int: V) -> <divstd/core/types/div: X|e> boolstd/core/types/bool: V ) : <divstd/core/types/div: X|e> intstd/core/types/int: V

Binary search on the interval of integers between ok and ng. If ng < ok, the result is the smallest integer satisfying p. Otherwise, the result is the largest integer satisfying p. p must be monotone such that p(ok) = Truestd/core/types/True: bool and p(ng) = Falsestd/core/types/False: bool.

fun binsearch-f64( ok : float64std/core/types/float64: V, ng : float64std/core/types/float64: V, t : intstd/core/types/int: V, p : (float64std/core/types/float64: V) -> <divstd/core/types/div: X|e> boolstd/core/types/bool: V ) : <divstd/core/types/div: X|e> float64std/core/types/float64: V

Binary search on the interval of float64 between ok and ng. This function returns a number close to the boundary of p, which divides a set of numbers into two intervals, all numbers in an interval satisfy p, and all numbers in another one do not. p must be monotone such that p(ok) = Truestd/core/types/True: bool and p(ng) = Falsestd/core/types/False: bool.

private import std/core/typesstd/core/types, std/core/hndstd/core/hnd, std/corestd/core