/** * @file Slice.hpp * * A simple matrix template library. * * @author Julian Kent < julian@auterion.com > */ #pragma once #include "math.hpp" namespace matrix { template class Matrix; template class Vector; template class Slice { public: Slice(size_t x0, size_t y0, const Matrix* data) : _x0(x0), _y0(y0), _data(const_cast*>(data)) { static_assert(P <= M, "Slice rows bigger than backing matrix"); static_assert(Q <= N, "Slice cols bigger than backing matrix"); } Type operator()(size_t i, size_t j) const { return (*_data)(_x0 + i, _y0 + j); } Type &operator()(size_t i, size_t j) { return (*_data)(_x0 + i, _y0 + j); } template Slice& operator=(const Slice& in_matrix) { Slice& self = *this; for (size_t i = 0; i < P; i++) { for (size_t j = 0; j < Q; j++) { self(i, j) = in_matrix(i, j); } } return self; } Slice& operator=(const Matrix& in_matrix) { Slice& self = *this; for (size_t i = 0; i < P; i++) { for (size_t j = 0; j < Q; j++) { self(i, j) = in_matrix(i, j); } } return self; } // allow assigning vectors to a slice that are in the axis Slice& operator=(const Vector& in_vector) { Slice& self = *this; for (size_t j = 0; j < Q; j++) { self(0, j) = in_vector(j); } return self; } private: size_t _x0, _y0; Matrix* _data; }; }