matrixcompare_core/
lib.rs

1/// Defines how the elements of a matrix may be accessed.
2pub enum Access<'a, T> {
3    Dense(&'a dyn DenseAccess<T>),
4    Sparse(&'a dyn SparseAccess<T>),
5}
6
7/// Main interface for access to the elements of a matrix.
8pub trait Matrix<T> {
9    fn rows(&self) -> usize;
10    fn cols(&self) -> usize;
11
12    /// Expose dense or sparse access to the matrix.
13    fn access(&self) -> Access<T>;
14}
15
16/// Access to a dense matrix.
17pub trait DenseAccess<T>: Matrix<T> {
18    fn fetch_single(&self, row: usize, col: usize) -> T;
19}
20
21/// Access to a sparse matrix.
22pub trait SparseAccess<T>: Matrix<T> {
23    /// Number of non-zero elements in the matrix.
24    fn nnz(&self) -> usize;
25
26    /// Retrieve the triplets that identify the coefficients of the sparse matrix.
27    fn fetch_triplets(&self) -> Vec<(usize, usize, T)>;
28}
29
30impl<T, X> Matrix<T> for &X
31    where
32        X: Matrix<T>,
33{
34    fn rows(&self) -> usize {
35        X::rows(*self)
36    }
37
38    fn cols(&self) -> usize {
39        X::cols(*self)
40    }
41
42    fn access(&self) -> Access<T> {
43        X::access(*self)
44    }
45}
46
47impl<T, X> DenseAccess<T> for &X
48    where
49        X: DenseAccess<T>,
50{
51    fn fetch_single(&self, row: usize, col: usize) -> T {
52        X::fetch_single(*self, row, col)
53    }
54}
55
56impl<T, X> SparseAccess<T> for &X
57    where
58        X: SparseAccess<T>,
59{
60    fn nnz(&self) -> usize {
61        X::nnz(*self)
62    }
63
64    fn fetch_triplets(&self) -> Vec<(usize, usize, T)> {
65        X::fetch_triplets(&self)
66    }
67}