1010
1111use std:: iter:: FromIterator ;
1212
13+ type Word = u128 ;
14+ const WORD_BITS : usize = 128 ;
15+
1316/// A very simple BitVector type.
1417#[ derive( Clone , Debug , PartialEq ) ]
1518pub struct BitVector {
16- data : Vec < u64 > ,
19+ data : Vec < Word > ,
1720}
1821
1922impl BitVector {
2023 #[ inline]
2124 pub fn new ( num_bits : usize ) -> BitVector {
22- let num_words = u64s ( num_bits) ;
25+ let num_words = words ( num_bits) ;
2326 BitVector { data : vec ! [ 0 ; num_words] }
2427 }
2528
@@ -78,7 +81,7 @@ impl BitVector {
7881
7982 #[ inline]
8083 pub fn grow ( & mut self , num_bits : usize ) {
81- let num_words = u64s ( num_bits) ;
84+ let num_words = words ( num_bits) ;
8285 if self . data . len ( ) < num_words {
8386 self . data . resize ( num_words, 0 )
8487 }
@@ -96,8 +99,8 @@ impl BitVector {
9699}
97100
98101pub struct BitVectorIter < ' a > {
99- iter : :: std:: slice:: Iter < ' a , u64 > ,
100- current : u64 ,
102+ iter : :: std:: slice:: Iter < ' a , Word > ,
103+ current : Word ,
101104 idx : usize ,
102105}
103106
@@ -107,10 +110,10 @@ impl<'a> Iterator for BitVectorIter<'a> {
107110 while self . current == 0 {
108111 self . current = if let Some ( & i) = self . iter . next ( ) {
109112 if i == 0 {
110- self . idx += 64 ;
113+ self . idx += WORD_BITS ;
111114 continue ;
112115 } else {
113- self . idx = u64s ( self . idx ) * 64 ;
116+ self . idx = words ( self . idx ) * WORD_BITS ;
114117 i
115118 }
116119 } else {
@@ -129,9 +132,9 @@ impl FromIterator<bool> for BitVector {
129132 fn from_iter < I > ( iter : I ) -> BitVector where I : IntoIterator < Item =bool > {
130133 let iter = iter. into_iter ( ) ;
131134 let ( len, _) = iter. size_hint ( ) ;
132- // Make the minimum length for the bitvector 64 bits since that's
135+ // Make the minimum length for the bitvector WORD_BITS bits since that's
133136 // the smallest non-zero size anyway.
134- let len = if len < 64 { 64 } else { len } ;
137+ let len = if len < WORD_BITS { WORD_BITS } else { len } ;
135138 let mut bv = BitVector :: new ( len) ;
136139 for ( idx, val) in iter. enumerate ( ) {
137140 if idx > len {
@@ -152,26 +155,26 @@ impl FromIterator<bool> for BitVector {
152155#[ derive( Clone , Debug ) ]
153156pub struct BitMatrix {
154157 columns : usize ,
155- vector : Vec < u64 > ,
158+ vector : Vec < Word > ,
156159}
157160
158161impl BitMatrix {
159162 /// Create a new `rows x columns` matrix, initially empty.
160163 pub fn new ( rows : usize , columns : usize ) -> BitMatrix {
161164 // For every element, we need one bit for every other
162- // element. Round up to an even number of u64s .
163- let u64s_per_row = u64s ( columns) ;
165+ // element. Round up to an even number of words .
166+ let words_per_row = words ( columns) ;
164167 BitMatrix {
165168 columns,
166- vector : vec ! [ 0 ; rows * u64s_per_row ] ,
169+ vector : vec ! [ 0 ; rows * words_per_row ] ,
167170 }
168171 }
169172
170173 /// The range of bits for a given row.
171174 fn range ( & self , row : usize ) -> ( usize , usize ) {
172- let u64s_per_row = u64s ( self . columns ) ;
173- let start = row * u64s_per_row ;
174- ( start, start + u64s_per_row )
175+ let words_per_row = words ( self . columns ) ;
176+ let start = row * words_per_row ;
177+ ( start, start + words_per_row )
175178 }
176179
177180 /// Sets the cell at `(row, column)` to true. Put another way, add
@@ -208,12 +211,12 @@ impl BitMatrix {
208211 let mut result = Vec :: with_capacity ( self . columns ) ;
209212 for ( base, ( i, j) ) in ( a_start..a_end) . zip ( b_start..b_end) . enumerate ( ) {
210213 let mut v = self . vector [ i] & self . vector [ j] ;
211- for bit in 0 ..64 {
214+ for bit in 0 ..WORD_BITS {
212215 if v == 0 {
213216 break ;
214217 }
215218 if v & 0x1 != 0 {
216- result. push ( base * 64 + bit) ;
219+ result. push ( base * WORD_BITS + bit) ;
217220 }
218221 v >>= 1 ;
219222 }
@@ -255,14 +258,14 @@ impl BitMatrix {
255258}
256259
257260#[ inline]
258- fn u64s ( elements : usize ) -> usize {
259- ( elements + 63 ) / 64
261+ fn words ( elements : usize ) -> usize {
262+ ( elements + WORD_BITS - 1 ) / WORD_BITS
260263}
261264
262265#[ inline]
263- fn word_mask ( index : usize ) -> ( usize , u64 ) {
264- let word = index / 64 ;
265- let mask = 1 << ( index % 64 ) ;
266+ fn word_mask ( index : usize ) -> ( usize , Word ) {
267+ let word = index / WORD_BITS ;
268+ let mask = 1 << ( index % WORD_BITS ) ;
266269 ( word, mask)
267270}
268271
0 commit comments