@@ -773,6 +773,17 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
773
773
}
774
774
}
775
775
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
+
776
787
/// An iterator which iterates two other iterators simultaneously
777
788
// FIXME #6967: Dummy A & B parameters to get around type inference bug
778
789
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> {
828
839
}
829
840
}
830
841
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
+
831
853
/// An iterator which filters the elements of `iter` with `predicate`
832
854
pub struct FilterIterator < ' self , A , T > {
833
855
priv iter : T ,
@@ -854,6 +876,24 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
854
876
}
855
877
}
856
878
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
+
857
897
/// An iterator which uses `f` to both filter and map elements from `iter`
858
898
pub struct FilterMapIterator < ' self , A , B , T > {
859
899
priv iter : T ,
@@ -879,6 +919,24 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
879
919
}
880
920
}
881
921
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
+
882
940
/// An iterator which yields the current count and the element during iteration
883
941
// FIXME #6967: Dummy A parameter to get around type inference bug
884
942
pub struct EnumerateIterator < A , T > {
@@ -1135,6 +1193,20 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for PeekIterator<'self, A, T> {
1135
1193
}
1136
1194
}
1137
1195
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
+
1138
1210
/// An iterator which just modifies the contained state throughout iteration.
1139
1211
pub struct UnfoldrIterator < ' self , A , St > {
1140
1212
priv f: & ' self fn( & mut St ) -> Option < A > ,
@@ -1526,4 +1598,53 @@ mod tests {
1526
1598
it. next ( ) ;
1527
1599
assert_eq ! ( it. invert( ) . transform( |& x| x) . collect:: <~[ int] >( ) , ~[ 16 , 14 , 12 , 10 , 8 , 6 ] ) ;
1528
1600
}
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
+ }
1529
1650
}
0 commit comments