diff options
| author | Georg Brandl <georg@python.org> | 2016-04-30 11:16:30 +0200 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2016-04-30 11:16:35 +0200 |
| commit | e6201cfb5cabc636a1dbfb1e543e5485639497a4 (patch) | |
| tree | de37087f250b8d5b881c7c4ae6b4b28ae217d68f /src/libcore/iter | |
| parent | 9b63263d0d2ee265765ba7f802d2b23fe5d413f5 (diff) | |
Implement find() on Chain iterators
This results in a roughly 2x speedup compared to the default impl "inherited" from Iterator.
Diffstat (limited to 'src/libcore/iter')
| -rw-r--r-- | src/libcore/iter/mod.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index abc199cd182..17f7c0a773e 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -542,6 +542,23 @@ impl<A, B> Iterator for Chain<A, B> where } #[inline] + fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where + P: FnMut(&Self::Item) -> bool, + { + match self.state { + ChainState::Both => match self.a.find(&mut predicate) { + None => { + self.state = ChainState::Back; + self.b.find(predicate) + } + v => v + }, + ChainState::Front => self.a.find(predicate), + ChainState::Back => self.b.find(predicate), + } + } + + #[inline] fn last(self) -> Option<A::Item> { match self.state { ChainState::Both => { |
