From 6d2055e9140ae298780484dd49da345918b8f7e5 Mon Sep 17 00:00:00 2001 From: ancarola Date: Fri, 28 Jun 2019 22:18:39 +0200 Subject: Iterators compacting code improvements, still not compiles --- include/mm/experiments/iterators.hpp | 622 +++++++++++++++++++++++++++++++++++ include/mm/mmmatrix.hpp | 381 ++++++--------------- 2 files changed, 729 insertions(+), 274 deletions(-) create mode 100644 include/mm/experiments/iterators.hpp diff --git a/include/mm/experiments/iterators.hpp b/include/mm/experiments/iterators.hpp new file mode 100644 index 0000000..4afd58f --- /dev/null +++ b/include/mm/experiments/iterators.hpp @@ -0,0 +1,622 @@ +#pragma once + +template +class mm::vector_iterator +{ + std::size_t index; // variable index + + mm::basic_matrix& M; + + const std::size_t position; // fixed index + const bool direction; // true = row, false = column + +public: + template + friend class vector_iterator; + + vector_iterator(mm::basic_matrix& M, std::size_t position, bool direction); + + mm::vector_iterator operator++() + { + vector_iterator it = *this; + ++index; + return it; + } + + mm::vector_iterator operator--() + { + vector_iterator it = *this; + --index; + return it; + } + + mm::vector_iterator& operator++(int) + { + ++index; + return *this; + } + + mm::vector_iterator& operator--(int) + { + --index; + return *this; + } + + bool operator==(const mm::vector_iterator& other) const + { + return index == other.index; + } + + bool operator=!(const mm::vector_iterator& other) const + { + return index != other.index; + } + + T& operator*() const; + T& operator[](std::size_t); +}; + +template +class diag_iterator +{ + std::size_t index; // variable index + + mm::square_matrix& M; + + const int position; // fixed diagonal index + +public: + template + friend class diag_iterator; + + diag_iterator(mm::square_matrix& M, std::size_t position, bool direction); + + mm::diag_iterator operator++() + { + diag_iterator it = *this; + ++index; + return it; + } + + mm::diag_iterator operator--() + { + diag_iterator it = *this; + --index; + return it; + } + + mm::diag_iterator& operator++(int) + { + ++index; + return *this; + } + + mm::diag_iterator& operator--(int) + { + --index; + return *this; + } + + bool operator==(const mm::diag_iterator& other) const + { + return index == other.index; + } + + bool operator=!(const mm::diag_iterator& other) const + { + return index != other.index; + } + + T& operator*() const; +}; + +/* Const Iterators */ + +template +class mm::const_vector_iterator +{ + std::size_t index; // variable index + + const mm::basic_matrix& M; + + const std::size_t position; // fixed index + const bool direction; // true = row, false = column + +public: + const_vector_iterator(mm::basic_matrix& M, std::size_t position, bool direction); + + mm::const_vector_iterator operator++() + { + vector_iterator it = *this; + ++index; + return it; + } + + mm::const_vector_iterator operator--() + { + vector_iterator it = *this; + --index; + return it; + } + + mm::const_vector_iterator& operator++(int) + { + ++index; + return *this; + } + + mm::const_vector_iterator& operator--(int) + { + --index; + return *this; + } + + bool operator==(const mm::const_vector_iterator& other) const + { + return index == other; + } + + bool operator=!(const mm::const_vector_iterator& other) const + { + return index != other; + } + + const T& operator*() const; + const T& operator[](std::size_t) const; +}; + +template +class const_diag_iterator +{ + std::size_t index; // variable index + + const mm::square_matrix& M; + + const int position; // fixed diagonal index + +public: + template + friend class const_diag_iterator; + + const_diag_iterator(const mm::square_matrix& M, std::size_t position, bool direction); + + mm::const_diag_iterator operator++() + { + const_diag_iterator it = *this; + ++index; + return it; + } + + mm::const_diag_iterator operator--() + { + const_diag_iterator it = *this; + --index; + return it; + } + + mm::const_diag_iterator& operator++(int) + { + ++index; + return *this; + } + + mm::const_diag_iterator& operator--(int) + { + --index; + return *this; + } + + bool operator==(const mm::const_diag_iterator& other) const + { + return index == other.index; + } + + bool operator=!(const mm::const_diag_iterator& other) const + { + return index != other.index; + } + + const T& operator*() const; +}; + +/* Iterators implementations */ + +template +mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +T& mm::vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +T& mm::vector_iterator::operator[](std::size_t i) +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +template +mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +const T& mm::const_vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +const T& mm::const_vector_iterator::operator[](std::size_t i) const +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::const_diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + + +/* Iterators implementations */ + +template +mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +T& mm::vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +T& mm::vector_iterator::operator[](std::size_t i) +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +template +mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +const T& mm::const_vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +const T& mm::const_vector_iterator::operator[](std::size_t i) const +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::const_diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +/* Iterators implementations */ + +template +mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +T& mm::vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +T& mm::vector_iterator::operator[](std::size_t i) +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +template +mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +const T& mm::const_vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +const T& mm::const_vector_iterator::operator[](std::size_t i) const +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::const_diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +/* Iterators implementations */ + +template +mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +T& mm::vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +T& mm::vector_iterator::operator[](std::size_t i) +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +template +mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +const T& mm::const_vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +const T& mm::const_vector_iterator::operator[](std::size_t i) const +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::const_diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +/* Iterators implementations */ + +template +mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +T& mm::vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +T& mm::vector_iterator::operator[](std::size_t i) +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + +template +mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) + index(0), M(_M), position(pos), direction(dir) +{ + assert((dir && pos < Cols) || (!dir && pos < Rows)) +} + +template +const T& mm::const_vector_iterator::operator*() const +{ + return (direction) ? + M.data[position * Cols + index] : + M.data[index * Cols + position]; +} + +template +const T& mm::const_vector_iterator::operator[](std::size_t i) const +{ + return (direction) ? + M.data[position * Cols + i] : + M.data[i * Cols + position]; +} + +template +mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) + index(0), M(_M), position(pos) +{ + assert(abs(pos) < N) // pos bounded between ]-N, N[ +} + +template +T& mm::const_diag_iterator::operator*() const +{ + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; +} + + diff --git a/include/mm/mmmatrix.hpp b/include/mm/mmmatrix.hpp index c11b626..d366df2 100644 --- a/include/mm/mmmatrix.hpp +++ b/include/mm/mmmatrix.hpp @@ -23,9 +23,8 @@ namespace mm { template class basic_matrix; - // TODO, not sure it's a good idea - //template - //class transposed_matrix; + template + class transposed_matrix; /* specialization of basic_matrx for Cols = 1 */ template @@ -43,246 +42,112 @@ namespace mm { template class square_matrix; - template + template class diagonal_matrix; /* * Iterators */ - template + /* + * Most abstract type of iterator + * IterType = Row, Col, Diag + * Grid = constness of mm::basic_matrix + */ + template class Grid> class vector_iterator; - - template - class diag_iterator; - - template - class const_vector_iterator; - - template - class const_diag_iterator; } -/* Non-const Iterators */ +#define MM_ROW_ITER 0 +#define MM_COL_ITER 1 +#define MM_DIAG_ITER 2 -template +template class Grid> class mm::vector_iterator { std::size_t index; // variable index - mm::basic_matrix& M; + Grid& M; - const std::size_t position; // fixed index - const bool direction; // true = row, false = column + const int position; // fixed index, negative too for diagonal iterator public: - template + template class OGrid> friend class vector_iterator; - vector_iterator(mm::basic_matrix& M, std::size_t position, bool direction); + vector_iterator(Grid& M, int position, std::size_t index = 0); - mm::vector_iterator operator++() + mm::vector_iterator operator++() { - vector_iterator it = *this; + vector_iterator it = *this; ++index; return it; } - mm::vector_iterator operator--() + mm::vector_iterator operator--() { - vector_iterator it = *this; + vector_iterator it = *this; --index; return it; } - mm::vector_iterator& operator++(int) + mm::vector_iterator& operator++(int) { ++index; return *this; } - mm::vector_iterator& operator--(int) + mm::vector_iterator& operator--(int) { --index; return *this; } - bool operator==(const mm::vector_iterator& other) const + bool operator==(const mm::vector_iterator& other) const { return index == other.index; } - bool operator=!(const mm::vector_iterator& other) const + bool operator!=(const mm::vector_iterator& other) const { return index != other.index; } - T& operator*() const; - T& operator[](std::size_t); -}; - -template -class diag_iterator -{ - std::size_t index; // variable index - - mm::square_matrix& M; - - const int position; // fixed diagonal index - -public: - template - friend class diag_iterator; - - diag_iterator(mm::square_matrix& M, std::size_t position, bool direction); - - mm::diag_iterator operator++() - { - diag_iterator it = *this; - ++index; - return it; - } - - mm::diag_iterator operator--() - { - diag_iterator it = *this; - --index; - return it; - } - - mm::diag_iterator& operator++(int) - { - ++index; - return *this; - } - - mm::diag_iterator& operator--(int) - { - --index; - return *this; - } - - bool operator==(const mm::diag_iterator& other) const + bool ok() const { - return index == other.index; - } - - bool operator=!(const mm::diag_iterator& other) const - { - return index != other.index; + if constexpr(IterType == MM_ROW_ITER) + return index < Cols; + else + return index < Rows; } T& operator*() const; + T& operator[](std::size_t); }; -/* Const Iterators */ - -template -class mm::const_vector_iterator -{ - std::size_t index; // variable index - - const mm::basic_matrix& M; - - const std::size_t position; // fixed index - const bool direction; // true = row, false = column - -public: - const_vector_iterator(mm::basic_matrix& M, std::size_t position, bool direction); - - mm::const_vector_iterator operator++() - { - vector_iterator it = *this; - ++index; - return it; - } - - mm::const_vector_iterator operator--() - { - vector_iterator it = *this; - --index; - return it; - } - - mm::const_vector_iterator& operator++(int) - { - ++index; - return *this; - } - - mm::const_vector_iterator& operator--(int) - { - --index; - return *this; - } - - bool operator==(const mm::const_vector_iterator& other) const - { - return index == other; - } - - bool operator=!(const mm::const_vector_iterator& other) const - { - return index != other; - } - - const T& operator*() const; - const T& operator[](std::size_t) const; -}; - -template -class const_diag_iterator -{ - std::size_t index; // variable index - - const mm::square_matrix& M; - - const int position; // fixed diagonal index +/* Row Iterators */ -public: - template - friend class const_diag_iterator; - - const_diag_iterator(const mm::square_matrix& M, std::size_t position, bool direction); +namespace mm { - mm::const_diag_iterator operator++() - { - const_diag_iterator it = *this; - ++index; - return it; - } + template + using row_iterator = vector_iterator; - mm::const_diag_iterator operator--() - { - const_diag_iterator it = *this; - --index; - return it; - } + template + using col_iterator = vector_iterator; - mm::const_diag_iterator& operator++(int) - { - ++index; - return *this; - } + template + using const_row_iterator = vector_iterator; - mm::const_diag_iterator& operator--(int) - { - --index; - return *this; - } + template + using const_col_iterator = vector_iterator; - bool operator==(const mm::const_diag_iterator& other) const - { - return index == other.index; - } + template + using diag_iterator = vector_iterator; - bool operator=!(const mm::const_diag_iterator& other) const - { - return index != other.index; - } + template + using const_diag_iterator = vector_iterator; +} - const T& operator*() const; -}; /* * Matrix class @@ -315,20 +180,25 @@ public: template basic_matrix(const basic_matrix& other); - // access data + // access data, basic definition virtual T& at(std::size_t row, std::size_t col); virtual const T& at(std::size_t row, std::size_t col) const; // allows to access a matrix M at row j col k with M[j][k] - virtual auto operator[](std::size_t index); + auto operator[](std::size_t index); + + virtual auto row_begin(std::size_t index) + { + return mm::row_iterator(*this, static_cast(index)); + } void swap_rows(std::size_t x, std::size_t y); void swap_cols(std::size_t x, std::size_t y); // mathematical operations // TODO, simply switch iteration mode - virtual basic_matrix transposed() const; - inline basic_matrix td() const { return transposed(); } + //virtual basic_matrix transposed() const; + //inline basic_matrix td() const { return transposed(); } /// downcast to square matrix @@ -441,13 +311,16 @@ auto mm::basic_matrix::operator[](std::size_t index) { if constexpr (is_row_vec() || is_col_vec()) { return data.at(index); } else { - return row_vec( + return row_begin(index); + + /*return row_vec( data.cbegin() + (index * Cols), data.cbegin() + ((index + 1) * Cols) + 1 - ); + );*/ } } + template void mm::basic_matrix::swap_rows(std::size_t x, std::size_t y) { if (x == y) @@ -466,7 +339,7 @@ void mm::basic_matrix::swap_cols(std::size_t x, std::size_t y) { std::swap(this->at(row, x), this->at(row, y)); } -template +/*template mm::basic_matrix mm::basic_matrix::transposed() const { mm::basic_matrix result; @@ -475,7 +348,7 @@ mm::basic_matrix mm::basic_matrix::transposed() const { result.at(col, row) = this->at(row, col); return result; -} +}*/ /* operator overloading */ @@ -582,7 +455,6 @@ public: /* * transposed matrix format - * TODO: write this class, or put a bool flag into the original one */ template @@ -602,11 +474,11 @@ public: } // allows to access a matrix M at row j col k with M[j][k] - virtual auto operator[](std::size_t index) override + virtual auto row_begin(std::size_t index) override { - // TODO, return other direction iterator + return mm::col_iterator(*this, static_cast(index)); } -} +}; /* square matrix specialization */ template @@ -615,8 +487,8 @@ public: using mm::basic_matrix::basic_matrix; /// in place transpose - void transpose(); - inline void t() { transpose(); } + //void transpose(); + //inline void t() { transpose(); } T trace(); inline T tr() { return trace(); } @@ -628,13 +500,13 @@ public: // TODO, downcast to K-diagonal, user defined cast - template + /*template operator mm::diagonal_matrix() const { // it's always possible to do it bidirectionally, // without loosing information - return dynamic_cast>(*this); - } + return reinterpret_cast>(*this); + }*/ // get the identity of size N static inline constexpr square_matrix identity() { @@ -653,7 +525,7 @@ public: */ template -class mm::diagonal_matrix : public mm::square_matrix +class mm::diagonal_matrix : public mm::square_matrix { public: using mm::square_matrix::square_matrix; @@ -662,100 +534,61 @@ public: // TODO, matrix multiplication }; -template +/*template void mm::square_matrix::transpose() { for (unsigned row = 0; row < N; row++) for (unsigned col = 0; col < row; col++) std::swap(this->at(row, col), this->at(col, row)); -} +}*/ template T mm::square_matrix::trace() { + T sum = 0; - for (unsigned i = 0; i < N; i++) - sum += this->at(i, i); + for (mm::diag_iterator it(*this, 0); it.ok(); ++it) + sum += *it; return sum; } -/* Iterators implementations */ - -template -mm::vector_iterator::vector_iterator(mm::basic_matrix& _M, std::size_t pos, bool dir) - index(0), M(_M), position(pos), direction(dir) -{ - assert((dir && pos < Cols) || (!dir && pos < Rows)) -} - -template -T& mm::vector_iterator::operator*() const -{ - return (direction) ? - M.data[position * Cols + index] : - M.data[index * Cols + position]; -} +/* Iterators implementation */ -template -T& mm::vector_iterator::operator[](std::size_t i) +template class Grid> +mm::vector_iterator::vector_iterator(Grid& _M, int pos, std::size_t i) + : index(i), M(_M), position(pos) { - return (direction) ? - M.data[position * Cols + i] : - M.data[i * Cols + position]; -} - -template -mm::diag_iterator::diag_iterator(mm::square_matrix& _M, int pos) - index(0), M(_M), position(pos) -{ - assert(abs(pos) < N) // pos bounded between ]-N, N[ -} - -template -T& mm::diag_iterator::operator*() const -{ - return (k > 0) ? - M.data[(index + position) * Cols + index] : - M.data[index * Cols + (index - position)]; -} - - -template -mm::const_vector_iterator::const_vector_iterator(const mm::basic_matrix& _M, std::size_t pos, bool dir) - index(0), M(_M), position(pos), direction(dir) -{ - assert((dir && pos < Cols) || (!dir && pos < Rows)) -} - -template -const T& mm::const_vector_iterator::operator*() const -{ - return (direction) ? - M.data[position * Cols + index] : - M.data[index * Cols + position]; -} - -template -const T& mm::const_vector_iterator::operator[](std::size_t i) const -{ - return (direction) ? - M.data[position * Cols + i] : - M.data[i * Cols + position]; + if constexpr (IterType == MM_ROW_ITER) { + assert(pos < Cols); + } else if constexpr (IterType == MM_COL_ITER) { + assert(pos < Rows); + } else if constexpr (IterType == MM_DIAG_ITER) { + assert(abs(pos) < Rows); + } } -template -mm::const_diag_iterator::const_diag_iterator(const mm::square_matrix& _M, int pos) - index(0), M(_M), position(pos) +template class Grid> +T& mm::vector_iterator::operator*() const { - assert(abs(pos) < N) // pos bounded between ]-N, N[ + if constexpr (IterType == MM_ROW_ITER) + return M.data[position * Cols + index]; + else if constexpr (IterType == MM_COL_ITER) + return M.data[index * Cols + position]; + else if constexpr (IterType == MM_DIAG_ITER) + return (k > 0) ? + M.data[(index + position) * Cols + index] : + M.data[index * Cols + (index - position)]; } -template -T& mm::const_diag_iterator::operator*() const +template class Grid> +T& mm::vector_iterator::operator[](std::size_t i) { - return (k > 0) ? - M.data[(index + position) * Cols + index] : - M.data[index * Cols + (index - position)]; + if constexpr (IterType == MM_ROW_ITER) + return M.data[position * Cols + i]; + else if constexpr (IterType == MM_COL_ITER) + return M.data[i * Cols + position]; + else if constexpr (IterType == MM_DIAG_ITER) + return (k > 0) ? + M.data[(i + position) * Cols + i] : + M.data[i * Cols + (i - position)]; } - - -- cgit v1.2.1