branchless .filter(_).count()

This commit is contained in:
Andre Bogus 2017-01-16 22:28:58 +01:00
parent 2d0baa71b9
commit 6c940893e5

View file

@ -1099,6 +1099,16 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
#[inline]
fn count(self) -> usize {
let (mut c, mut predicate, mut iter) = (0, self.predicate, self.iter);
for x in iter.by_ref() {
// branchless count
c += (&mut predicate)(&x) as usize;
}
c
}
}
#[stable(feature = "rust1", since = "1.0.0")]