fn bsearch<'a, K, T, F>(key: K, items: &'a [T], cmp: F) -> Option<&'a T>
where
    F: Fn(&T, &K) -> Ordering,
{
    let mut slice = items;
    while !slice.is_empty() {
        let middle = slice.len() / 2;
        match cmp(&slice[middle], &key) {
            Ordering::Equal => {
                let found_after = if slice.len() == 1 {
                    None
                } else {
                    bsearch(key, &slice[middle + 1..], cmp)
                };
                return found_after.or(Some(&slice[middle]));
            }
            Ordering::Greater => {
                slice = &slice[..middle];
            }
            Ordering::Less => {
                slice = &slice[middle + 1..];
            }
        }
    }
    None
}
/// Binary search for a `key` in a sorted array of items, given a comparison
/// function. This implementation is tweaked to handle the case where the
/// comparison function does prefix matching, where multiple items in the array
/// might match, but the last match is the one actually matching.
Binary search for a `key` in a sorted array of items, given a comparison
function. This implementation is tweaked to handle the case where the
comparison function does prefix matching, where multiple items in the array
might match, but the last match is the one actually matching.
Binary search for a ` key ` in a sorted array of items given a comparison function . This implementation is tweaked to handle the case where the comparison function does prefix matching where multiple items in the array might match but the last match is the one actually matching .
fn bsearch<'a, K, T, F>(key: K, items: &'a [T], cmp: F) -> Option<&'a T>
where
    F: Fn(&T, &K) -> Ordering,
{
    let mut slice = items;
    while !slice.is_empty() {
        let middle = slice.len() / 2;
        match cmp(&slice[middle], &key) {
            Ordering::Equal => {
                let found_after = if slice.len() == 1 {
                    None
                } else {
                    bsearch(key, &slice[middle + 1..], cmp)
                };
                return found_after.or(Some(&slice[middle]));
            }
            Ordering::Greater => {
                slice = &slice[..middle];
            }
            Ordering::Less => {
                slice = &slice[middle + 1..];
            }
        }
    }
    None
}
fn bsearch < ' a , K , T , F > ( key : K , items : & ' a [ T ] , cmp : F ) -> Option < & ' a T > where F : Fn ( & T , & K ) -> Ordering , { let mut slice = items ; while ! slice . is_empty ( ) { let middle = slice . len ( ) / 2 ; match cmp ( & slice [ middle ] , & key ) { Ordering :: Equal => { let found_after = if slice . len ( ) == 1 { None } else { bsearch ( key , & slice [ middle + 1 .. ] , cmp ) } ; return found_after . or ( Some ( & slice [ middle ] ) ) ; } Ordering :: Greater => { slice = & slice [ .. middle ] ; } Ordering :: Less => { slice = & slice [ middle + 1 .. ] ; } } } None }
Binary search for a `key` in a sorted array of items, given a comparison
function.
Binary search for a ` key ` in a sorted array of items given a comparison function .
key
K
items
&'a [T]
cmp
F