Skip to content

Commit e6201cf

Browse files
committed
Implement find() on Chain iterators
This results in a roughly 2x speedup compared to the default impl "inherited" from Iterator.
1 parent 9b63263 commit e6201cf

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/libcore/iter/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ impl<A, B> Iterator for Chain<A, B> where
541541
}
542542
}
543543

544+
#[inline]
545+
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
546+
P: FnMut(&Self::Item) -> bool,
547+
{
548+
match self.state {
549+
ChainState::Both => match self.a.find(&mut predicate) {
550+
None => {
551+
self.state = ChainState::Back;
552+
self.b.find(predicate)
553+
}
554+
v => v
555+
},
556+
ChainState::Front => self.a.find(predicate),
557+
ChainState::Back => self.b.find(predicate),
558+
}
559+
}
560+
544561
#[inline]
545562
fn last(self) -> Option<A::Item> {
546563
match self.state {

src/libcoretest/iter.rs

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ fn test_iterator_chain_count() {
133133
assert_eq!(zs.iter().chain(&ys).count(), 4);
134134
}
135135

136+
#[test]
137+
fn test_iterator_chain_find() {
138+
let xs = [0, 1, 2, 3, 4, 5];
139+
let ys = [30, 40, 50, 60];
140+
let mut iter = xs.iter().chain(&ys);
141+
assert_eq!(iter.find(|&&i| i == 4), Some(&4));
142+
assert_eq!(iter.next(), Some(&5));
143+
assert_eq!(iter.find(|&&i| i == 40), Some(&40));
144+
assert_eq!(iter.next(), Some(&50));
145+
assert_eq!(iter.find(|&&i| i == 100), None);
146+
assert_eq!(iter.next(), None);
147+
}
148+
136149
#[test]
137150
fn test_filter_map() {
138151
let it = (0..).step_by(1).take(10)

0 commit comments

Comments
 (0)