csvbinmatrix 0.8.0

Binary matrix Compressed Sparse Vector
Documentation
#![doc = include_str!("../../docs/matrix/mod.md")]

pub mod cmp;
pub mod create;
pub mod io;
pub mod items;
pub mod iter;
pub mod ops;
pub mod update;
pub mod views;

/// Binary matrix Compressed Sparse Vector.
///
/// For all the example sections, we use `matrix` is the following matrix:
/// ```text
/// 0 0 0
/// 0 0 1
/// 0 1 1
/// 1 1 1
/// ```
///
/// You can obtain this matrix with the following code:
///
/// ```rust
/// use csvbinmatrix::prelude::CSVBinaryMatrix;
///
/// let matrix = CSVBinaryMatrix::try_from(&[
///     [0, 0, 0],
///     [0, 0, 1],
///     [0, 1, 1],
///     [1, 1, 1],
/// ]).unwrap();
/// ```
///
/// See [`csvbinmatrix::matrix::create`](crate::matrix::create) module for more details.
///
#[derive(Debug, Clone)]
#[allow(clippy::module_name_repetitions)]
pub struct CSVBinaryMatrix {
    number_of_rows: usize,
    number_of_columns: usize,
    distances: Vec<usize>,
    is_reversed: bool,
}

impl CSVBinaryMatrix {
    /// Returns the number of rows.
    #[must_use]
    pub fn number_of_rows(&self) -> usize {
        self.number_of_rows
    }

    /// Returns the number of columns.
    #[must_use]
    pub fn number_of_columns(&self) -> usize {
        self.number_of_columns
    }
}

#[cfg(test)]
mod tests {

    use pretty_assertions::assert_eq;
    use rstest::{fixture, rstest};

    use super::CSVBinaryMatrix;

    /// 3x3 zeros matrix
    ///
    /// ```text
    /// 0 0 0
    /// 0 0 0
    /// 0 0 0
    /// ```
    #[fixture]
    pub fn zeros_matrix() -> CSVBinaryMatrix {
        CSVBinaryMatrix {
            number_of_rows: 3,
            number_of_columns: 3,
            distances: vec![8],
            is_reversed: false,
        }
    }

    /// 3x3 zeros matrix with reversed distances
    #[fixture]
    pub fn zeros_matrix_bis() -> CSVBinaryMatrix {
        CSVBinaryMatrix {
            number_of_rows: 3,
            number_of_columns: 3,
            distances: vec![8],
            is_reversed: true,
        }
    }

    /// A complete 3x3 binary matrix
    ///
    /// ```text
    /// 1 1 1
    /// 1 1 1
    /// 1 1 1
    /// ```
    #[fixture]
    pub fn ones_matrix() -> CSVBinaryMatrix {
        CSVBinaryMatrix {
            number_of_rows: 3,
            number_of_columns: 3,
            distances: vec![0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
            is_reversed: false,
        }
    }

    /// A 3x3 matrix
    ///
    /// ```text
    /// 1 1 1
    /// 1 0 0
    /// 0 1 0
    /// ```
    #[fixture]
    pub fn matrix_a() -> CSVBinaryMatrix {
        CSVBinaryMatrix {
            number_of_rows: 3,
            number_of_columns: 3,
            distances: vec![0, 1, 1, 1, 4, 1],
            is_reversed: false,
        }
    }

    /// A 3x3 matrix with reversed distances
    ///
    /// ```text
    /// 1 1 1
    /// 1 0 0
    /// 0 1 0
    /// ```
    #[fixture]
    pub fn matrix_a_bis() -> CSVBinaryMatrix {
        CSVBinaryMatrix {
            number_of_rows: 3,
            number_of_columns: 3,
            distances: vec![1, 4, 1, 1, 1, 0],
            is_reversed: true,
        }
    }

    #[rstest]
    fn debug(zeros_matrix: CSVBinaryMatrix, matrix_a: CSVBinaryMatrix) {
        assert_eq!(
            format!("{zeros_matrix:?}"),
            "CSVBinaryMatrix { number_of_rows: 3, number_of_columns: 3, distances: [8], is_reversed: false }"
        );
        assert_eq!(
                format!("{matrix_a:?}"),
                "CSVBinaryMatrix { number_of_rows: 3, number_of_columns: 3, distances: [0, 1, 1, 1, 4, 1], is_reversed: false }"
            );
    }

    #[rstest]
    fn number_of_rows(matrix_a: CSVBinaryMatrix) {
        assert_eq!(matrix_a.number_of_rows(), 3);
    }

    #[rstest]
    fn number_of_columns(matrix_a: CSVBinaryMatrix) {
        assert_eq!(matrix_a.number_of_columns(), 3);
    }
}