aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics/string_matrix.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-19 13:21:54 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:56 +0200
commit55c93fe609888be73677317978959040cf35b2ff (patch)
tree41ee53961eb5667136432b69773ef7acf3ad8259 /src/engine/graphics/string_matrix.cpp
parent020303df1410d10546f53d0bfee4f48797d4f067 (diff)
refactor: implement matrix iterator
Diffstat (limited to 'src/engine/graphics/string_matrix.cpp')
-rw-r--r--src/engine/graphics/string_matrix.cpp117
1 files changed, 105 insertions, 12 deletions
diff --git a/src/engine/graphics/string_matrix.cpp b/src/engine/graphics/string_matrix.cpp
index ae06755..3920b8e 100644
--- a/src/engine/graphics/string_matrix.cpp
+++ b/src/engine/graphics/string_matrix.cpp
@@ -1,30 +1,59 @@
#include "string_matrix.hpp"
+#include <iostream>
+#include <ranges>
+
StringMatrix::StringMatrix(const Bounds &bounds) noexcept
- : _rows(bounds.get_height()), _columns(bounds.get_width())
+ : _matrix(new std::string_view *[bounds.get_height()]),
+ _row_cnt(bounds.get_height()),
+ _column_cnt(bounds.get_width())
{
- _matrix.reserve(bounds.get_height());
- _matrix.assign(_matrix.capacity(), std::vector<std::string_view>(bounds.get_width()));
+ for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt; row++)
+ {
+ *row = static_cast<gsl::owner<std::string_view *>>(
+ new std::string_view[bounds.get_width()]);
+ }
};
+StringMatrix::StringMatrix(const StringMatrix &string_matrix) noexcept
+ : _matrix(new std::string_view *[string_matrix._row_cnt]),
+ _row_cnt(string_matrix._row_cnt),
+ _column_cnt(string_matrix._column_cnt)
+{
+ _copy_matrix_from(string_matrix);
+}
+
+StringMatrix::StringMatrix(StringMatrix &&string_matrix) noexcept
+ : _matrix(string_matrix._matrix),
+ _row_cnt(string_matrix._row_cnt),
+ _column_cnt(string_matrix._column_cnt)
+{
+ string_matrix._matrix = nullptr;
+}
+
+StringMatrix::~StringMatrix() noexcept
+{
+ _delete_matrix();
+}
+
void StringMatrix::fill(std::string_view element) noexcept
{
- for (uint32_t row = 0U; row < _matrix.capacity(); row++)
+ for (auto row : *this)
{
- std::vector<std::string_view> row_vector = _matrix[row];
-
- for (uint32_t column = 0U; column < row_vector.capacity(); column++)
+ for (auto &col : row)
{
- _matrix[row][column] = element;
+ col = element;
}
}
}
std::string_view StringMatrix::get(const Vector2 &pos) const noexcept
{
+
auto x = static_cast<std::size_t>(pos.get_x());
auto y = static_cast<std::size_t>(pos.get_y());
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
return _matrix[y][x];
}
@@ -33,15 +62,79 @@ void StringMatrix::set(const Vector2 &pos, std::string_view element) noexcept
auto x = static_cast<std::size_t>(pos.get_x());
auto y = static_cast<std::size_t>(pos.get_y());
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
_matrix[y][x] = element;
}
-uint32_t StringMatrix::rows() const noexcept
+uint32_t StringMatrix::get_row_cnt() const noexcept
+{
+ return _row_cnt;
+}
+
+uint32_t StringMatrix::get_column_cnt() const noexcept
+{
+ return _column_cnt;
+}
+
+MatrixIterator<std::string_view> StringMatrix::begin() const noexcept
+{
+ return MatrixIterator(_matrix, _column_cnt);
+}
+
+MatrixIterator<std::string_view> StringMatrix::end() const noexcept
{
- return _rows;
+ return MatrixIterator(_matrix + _row_cnt, _column_cnt);
}
-uint32_t StringMatrix::columns() const noexcept
+StringMatrix &StringMatrix::operator=(const StringMatrix &rhs) noexcept
{
- return _columns;
+ if (&rhs != this)
+ {
+ _delete_matrix();
+ _matrix = nullptr;
+
+ _matrix = new std::string_view *[rhs._row_cnt];
+ _copy_matrix_from(rhs);
+ }
+
+ return *this;
+}
+
+StringMatrix &StringMatrix::operator=(StringMatrix &&rhs) noexcept
+{
+ if (&rhs != this)
+ {
+ _delete_matrix();
+ _matrix = rhs._matrix;
+ rhs._matrix = nullptr;
+ }
+
+ return *this;
+}
+
+void StringMatrix::_delete_matrix() noexcept
+{
+ for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt; row++)
+ {
+ delete[](*row);
+ }
+
+ delete[] _matrix;
+}
+
+void StringMatrix::_copy_matrix_from(const StringMatrix &source) noexcept
+{
+ gsl::owner<std::string_view **> source_row = source._matrix;
+
+ for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt;
+ row++, source_row++)
+ {
+ *row = static_cast<gsl::owner<std::string_view *>>(
+ new std::string_view[_column_cnt]);
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ auto *end = *source_row + _column_cnt;
+
+ std::copy(*source_row, end, *row);
+ }
}