csr_matrix 0.1.0

Simple implementation of a generic Compressed Sparse Row (CSR) matrix.
Documentation
use super::CsrMatrix;

/// Iterator for iterating over all the entries of CsrMatrix efficiently.
pub struct EntriesIterator<'a, T> {
    csr_matrix: &'a CsrMatrix<T>,
    row: usize,
    entry: usize,
}

impl<'a, T> EntriesIterator<'a, T> {
    /// Creates a new iterator over all the entries of the given CsrMatrix.
    pub fn new(csr_matrix: &'a CsrMatrix<T>) -> Self {
        Self {
            csr_matrix,
            row: 0,
            entry: 0,
        }
    }
}

impl<'a, T> Iterator for EntriesIterator<'a, T> {
    type Item = (usize, &'a (usize, T));

    fn next(&mut self) -> Option<Self::Item> {
        if self.entry == self.csr_matrix.entries.len() {
            return None;
        }
        while self.row < self.csr_matrix.m && self.csr_matrix.offsets[self.row + 1] == self.entry {
            self.row += 1;
        }
        let result = Some((self.row, &self.csr_matrix.entries[self.entry]));
        self.entry += 1;
        result
    }
}