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);
}
/// 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<T>
/// Clear all triplets from the matrix.
pub fn clear_triplets(&mut self, i: usize, j: usize, v: T) -> Option<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 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.

View File

@ -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<_>>(),
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<_>>(),
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![]);
coo.clear_triplets();
assert_eq(coo.triplet_iter.collect::<Vec<_>>(), vec![]);
}
#[test]