@@ -26,7 +26,6 @@ use option::{Option, Some, None};
26
26
use ops:: { Add , Mul } ;
27
27
use cmp:: Ord ;
28
28
use clone:: Clone ;
29
- use uint;
30
29
31
30
/// Conversion from an `Iterator`
32
31
pub trait FromIterator < A , T : Iterator < A > > {
@@ -44,7 +43,7 @@ pub trait Iterator<A> {
44
43
/// Return a lower bound and upper bound on the remaining length of the iterator.
45
44
///
46
45
/// The common use case for the estimate is pre-allocating space to store the results.
47
- fn size_hint ( & self ) -> ( uint , Option < uint > ) { ( 0 , None ) }
46
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) { ( None , None ) }
48
47
}
49
48
50
49
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -685,18 +684,18 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
685
684
}
686
685
687
686
#[ inline]
688
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
687
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
689
688
let ( a_lower, a_upper) = self . a . size_hint ( ) ;
690
689
let ( b_lower, b_upper) = self . b . size_hint ( ) ;
691
690
692
- let lower = if uint:: max_value - a_lower < b_lower {
693
- uint:: max_value
694
- } else {
695
- a_lower + b_lower
691
+ let lower = match ( a_lower, b_lower) {
692
+ ( Some ( x) , Some ( y) ) => Some ( x + y) ,
693
+ ( Some ( x) , None ) => Some ( x) ,
694
+ ( None , Some ( y) ) => Some ( y) ,
695
+ ( None , None ) => None
696
696
} ;
697
697
698
698
let upper = match ( a_upper, b_upper) {
699
- ( Some ( x) , Some ( y) ) if uint:: max_value - x < y => Some ( uint:: max_value) ,
700
699
( Some ( x) , Some ( y) ) => Some ( x + y) ,
701
700
_ => None
702
701
} ;
@@ -720,23 +719,6 @@ impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<A, T
720
719
_ => None
721
720
}
722
721
}
723
-
724
- #[ inline]
725
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
726
- let ( a_lower, a_upper) = self . a . size_hint ( ) ;
727
- let ( b_lower, b_upper) = self . b . size_hint ( ) ;
728
-
729
- let lower = cmp:: min ( a_lower, b_lower) ;
730
-
731
- let upper = match ( a_upper, b_upper) {
732
- ( Some ( x) , Some ( y) ) => Some ( cmp:: min ( x, y) ) ,
733
- ( Some ( x) , None ) => Some ( x) ,
734
- ( None , Some ( y) ) => Some ( y) ,
735
- ( None , None ) => None
736
- } ;
737
-
738
- ( lower, upper)
739
- }
740
722
}
741
723
742
724
/// An iterator which maps the values of `iter` with `f`
@@ -755,7 +737,7 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
755
737
}
756
738
757
739
#[ inline]
758
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
740
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
759
741
self . iter . size_hint ( )
760
742
}
761
743
}
@@ -780,9 +762,9 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
780
762
}
781
763
782
764
#[ inline]
783
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
765
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
784
766
let ( _, upper) = self . iter . size_hint ( ) ;
785
- ( 0 , upper) // can't know a lower bound, due to the predicate
767
+ ( None , upper) // can't know a lower bound, due to the predicate
786
768
}
787
769
}
788
770
@@ -805,9 +787,9 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
805
787
}
806
788
807
789
#[ inline]
808
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
790
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
809
791
let ( _, upper) = self . iter . size_hint ( ) ;
810
- ( 0 , upper) // can't know a lower bound, due to the predicate
792
+ ( None , upper) // can't know a lower bound, due to the predicate
811
793
}
812
794
}
813
795
@@ -830,11 +812,6 @@ impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<A, T> {
830
812
_ => None
831
813
}
832
814
}
833
-
834
- #[ inline]
835
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
836
- self . iter . size_hint ( )
837
- }
838
815
}
839
816
840
817
/// An iterator which rejects elements while `predicate` is true
@@ -867,12 +844,6 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for SkipWhileIterator<'self, A, T> {
867
844
}
868
845
}
869
846
}
870
-
871
- #[ inline]
872
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
873
- let ( _, upper) = self . iter . size_hint ( ) ;
874
- ( 0 , upper) // can't know a lower bound, due to the predicate
875
- }
876
847
}
877
848
878
849
/// An iterator which only accepts elements while `predicate` is true
@@ -901,12 +872,6 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for TakeWhileIterator<'self, A, T> {
901
872
}
902
873
}
903
874
}
904
-
905
- #[ inline]
906
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
907
- let ( _, upper) = self . iter . size_hint ( ) ;
908
- ( 0 , upper) // can't know a lower bound, due to the predicate
909
- }
910
875
}
911
876
912
877
/// An iterator which skips over `n` elements of `iter`.
@@ -940,21 +905,6 @@ impl<A, T: Iterator<A>> Iterator<A> for SkipIterator<A, T> {
940
905
next
941
906
}
942
907
}
943
-
944
- #[ inline]
945
- fn size_hint( & self ) -> ( uint, Option < uint > ) {
946
- let ( lower, upper) = self . iter. size_hint( ) ;
947
-
948
- let lower = if lower >= self . n { lower - self . n } else { 0 } ;
949
-
950
- let upper = match upper {
951
- Some ( x) if x >= self . n => Some ( x - self . n) ,
952
- Some ( _) => Some ( 0 ) ,
953
- None => None
954
- } ;
955
-
956
- ( lower, upper)
957
- }
958
908
}
959
909
960
910
/// An iterator which only iterates over the first `n` iterations of `iter`.
@@ -975,20 +925,6 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<A, T> {
975
925
None
976
926
}
977
927
}
978
-
979
- #[ inline]
980
- fn size_hint( & self ) -> ( uint, Option < uint > ) {
981
- let ( lower, upper) = self . iter. size_hint( ) ;
982
-
983
- let lower = cmp:: min( lower, self . n) ;
984
-
985
- let upper = match upper {
986
- Some ( x) if x < self . n => Some ( x ) ,
987
- _ => Some ( self . n)
988
- } ;
989
-
990
- ( lower , upper )
991
- }
992
928
}
993
929
994
930
/// An iterator to maintain state while iterating another iterator
@@ -1005,12 +941,6 @@ impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B,
1005
941
fn next( & mut self ) -> Option < B > {
1006
942
self . iter. next( ) . chain( |a| ( self . f) ( & mut self . state, a) )
1007
943
}
1008
-
1009
- #[ inline]
1010
- fn size_hint( & self ) -> ( uint, Option < uint > ) {
1011
- let ( _, upper) = self . iter. size_hint( ) ;
1012
- ( 0 , upper) // can't know a lower bound, due to the scan function
1013
- }
1014
944
}
1015
945
1016
946
/// An iterator that maps each element to an iterator,
@@ -1092,11 +1022,6 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
1092
1022
self . state = self . state. add( & self . step) ; // FIXME: #6050
1093
1023
Some ( result)
1094
1024
}
1095
-
1096
- #[ inline]
1097
- fn size_hint( & self ) -> ( uint, Option < uint > ) {
1098
- ( uint:: max_value, None ) // Too bad we can't specify an infinite lower bound
1099
- }
1100
1025
}
1101
1026
1102
1027
#[ cfg( test) ]
@@ -1312,43 +1237,6 @@ mod tests {
1312
1237
assert_eq ! ( v. slice( 0 , 0 ) . iter( ) . transform( |& x| x) . min( ) , None ) ;
1313
1238
}
1314
1239
1315
- #[ test]
1316
- fn test_iterator_size_hint ( ) {
1317
- let c = Counter : : new( 0 , 1 ) ;
1318
- let v = & [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
1319
- let v2 = & [ 10 , 11 , 12 ] ;
1320
- let vi = v. iter( ) ;
1321
-
1322
- assert_eq ! ( c. size_hint( ) , ( uint:: max_value, None ) ) ;
1323
- assert_eq ! ( vi. size_hint( ) , ( 10 , Some ( 10 ) ) ) ;
1324
-
1325
- assert_eq ! ( c. take_( 5 ) . size_hint( ) , ( 5 , Some ( 5 ) ) ) ;
1326
- assert_eq ! ( c. skip( 5 ) . size_hint( ) . second( ) , None ) ;
1327
- assert_eq ! ( c. take_while( |_| false ) . size_hint( ) , ( 0 , None ) ) ;
1328
- assert_eq ! ( c. skip_while( |_| false ) . size_hint( ) , ( 0 , None ) ) ;
1329
- assert_eq ! ( c. enumerate( ) . size_hint( ) , ( uint:: max_value, None ) ) ;
1330
- assert_eq ! ( c. chain_( vi. transform( |& i| i) ) . size_hint( ) , ( uint:: max_value, None ) ) ;
1331
- assert_eq ! ( c. zip( vi) . size_hint( ) , ( 10 , Some ( 10 ) ) ) ;
1332
- assert_eq ! ( c. scan( 0 , |_, _| Some ( 0 ) ) . size_hint( ) , ( 0 , None ) ) ;
1333
- assert_eq ! ( c. filter( |_| false ) . size_hint( ) , ( 0 , None ) ) ;
1334
- assert_eq ! ( c. transform( |_| 0 ) . size_hint( ) , ( uint:: max_value, None ) ) ;
1335
- assert_eq ! ( c. filter_map( |_| Some ( 0 ) ) . size_hint( ) , ( 0 , None ) ) ;
1336
-
1337
- assert_eq ! ( vi. take_( 5 ) . size_hint( ) , ( 5 , Some ( 5 ) ) ) ;
1338
- assert_eq ! ( vi. take_( 12 ) . size_hint( ) , ( 10 , Some ( 10 ) ) ) ;
1339
- assert_eq ! ( vi. skip( 3 ) . size_hint( ) , ( 7 , Some ( 7 ) ) ) ;
1340
- assert_eq ! ( vi. skip( 12 ) . size_hint( ) , ( 0 , Some ( 0 ) ) ) ;
1341
- assert_eq ! ( vi. take_while( |_| false ) . size_hint( ) , ( 0 , Some ( 10 ) ) ) ;
1342
- assert_eq ! ( vi. skip_while( |_| false ) . size_hint( ) , ( 0 , Some ( 10 ) ) ) ;
1343
- assert_eq ! ( vi. enumerate( ) . size_hint( ) , ( 10 , Some ( 10 ) ) ) ;
1344
- assert_eq ! ( vi. chain_( v2. iter( ) ) . size_hint( ) , ( 13 , Some ( 13 ) ) ) ;
1345
- assert_eq ! ( vi. zip( v2. iter( ) ) . size_hint( ) , ( 3 , Some ( 3 ) ) ) ;
1346
- assert_eq ! ( vi. scan( 0 , |_, _| Some ( 0 ) ) . size_hint( ) , ( 0 , Some ( 10 ) ) ) ;
1347
- assert_eq ! ( vi. filter( |_| false ) . size_hint( ) , ( 0 , Some ( 10 ) ) ) ;
1348
- assert_eq ! ( vi. transform( |i| i+1 ) . size_hint( ) , ( 10 , Some ( 10 ) ) ) ;
1349
- assert_eq ! ( vi. filter_map( |_| Some ( 0 ) ) . size_hint( ) , ( 0 , Some ( 10 ) ) ) ;
1350
- }
1351
-
1352
1240
#[ test]
1353
1241
fn test_collect( ) {
1354
1242
let a = ~[ 1 , 2 , 3 , 4 , 5 ] ;
0 commit comments