diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-08 16:47:48 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 7e84d664079d9c407bdf94861825bb05ccf1b0f7 (patch) | |
tree | 6c0caeb168379a4978b2bc074133f3942276af47 /src/engine/graphics | |
parent | 45e3ae285c63d91dcac426cffdd9d9648e48399b (diff) |
refactor: move matrix iterator to engine graphics folder
Diffstat (limited to 'src/engine/graphics')
-rw-r--r-- | src/engine/graphics/matrix.hpp | 2 | ||||
-rw-r--r-- | src/engine/graphics/matrix_iterator.hpp | 65 | ||||
-rw-r--r-- | src/engine/graphics/matrix_iterator_impl.hpp | 115 |
3 files changed, 181 insertions, 1 deletions
diff --git a/src/engine/graphics/matrix.hpp b/src/engine/graphics/matrix.hpp index fb08b29..7b4d037 100644 --- a/src/engine/graphics/matrix.hpp +++ b/src/engine/graphics/matrix.hpp @@ -4,7 +4,7 @@ #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" -#include "engine/matrix_iterator.hpp" +#include "engine/graphics/matrix_iterator.hpp" #include <gsl/pointers> diff --git a/src/engine/graphics/matrix_iterator.hpp b/src/engine/graphics/matrix_iterator.hpp new file mode 100644 index 0000000..70a59bf --- /dev/null +++ b/src/engine/graphics/matrix_iterator.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include <gsl/pointers> + +template <typename Element> +class MatrixRowIterator +{ +public: + using ColumnPtr = gsl::owner<Element *>; + + explicit MatrixRowIterator(ColumnPtr column_ptr) noexcept; + + auto operator++() noexcept -> MatrixRowIterator &; + auto operator++(int) noexcept -> MatrixRowIterator; + + auto operator*() const noexcept -> Element &; + + auto operator==(const MatrixRowIterator &rhs) const noexcept -> bool; + auto operator!=(const MatrixRowIterator &rhs) const noexcept -> bool; + +private: + ColumnPtr _column_ptr; +}; + +template <typename Element> +class MatrixRow +{ +public: + using RowPtr = gsl::owner<Element *>; + + explicit MatrixRow(RowPtr row_ptr, const uint32_t &column_cnt) noexcept; + + [[nodiscard]] auto begin() const noexcept -> MatrixRowIterator<Element>; + + [[nodiscard]] auto end() const noexcept -> MatrixRowIterator<Element>; + +private: + RowPtr _row_ptr; + + const uint32_t &_column_cnt; +}; + +template <typename Element> +class MatrixIterator +{ +public: + using RowPtr = gsl::owner<Element **>; + + explicit MatrixIterator(RowPtr row_ptr, const uint32_t &column_cnt) noexcept; + + auto operator++() noexcept -> MatrixIterator &; + auto operator++(int) noexcept -> MatrixIterator; + + auto operator*() const noexcept -> MatrixRow<Element>; + + auto operator==(const MatrixIterator &rhs) const noexcept -> bool; + auto operator!=(const MatrixIterator &rhs) const noexcept -> bool; + +private: + RowPtr _row_ptr; + + const uint32_t &_column_cnt; +}; + +#include "matrix_iterator_impl.hpp" diff --git a/src/engine/graphics/matrix_iterator_impl.hpp b/src/engine/graphics/matrix_iterator_impl.hpp new file mode 100644 index 0000000..4b2c785 --- /dev/null +++ b/src/engine/graphics/matrix_iterator_impl.hpp @@ -0,0 +1,115 @@ +#pragma once + +#include "matrix_iterator.hpp" + +// Matrix row iterator + +template <typename Element> +MatrixRowIterator<Element>::MatrixRowIterator(ColumnPtr column_ptr) noexcept + : _column_ptr(column_ptr) +{ +} + +template <typename Element> +auto MatrixRowIterator<Element>::operator++() noexcept -> MatrixRowIterator<Element> & +{ + ++_column_ptr; + + return *this; +} + +template <typename Element> +auto MatrixRowIterator<Element>::operator++(int) noexcept -> MatrixRowIterator<Element> +{ + auto copy = *this; + + ++(*this); + + return copy; +} + +template <typename Element> +auto MatrixRowIterator<Element>::operator*() const noexcept -> Element & +{ + return *_column_ptr; +} + +template <typename Element> +auto MatrixRowIterator<Element>::operator==(const MatrixRowIterator &rhs) const noexcept + -> bool +{ + return _column_ptr == rhs._column_ptr; +} + +template <typename Element> +auto MatrixRowIterator<Element>::operator!=(const MatrixRowIterator &rhs) const noexcept + -> bool +{ + return _column_ptr != rhs._column_ptr; +} + +// Matrix row + +template <typename Element> +MatrixRow<Element>::MatrixRow(RowPtr row_ptr, const uint32_t &column_cnt) noexcept + : _row_ptr(row_ptr), _column_cnt(column_cnt) +{ +} + +template <typename Element> +auto MatrixRow<Element>::begin() const noexcept -> MatrixRowIterator<Element> +{ + return MatrixRowIterator<Element>(_row_ptr); +} + +template <typename Element> +auto MatrixRow<Element>::end() const noexcept -> MatrixRowIterator<Element> +{ + return MatrixRowIterator<Element>(_row_ptr + _column_cnt); +} + +// Matrix iterator + +template <typename Element> +MatrixIterator<Element>::MatrixIterator( + RowPtr row_ptr, + const uint32_t &column_cnt) noexcept + : _row_ptr(row_ptr), _column_cnt(column_cnt) +{ +} + +template <typename Element> +auto MatrixIterator<Element>::operator++() noexcept -> MatrixIterator<Element> & +{ + ++_row_ptr; + + return *this; +} + +template <typename Element> +auto MatrixIterator<Element>::operator++(int) noexcept -> MatrixIterator<Element> +{ + auto copy = *this; + + ++(*this); + + return copy; +} + +template <typename Element> +auto MatrixIterator<Element>::operator*() const noexcept -> MatrixRow<Element> +{ + return MatrixRow(*_row_ptr, _column_cnt); +} + +template <typename Element> +auto MatrixIterator<Element>::operator==(const MatrixIterator &rhs) const noexcept -> bool +{ + return _row_ptr == rhs._row_ptr; +} + +template <typename Element> +auto MatrixIterator<Element>::operator!=(const MatrixIterator &rhs) const noexcept -> bool +{ + return _row_ptr != rhs._row_ptr; +} |