1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::join::{IntoJoin, Join};
use crate::smat::{DataPosition, DataRange, Entry, MatrixMask, MatrixMaskExt, SMatrix, Store, StoreMut};
use crate::traits::{IndexExcl, IndexLowerBound};
use std::mem;

/// Access a single row in the matrix.
pub struct RowRead<'a, M, S>
where
    M: MatrixMask,
    S: Store,
{
    //crate row_index: usize,
    pub(crate) mask: &'a M,
    pub(crate) store: &'a S,
    pub(crate) data_range: DataRange,
}

impl<'a, M, S> IndexExcl<usize> for RowRead<'a, M, S>
where
    M: MatrixMask,
    S: Store,
{
    type Item = &'a S::Item;

    fn index(&mut self, idx: usize) -> Self::Item {
        let DataPosition(pos) = self.mask.find_column_position(idx, self.data_range).unwrap();
        self.store.get(pos)
    }
}

impl<'a, M, S> IndexLowerBound<usize> for RowRead<'a, M, S>
where
    M: MatrixMask,
    S: Store,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        self.mask
            .lower_bound_column_position(idx, self.data_range)
            .map(|(idx, _)| idx)
    }
}

impl<'a, M, S> IntoJoin for RowRead<'a, M, S>
where
    M: MatrixMask,
    S: Store,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(self.mask.get_column_range(self.data_range), self)
    }
}

/// Access a single row in the matrix.
pub struct RowUpdate<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    //crate row_index: usize,
    pub(crate) mask: &'a M,
    pub(crate) store: &'a mut S,
    pub(crate) data_range: DataRange,
}

impl<'a, M, S> IndexExcl<usize> for RowUpdate<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    type Item = &'a mut <S as Store>::Item;

    fn index(&mut self, idx: usize) -> Self::Item {
        let DataPosition(pos) = self.mask.find_column_position(idx, self.data_range).unwrap();
        unsafe { mem::transmute(self.store.get_mut(pos)) } // GAT
    }
}

impl<'a, M, S> IndexLowerBound<usize> for RowUpdate<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        //perf: we could also increment data range if it's guranted that no "step" occures.
        self.mask
            .lower_bound_column_position(idx, self.data_range)
            .map(|(idx, _)| idx)
    }
}

impl<'a, M, S> IntoJoin for RowUpdate<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(self.mask.get_column_range(self.data_range), self)
    }
}

/// Access a single row in the matrix.
pub struct RowWrite<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    //pub(crate) row_index: usize,
    pub(crate) row: usize,
    pub(crate) mat: &'a mut SMatrix<M, S>,
}

impl<'a, M, S> IndexExcl<usize> for RowWrite<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    type Item = Entry<'a, M, S>;

    fn index(&mut self, idx: usize) -> Self::Item {
        unsafe { mem::transmute(self.mat.get_entry(self.row, idx)) } // GAT
    }
}

impl<'a, M, S> IndexLowerBound<usize> for RowWrite<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        Some(idx)
    }
}

impl<'a, M, S> IntoJoin for RowWrite<'a, M, S>
where
    M: MatrixMask,
    S: StoreMut,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(0..usize::max_value(), self)
    }
}