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
use crate::tensor::{
    borrow_tensor::BorrowTensor,
    matrix::{Matrix, MatrixTrait},
    tensor_shape::{HasTensorShape, TensorShape},
    tensor_storage::{HasTensorData, TensorStorage},
    Unitless,
};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MatrixView<'a, Dtype> {
    shape: TensorShape,
    data: &'a TensorStorage<Dtype>,
}

impl<'a, Dtype: 'a> BorrowTensor<'a, Dtype> for Matrix<Dtype> {
    type Output = MatrixView<'a, Dtype>;

    fn create_borrowed_tensor(
        shape: TensorShape,
        data: &'a TensorStorage<Dtype>,
    ) -> Self::Output {
        MatrixView {
            shape,
            data,
        }
    }
}

impl<'a, Dtype> HasTensorData<Dtype> for MatrixView<'a, Dtype> {
    fn data(&self) -> &TensorStorage<Dtype> {
        &self.data
    }
}

impl<'a, Dtype> HasTensorShape for MatrixView<'a, Dtype> {
    fn shape(&self) -> &TensorShape {
        &self.shape
    }
}

impl<'a, Dtype> MatrixTrait<Dtype> for MatrixView<'a, Dtype> {
    fn num_rows(&self) -> Unitless {
        self.shape.dims_strides[0].0
    }

    fn num_columns(&self) -> Unitless {
        self.shape.dims_strides[1].0
    }
}