AlgoPlus v0.1.0
Loading...
Searching...
No Matches
mat_2d.h
1#ifndef MAT_2D_H
2#define MAT_2D_H
3
4#ifdef __cplusplus
5#include <climits>
6#include <iostream>
7#include <vector>
8#include "mat_1d.h"
9#endif
10
15
16template <typename T, size_t ROWS, size_t COLS> class Mat2d {
17 private:
18 T* arr;
19 size_t _size;
20 size_t _cols;
21 size_t _rows;
23
24 public:
29 explicit Mat2d(std::vector<std::vector<T>> v = {})
30 : arr(new T[ROWS * COLS]), _cols(COLS), _rows(ROWS), _size(ROWS * COLS) {
31
32 if (!v.empty()) {
33 try {
34 if (v.size() * v[0].size() != _size || v[0].size() != _cols || v.size() != _rows) {
35 throw std::logic_error("Initializer array don't have the same "
36 "dimensions as the constructed array");
37 }
38 for (size_t i = 0; i < v.size(); i++) {
39 for (size_t j = 0; j < v[0].size(); j++) {
40 arr[i * v[0].size() + j] = v[i][j];
41 }
42 }
43 } catch (std::logic_error& e) {
44 std::cerr << e.what() << '\n';
45 }
46 }
47 }
48
49 ~Mat2d() { delete[] arr; }
54 explicit Mat2d(const T val) noexcept : _size(ROWS * COLS), _cols(COLS), _rows(ROWS) {
55 arr = new T[_size];
56 for (size_t i = 0; i < _size; i++) {
57 arr[i] = val;
58 }
59 }
60
66 explicit Mat2d(const Mat2d& mat) : _size(ROWS * COLS), _cols(COLS), _rows(ROWS) {
67 try {
68 if (mat.size() != _size || mat.cols() != _cols || mat.rows() != _rows) {
69 throw std::logic_error("Tried to copy matrixes with different dimensions");
70 }
71 this->arr = new T[_size];
72 for (size_t i = 0; i < _size; i++) {
73 this->arr[i] = mat.arr[i];
74 }
75 } catch (std::logic_error& e) {
76 std::cerr << e.what() << '\n';
77 }
78 }
79
86 if (this == &mat) {
87 return *(this);
88 }
89 try {
90 if (mat.size() != _size || mat.cols() != _cols || mat.rows() != _rows) {
91 throw std::logic_error("Tried to copy matrixes with different dimensions");
92 }
93 for (size_t i = 0; i < _size; i++) {
94 this->arr[i] = mat.arr[i];
95 }
96 return *(this);
97 } catch (std::logic_error& e) {
98 std::cerr << e.what() << '\n';
99 }
100 return *(this);
101 }
102
109 T& operator()(size_t i, size_t j) const { return arr[i * _cols + j]; }
110
117 size_t index = 0;
118 for (size_t j = i * _cols; j < i * _cols + _rows; j++) {
119 mat[index++] = arr[j];
120 }
121 return mat;
122 }
123
128 size_t size() const { return _size; }
129
134 size_t cols() const { return _cols; }
135
140 size_t rows() const { return _rows; }
141
142 class Iterator;
143 typedef T* iterator;
148 Iterator begin() { return Iterator(arr, _rows, _cols, 0); }
149
154 Iterator end() { return Iterator(arr, _rows, _cols, _size); }
155
159 friend std::ostream& operator<<(std::ostream& out, Mat2d& mat) {
160 out << '[';
161 for (size_t i = 0; i < mat.rows(); i++) {
162 out << '[';
163 for (size_t j = 0; j < mat.cols(); j++) {
164 out << mat.arr[i * mat.cols() + j];
165 if (j != mat.cols() - 1) {
166 out << " ";
167 }
168 }
169 out << ']';
170 if (i != mat.rows() - 1) {
171 out << '\n';
172 }
173 }
174 out << ']';
175 return out;
176 }
177};
178
182template <typename T, size_t ROWS, size_t COLS> class Mat2d<T, ROWS, COLS>::Iterator {
183 private:
184 T* arr;
185 size_t _rows;
186 size_t _cols;
187 size_t _index;
188 size_t _size;
189
190 public:
199 explicit Iterator(T* _arr, size_t rows, size_t cols, size_t index) noexcept
200 : _rows(rows), _cols(cols), _index(index), _size(rows * cols) {
201
202 arr = new T[_size];
203 for (size_t i = 0; i < _size; i++) {
204 arr[i] = _arr[i];
205 }
206 }
207
213 Iterator& operator=(T* curr) {
214 for (size_t i = 0; i < _size; i++) {
215 arr[i] = curr[i];
216 }
217 return *(this);
218 }
219
225 if (this->_index < _size) {
226 this->_index++;
227 }
228 return *(this);
229 }
230
236 Iterator it = *(this);
237 ++*(this);
238 return it;
239 }
240
246 if (this->_index > 0) {
247 this->_index--;
248 }
249 return *(this);
250 }
251
257 Iterator it = *(this);
258 --*(this);
259 return it;
260 }
261
267 bool operator!=(const Iterator& it) { return it.arr[it._index] != arr[this->_index]; }
268
273 template <typename A, size_t CCOLS> Mat1d<A, CCOLS>& operator*() {
274 Mat1d<T, COLS> mat(0);
275 size_t index = 0;
276 for (size_t j = this->_index * _cols; j < this->_index * _cols + _rows; ++j) {
277 mat[index++] = arr[j];
278 }
279 return mat;
280 }
281};
282#endif
Class for 1-dimensional Matrix.
Definition mat_1d.h:19
Iterator for Mat2d class.
Definition mat_2d.h:182
bool operator!=(const Iterator &it)
operator != for Iterator class
Definition mat_2d.h:267
Iterator(T *_arr, size_t rows, size_t cols, size_t index) noexcept
constructor for Iterator class
Definition mat_2d.h:199
Iterator & operator--(int)
operator – for Iterator class
Definition mat_2d.h:256
Mat1d< A, CCOLS > & operator*()
operator * for Iterator class
Definition mat_2d.h:273
Iterator & operator++()
operator ++ for Iterator class
Definition mat_2d.h:224
Iterator & operator=(T *curr)
operator = for Iterator class
Definition mat_2d.h:213
Iterator & operator--()
operator – for Iterator class
Definition mat_2d.h:245
Iterator & operator++(int)
Definition mat_2d.h:235
Class for 2-dimensional Matrix.
Definition mat_2d.h:16
size_t cols() const
cols function
Definition mat_2d.h:134
friend std::ostream & operator<<(std::ostream &out, Mat2d &mat)
operator << for Mat2d class
Definition mat_2d.h:159
T & operator()(size_t i, size_t j) const
operator (i, j) for Mat2d class
Definition mat_2d.h:109
Iterator begin()
begin() Iterator for Mat2d class
Definition mat_2d.h:148
Mat2d(const T val) noexcept
constructor for Mat2d class with initializer value
Definition mat_2d.h:54
Mat1d< T, COLS > & operator()(size_t i)
operator () for Mat2d class
Definition mat_2d.h:116
Mat2d(std::vector< std::vector< T > > v={})
constructor for Mat2d class
Definition mat_2d.h:29
Iterator end()
end() Iterator for Mat2d class
Definition mat_2d.h:154
size_t rows() const
rows function
Definition mat_2d.h:140
Mat2d & operator=(Mat2d &mat)
operator = for Mat2d class
Definition mat_2d.h:85
size_t size() const
size function
Definition mat_2d.h:128
Mat2d(const Mat2d &mat)
copy constructor for Mat2d class
Definition mat_2d.h:66