clear_triplets

This commit is contained in:
lukas 2022-08-16 00:15:03 -07:00
parent bcc5527baa
commit bdfa643e3c
2 changed files with 8 additions and 32 deletions

View File

@ -211,24 +211,14 @@ impl<T> CooMatrix<T> {
self.values.push(v); self.values.push(v);
} }
/// Remove a single triplet from the matrix. /// Clear all triplets from the matrix.
/// pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) -> Option<T>
/// 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<T>
where where
T: PartialEq, T: PartialEq,
{ {
let triple_idx = self self.col_indices.clear();
.triplet_iter() self.row_indices.clear();
.position(|triplet| triplet == (i, j, &v)); self.values.clear();
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
}
} }
/// The number of rows in the matrix. /// The number of rows in the matrix.

View File

@ -227,32 +227,18 @@ fn coo_push_valid_entries() {
} }
#[test] #[test]
fn coo_clear_triplet_valid_entries() { fn coo_clear_triplets_valid_entries() {
let mut coo = CooMatrix::new(3, 3); let mut coo = CooMatrix::new(3, 3);
coo.push(0, 0, 1); coo.push(0, 0, 1);
coo.push(0, 0, 2); coo.push(0, 0, 2);
coo.push(2, 2, 3); 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!( assert_eq!(
coo.triplet_iter().collect::<Vec<_>>(), coo.triplet_iter().collect::<Vec<_>>(),
vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)] vec![(0, 0, &1), (0, 0, &2), (2, 2, &3)]
); );
let triplet = coo.clear_triplet(0, 0, 1); coo.clear_triplets();
assert_eq!(triplet, Some(1)); assert_eq(coo.triplet_iter.collect::<Vec<_>>(), vec![]);
assert_eq!(
coo.triplet_iter().collect::<Vec<_>>(),
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<_>>(), vec![(2, 2, &3)]);
let triplet = coo.clear_triplet(2, 2, 3);
assert_eq!(triplet, Some(3));
assert_eq!(coo.triplet_iter().collect::<Vec<_>>(), vec![]);
} }
#[test] #[test]