csr_matrix 0.1.0

Simple implementation of a generic Compressed Sparse Row (CSR) matrix.
Documentation
//! Simple implementation of a generic Compressed Sparse Row (CSR) matrix.
//!
//! Features (an exhaustive list):
//! - The type of the values stored does not have any trait requirements.
//! - A separate builder type to populate the matrix with the desired values.
//! - Access all the populated entries in a matrix as a single iterator.
//! - Access the populated entries of a given row as a single iterator.
//!
//! This crate was conceived for personal use, specifically for use in a WIP generic graphs crate.

mod builder_impl;
mod iterator;

use iterator::EntriesIterator;

/// CSR Matrix type.
pub struct CsrMatrix<T> {
    pub m: usize,        // number of rows
    pub n: usize,        // number of columns
    offsets: Vec<usize>, // offsets to get the entries of specific rows
    entries: Vec<(usize, T)>,
}

impl<T> CsrMatrix<T> {
    /// Returns an iterator to all the populated entries.
    pub fn entries_iter(&self) -> impl Iterator<Item = (usize, &(usize, T))> {
        EntriesIterator::<T>::new(&self)
    }

    /// Returns an iterator to all the populated entries of a given row.
    pub fn row_iter(&self, row: usize) -> impl Iterator<Item = &(usize, T)> {
        self.entries
            .iter()
            .skip(self.offsets[row])
            .take(self.offsets[row + 1] - self.offsets[row])
    }
}

/// Builder type for CsrMatrix
pub struct CsrMatrixBuilder<T> {
    m: usize, // number of rows
    n: usize, // number of columns
    entries: Vec<(usize, usize, T)>,
}