2021-01-13 20:10:21 +08:00
|
|
|
//! Implements core traits for use with `matrixcompare`.
|
2021-01-26 00:26:27 +08:00
|
|
|
use crate::coo::CooMatrix;
|
2021-01-13 20:10:21 +08:00
|
|
|
use crate::csc::CscMatrix;
|
2021-01-26 00:26:27 +08:00
|
|
|
use crate::csr::CsrMatrix;
|
2021-01-13 20:10:21 +08:00
|
|
|
use matrixcompare_core;
|
|
|
|
use matrixcompare_core::{Access, SparseAccess};
|
|
|
|
|
|
|
|
macro_rules! impl_matrix_for_csr_csc {
|
|
|
|
($MatrixType:ident) => {
|
2021-01-26 00:26:27 +08:00
|
|
|
impl<T: Clone> SparseAccess<T> for $MatrixType<T> {
|
|
|
|
fn nnz(&self) -> usize {
|
|
|
|
$MatrixType::nnz(self)
|
|
|
|
}
|
2021-01-13 20:10:21 +08:00
|
|
|
|
2021-01-26 00:26:27 +08:00
|
|
|
fn fetch_triplets(&self) -> Vec<(usize, usize, T)> {
|
|
|
|
self.triplet_iter()
|
|
|
|
.map(|(i, j, v)| (i, j, v.clone()))
|
|
|
|
.collect()
|
|
|
|
}
|
2021-01-13 20:10:21 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 00:26:27 +08:00
|
|
|
impl<T: Clone> matrixcompare_core::Matrix<T> for $MatrixType<T> {
|
|
|
|
fn rows(&self) -> usize {
|
|
|
|
self.nrows()
|
|
|
|
}
|
2021-01-13 20:10:21 +08:00
|
|
|
|
2021-01-26 00:26:27 +08:00
|
|
|
fn cols(&self) -> usize {
|
|
|
|
self.ncols()
|
|
|
|
}
|
2021-01-13 20:10:21 +08:00
|
|
|
|
2021-07-28 07:18:29 +08:00
|
|
|
fn access(&self) -> Access<'_, T> {
|
2021-01-26 00:26:27 +08:00
|
|
|
Access::Sparse(self)
|
|
|
|
}
|
2021-01-13 20:10:21 +08:00
|
|
|
}
|
2021-01-26 00:26:27 +08:00
|
|
|
};
|
2021-01-13 20:10:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_matrix_for_csr_csc!(CsrMatrix);
|
|
|
|
impl_matrix_for_csr_csc!(CscMatrix);
|
|
|
|
|
|
|
|
impl<T: Clone> SparseAccess<T> for CooMatrix<T> {
|
|
|
|
fn nnz(&self) -> usize {
|
|
|
|
CooMatrix::nnz(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_triplets(&self) -> Vec<(usize, usize, T)> {
|
2021-01-26 00:26:27 +08:00
|
|
|
self.triplet_iter()
|
|
|
|
.map(|(i, j, v)| (i, j, v.clone()))
|
|
|
|
.collect()
|
2021-01-13 20:10:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> matrixcompare_core::Matrix<T> for CooMatrix<T> {
|
|
|
|
fn rows(&self) -> usize {
|
|
|
|
self.nrows()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cols(&self) -> usize {
|
|
|
|
self.ncols()
|
|
|
|
}
|
|
|
|
|
2021-07-28 07:18:29 +08:00
|
|
|
fn access(&self) -> Access<'_, T> {
|
2021-01-13 20:10:21 +08:00
|
|
|
Access::Sparse(self)
|
|
|
|
}
|
2021-01-26 00:26:27 +08:00
|
|
|
}
|