-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.h
More file actions
249 lines (204 loc) · 7.07 KB
/
Copy pathmatrix.h
File metadata and controls
249 lines (204 loc) · 7.07 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include <cstddef>
#include <stdexcept>
#include <string>
#include <fstream>
#include <iostream>
template <typename T>
class Matrix {
public:
// Ожидаемые (стандартом) псевдонимы для типов данных, производных от T
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = value_type&;
using const_reference = const value_type&;
//Представляет абстракцию над строчкой матрицы,
//самодостаточную и компактную для копирования по значению
class Row {
friend class Matrix<T>;
public:
T& operator[](size_t colnum) {
return matrix_->operator()(rownum_, colnum);
}
size_t Size() {
return matrix_->GetCollsNum();
};
protected:
// Разрешаем доступ к конструктору только матрице
Row(Matrix<T>* m, size_t rownum) : rownum_(rownum), matrix_(m) {
}
protected:
size_t rownum_;
Matrix<T>* matrix_;
};
class ConstRow {
friend class Matrix<T>;
public:
T operator[](size_t colnum) const {
return matrix_->operator()(rownum_, colnum);
}
size_t Size() {
return matrix_->GetCollsNum();
};
protected:
// Разрешаем доступ к конструктору только матрице
ConstRow(const Matrix<T>* m, size_t rownum): rownum_(rownum), matrix_(m) {
}
protected:
size_t rownum_;
const Matrix<T>* matrix_;
};
//Представляет абстракцию над столбцом матрицы,
//самодостаточную и компактную для копирования по значению
class Col {
friend class Matrix<T>;
public:
T& operator[](size_t rownum) {
return matrix_->operator()(rownum, colnum_);
}
protected:
// Разрешаем доступ к конструктору только матрице
Col(Matrix<T>* m, size_t colnum) : colnum_(colnum), matrix_(m) {
}
protected:
size_t colnum_;
Matrix<T>* matrix_;
};
class ConstCol {
friend class Matrix<T>;
public:
T operator[](size_t rownum) const {
return matrix_->operator()(rownum, colnum_);
}
protected:
// Разрешаем доступ к конструктору только матрице
ConstCol(const Matrix<T>* m, size_t colnum): colnum_(colnum), matrix_(m) {
}
protected:
size_t colnum_;
const Matrix<T>* matrix_;
};
public:
Matrix(const Matrix<T>&) = delete;
// Конструктор создаёт матрицу размера m x n и задаёт значение элементов по умолчанию
// Если один из параметров не равен 0, то другой тоже. Иначе кидаем исключительную ситуацию
Matrix(size_t rows_num = 0, size_t cols_num = 0, T def = T{}) {
if (rows_num == 0 && cols_num == 0) {
rows_num_ = 0;
cols_num_ = 0;
table_ = nullptr;
return;
}
if (rows_num == 0 || cols_num == 0) {
throw std::invalid_argument("Can't make matrix with 0 rows or columns");
}
rows_num_ = rows_num;
cols_num_ = cols_num;
table_ = AllocateMatrix(rows_num_, cols_num_);
for (size_t i = 0; i < rows_num; ++i) {
for (size_t j = 0; j < cols_num; ++j) {
this->operator()(i, j) = def;
}
}
};
~Matrix() {
Clear();
};
// Возвращает i,j -й элемент матрицы
T operator()(size_t i, size_t j) const {
return GetIJEl(table_, i, j, cols_num_);
//return table_[i * cols_num_ + j];
}
T& operator()(size_t i, size_t j) {
return GetIJEl(table_, i, j, cols_num_);
//return table_[i * cols_num_ + j];
}
// Возвращает i-ю строку, обёрнутую в абстракцию
Row operator[](size_t i) {
return Row(this, i);
}
ConstRow operator[](size_t i) const {
return ConstRow(this, i);
};
Col operator()(size_t i) {
return Col(this, i);
};
ConstCol operator()(size_t i) const {
return ConstCol(this, i);
};
T At(size_t i, size_t j) const {
if (i < rows_num_ && j < cols_num_) {
return this->operator()(i, j);
}
throw std::out_of_range("IOJ (matrix).");
};
reference At(size_t i, size_t j) {
if (i < rows_num_ && j < cols_num_) {
return this->operator()(i, j);
}
throw std::out_of_range("IOJ (matrix).");
};
public:
void Resize(size_t new_rows, size_t new_cols, T def = T{}) {
if (new_rows == rows_num_ && new_cols == cols_num_) {
return;
}
if (new_rows == 0 && new_cols == 0) {
Clear();
return;
}
if (new_rows == 0 || new_cols == 0) {
throw std::invalid_argument("Can't make matrix with 0 rows or columns");
}
T* new_table = AllocateMatrix(new_rows, new_cols);
for (size_t i = 0; i < new_rows; ++i) {
for (size_t j = 0; j < new_cols; ++j) {
if (i >= rows_num_ || j >= cols_num_) {
GetIJEl(new_table, new_rows - i - 1, new_cols - j - 1, new_cols) = def;
} else {
GetIJEl(new_table, new_rows - i - 1, j, new_cols) = GetIJEl(table_, rows_num_ - i - 1, j, cols_num_);
}
}
}
delete[] table_;
table_ = new_table;
rows_num_ = new_rows;
cols_num_ = new_cols;
}
void Clear() {
if (!table_) {
return;
}
delete[] table_;
rows_num_ = 0;
cols_num_ = 0;
};
size_t GetRowsNum() const {
return rows_num_;
}
size_t GetColsNum() const {
return cols_num_;
}
T* GetPtr() {
return table_;
}
const T* GetPtr() const {
return table_;
}
// Вспомогательная функция, позволяющая по 3 параметрам получать правильное место элемента в массиве 4 параметра
static reference GetIJEl(T* arr, size_t i, size_t j, size_t cols_num) {
return arr[i * cols_num + j];
}
protected:
// Метод аллоцирует память в кучу для заданных параметров n и m
// Методу от this ничего не надо, поэтому он статический
static T* AllocateMatrix(size_t newRowsNum, size_t newColsNum) {
T* ans = new T[newRowsNum * newColsNum];
return ans;
};
protected:
T* table_;
size_t rows_num_;
size_t cols_num_;
};