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
//! Tools for matrix access and transformation.

use crate::strategies::*;

use crate::{ MatrixExt, MatrixMutExt };
use crate::req::MatrixExtFromIter;


/// A `MatrixExt` which provides immutable access to another matrix by following a certain access strategy.
/// 
/// This `struct` is created by the [`access`](crate::MatrixExt::access) method on `MatrixExt`. See its documentation for more.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Hash, Clone, Debug)]
pub struct Access<'a, M: MatrixExt, S: AccessStrategy<M>>{
    matrix: &'a M,
    pub strategy: S,
}

/// A `MatrixMutExt` which provides mutable access to another matrix by following a certain access strategy.
/// 
/// This `struct` is created by the [`access_mut`](crate::MatrixMutExt::access_mut) method on `MatrixMutExt`. See its documentation for more.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Hash, Debug)]
pub struct AccessMut<'a, M: MatrixExt, S: AccessStrategy<M>>{
    matrix: &'a mut M,
    strategy: S,
}

/// Used by [`AccessStrategySet`].
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct Observer {
    rows: usize,
    cols: usize
}

// ### Self Implementation

impl<'a, M: MatrixExt, S: AccessStrategy<M>> Access<'a, M, S> {
    pub(crate) fn new(matrix: &'a M, strategy: S) -> Self {
        Self { matrix, strategy }
    }
    
    pub fn clone_into2(&self) -> M 
    where M: for<'b> MatrixExtFromIter<&'b M::Element> {
        MatrixExtFromIter::from_iter(self.iter(), self.num_cols())
    }
}
impl<'a, M: MatrixMutExt, S: AccessStrategy<M>> Access<'a, M, S> {    
    pub fn clone_into(&self) -> M 
    where 
        M: Clone,
        <M as MatrixExt>::Element: Clone
    {
        self.matrix.duplicate()
    }
}

impl<'a, M: MatrixMutExt, S: AccessStrategy<M>> AccessMut<'a, M, S> {
    pub(crate) fn new(matrix: &'a mut M, strategy: S) -> Self {
        Self { matrix, strategy }
    }
        
    pub fn clone_into(&self) -> M 
    where 
        M: Clone,
        <M as MatrixExt>::Element: Clone
    {
        self.matrix.duplicate()
    }
}

impl Observer {
    pub fn new(dimensions: (usize, usize)) -> Self {
        Self {
            rows: dimensions.0,
            cols: dimensions.1
        }
    }
    
    pub fn update_dimensions(&mut self, s: &dyn AccessStrategy<Self>) {
        self.rows = s.nrows(&self);
        self.cols = s.ncols(&self);
    }
}



// ### MatrixExt Implementation

impl MatrixExt for Observer {
    type Element = ();
    
    fn num_rows(&self) -> usize { self.rows }
    fn num_cols(&self) -> usize { self.cols }
    fn get(&self, i: usize, j: usize) -> Option<&()> { 
        if self.check(i, j) {
            return Some(&())
        } 
        None
    }
}

impl<'a, M: MatrixExt, S: AccessStrategy<M>> MatrixExt for Access<'a, M, S> {
    type Element = <M as MatrixExt>::Element;
    
    fn num_rows(&self) -> usize { self.strategy.nrows(&self.matrix) }
    fn num_cols(&self) -> usize { self.strategy.ncols(&self.matrix) }

    fn get(&self, row: usize, column: usize) -> Option<&Self::Element> { 
        let (i, j) = self.strategy.access(&self.matrix, row, column)?;
        self.matrix.get(i, j)
    }
}
impl<'a, M: MatrixMutExt, S: AccessStrategy<M>> MatrixExt for AccessMut<'a, M, S> {
    type Element = M::Element;
    
    fn num_rows(&self) -> usize { self.strategy.nrows(&self.matrix) }
    fn num_cols(&self) -> usize { self.strategy.ncols(&self.matrix) }

    fn get(&self, row: usize, column: usize) -> Option<&Self::Element> { 
        let (i, j) = self.strategy.access(&self.matrix, row, column)?;
        self.matrix.get(i, j) 
    }
}
impl<'a, M: MatrixMutExt, S: AccessStrategy<M>> MatrixMutExt for AccessMut<'a, M, S> {
    fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut Self::Element> { 
        let (i, j) = self.strategy.access(&self.matrix, row, column)?;
        self.matrix.get_mut(i, j) 
    }
}