From af1378cc5598f7695e4301910dc2aabbcc919dcb Mon Sep 17 00:00:00 2001 From: Julian Kent Date: Mon, 16 Sep 2019 15:53:15 +0200 Subject: [PATCH] Add Slice class --- matrix/Slice.hpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 matrix/Slice.hpp diff --git a/matrix/Slice.hpp b/matrix/Slice.hpp new file mode 100644 index 0000000000..cff1521690 --- /dev/null +++ b/matrix/Slice.hpp @@ -0,0 +1,81 @@ +/** + * @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; +}; + +}