Add `clear_triplet()`

This commit is contained in:
lukas 2022-08-15 19:14:38 -07:00
parent d09d06858f
commit b90dc7c042
2 changed files with 49 additions and 0 deletions

View File

@ -211,6 +211,26 @@ 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<(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]

View File

@ -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<_>>(),
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<_>>(),
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<_>>(), 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<_>>(), vec![]);
}
#[test]
fn coo_push_out_of_bounds_entries() {
{