12
12
13
13
14
14
use std:: cmp;
15
+ use std:: iterator:: { DoubleEndedIterator , RandomAccessIterator , Invert } ;
15
16
use std:: num;
16
17
use std:: ops;
17
18
use std:: uint;
18
19
use std:: vec;
19
20
21
+
20
22
#[ deriving( Clone ) ]
21
23
struct SmallBitv {
22
24
/// only the lowest nbits of this value are used. the rest is undefined.
@@ -404,7 +406,7 @@ impl Bitv {
404
406
405
407
#[ inline]
406
408
pub fn iter < ' a > ( & ' a self) -> BitvIterator < ' a > {
407
- BitvIterator { bitv : self , next_idx : 0 }
409
+ BitvIterator { bitv : self , next_idx : 0 , end_idx : self . nbits }
408
410
}
409
411
410
412
/// Returns true if all bits are 0
@@ -564,13 +566,14 @@ fn iterate_bits(base: uint, bits: uint, f: &fn(uint) -> bool) -> bool {
564
566
/// An iterator for Bitv
565
567
pub struct BitvIterator < ' self > {
566
568
priv bitv : & ' self Bitv ,
567
- priv next_idx : uint
569
+ priv next_idx : uint ,
570
+ priv end_idx : uint ,
568
571
}
569
572
570
573
impl < ' self > Iterator < bool > for BitvIterator < ' self > {
571
574
#[ inline]
572
575
fn next ( & mut self ) -> Option < bool > {
573
- if self . next_idx < self . bitv . nbits {
576
+ if self . next_idx != self . end_idx {
574
577
let idx = self . next_idx ;
575
578
self . next_idx += 1 ;
576
579
Some ( self . bitv . get ( idx) )
@@ -580,11 +583,39 @@ impl<'self> Iterator<bool> for BitvIterator<'self> {
580
583
}
581
584
582
585
fn size_hint ( & self ) -> ( uint , Option < uint > ) {
583
- let rem = self . bitv . nbits - self . next_idx ;
586
+ let rem = self . end_idx - self . next_idx ;
584
587
( rem, Some ( rem) )
585
588
}
586
589
}
587
590
591
+ impl < ' self > DoubleEndedIterator < bool > for BitvIterator < ' self > {
592
+ #[ inline]
593
+ fn next_back ( & mut self ) -> Option < bool > {
594
+ if self . next_idx != self . end_idx {
595
+ self . end_idx -= 1 ;
596
+ Some ( self . bitv . get ( self . end_idx ) )
597
+ } else {
598
+ None
599
+ }
600
+ }
601
+ }
602
+
603
+ impl < ' self > RandomAccessIterator < bool > for BitvIterator < ' self > {
604
+ #[ inline]
605
+ fn indexable ( & self ) -> uint {
606
+ self . end_idx - self . next_idx
607
+ }
608
+
609
+ #[ inline]
610
+ fn idx ( & self , index : uint ) -> Option < bool > {
611
+ if index >= self . indexable ( ) {
612
+ None
613
+ } else {
614
+ Some ( self . bitv . get ( index) )
615
+ }
616
+ }
617
+ }
618
+
588
619
/// An implementation of a set using a bit vector as an underlying
589
620
/// representation for holding numerical elements.
590
621
///
0 commit comments