csvbinmatrix/matrix/
mod.rs

1#![doc = include_str!("../../docs/matrix/mod.md")]
2
3pub mod cmp;
4pub mod create;
5pub mod io;
6pub mod items;
7pub mod iter;
8pub mod ops;
9pub mod update;
10pub mod views;
11
12/// Binary matrix Compressed Sparse Vector.
13///
14/// For all the example sections, we use `matrix` is the following matrix:
15/// ```text
16/// 0 0 0
17/// 0 0 1
18/// 0 1 1
19/// 1 1 1
20/// ```
21///
22/// You can obtain this matrix with the following code:
23///
24/// ```rust
25/// use csvbinmatrix::prelude::CSVBinaryMatrix;
26///
27/// let matrix = CSVBinaryMatrix::try_from(&[
28///     [0, 0, 0],
29///     [0, 0, 1],
30///     [0, 1, 1],
31///     [1, 1, 1],
32/// ]).unwrap();
33/// ```
34///
35/// See [`csvbinmatrix::matrix::create`](crate::matrix::create) module for more details.
36///
37#[derive(Debug, Clone)]
38#[allow(clippy::module_name_repetitions)]
39pub struct CSVBinaryMatrix {
40    number_of_rows: usize,
41    number_of_columns: usize,
42    distances: Vec<usize>,
43    is_reversed: bool,
44}
45
46impl CSVBinaryMatrix {
47    /// Returns the number of rows.
48    #[must_use]
49    pub fn number_of_rows(&self) -> usize {
50        self.number_of_rows
51    }
52
53    /// Returns the number of columns.
54    #[must_use]
55    pub fn number_of_columns(&self) -> usize {
56        self.number_of_columns
57    }
58}
59
60#[cfg(test)]
61mod tests {
62
63    use pretty_assertions::assert_eq;
64    use rstest::{fixture, rstest};
65
66    use super::CSVBinaryMatrix;
67
68    /// 3x3 zeros matrix
69    ///
70    /// ```text
71    /// 0 0 0
72    /// 0 0 0
73    /// 0 0 0
74    /// ```
75    #[fixture]
76    pub fn zeros_matrix() -> CSVBinaryMatrix {
77        CSVBinaryMatrix {
78            number_of_rows: 3,
79            number_of_columns: 3,
80            distances: vec![8],
81            is_reversed: false,
82        }
83    }
84
85    /// 3x3 zeros matrix with reversed distances
86    #[fixture]
87    pub fn zeros_matrix_bis() -> CSVBinaryMatrix {
88        CSVBinaryMatrix {
89            number_of_rows: 3,
90            number_of_columns: 3,
91            distances: vec![8],
92            is_reversed: true,
93        }
94    }
95
96    /// A complete 3x3 binary matrix
97    ///
98    /// ```text
99    /// 1 1 1
100    /// 1 1 1
101    /// 1 1 1
102    /// ```
103    #[fixture]
104    pub fn ones_matrix() -> CSVBinaryMatrix {
105        CSVBinaryMatrix {
106            number_of_rows: 3,
107            number_of_columns: 3,
108            distances: vec![0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
109            is_reversed: false,
110        }
111    }
112
113    /// A 3x3 matrix
114    ///
115    /// ```text
116    /// 1 1 1
117    /// 1 0 0
118    /// 0 1 0
119    /// ```
120    #[fixture]
121    pub fn matrix_a() -> CSVBinaryMatrix {
122        CSVBinaryMatrix {
123            number_of_rows: 3,
124            number_of_columns: 3,
125            distances: vec![0, 1, 1, 1, 4, 1],
126            is_reversed: false,
127        }
128    }
129
130    /// A 3x3 matrix with reversed distances
131    ///
132    /// ```text
133    /// 1 1 1
134    /// 1 0 0
135    /// 0 1 0
136    /// ```
137    #[fixture]
138    pub fn matrix_a_bis() -> CSVBinaryMatrix {
139        CSVBinaryMatrix {
140            number_of_rows: 3,
141            number_of_columns: 3,
142            distances: vec![1, 4, 1, 1, 1, 0],
143            is_reversed: true,
144        }
145    }
146
147    #[rstest]
148    fn debug(zeros_matrix: CSVBinaryMatrix, matrix_a: CSVBinaryMatrix) {
149        assert_eq!(
150            format!("{zeros_matrix:?}"),
151            "CSVBinaryMatrix { number_of_rows: 3, number_of_columns: 3, distances: [8], is_reversed: false }"
152        );
153        assert_eq!(
154                format!("{matrix_a:?}"),
155                "CSVBinaryMatrix { number_of_rows: 3, number_of_columns: 3, distances: [0, 1, 1, 1, 4, 1], is_reversed: false }"
156            );
157    }
158
159    #[rstest]
160    fn number_of_rows(matrix_a: CSVBinaryMatrix) {
161        assert_eq!(matrix_a.number_of_rows(), 3);
162    }
163
164    #[rstest]
165    fn number_of_columns(matrix_a: CSVBinaryMatrix) {
166        assert_eq!(matrix_a.number_of_columns(), 3);
167    }
168}