Merge pull request #1141 from lsh/sparse-clear-triplet

[Sparse] Add `clear_triplets()`
This commit is contained in:
Sébastien Crozet 2022-08-22 09:09:39 +02:00 committed by GitHub
commit 5cf6afb254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -211,6 +211,13 @@ impl<T> CooMatrix<T> {
self.values.push(v); self.values.push(v);
} }
/// Clear all triplets from the matrix.
pub fn clear_triplets(&mut self) {
self.col_indices.clear();
self.row_indices.clear();
self.values.clear();
}
/// The number of rows in the matrix. /// The number of rows in the matrix.
#[inline] #[inline]
#[must_use] #[must_use]

View File

@ -226,6 +226,29 @@ fn coo_push_valid_entries() {
); );
} }
#[test]
fn coo_clear_triplets_valid_entries() {
let mut coo = CooMatrix::new(3, 3);
coo.push(0, 0, 1);
coo.push(0, 0, 2);
coo.push(2, 2, 3);
assert_eq!(
coo.triplet_iter().collect::<Vec<_>>(),
vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)]
);
coo.clear_triplets();
assert_eq!(coo.triplet_iter().collect::<Vec<_>>(), vec![]);
// making sure everyhting works after clearing
coo.push(0, 0, 1);
coo.push(0, 0, 2);
coo.push(2, 2, 3);
assert_eq!(
coo.triplet_iter().collect::<Vec<_>>(),
vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)]
);
}
#[test] #[test]
fn coo_push_out_of_bounds_entries() { fn coo_push_out_of_bounds_entries() {
{ {