From bdfa643e3cc749b44058ab467068e4aa96287513 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 16 Aug 2022 00:15:03 -0700 Subject: [PATCH] clear_triplets --- nalgebra-sparse/src/coo.rs | 20 +++++--------------- nalgebra-sparse/tests/unit_tests/coo.rs | 20 +++----------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 197ef6f4..9793fa1e 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -211,24 +211,14 @@ impl CooMatrix { self.values.push(v); } - /// Remove a single triplet from the matrix. - /// - /// This removes the value `v` from the `i`th row and `j`th column in the matrix. - pub fn clear_triplet(&mut self, i: usize, j: usize, v: T) -> Option + /// Clear all triplets from the matrix. + pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) -> Option where T: PartialEq, { - let triple_idx = self - .triplet_iter() - .position(|triplet| triplet == (i, j, &v)); - if let Some(triple_idx) = triple_idx { - self.row_indices.remove(triple_idx); - self.col_indices.remove(triple_idx); - let removed_value = self.values.remove(triple_idx); - Some(removed_value) - } else { - None - } + self.col_indices.clear(); + self.row_indices.clear(); + self.values.clear(); } /// The number of rows in the matrix. diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 4b640596..b9c22885 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -227,32 +227,18 @@ fn coo_push_valid_entries() { } #[test] -fn coo_clear_triplet_valid_entries() { +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); - - // clear a triplet that is not included - let triplet = coo.clear_triplet(0, 0, 0); - assert_eq!(triplet, None); assert_eq!( coo.triplet_iter().collect::>(), vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] ); - let triplet = coo.clear_triplet(0, 0, 1); - assert_eq!(triplet, Some(1)); - assert_eq!( - coo.triplet_iter().collect::>(), - vec![(0, 0, &2), (2, 2, &3)] - ); - let triplet = coo.clear_triplet(0, 0, 2); - assert_eq!(triplet, Some(2)); - assert_eq!(coo.triplet_iter().collect::>(), vec![(2, 2, &3)]); - let triplet = coo.clear_triplet(2, 2, 3); - assert_eq!(triplet, Some(3)); - assert_eq!(coo.triplet_iter().collect::>(), vec![]); + coo.clear_triplets(); + assert_eq(coo.triplet_iter.collect::>(), vec![]); } #[test]