Skip to content

Commit c6c1b61

Browse files
committed
---
yaml --- r: 142983 b: refs/heads/try2 c: 404de4f h: refs/heads/master i: 142981: 2e239b9 142979: 1337bec 142975: 023901f v: v3
1 parent 6aa63c1 commit c6c1b61

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 0d04aa78e5e8a7e3c48141028137c1a69e7e32ee
8+
refs/heads/try2: 404de4f4aed8902e882fda35565a3e6100f6743f
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/iterator.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,17 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
773773
}
774774
}
775775

776+
impl<A, T: DoubleEndedIterator<A>, U: DoubleEndedIterator<A>> DoubleEndedIterator<A>
777+
for ChainIterator<A, T, U> {
778+
#[inline]
779+
fn next_back(&mut self) -> Option<A> {
780+
match self.b.next_back() {
781+
Some(x) => Some(x),
782+
None => self.a.next_back()
783+
}
784+
}
785+
}
786+
776787
/// An iterator which iterates two other iterators simultaneously
777788
// FIXME #6967: Dummy A & B parameters to get around type inference bug
778789
pub struct ZipIterator<A, T, B, U> {
@@ -828,6 +839,17 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
828839
}
829840
}
830841

842+
impl<'self, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B>
843+
for MapIterator<'self, A, B, T> {
844+
#[inline]
845+
fn next_back(&mut self) -> Option<B> {
846+
match self.iter.next_back() {
847+
Some(a) => Some((self.f)(a)),
848+
_ => None
849+
}
850+
}
851+
}
852+
831853
/// An iterator which filters the elements of `iter` with `predicate`
832854
pub struct FilterIterator<'self, A, T> {
833855
priv iter: T,
@@ -854,6 +876,24 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
854876
}
855877
}
856878

879+
impl<'self, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for FilterIterator<'self, A, T> {
880+
#[inline]
881+
fn next_back(&mut self) -> Option<A> {
882+
loop {
883+
match self.iter.next_back() {
884+
None => return None,
885+
Some(x) => {
886+
if (self.predicate)(&x) {
887+
return Some(x);
888+
} else {
889+
loop
890+
}
891+
}
892+
}
893+
}
894+
}
895+
}
896+
857897
/// An iterator which uses `f` to both filter and map elements from `iter`
858898
pub struct FilterMapIterator<'self, A, B, T> {
859899
priv iter: T,
@@ -879,6 +919,24 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
879919
}
880920
}
881921

922+
impl<'self, A, B, T: DoubleEndedIterator<A>> DoubleEndedIterator<B>
923+
for FilterMapIterator<'self, A, B, T> {
924+
#[inline]
925+
fn next_back(&mut self) -> Option<B> {
926+
loop {
927+
match self.iter.next_back() {
928+
None => return None,
929+
Some(x) => {
930+
match (self.f)(x) {
931+
Some(y) => return Some(y),
932+
None => ()
933+
}
934+
}
935+
}
936+
}
937+
}
938+
}
939+
882940
/// An iterator which yields the current count and the element during iteration
883941
// FIXME #6967: Dummy A parameter to get around type inference bug
884942
pub struct EnumerateIterator<A, T> {
@@ -1135,6 +1193,20 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for PeekIterator<'self, A, T> {
11351193
}
11361194
}
11371195

1196+
impl<'self, A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for PeekIterator<'self, A, T> {
1197+
#[inline]
1198+
fn next_back(&mut self) -> Option<A> {
1199+
let next = self.iter.next_back();
1200+
1201+
match next {
1202+
Some(ref a) => (self.f)(a),
1203+
None => ()
1204+
}
1205+
1206+
next
1207+
}
1208+
}
1209+
11381210
/// An iterator which just modifies the contained state throughout iteration.
11391211
pub struct UnfoldrIterator<'self, A, St> {
11401212
priv f: &'self fn(&mut St) -> Option<A>,
@@ -1526,4 +1598,53 @@ mod tests {
15261598
it.next();
15271599
assert_eq!(it.invert().transform(|&x| x).collect::<~[int]>(), ~[16, 14, 12, 10, 8, 6]);
15281600
}
1601+
1602+
#[test]
1603+
fn test_double_ended_map() {
1604+
let xs = [1, 2, 3, 4, 5, 6];
1605+
let mut it = xs.iter().transform(|&x| x * -1);
1606+
assert_eq!(it.next(), Some(-1));
1607+
assert_eq!(it.next(), Some(-2));
1608+
assert_eq!(it.next_back(), Some(-6));
1609+
assert_eq!(it.next_back(), Some(-5));
1610+
assert_eq!(it.next(), Some(-3));
1611+
assert_eq!(it.next_back(), Some(-4));
1612+
assert_eq!(it.next(), None);
1613+
}
1614+
1615+
#[test]
1616+
fn test_double_ended_filter() {
1617+
let xs = [1, 2, 3, 4, 5, 6];
1618+
let mut it = xs.iter().filter(|&x| *x & 1 == 0);
1619+
assert_eq!(it.next_back().unwrap(), &6);
1620+
assert_eq!(it.next_back().unwrap(), &4);
1621+
assert_eq!(it.next().unwrap(), &2);
1622+
assert_eq!(it.next_back(), None);
1623+
}
1624+
1625+
#[test]
1626+
fn test_double_ended_filter_map() {
1627+
let xs = [1, 2, 3, 4, 5, 6];
1628+
let mut it = xs.iter().filter_map(|&x| if x & 1 == 0 { Some(x * 2) } else { None });
1629+
assert_eq!(it.next_back().unwrap(), 12);
1630+
assert_eq!(it.next_back().unwrap(), 8);
1631+
assert_eq!(it.next().unwrap(), 4);
1632+
assert_eq!(it.next_back(), None);
1633+
}
1634+
1635+
#[test]
1636+
fn test_double_ended_chain() {
1637+
let xs = [1, 2, 3, 4, 5];
1638+
let ys = ~[7, 9, 11];
1639+
let mut it = xs.iter().chain_(ys.iter()).invert();
1640+
assert_eq!(it.next().unwrap(), &11)
1641+
assert_eq!(it.next().unwrap(), &9)
1642+
assert_eq!(it.next_back().unwrap(), &1)
1643+
assert_eq!(it.next_back().unwrap(), &2)
1644+
assert_eq!(it.next_back().unwrap(), &3)
1645+
assert_eq!(it.next_back().unwrap(), &4)
1646+
assert_eq!(it.next_back().unwrap(), &5)
1647+
assert_eq!(it.next_back().unwrap(), &7)
1648+
assert_eq!(it.next_back(), None)
1649+
}
15291650
}

0 commit comments

Comments
 (0)