@@ -392,28 +392,23 @@ impl Bitv {
392
392
match self . rep {
393
393
Small ( ref b) => b. is_true ( self . nbits ) ,
394
394
_ => {
395
- for self . each ( ) |i| { if !i { return false ; } }
395
+ for self . iter ( ) . advance |i| { if !i { return false; } }
396
396
true
397
397
}
398
398
}
399
399
}
400
400
401
401
#[ inline]
402
- pub fn each ( & self , f : & fn ( bool ) -> bool ) -> bool {
403
- let mut i = 0 ;
404
- while i < self . nbits {
405
- if !f ( self . get ( i) ) { return false ; }
406
- i += 1 ;
407
- }
408
- return true ;
402
+ pub fn iter < ' a > ( & ' a self) -> BitvIterator < ' a > {
403
+ BitvIterator { bitv : self , next_idx : 0 }
409
404
}
410
405
411
406
/// Returns true if all bits are 0
412
407
pub fn is_false ( & self ) -> bool {
413
408
match self . rep {
414
409
Small ( ref b) => b. is_false ( self . nbits ) ,
415
410
Big ( _) => {
416
- for self . each ( ) |i| { if i { return false ; } }
411
+ for self . iter ( ) . advance |i| { if i { return false; } }
417
412
true
418
413
}
419
414
}
@@ -477,7 +472,7 @@ impl Bitv {
477
472
*/
478
473
pub fn to_str ( & self ) -> ~str {
479
474
let mut rs = ~"";
480
- for self . each ( ) |i| {
475
+ for self . iter ( ) . advance |i| {
481
476
if i {
482
477
rs. push_char ( '1' ) ;
483
478
} else {
@@ -580,6 +575,29 @@ fn iterate_bits(base: uint, bits: uint, f: &fn(uint) -> bool) -> bool {
580
575
return true ;
581
576
}
582
577
578
+ /// An iterator for Bitv
579
+ pub struct BitvIterator < ' self > {
580
+ priv bitv : & ' self Bitv ,
581
+ priv next_idx : uint
582
+ }
583
+
584
+ impl < ' self > Iterator < bool > for BitvIterator < ' self > {
585
+ fn next ( & mut self ) -> Option < bool > {
586
+ if self . next_idx < self . bitv . nbits {
587
+ let idx = self . next_idx ;
588
+ self . next_idx += 1 ;
589
+ Some ( self . bitv . get ( idx) )
590
+ } else {
591
+ None
592
+ }
593
+ }
594
+
595
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
596
+ let rem = self . bitv . nbits - self . next_idx ;
597
+ ( rem, Some ( rem) )
598
+ }
599
+ }
600
+
583
601
/// An implementation of a set using a bit vector as an underlying
584
602
/// representation for holding numerical elements.
585
603
///
@@ -670,13 +688,8 @@ impl BitvSet {
670
688
self . other_op ( other, |w1, w2| w1 ^ w2) ;
671
689
}
672
690
673
- pub fn each ( & self , blk : & fn ( v : & uint ) -> bool ) -> bool {
674
- for self . bitv. storage. iter( ) . enumerate( ) . advance |( i, & w) | {
675
- if !iterate_bits( i * uint:: bits, w, |b| blk( & b) ) {
676
- return false ;
677
- }
678
- }
679
- return true ;
691
+ pub fn iter < ' a > ( & ' a self ) -> BitvSetIterator < ' a > {
692
+ BitvSetIterator { set : self , next_idx : 0 }
680
693
}
681
694
}
682
695
@@ -860,6 +873,30 @@ impl BitvSet {
860
873
}
861
874
}
862
875
876
+ pub struct BitvSetIterator < ' self > {
877
+ priv set: & ' self BitvSet ,
878
+ priv next_idx: uint
879
+ }
880
+
881
+ impl < ' self > Iterator < uint > for BitvSetIterator < ' self > {
882
+ fn next( & mut self ) -> Option < uint > {
883
+ while self . next_idx < self . set. capacity ( ) {
884
+ let idx = self . next_idx ;
885
+ self . next_idx += 1 ;
886
+
887
+ if self . set . contains ( & idx) {
888
+ return Some ( idx) ;
889
+ }
890
+ }
891
+
892
+ return None ;
893
+ }
894
+
895
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
896
+ ( 0 , Some ( self . set . capacity ( ) - self . next_idx ) )
897
+ }
898
+ }
899
+
863
900
#[ cfg( test) ]
864
901
mod tests {
865
902
use extra:: test:: BenchHarness ;
0 commit comments