11use std:: ops:: Range ;
22
3+ /// A nasic bitvector trait that we implement for mmap
34pub trait BitVector {
5+ /// Get the value at bit `i`
46 fn get ( & self , i : usize ) -> bool ;
7+ /// Set the value at bit `i`
58 fn set ( & mut self , i : usize , x : bool ) ;
9+ /// Returns the size of the bitvector
610 fn size ( & self ) -> usize ;
711
12+ /// Returns the number of bits sets in the given range
813 fn rank ( & self , r : Range < usize > ) -> usize {
914 r. fold ( 0 , |a, x| a + if self . get ( x) { 1 } else { 0 } )
1015 }
1116
17+ /// Returns the position of the n-th bit set
1218 fn select ( & self , n : usize , start : usize ) -> Option < usize > {
1319 let mut bits_left = n;
1420
@@ -24,6 +30,7 @@ pub trait BitVector {
2430 None
2531 }
2632
33+ /// Return all the bits in the given range as a u128
2734 fn get_range ( & self , r : Range < usize > ) -> u128 {
2835 if r. end - r. start > 128 {
2936 panic ! ( "Range too large (>128)" )
@@ -42,6 +49,7 @@ pub trait BitVector {
4249 bvs
4350 }
4451
52+ /// Sets all the bits in the given range from the given u128
4553 fn set_range ( & mut self , r : Range < usize > , x : u128 ) {
4654 let mut cur = x;
4755 for i in r. rev ( ) {
@@ -50,6 +58,7 @@ pub trait BitVector {
5058 }
5159 }
5260
61+ /// Sets all the bit in the given range to false
5362 fn clear_range ( & mut self , r : Range < usize > ) {
5463 for i in r. rev ( ) {
5564 self . set ( i, false ) ;
@@ -128,5 +137,3 @@ impl BitVector for Vec<u8> {
128137 self . len ( ) / 8
129138 }
130139}
131-
132- // TODO: impl for `bv::BitVec` and `bit-vec::BitVec`?
0 commit comments