Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sparse] Add clear_triplets() #1141

Merged
merged 7 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions nalgebra-sparse/src/coo.rs
Expand Up @@ -211,6 +211,16 @@ impl<T> CooMatrix<T> {
self.values.push(v);
}

/// Clear all triplets from the matrix.
pub fn clear_triplets(&mut self)
where
T: PartialEq,
lsh marked this conversation as resolved.
Show resolved Hide resolved
{
self.col_indices.clear();
self.row_indices.clear();
self.values.clear();
}

/// The number of rows in the matrix.
#[inline]
#[must_use]
Expand Down
23 changes: 23 additions & 0 deletions nalgebra-sparse/tests/unit_tests/coo.rs
Expand Up @@ -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]
fn coo_push_out_of_bounds_entries() {
{
Expand Down