@@ -145,7 +145,7 @@ pub struct BitMatrix {
145
145
}
146
146
147
147
impl BitMatrix {
148
- // Create a new `rows x columns` matrix, initially empty.
148
+ /// Create a new `rows x columns` matrix, initially empty.
149
149
pub fn new ( rows : usize , columns : usize ) -> BitMatrix {
150
150
// For every element, we need one bit for every other
151
151
// element. Round up to an even number of u64s.
@@ -163,29 +163,33 @@ impl BitMatrix {
163
163
( start, start + u64s_per_row)
164
164
}
165
165
166
- pub fn add ( & mut self , source : usize , target : usize ) -> bool {
167
- let ( start, _) = self . range ( source) ;
168
- let ( word, mask) = word_mask ( target) ;
166
+ /// Sets the cell at `(row, column)` to true. Put another way, add
167
+ /// `column` to the bitset for `row`.
168
+ ///
169
+ /// Returns true if this changed the matrix, and false otherwies.
170
+ pub fn add ( & mut self , row : usize , column : usize ) -> bool {
171
+ let ( start, _) = self . range ( row) ;
172
+ let ( word, mask) = word_mask ( column) ;
169
173
let vector = & mut self . vector [ ..] ;
170
174
let v1 = vector[ start + word] ;
171
175
let v2 = v1 | mask;
172
176
vector[ start + word] = v2;
173
177
v1 != v2
174
178
}
175
179
176
- /// Do the bits from `source ` contain `target`?
177
- ///
178
- /// Put another way, if the matrix represents (transitive)
179
- /// reachability, can `source ` reach `target `?
180
- pub fn contains ( & self , source : usize , target : usize ) -> bool {
181
- let ( start, _) = self . range ( source ) ;
182
- let ( word, mask) = word_mask ( target ) ;
180
+ /// Do the bits from `row ` contain `column`? Put another way, is
181
+ /// the matrix cell at `(row, column)` true? Put yet another way,
182
+ /// if the matrix represents (transitive) reachability, can
183
+ /// `row ` reach `column `?
184
+ pub fn contains ( & self , row : usize , column : usize ) -> bool {
185
+ let ( start, _) = self . range ( row ) ;
186
+ let ( word, mask) = word_mask ( column ) ;
183
187
( self . vector [ start + word] & mask) != 0
184
188
}
185
189
186
- /// Returns those indices that are reachable from both `a` and
187
- /// `b`. This is an O(n) operation where `n` is the number of
188
- /// elements (somewhat independent from the actual size of the
190
+ /// Returns those indices that are true in rows `a` and `b`. This
191
+ /// is an O(n) operation where `n` is the number of elements
192
+ /// (somewhat independent from the actual size of the
189
193
/// intersection, in particular).
190
194
pub fn intersection ( & self , a : usize , b : usize ) -> Vec < usize > {
191
195
let ( a_start, a_end) = self . range ( a) ;
@@ -206,7 +210,7 @@ impl BitMatrix {
206
210
result
207
211
}
208
212
209
- /// Add the bits from `read` to the bits from `write`,
213
+ /// Add the bits from row `read` to the bits from row `write`,
210
214
/// return true if anything changed.
211
215
///
212
216
/// This is used when computing transitive reachability because if
@@ -227,6 +231,8 @@ impl BitMatrix {
227
231
changed
228
232
}
229
233
234
+ /// Iterates through all the columns set to true in a given row of
235
+ /// the matrix.
230
236
pub fn iter < ' a > ( & ' a self , row : usize ) -> BitVectorIter < ' a > {
231
237
let ( start, end) = self . range ( row) ;
232
238
BitVectorIter {
0 commit comments