mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-21 22:34:05 +00:00
Add comments and unit tests for new SparseBitMatrix methods
This commit is contained in:
parent
7e148b0cef
commit
2166c6db43
@ -1087,6 +1087,11 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||
self.ensure_row(row).insert(column)
|
||||
}
|
||||
|
||||
/// Sets the cell at `(row, column)` to false. Put another way, delete
|
||||
/// `column` from the bitset for `row`. Has no effect if `row` does not
|
||||
/// exist.
|
||||
///
|
||||
/// Returns `true` if this changed the matrix.
|
||||
pub fn remove(&mut self, row: R, column: C) -> bool {
|
||||
match self.rows.get_mut(row) {
|
||||
Some(Some(row)) => row.remove(column),
|
||||
@ -1094,6 +1099,8 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets all columns at `row` to false. Has no effect if `row` does
|
||||
/// not exist.
|
||||
pub fn clear(&mut self, row: R) {
|
||||
if let Some(Some(row)) = self.rows.get_mut(row) {
|
||||
row.clear();
|
||||
@ -1147,6 +1154,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||
if let Some(Some(row)) = self.rows.get(row) { Some(row) } else { None }
|
||||
}
|
||||
|
||||
/// Interescts `row` with `set`. `set` can be either `BitSet` or
|
||||
/// `HybridBitSet`. Has no effect if `row` does not exist.
|
||||
///
|
||||
/// Returns true if the row was changed.
|
||||
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||
where
|
||||
HybridBitSet<C>: BitRelations<Set>,
|
||||
@ -1157,6 +1168,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Subtracts `set from `row`. `set` can be either `BitSet` or
|
||||
/// `HybridBitSet`. Has no effect if `row` does not exist.
|
||||
///
|
||||
/// Returns true if the row was changed.
|
||||
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||
where
|
||||
HybridBitSet<C>: BitRelations<Set>,
|
||||
@ -1167,6 +1182,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unions `row` with `set`. `set` can be either `BitSet` or
|
||||
/// `HybridBitSet`.
|
||||
///
|
||||
/// Returns true if the row was changed.
|
||||
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||
where
|
||||
HybridBitSet<C>: BitRelations<Set>,
|
||||
|
@ -304,6 +304,72 @@ fn sparse_matrix_iter() {
|
||||
assert!(iter.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparse_matrix_operations() {
|
||||
let mut matrix: SparseBitMatrix<usize, usize> = SparseBitMatrix::new(100);
|
||||
matrix.insert(3, 22);
|
||||
matrix.insert(3, 75);
|
||||
matrix.insert(2, 99);
|
||||
matrix.insert(4, 0);
|
||||
|
||||
let mut disjoint: HybridBitSet<usize> = HybridBitSet::new_empty(100);
|
||||
disjoint.insert(33);
|
||||
|
||||
let mut superset = HybridBitSet::new_empty(100);
|
||||
superset.insert(22);
|
||||
superset.insert(75);
|
||||
superset.insert(33);
|
||||
|
||||
let mut subset = HybridBitSet::new_empty(100);
|
||||
subset.insert(22);
|
||||
|
||||
// SparseBitMatrix::remove
|
||||
{
|
||||
let mut matrix = matrix.clone();
|
||||
matrix.remove(3, 22);
|
||||
assert!(!matrix.row(3).unwrap().contains(22));
|
||||
matrix.remove(0, 0);
|
||||
assert!(matrix.row(0).is_none());
|
||||
}
|
||||
|
||||
// SparseBitMatrix::clear
|
||||
{
|
||||
let mut matrix = matrix.clone();
|
||||
matrix.clear(3);
|
||||
assert!(!matrix.row(3).unwrap().contains(75));
|
||||
matrix.clear(0);
|
||||
assert!(matrix.row(0).is_none());
|
||||
}
|
||||
|
||||
// SparseBitMatrix::intersect_row
|
||||
{
|
||||
let mut matrix = matrix.clone();
|
||||
assert!(!matrix.intersect_row(2, &superset));
|
||||
assert!(matrix.intersect_row(2, &subset));
|
||||
matrix.intersect_row(0, &disjoint);
|
||||
assert!(matrix.row(0).is_none());
|
||||
}
|
||||
|
||||
// SparseBitMatrix::subtract_row
|
||||
{
|
||||
let mut matrix = matrix.clone();
|
||||
assert!(!matrix.subtract_row(2, &disjoint));
|
||||
assert!(matrix.subtract_row(2, &subset));
|
||||
assert!(matrix.subtract_row(2, &superset));
|
||||
matrix.intersect_row(0, &disjoint);
|
||||
assert!(matrix.row(0).is_none());
|
||||
}
|
||||
|
||||
// SparseBitMatrix::union_row
|
||||
{
|
||||
let mut matrix = matrix.clone();
|
||||
assert!(!matrix.union_row(2, &subset));
|
||||
assert!(matrix.union_row(2, &disjoint));
|
||||
matrix.union_row(0, &disjoint);
|
||||
assert!(matrix.row(0).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge dense hybrid set into empty sparse hybrid set.
|
||||
#[bench]
|
||||
fn union_hybrid_sparse_empty_to_dense(b: &mut Bencher) {
|
||||
|
Loading…
Reference in New Issue
Block a user