From b90dc7c042e9278786ba54c5b0fc8d85db8412d7 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 15 Aug 2022 19:14:38 -0700 Subject: [PATCH 1/7] Add `clear_triplet()` --- nalgebra-sparse/src/coo.rs | 20 +++++++++++++++++ nalgebra-sparse/tests/unit_tests/coo.rs | 29 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 2b302e37..e047745a 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -211,6 +211,26 @@ 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<(usize, usize, T)> + 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 retv = self.values.remove(triple_idx); + Some((i, j, retv)) + } else { + None + } + } + /// 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..6ec88fb1 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -226,6 +226,35 @@ fn coo_push_valid_entries() { ); } +#[test] +fn coo_clear_triplet_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((0, 0, 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((0, 0, 2))); + assert_eq!(coo.triplet_iter().collect::>(), vec![(2, 2, &3)]); + let triplet = coo.clear_triplet(2, 2, 3); + assert_eq!(triplet, Some((2, 2, 3))); + assert_eq!(coo.triplet_iter().collect::>(), vec![]); +} + #[test] fn coo_push_out_of_bounds_entries() { { From bcc5527baac743df259b85c9a61a5793a63ce8fc Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 15 Aug 2022 19:28:58 -0700 Subject: [PATCH 2/7] Switch return type to just T --- nalgebra-sparse/src/coo.rs | 6 +++--- nalgebra-sparse/tests/unit_tests/coo.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index e047745a..197ef6f4 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -214,7 +214,7 @@ impl CooMatrix { /// 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<(usize, usize, T)> + pub fn clear_triplet(&mut self, i: usize, j: usize, v: T) -> Option where T: PartialEq, { @@ -224,8 +224,8 @@ impl CooMatrix { if let Some(triple_idx) = triple_idx { self.row_indices.remove(triple_idx); self.col_indices.remove(triple_idx); - let retv = self.values.remove(triple_idx); - Some((i, j, retv)) + let removed_value = self.values.remove(triple_idx); + Some(removed_value) } else { None } diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 6ec88fb1..4b640596 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -242,16 +242,16 @@ fn coo_clear_triplet_valid_entries() { vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] ); let triplet = coo.clear_triplet(0, 0, 1); - assert_eq!(triplet, Some((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((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((2, 2, 3))); + assert_eq!(triplet, Some(3)); assert_eq!(coo.triplet_iter().collect::>(), vec![]); } From bdfa643e3cc749b44058ab467068e4aa96287513 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 16 Aug 2022 00:15:03 -0700 Subject: [PATCH 3/7] 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] From 41e1cc0db223bdbd4fc5547212760d68fd262f30 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 16 Aug 2022 00:20:13 -0700 Subject: [PATCH 4/7] extend test case --- nalgebra-sparse/tests/unit_tests/coo.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index b9c22885..16ee5647 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -239,6 +239,14 @@ fn coo_clear_triplets_valid_entries() { ); 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] From b9483ab545621517974390a7d4ff1d846318683c Mon Sep 17 00:00:00 2001 From: Lukas Hermann Date: Tue, 16 Aug 2022 22:06:43 +0000 Subject: [PATCH 5/7] remove bad return type --- nalgebra-sparse/src/coo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 9793fa1e..240e2c57 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -212,7 +212,7 @@ impl CooMatrix { } /// Clear all triplets from the matrix. - pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) -> Option + pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) where T: PartialEq, { From 0eb1f5c125b65847fea06f5d0f01f030096c3450 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 16 Aug 2022 20:10:17 -0700 Subject: [PATCH 6/7] 'fix error's --- nalgebra-sparse/src/coo.rs | 2 +- nalgebra-sparse/tests/unit_tests/coo.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 240e2c57..1a1720ed 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -212,7 +212,7 @@ impl CooMatrix { } /// Clear all triplets from the matrix. - pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) + pub fn clear_triplets(&mut self) where T: PartialEq, { diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 16ee5647..d232b041 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -238,7 +238,7 @@ fn coo_clear_triplets_valid_entries() { vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] ); coo.clear_triplets(); - assert_eq(coo.triplet_iter.collect::>(), vec![]); + assert_eq!(coo.triplet_iter().collect::>(), vec![]); // making sure everyhting works after clearing coo.push(0, 0, 1); coo.push(0, 0, 2); From 498fbf51b13d3af325c2f4c546fa4b394d53818f Mon Sep 17 00:00:00 2001 From: lukas Date: Sat, 20 Aug 2022 00:09:12 -0700 Subject: [PATCH 7/7] remove unnecessary trait bounds --- nalgebra-sparse/src/coo.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 1a1720ed..25dc07fb 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -212,10 +212,7 @@ impl CooMatrix { } /// Clear all triplets from the matrix. - pub fn clear_triplets(&mut self) - where - T: PartialEq, - { + pub fn clear_triplets(&mut self) { self.col_indices.clear(); self.row_indices.clear(); self.values.clear();