Skip to content

Commit b779641

Browse files
author
blake2-ppc
committed
---
yaml --- r: 63627 b: refs/heads/snap-stage3 c: 6291702 h: refs/heads/master i: 63625: 01cc9c0 63623: d8f247f v: v3
1 parent 3937b22 commit b779641

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: dfb7de8e0e6d305b0dc42c0f30a0c388b49b2493
4+
refs/heads/snap-stage3: 6291702cf334f4b1fe41662bc3b0d996ca7ced19
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/iterator.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,26 @@ pub trait IteratorUtil<A> {
226226
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
227227
-> ScanIterator<'r, A, B, Self, St>;
228228

229+
/// Creates an iterator that maps each element to an iterator,
230+
/// and yields the elements of the produced iterators
231+
///
232+
/// # Example
233+
///
234+
/// ~~~ {.rust}
235+
/// let xs = [2u, 3];
236+
/// let ys = [0u, 1, 0, 1, 2];
237+
/// let mut it = xs.iter().flat_map_(|&x| Counter::new(0u, 1).take_(x));
238+
/// // Check that `it` has the same elements as `ys`
239+
/// let mut i = 0;
240+
/// for it.advance |x: uint| {
241+
/// assert_eq!(x, ys[i]);
242+
/// i += 1;
243+
/// }
244+
/// ~~~
245+
// FIXME: #5898: should be called `flat_map`
246+
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
247+
-> FlatMapIterator<'r, A, B, Self, U>;
248+
229249
/// An adaptation of an external iterator to the for-loop protocol of rust.
230250
///
231251
/// # Example
@@ -397,6 +417,12 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
397417
ScanIterator{iter: self, f: f, state: initial_state}
398418
}
399419

420+
#[inline]
421+
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
422+
-> FlatMapIterator<'r, A, B, T, U> {
423+
FlatMapIterator{iter: self, f: f, subiter: None }
424+
}
425+
400426
/// A shim implementing the `for` loop iteration protocol for iterator objects
401427
#[inline]
402428
fn advance(&mut self, f: &fn(A) -> bool) -> bool {
@@ -873,6 +899,34 @@ impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B,
873899
}
874900
}
875901

902+
/// An iterator that maps each element to an iterator,
903+
/// and yields the elements of the produced iterators
904+
///
905+
// FIXME #6967: Dummy B parameter to get around type inference bug
906+
pub struct FlatMapIterator<'self, A, B, T, U> {
907+
priv iter: T,
908+
priv f: &'self fn(A) -> U,
909+
priv subiter: Option<U>,
910+
}
911+
912+
impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
913+
FlatMapIterator<'self, A, B, T, U> {
914+
#[inline]
915+
fn next(&mut self) -> Option<B> {
916+
loop {
917+
for self.subiter.mut_iter().advance |inner| {
918+
for inner.advance |x| {
919+
return Some(x)
920+
}
921+
}
922+
match self.iter.next().map_consume(self.f) {
923+
None => return None,
924+
next => self.subiter = next,
925+
}
926+
}
927+
}
928+
}
929+
876930
/// An iterator which just modifies the contained state throughout iteration.
877931
pub struct UnfoldrIterator<'self, A, St> {
878932
priv f: &'self fn(&mut St) -> Option<A>,
@@ -1051,6 +1105,19 @@ mod tests {
10511105
assert_eq!(i, ys.len());
10521106
}
10531107

1108+
#[test]
1109+
fn test_iterator_flat_map() {
1110+
let xs = [0u, 3, 6];
1111+
let ys = [0u, 1, 2, 3, 4, 5, 6, 7, 8];
1112+
let mut it = xs.iter().flat_map_(|&x| Counter::new(x, 1).take_(3));
1113+
let mut i = 0;
1114+
for it.advance |x: uint| {
1115+
assert_eq!(x, ys[i]);
1116+
i += 1;
1117+
}
1118+
assert_eq!(i, ys.len());
1119+
}
1120+
10541121
#[test]
10551122
fn test_unfoldr() {
10561123
fn count(st: &mut uint) -> Option<uint> {

0 commit comments

Comments
 (0)