16template <
typename T,
size_t ROWS,
size_t COLS>
class Mat2d {
29 explicit Mat2d(std::vector<std::vector<T>> v = {})
30 : arr(new T[ROWS * COLS]), _cols(COLS), _rows(ROWS), _size(ROWS * COLS) {
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");
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];
43 } catch (std::logic_error& e) {
44 std::cerr << e.what() <<
'\n';
54 explicit Mat2d(
const T val) noexcept : _size(ROWS * COLS), _cols(COLS), _rows(ROWS) {
56 for (
size_t i = 0; i < _size; i++) {
66 explicit Mat2d(
const Mat2d& mat) : _size(ROWS * COLS), _cols(COLS), _rows(ROWS) {
68 if (mat.size() != _size || mat.cols() != _cols || mat.rows() != _rows) {
69 throw std::logic_error(
"Tried to copy matrixes with different dimensions");
71 this->arr =
new T[_size];
72 for (
size_t i = 0; i < _size; i++) {
73 this->arr[i] = mat.arr[i];
75 }
catch (std::logic_error& e) {
76 std::cerr << e.what() <<
'\n';
90 if (mat.size() != _size || mat.cols() != _cols || mat.rows() != _rows) {
91 throw std::logic_error(
"Tried to copy matrixes with different dimensions");
93 for (
size_t i = 0; i < _size; i++) {
94 this->arr[i] = mat.arr[i];
97 }
catch (std::logic_error& e) {
98 std::cerr << e.what() <<
'\n';
109 T&
operator()(
size_t i,
size_t j)
const {
return arr[i * _cols + j]; }
118 for (
size_t j = i * _cols; j < i * _cols + _rows; j++) {
119 mat[index++] = arr[j];
128 size_t size()
const {
return _size; }
134 size_t cols()
const {
return _cols; }
140 size_t rows()
const {
return _rows; }
161 for (
size_t i = 0; i < mat.rows(); i++) {
163 for (
size_t j = 0; j < mat.cols(); j++) {
164 out << mat.arr[i * mat.cols() + j];
165 if (j != mat.cols() - 1) {
170 if (i != mat.rows() - 1) {
182template <
typename T,
size_t ROWS,
size_t COLS>
class Mat2d<T, ROWS, COLS>::
Iterator {
203 for (
size_t i = 0; i < _size; i++) {
214 for (
size_t i = 0; i < _size; i++) {
225 if (this->_index < _size) {
246 if (this->_index > 0) {
276 for (
size_t j = this->_index * _cols; j < this->_index * _cols + _rows; ++j) {
277 mat[index++] = arr[j];
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