@@ -26,6 +26,7 @@ use option::{Option, Some, None};
26
26
use ops:: { Add , Mul } ;
27
27
use cmp:: Ord ;
28
28
use clone:: Clone ;
29
+ use uint;
29
30
30
31
/// Conversion from an `Iterator`
31
32
pub trait FromIterator < A , T : Iterator < A > > {
@@ -43,7 +44,7 @@ pub trait Iterator<A> {
43
44
/// Return a lower bound and upper bound on the remaining length of the iterator.
44
45
///
45
46
/// The common use case for the estimate is pre-allocating space to store the results.
46
- fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) { ( None , None ) }
47
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { ( 0 , None ) }
47
48
}
48
49
49
50
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -684,18 +685,18 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
684
685
}
685
686
686
687
#[ inline]
687
- fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
688
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
688
689
let ( a_lower, a_upper) = self . a . size_hint ( ) ;
689
690
let ( b_lower, b_upper) = self . b . size_hint ( ) ;
690
691
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
692
+ let lower = if uint:: max_value - a_lower < b_lower {
693
+ uint:: max_value
694
+ } else {
695
+ a_lower + b_lower
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) ,
699
700
( Some ( x) , Some ( y) ) => Some ( x + y) ,
700
701
_ => None
701
702
} ;
@@ -719,6 +720,23 @@ impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<A, T
719
720
_ => None
720
721
}
721
722
}
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
+ }
722
740
}
723
741
724
742
/// An iterator which maps the values of `iter` with `f`
@@ -737,7 +755,7 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
737
755
}
738
756
739
757
#[ inline]
740
- fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
758
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
741
759
self . iter . size_hint ( )
742
760
}
743
761
}
@@ -762,9 +780,9 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
762
780
}
763
781
764
782
#[ inline]
765
- fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
783
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
766
784
let ( _, upper) = self . iter . size_hint ( ) ;
767
- ( None , upper) // can't know a lower bound, due to the predicate
785
+ ( 0 , upper) // can't know a lower bound, due to the predicate
768
786
}
769
787
}
770
788
@@ -787,9 +805,9 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
787
805
}
788
806
789
807
#[ inline]
790
- fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
808
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
791
809
let ( _, upper) = self . iter . size_hint ( ) ;
792
- ( None , upper) // can't know a lower bound, due to the predicate
810
+ ( 0 , upper) // can't know a lower bound, due to the predicate
793
811
}
794
812
}
795
813
@@ -812,6 +830,11 @@ impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<A, T> {
812
830
_ => None
813
831
}
814
832
}
833
+
834
+ #[ inline]
835
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
836
+ self . iter . size_hint ( )
837
+ }
815
838
}
816
839
817
840
/// An iterator which rejects elements while `predicate` is true
@@ -844,6 +867,12 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for SkipWhileIterator<'self, A, T> {
844
867
}
845
868
}
846
869
}
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
+ }
847
876
}
848
877
849
878
/// An iterator which only accepts elements while `predicate` is true
@@ -872,6 +901,12 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for TakeWhileIterator<'self, A, T> {
872
901
}
873
902
}
874
903
}
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
+ }
875
910
}
876
911
877
912
/// An iterator which skips over `n` elements of `iter`.
@@ -905,6 +940,21 @@ impl<A, T: Iterator<A>> Iterator<A> for SkipIterator<A, T> {
905
940
next
906
941
}
907
942
}
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
+ }
908
958
}
909
959
910
960
/// An iterator which only iterates over the first `n` iterations of `iter`.
@@ -925,6 +975,20 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<A, T> {
925
975
None
926
976
}
927
977
}
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
+ }
928
992
}
929
993
930
994
/// An iterator to maintain state while iterating another iterator
@@ -941,6 +1005,12 @@ impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B,
941
1005
fn next( & mut self ) -> Option < B > {
942
1006
self . iter. next( ) . chain( |a| ( self . f) ( & mut self . state, a) )
943
1007
}
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
+ }
944
1014
}
945
1015
946
1016
/// An iterator that maps each element to an iterator,
@@ -1022,6 +1092,11 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
1022
1092
self . state = self . state. add( & self . step) ; // FIXME: #6050
1023
1093
Some ( result)
1024
1094
}
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
+ }
1025
1100
}
1026
1101
1027
1102
#[ cfg( test) ]
@@ -1237,6 +1312,43 @@ mod tests {
1237
1312
assert_eq ! ( v. slice( 0 , 0 ) . iter( ) . transform( |& x| x) . min( ) , None ) ;
1238
1313
}
1239
1314
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
+
1240
1352
#[ test]
1241
1353
fn test_collect( ) {
1242
1354
let a = ~[ 1 , 2 , 3 , 4 , 5 ] ;
0 commit comments