Skip to content

Commit

Permalink
Add try_from_triplets_iter
Browse files Browse the repository at this point in the history
Calls `try_from_triplets` for now, and is mentioned in the documentation.
  • Loading branch information
JulianKnodt committed Aug 22, 2023
1 parent f404bcb commit 5686d18
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions nalgebra-sparse/src/coo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ impl<T> CooMatrix<T> {
}
}

/// Try to construct a COO matrix from the given dimensions and a finite iterator of
/// (i, j, v) triplets.
///
/// Returns an error if either row or column indices contain indices out of bounds.
/// Note that the COO format inherently supports duplicate entries, but they are not
/// eagerly summed.
///
/// Implementation note:
/// Calls try_from_triplets so each value is scanned twice.
pub fn try_from_triplets_iter(
nrows: usize,
ncols: usize,
triplets: impl IntoIterator<Item = (usize, usize, T)>,
) -> Result<Self, SparseFormatError> {
let (row_indices, (col_indices, values)) =
triplets.into_iter().map(|(r, c, v)| (r, (c, v))).unzip();
Self::try_from_triplets(nrows, ncols, row_indices, col_indices, values)
}

/// An iterator over triplets (i, j, v).
// TODO: Consider giving the iterator a concrete type instead of impl trait...?
pub fn triplet_iter(&self) -> impl Iterator<Item = (usize, usize, &T)> {
Expand Down

0 comments on commit 5686d18

Please sign in to comment.