dsalgo/
binary_search_2_ok_usize.rs

1//! for sequence, see crate::binary_search_on_sequence.
2
3pub fn binary_search<F>(
4    is_ok: F,
5    mut lo_ok: usize,
6    mut hi_ok: usize,
7) -> usize
8where
9    F: Fn(usize) -> bool,
10{
11    while lo_ok != hi_ok {
12        let x = (lo_ok + hi_ok) >> 1;
13
14        if is_ok(x) {
15            hi_ok = x;
16        } else {
17            lo_ok = x + 1;
18        }
19    }
20
21    lo_ok
22}
23
24#[cfg(test)]
25
26mod tests {
27
28    use super::*;
29
30    #[test]
31
32    fn test() {
33        assert_eq!(binary_search(|x| x * x >= 1000, 0, 100), 32);
34    }
35}