use super::ValuedMatrix2D;
use crate::traits::{SparseValuedMatrix2D, SparseValuedMatrixRef, ValuedMatrix};
pub trait SparseValuedMatrix2DRef:
SparseValuedMatrix2D + ValuedMatrix2D + SparseValuedMatrixRef
{
type SparseRowValuesRef<'a>: Iterator<Item = &'a <Self as ValuedMatrix>::Value>
+ DoubleEndedIterator<Item = &'a <Self as ValuedMatrix>::Value>
where
Self: 'a,
<Self as ValuedMatrix>::Value: 'a;
fn sparse_row_values_ref(&self, row: Self::RowIndex) -> Self::SparseRowValuesRef<'_>;
#[inline]
fn sparse_value_at_ref(
&self,
row: Self::RowIndex,
column: Self::ColumnIndex,
) -> Option<&<Self as ValuedMatrix>::Value>
where
Self::ColumnIndex: PartialEq,
{
self.sparse_row(row)
.zip(self.sparse_row_values_ref(row))
.find(|&(col, _)| col == column)
.map(|(_, val)| val)
}
#[inline]
fn sparse_row_entries(
&self,
row: Self::RowIndex,
) -> impl Iterator<Item = (Self::ColumnIndex, &<Self as ValuedMatrix>::Value)> {
self.sparse_row(row).zip(self.sparse_row_values_ref(row))
}
}
impl<M: SparseValuedMatrix2DRef> SparseValuedMatrix2DRef for &M {
type SparseRowValuesRef<'a>
= M::SparseRowValuesRef<'a>
where
Self: 'a,
M::Value: 'a;
#[inline]
fn sparse_row_values_ref(&self, row: Self::RowIndex) -> Self::SparseRowValuesRef<'_> {
(*self).sparse_row_values_ref(row)
}
#[inline]
fn sparse_value_at_ref(
&self,
row: Self::RowIndex,
column: Self::ColumnIndex,
) -> Option<&<Self as ValuedMatrix>::Value>
where
Self::ColumnIndex: PartialEq,
{
(*self).sparse_value_at_ref(row, column)
}
}