@@ -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
778789pub 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`
832854pub 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`
858898pub 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
884942pub 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.
11391211pub 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