diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 2b302e37..25dc07fb 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -211,6 +211,13 @@ impl CooMatrix { 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. #[inline] #[must_use] diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index c70c5f97..d232b041 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -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![(0, 0, &1), (0, 0, &2), (2, 2, &3)] + ); + coo.clear_triplets(); + assert_eq!(coo.triplet_iter().collect::>(), 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![(0, 0, &1), (0, 0, &2), (2, 2, &3)] + ); +} + #[test] fn coo_push_out_of_bounds_entries() { {