From 7352b8c0d4ee1df2115d7f79c1b9a4b6eca7e467 Mon Sep 17 00:00:00 2001 From: julianknodt Date: Sat, 16 Sep 2023 00:13:07 -0700 Subject: [PATCH] Add CscBuilder For partial construction of Csc matrices --- nalgebra-sparse/src/cs.rs | 22 +++++++++++++++++++++- nalgebra-sparse/src/csc.rs | 21 +++++++++++++++++++-- nalgebra-sparse/src/lib.rs | 2 +- nalgebra-sparse/src/pattern.rs | 4 ++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index e000e2de5..63f9bf0ab 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -5,7 +5,7 @@ use num_traits::One; use nalgebra::Scalar; -use crate::pattern::SparsityPattern; +use crate::pattern::{BuilderInsertError, SparsityPattern, SparsityPatternBuilder}; use crate::utils::{apply_permutation, compute_sort_permutation}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; @@ -692,3 +692,23 @@ where Ok(()) } + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CsBuilder { + sparsity_builder: SparsityPatternBuilder, + values: Vec, +} + +impl CsBuilder { + pub fn new(major_dim: usize, minor_dim: usize) -> Self { + Self { + sparsity_builder: SparsityPatternBuilder::new(major_dim, minor_dim), + values: vec![], + } + } + pub fn insert(&mut self, maj: usize, min: usize, val: T) -> Result<(), BuilderInsertError> { + self.sparsity_builder.insert(maj, min)?; + self.values.push(val); + Ok(()) + } +} diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 59823b3ca..2b39d106d 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -7,9 +7,11 @@ mod csc_serde; use crate::cs; -use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; +use crate::cs::{CsBuilder, CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix}; use crate::csr::CsrMatrix; -use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; +use crate::pattern::{ + BuilderInsertError, SparsityPattern, SparsityPatternFormatError, SparsityPatternIter, +}; use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind}; use nalgebra::{RealField, Scalar}; @@ -925,3 +927,18 @@ where self.lane_iter.next().map(|lane| CscColMut { lane }) } } + +/// An incremental builder for a Csc matrix. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CscBuilder(CsBuilder); + +impl CscBuilder { + /// Constructs a new instance of a Csc Builder. + pub fn new(rows: usize, cols: usize) -> Self { + Self(CsBuilder::new(cols, rows)) + } + /// Inserts a value into the builder. Must be called in ascending col, row order. + pub fn insert(&mut self, row: usize, col: usize, val: T) -> Result<(), BuilderInsertError> { + self.0.insert(col, row, val) + } +} diff --git a/nalgebra-sparse/src/lib.rs b/nalgebra-sparse/src/lib.rs index fc12509e7..4988a8e85 100644 --- a/nalgebra-sparse/src/lib.rs +++ b/nalgebra-sparse/src/lib.rs @@ -171,7 +171,7 @@ use std::error::Error; use std::fmt; pub use self::coo::CooMatrix; -pub use self::csc::CscMatrix; +pub use self::csc::{CscBuilder, CscMatrix}; pub use self::csr::CsrMatrix; /// Errors produced by functions that expect well-formed sparse format data. diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 3440cfff0..549abe342 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -358,13 +358,13 @@ impl SparsityPattern { /// It requires elements to be added in sorted order. Specifically, /// For each element the major must be >= the previous element's major. /// If the major is the same, the minor must be in ascending order. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct SparsityPatternBuilder { buf: SparsityPattern, major_dim: usize, } -/// +/// An error when adding into the SparsityPatternBuilder #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum BuilderInsertError { ///