Skip to content

Commit

Permalink
Add CscBuilder
Browse files Browse the repository at this point in the history
For partial construction of Csc matrices
  • Loading branch information
JulianKnodt committed Sep 16, 2023
1 parent 0db2532 commit 7352b8c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
22 changes: 21 additions & 1 deletion nalgebra-sparse/src/cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -692,3 +692,23 @@ where

Ok(())
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsBuilder<T> {
sparsity_builder: SparsityPatternBuilder,
values: Vec<T>,
}

impl<T> CsBuilder<T> {
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(())
}
}
21 changes: 19 additions & 2 deletions nalgebra-sparse/src/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<T>(CsBuilder<T>);

impl<T> CscBuilder<T> {
/// 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)
}
}
2 changes: 1 addition & 1 deletion nalgebra-sparse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions nalgebra-sparse/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
///
Expand Down

0 comments on commit 7352b8c

Please sign in to comment.