Skip to content

Commit 6c94089

Browse files
committed
branchless .filter(_).count()
1 parent 2d0baa7 commit 6c94089

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libcore/iter/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,16 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool
10991099
let (_, upper) = self.iter.size_hint();
11001100
(0, upper) // can't know a lower bound, due to the predicate
11011101
}
1102+
1103+
#[inline]
1104+
fn count(self) -> usize {
1105+
let (mut c, mut predicate, mut iter) = (0, self.predicate, self.iter);
1106+
for x in iter.by_ref() {
1107+
// branchless count
1108+
c += (&mut predicate)(&x) as usize;
1109+
}
1110+
c
1111+
}
11021112
}
11031113

11041114
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)