1
1
use std:: ops:: Range ;
2
2
3
+ /// A nasic bitvector trait that we implement for mmap
3
4
pub trait BitVector {
5
+ /// Get the value at bit `i`
4
6
fn get ( & self , i : usize ) -> bool ;
7
+ /// Set the value at bit `i`
5
8
fn set ( & mut self , i : usize , x : bool ) ;
9
+ /// Returns the size of the bitvector
6
10
fn size ( & self ) -> usize ;
7
11
12
+ /// Returns the number of bits sets in the given range
8
13
fn rank ( & self , r : Range < usize > ) -> usize {
9
14
r. fold ( 0 , |a, x| a + if self . get ( x) { 1 } else { 0 } )
10
15
}
11
16
17
+ /// Returns the position of the n-th bit set
12
18
fn select ( & self , n : usize , start : usize ) -> Option < usize > {
13
19
let mut bits_left = n;
14
20
@@ -24,6 +30,7 @@ pub trait BitVector {
24
30
None
25
31
}
26
32
33
+ /// Return all the bits in the given range as a u128
27
34
fn get_range ( & self , r : Range < usize > ) -> u128 {
28
35
if r. end - r. start > 128 {
29
36
panic ! ( "Range too large (>128)" )
@@ -42,6 +49,7 @@ pub trait BitVector {
42
49
bvs
43
50
}
44
51
52
+ /// Sets all the bits in the given range from the given u128
45
53
fn set_range ( & mut self , r : Range < usize > , x : u128 ) {
46
54
let mut cur = x;
47
55
for i in r. rev ( ) {
@@ -50,6 +58,7 @@ pub trait BitVector {
50
58
}
51
59
}
52
60
61
+ /// Sets all the bit in the given range to false
53
62
fn clear_range ( & mut self , r : Range < usize > ) {
54
63
for i in r. rev ( ) {
55
64
self . set ( i, false ) ;
@@ -128,5 +137,3 @@ impl BitVector for Vec<u8> {
128
137
self . len ( ) / 8
129
138
}
130
139
}
131
-
132
- // TODO: impl for `bv::BitVec` and `bit-vec::BitVec`?
0 commit comments