VecDeque: binary_search_by(): return right away if hit found at back.first() #78021

This commit is contained in:
Vojtech Kral 2021-04-13 00:10:03 +02:00
parent 60158f4a7c
commit bccbf9db1c

View File

@ -2481,8 +2481,11 @@ pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
F: FnMut(&'a T) -> Ordering,
{
let (front, back) = self.as_slices();
let cmp_back = back.first().map(|elem| f(elem));
if let Some(Ordering::Less | Ordering::Equal) = back.first().map(|elem| f(elem)) {
if let Some(Ordering::Equal) = cmp_back {
Ok(front.len())
} else if let Some(Ordering::Less) = cmp_back {
back.binary_search_by(f).map(|idx| idx + front.len()).map_err(|idx| idx + front.len())
} else {
front.binary_search_by(f)