Skip to content

Commit 2166c6d

Browse files
committed
Add comments and unit tests for new SparseBitMatrix methods
1 parent 7e148b0 commit 2166c6d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

compiler/rustc_index/src/bit_set.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,13 +1087,20 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
10871087
self.ensure_row(row).insert(column)
10881088
}
10891089

1090+
/// Sets the cell at `(row, column)` to false. Put another way, delete
1091+
/// `column` from the bitset for `row`. Has no effect if `row` does not
1092+
/// exist.
1093+
///
1094+
/// Returns `true` if this changed the matrix.
10901095
pub fn remove(&mut self, row: R, column: C) -> bool {
10911096
match self.rows.get_mut(row) {
10921097
Some(Some(row)) => row.remove(column),
10931098
_ => false,
10941099
}
10951100
}
10961101

1102+
/// Sets all columns at `row` to false. Has no effect if `row` does
1103+
/// not exist.
10971104
pub fn clear(&mut self, row: R) {
10981105
if let Some(Some(row)) = self.rows.get_mut(row) {
10991106
row.clear();
@@ -1147,6 +1154,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
11471154
if let Some(Some(row)) = self.rows.get(row) { Some(row) } else { None }
11481155
}
11491156

1157+
/// Interescts `row` with `set`. `set` can be either `BitSet` or
1158+
/// `HybridBitSet`. Has no effect if `row` does not exist.
1159+
///
1160+
/// Returns true if the row was changed.
11501161
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
11511162
where
11521163
HybridBitSet<C>: BitRelations<Set>,
@@ -1157,6 +1168,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
11571168
}
11581169
}
11591170

1171+
/// Subtracts `set from `row`. `set` can be either `BitSet` or
1172+
/// `HybridBitSet`. Has no effect if `row` does not exist.
1173+
///
1174+
/// Returns true if the row was changed.
11601175
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
11611176
where
11621177
HybridBitSet<C>: BitRelations<Set>,
@@ -1167,6 +1182,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
11671182
}
11681183
}
11691184

1185+
/// Unions `row` with `set`. `set` can be either `BitSet` or
1186+
/// `HybridBitSet`.
1187+
///
1188+
/// Returns true if the row was changed.
11701189
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
11711190
where
11721191
HybridBitSet<C>: BitRelations<Set>,

compiler/rustc_index/src/bit_set/tests.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,72 @@ fn sparse_matrix_iter() {
304304
assert!(iter.next().is_none());
305305
}
306306

307+
#[test]
308+
fn sparse_matrix_operations() {
309+
let mut matrix: SparseBitMatrix<usize, usize> = SparseBitMatrix::new(100);
310+
matrix.insert(3, 22);
311+
matrix.insert(3, 75);
312+
matrix.insert(2, 99);
313+
matrix.insert(4, 0);
314+
315+
let mut disjoint: HybridBitSet<usize> = HybridBitSet::new_empty(100);
316+
disjoint.insert(33);
317+
318+
let mut superset = HybridBitSet::new_empty(100);
319+
superset.insert(22);
320+
superset.insert(75);
321+
superset.insert(33);
322+
323+
let mut subset = HybridBitSet::new_empty(100);
324+
subset.insert(22);
325+
326+
// SparseBitMatrix::remove
327+
{
328+
let mut matrix = matrix.clone();
329+
matrix.remove(3, 22);
330+
assert!(!matrix.row(3).unwrap().contains(22));
331+
matrix.remove(0, 0);
332+
assert!(matrix.row(0).is_none());
333+
}
334+
335+
// SparseBitMatrix::clear
336+
{
337+
let mut matrix = matrix.clone();
338+
matrix.clear(3);
339+
assert!(!matrix.row(3).unwrap().contains(75));
340+
matrix.clear(0);
341+
assert!(matrix.row(0).is_none());
342+
}
343+
344+
// SparseBitMatrix::intersect_row
345+
{
346+
let mut matrix = matrix.clone();
347+
assert!(!matrix.intersect_row(2, &superset));
348+
assert!(matrix.intersect_row(2, &subset));
349+
matrix.intersect_row(0, &disjoint);
350+
assert!(matrix.row(0).is_none());
351+
}
352+
353+
// SparseBitMatrix::subtract_row
354+
{
355+
let mut matrix = matrix.clone();
356+
assert!(!matrix.subtract_row(2, &disjoint));
357+
assert!(matrix.subtract_row(2, &subset));
358+
assert!(matrix.subtract_row(2, &superset));
359+
matrix.intersect_row(0, &disjoint);
360+
assert!(matrix.row(0).is_none());
361+
}
362+
363+
// SparseBitMatrix::union_row
364+
{
365+
let mut matrix = matrix.clone();
366+
assert!(!matrix.union_row(2, &subset));
367+
assert!(matrix.union_row(2, &disjoint));
368+
matrix.union_row(0, &disjoint);
369+
assert!(matrix.row(0).is_some());
370+
}
371+
}
372+
307373
/// Merge dense hybrid set into empty sparse hybrid set.
308374
#[bench]
309375
fn union_hybrid_sparse_empty_to_dense(b: &mut Bencher) {

0 commit comments

Comments
 (0)