Skip to content

Commit 4b2931c

Browse files
author
blake2-ppc
committed
iterator: implement DoubleEndedIterator for FlatMap
1 parent bb996bf commit 4b2931c

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

src/libstd/iterator.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
529529
#[inline]
530530
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
531531
-> FlatMap<'r, A, T, U> {
532-
FlatMap{iter: self, f: f, subiter: None }
532+
FlatMap{iter: self, f: f, frontiter: None, backiter: None }
533533
}
534534

535535
// FIXME: #5898: should be called `peek`
@@ -1251,22 +1251,44 @@ impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for Scan<'self, A, B, T, St> {
12511251
pub struct FlatMap<'self, A, T, U> {
12521252
priv iter: T,
12531253
priv f: &'self fn(A) -> U,
1254-
priv subiter: Option<U>,
1254+
priv frontiter: Option<U>,
1255+
priv backiter: Option<U>,
12551256
}
12561257

12571258
impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
12581259
FlatMap<'self, A, T, U> {
12591260
#[inline]
12601261
fn next(&mut self) -> Option<B> {
12611262
loop {
1262-
for self.subiter.mut_iter().advance |inner| {
1263+
for self.frontiter.mut_iter().advance |inner| {
12631264
for inner.advance |x| {
12641265
return Some(x)
12651266
}
12661267
}
12671268
match self.iter.next().map_consume(|x| (self.f)(x)) {
1268-
None => return None,
1269-
next => self.subiter = next,
1269+
None => return self.backiter.chain_mut_ref(|it| it.next()),
1270+
next => self.frontiter = next,
1271+
}
1272+
}
1273+
}
1274+
}
1275+
1276+
impl<'self,
1277+
A, T: DoubleEndedIterator<A>,
1278+
B, U: DoubleEndedIterator<B>> DoubleEndedIterator<B>
1279+
for FlatMap<'self, A, T, U> {
1280+
#[inline]
1281+
fn next_back(&mut self) -> Option<B> {
1282+
loop {
1283+
for self.backiter.mut_iter().advance |inner| {
1284+
match inner.next_back() {
1285+
None => (),
1286+
y => return y
1287+
}
1288+
}
1289+
match self.iter.next_back().map_consume(|x| (self.f)(x)) {
1290+
None => return self.frontiter.chain_mut_ref(|it| it.next_back()),
1291+
next => self.backiter = next,
12701292
}
12711293
}
12721294
}
@@ -1768,6 +1790,23 @@ mod tests {
17681790
assert_eq!(it.next_back(), None)
17691791
}
17701792

1793+
#[test]
1794+
fn test_double_ended_flat_map() {
1795+
let u = [0u,1];
1796+
let v = [5,6,7,8];
1797+
let mut it = u.iter().flat_map_(|x| v.slice(*x, v.len()).iter());
1798+
assert_eq!(it.next_back().unwrap(), &8);
1799+
assert_eq!(it.next().unwrap(), &5);
1800+
assert_eq!(it.next_back().unwrap(), &7);
1801+
assert_eq!(it.next_back().unwrap(), &6);
1802+
assert_eq!(it.next_back().unwrap(), &8);
1803+
assert_eq!(it.next().unwrap(), &6);
1804+
assert_eq!(it.next_back().unwrap(), &7);
1805+
assert_eq!(it.next_back(), None);
1806+
assert_eq!(it.next(), None);
1807+
assert_eq!(it.next_back(), None);
1808+
}
1809+
17711810
#[test]
17721811
fn test_random_access_chain() {
17731812
let xs = [1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)