10
10
11
11
use std:: iter:: FromIterator ;
12
12
13
+ type Word = u128 ;
14
+ const WORD_BITS : usize = 128 ;
15
+
13
16
/// A very simple BitVector type.
14
17
#[ derive( Clone , Debug , PartialEq ) ]
15
18
pub struct BitVector {
16
- data : Vec < u64 > ,
19
+ data : Vec < Word > ,
17
20
}
18
21
19
22
impl BitVector {
20
23
#[ inline]
21
24
pub fn new ( num_bits : usize ) -> BitVector {
22
- let num_words = u64s ( num_bits) ;
25
+ let num_words = words ( num_bits) ;
23
26
BitVector { data : vec ! [ 0 ; num_words] }
24
27
}
25
28
@@ -78,7 +81,7 @@ impl BitVector {
78
81
79
82
#[ inline]
80
83
pub fn grow ( & mut self , num_bits : usize ) {
81
- let num_words = u64s ( num_bits) ;
84
+ let num_words = words ( num_bits) ;
82
85
if self . data . len ( ) < num_words {
83
86
self . data . resize ( num_words, 0 )
84
87
}
@@ -96,8 +99,8 @@ impl BitVector {
96
99
}
97
100
98
101
pub struct BitVectorIter < ' a > {
99
- iter : :: std:: slice:: Iter < ' a , u64 > ,
100
- current : u64 ,
102
+ iter : :: std:: slice:: Iter < ' a , Word > ,
103
+ current : Word ,
101
104
idx : usize ,
102
105
}
103
106
@@ -107,10 +110,10 @@ impl<'a> Iterator for BitVectorIter<'a> {
107
110
while self . current == 0 {
108
111
self . current = if let Some ( & i) = self . iter . next ( ) {
109
112
if i == 0 {
110
- self . idx += 64 ;
113
+ self . idx += WORD_BITS ;
111
114
continue ;
112
115
} else {
113
- self . idx = u64s ( self . idx ) * 64 ;
116
+ self . idx = words ( self . idx ) * WORD_BITS ;
114
117
i
115
118
}
116
119
} else {
@@ -129,9 +132,9 @@ impl FromIterator<bool> for BitVector {
129
132
fn from_iter < I > ( iter : I ) -> BitVector where I : IntoIterator < Item =bool > {
130
133
let iter = iter. into_iter ( ) ;
131
134
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
133
136
// 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 } ;
135
138
let mut bv = BitVector :: new ( len) ;
136
139
for ( idx, val) in iter. enumerate ( ) {
137
140
if idx > len {
@@ -152,26 +155,26 @@ impl FromIterator<bool> for BitVector {
152
155
#[ derive( Clone , Debug ) ]
153
156
pub struct BitMatrix {
154
157
columns : usize ,
155
- vector : Vec < u64 > ,
158
+ vector : Vec < Word > ,
156
159
}
157
160
158
161
impl BitMatrix {
159
162
/// Create a new `rows x columns` matrix, initially empty.
160
163
pub fn new ( rows : usize , columns : usize ) -> BitMatrix {
161
164
// 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) ;
164
167
BitMatrix {
165
168
columns,
166
- vector : vec ! [ 0 ; rows * u64s_per_row ] ,
169
+ vector : vec ! [ 0 ; rows * words_per_row ] ,
167
170
}
168
171
}
169
172
170
173
/// The range of bits for a given row.
171
174
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 )
175
178
}
176
179
177
180
/// Sets the cell at `(row, column)` to true. Put another way, add
@@ -208,12 +211,12 @@ impl BitMatrix {
208
211
let mut result = Vec :: with_capacity ( self . columns ) ;
209
212
for ( base, ( i, j) ) in ( a_start..a_end) . zip ( b_start..b_end) . enumerate ( ) {
210
213
let mut v = self . vector [ i] & self . vector [ j] ;
211
- for bit in 0 ..64 {
214
+ for bit in 0 ..WORD_BITS {
212
215
if v == 0 {
213
216
break ;
214
217
}
215
218
if v & 0x1 != 0 {
216
- result. push ( base * 64 + bit) ;
219
+ result. push ( base * WORD_BITS + bit) ;
217
220
}
218
221
v >>= 1 ;
219
222
}
@@ -255,14 +258,14 @@ impl BitMatrix {
255
258
}
256
259
257
260
#[ inline]
258
- fn u64s ( elements : usize ) -> usize {
259
- ( elements + 63 ) / 64
261
+ fn words ( elements : usize ) -> usize {
262
+ ( elements + WORD_BITS - 1 ) / WORD_BITS
260
263
}
261
264
262
265
#[ 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 ) ;
266
269
( word, mask)
267
270
}
268
271
0 commit comments