1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#pragma once
#include "matrix.hpp"
template <typename Element>
Matrix<Element>::Matrix(const Bounds &bounds) noexcept
: _matrix(new Element *[bounds.get_height()]),
_row_cnt(bounds.get_height()),
_column_cnt(bounds.get_width())
{
for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++)
{
*row = static_cast<gsl::owner<Element *>>(new Element[bounds.get_width()]);
}
};
template <typename Element>
Matrix<Element>::Matrix(const Matrix &matrix) noexcept
: _matrix(new Element *[matrix._row_cnt]),
_row_cnt(matrix._row_cnt),
_column_cnt(matrix._column_cnt)
{
_copy_matrix_from(matrix);
}
template <typename Element>
Matrix<Element>::Matrix(Matrix &&matrix) noexcept
: _matrix(matrix._matrix), _row_cnt(matrix._row_cnt), _column_cnt(matrix._column_cnt)
{
matrix._matrix = nullptr;
}
template <typename Element>
Matrix<Element>::~Matrix() noexcept
{
_delete_matrix();
}
template <typename Element>
void Matrix<Element>::fill(Element element) noexcept
{
for (auto row : *this)
{
for (auto &col : row)
{
col = element;
}
}
}
template <typename Element>
Element Matrix<Element>::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];
}
template <typename Element>
void Matrix<Element>::set(const Vector2 &pos, Element 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;
}
template <typename Element>
uint32_t Matrix<Element>::get_row_cnt() const noexcept
{
return _row_cnt;
}
template <typename Element>
uint32_t Matrix<Element>::get_column_cnt() const noexcept
{
return _column_cnt;
}
template <typename Element>
MatrixIterator<Element> Matrix<Element>::begin() const noexcept
{
return MatrixIterator(_matrix, _column_cnt);
}
template <typename Element>
MatrixIterator<Element> Matrix<Element>::end() const noexcept
{
return MatrixIterator(_matrix + _row_cnt, _column_cnt);
}
template <typename Element>
Matrix<Element> &Matrix<Element>::operator=(const Matrix &rhs) noexcept
{
if (&rhs != this)
{
_delete_matrix();
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
_matrix = nullptr;
_matrix = new Element *[rhs._row_cnt];
_copy_matrix_from(rhs);
}
return *this;
}
template <typename Element>
Matrix<Element> &Matrix<Element>::operator=(Matrix &&rhs) noexcept
{
if (&rhs != this)
{
_delete_matrix();
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
_matrix = rhs._matrix;
rhs._matrix = nullptr;
}
return *this;
}
template <typename Element>
void Matrix<Element>::_delete_matrix() noexcept
{
for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++)
{
delete[](*row);
}
delete[] _matrix;
}
template <typename Element>
void Matrix<Element>::_copy_matrix_from(const Matrix &source) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
gsl::owner<Element **> source_row = source._matrix;
for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt;
row++, source_row++)
{
*row = static_cast<gsl::owner<Element *>>(new Element[_column_cnt]);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
auto *end = *source_row + _column_cnt;
std::copy(*source_row, end, *row);
}
}
|