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
use crate::*;
use std::ops::{Index, IndexMut};

type Pair = (usize, usize);

// MatrixDxD
// --------------------------------------------------
impl<T> Index<Pair> for MatrixDxD<T> {
    type Output = T;
    fn index<'a>(&'a self, (row, column): (usize, usize)) -> &'a Self::Output {
        assert!(row < self.rows, "Row out of bounds");
        assert!(column < self.columns, "Columns out of bounds");
        &self.data[row * self.columns + column]
    }
}
impl<T> IndexMut<Pair> for MatrixDxD<T> {
    fn index_mut<'a>(&'a mut self, (row, column): (usize, usize)) -> &'a mut T {
        assert!(row < self.rows, "Row out of bounds");
        assert!(column < self.columns, "Columns out of bounds");
        &mut self.data[row * self.columns + column]
    }
}
// MatrixDxS
// --------------------------------------------------
impl<T, const COLUMNS: usize> Index<Pair> for MatrixDxS<T, COLUMNS> {
    type Output = T;
    fn index<'a>(&'a self, (row, column): (usize, usize)) -> &'a Self::Output {
        assert!(row < self.rows, "Row out of bounds");
        assert!(column < COLUMNS, "Columns out of bounds");
        &self.data[row * COLUMNS + column]
    }
}
impl<T, const COLUMNS: usize> IndexMut<Pair> for MatrixDxS<T, COLUMNS> {
    fn index_mut<'a>(&'a mut self, (row, column): (usize, usize)) -> &'a mut T {
        assert!(row < self.rows, "Row out of bounds");
        assert!(column < COLUMNS, "Columns out of bounds");
        &mut self.data[row * COLUMNS + column]
    }
}
// MatrixSxD
// --------------------------------------------------
impl<T, const ROWS: usize> Index<Pair> for MatrixSxD<T, ROWS> {
    type Output = T;
    fn index<'a>(&'a self, (row, column): (usize, usize)) -> &'a Self::Output {
        assert!(row < ROWS, "Row out of bounds");
        assert!(column < self.columns, "Columns out of bounds");
        &self.data[row * self.columns + column]
    }
}
impl<T, const ROWS: usize> IndexMut<Pair> for MatrixSxD<T, ROWS> {
    fn index_mut<'a>(&'a mut self, (row, column): (usize, usize)) -> &'a mut T {
        assert!(row < ROWS, "Row out of bounds");
        assert!(column < self.columns, "Columns out of bounds");
        &mut self.data[row * self.columns + column]
    }
}
// MatrixSxS
// --------------------------------------------------
impl<T, const ROWS: usize, const COLUMNS: usize> Index<Pair> for MatrixSxS<T, ROWS, COLUMNS>
where
    [(); ROWS * COLUMNS]:,
{
    type Output = T;
    fn index<'a>(&'a self, (row, column): (usize, usize)) -> &'a Self::Output {
        assert!(row < ROWS, "Row out of bounds");
        assert!(column < COLUMNS, "Columns out of bounds");
        &self.data[row * COLUMNS + column]
    }
}
impl<T, const ROWS: usize, const COLUMNS: usize> IndexMut<Pair> for MatrixSxS<T, ROWS, COLUMNS>
where
    [(); ROWS * COLUMNS]:,
{
    fn index_mut<'a>(&'a mut self, (row, column): (usize, usize)) -> &'a mut T {
        assert!(row < ROWS, "Row out of bounds");
        assert!(column < COLUMNS, "Columns out of bounds");
        &mut self.data[row * COLUMNS + column]
    }
}
// Tests
// --------------------------------------------------
#[cfg(test)]
mod tests {
    use crate::*;
    // MatrixDxD
    // --------------------------------------------------
    #[test]
    fn dxd() {
        let a = MatrixDxD::try_from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
        assert_eq!(a[(0, 0)], 1);
        assert_eq!(a[(0, 1)], 2);
        assert_eq!(a[(0, 2)], 3);
        assert_eq!(a[(1, 0)], 4);
        assert_eq!(a[(1, 1)], 5);
        assert_eq!(a[(1, 2)], 6);
    }
    // MatrixDxS
    // --------------------------------------------------
    #[test]
    fn dxs() {
        let a = MatrixDxS::from(vec![[1, 2, 3], [4, 5, 6]]);
        assert_eq!(a[(0, 0)], 1);
        assert_eq!(a[(0, 1)], 2);
        assert_eq!(a[(0, 2)], 3);
        assert_eq!(a[(1, 0)], 4);
        assert_eq!(a[(1, 1)], 5);
        assert_eq!(a[(1, 2)], 6);
    }
    // MatrixSxD
    // --------------------------------------------------
    #[test]
    fn sxd() {
        let a = MatrixSxD::try_from([vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
        assert_eq!(a[(0, 0)], 1);
        assert_eq!(a[(0, 1)], 2);
        assert_eq!(a[(0, 2)], 3);
        assert_eq!(a[(1, 0)], 4);
        assert_eq!(a[(1, 1)], 5);
        assert_eq!(a[(1, 2)], 6);
    }
    // MatrixSxS
    // --------------------------------------------------
    #[test]
    fn sxs() {
        let a = MatrixSxS::<i32, 2, 3>::from([[1, 2, 3], [4, 5, 6]]);
        assert_eq!(a[(0, 0)], 1);
        assert_eq!(a[(0, 1)], 2);
        assert_eq!(a[(0, 2)], 3);
        assert_eq!(a[(1, 0)], 4);
        assert_eq!(a[(1, 1)], 5);
        assert_eq!(a[(1, 2)], 6);
    }
}