-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat.hpp
631 lines (556 loc) · 15.8 KB
/
mat.hpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#ifndef _MAT_H
#define _MAT_H
#include <iostream>
#include <cstring>
#include <sstream>
#include <cstddef>
#include <omp.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
template <class T>
class Matrix
{
private:
size_t row;
size_t col;
size_t channel;
T *data;
size_t ROIBeginRow;
size_t ROIBeginCol;
size_t ROIRow;
size_t ROICol;
size_t span;
size_t *ref_count;
public:
// 一系列get函数
size_t get_row()
{
return this->row;
}
size_t get_col()
{
return this->col;
}
size_t get_channel()
{
return this->channel;
}
size_t get_ref_count()
{
return this->ref_count;
}
size_t get_span()
{
return this->span;
}
// 默认构造器
// 根据数组创建矩阵
Matrix(size_t row = 3, size_t col = 3, size_t channel = 1, T *data = nullptr, size_t ROIBeginRow = 0, size_t ROIBeginCol = 0, size_t ROIRow = 0, size_t ROICol = 0, size_t span = 0, size_t *ref_count = nullptr);
// copy函数
Matrix(const Matrix &);
// 赋值的重载(soft copy)
Matrix &operator=(const Matrix &mat);
// 析构函数
~Matrix();
// 运算符的重载
// 重载<<方便打印
template <class TT>
friend std::ostream &operator<<(std::ostream &os, Matrix<TT> &mat);
// 重载()便于访问元素
T &operator()(size_t row, size_t col, size_t channel);
// 加法
Matrix operator+(T data) const;
Matrix operator+(const Matrix &addend) const;
// 友元函数实现 num+Matrix
template <class TT>
friend Matrix operator+(TT data, const Matrix<TT> &mat)
{
return mat + data;
}
// 减法
Matrix operator-(T data) const;
Matrix operator-(const Matrix &mat) const;
// 友元函数实现 num-Matrix
template <class TT>
friend Matrix operator-(TT data, const Matrix<TT> &mat)
{
return mat - data;
}
// 乘法
Matrix operator*(T data) const;
Matrix operator*(Matrix &mat);
// 友元函数实现 num*Matrix
template <class TT>
friend Matrix operator*(TT data, const Matrix<TT> &mat)
{
return mat * data;
}
//==的重载
bool operator==(Matrix &mat);
// 将!重载为矩阵转置
Matrix operator!() const;
// 返回矩阵中的元素个数
size_t size()
{
size_t size = this->col * this->row;
return size;
}
// ROI的相关操作
// 子矩阵返回
Matrix ROI() const;
// 调整子矩阵位置
void setROI(size_t row, size_t col, size_t ROIRow, size_t ROICol);
void setROIPosition(size_t row, size_t col);
void setROISize(size_t r, size_t c);
};
// 默认构造器
template <typename T>
Matrix<T>::Matrix(size_t row, size_t col, size_t channel, T *data, size_t ROIBeginRow, size_t ROIBeginCol, size_t ROIRow, size_t ROICol, size_t span, size_t *ref_count)
{
if (row < 0 || col < 0 || ROIBeginCol < 0 || ROIBeginRow < 0 || ROIRow < 0 || ROICol < 0)
{
std::cerr << "In default constructor" << std::endl;
std::cerr << "The input of rows and columns should not less than 0." << std::endl;
exit(EXIT_FAILURE);
}
if (channel < 1 || channel > 3)
{
std::cerr << "In default constructor" << std::endl;
std::cerr << "The input of channel should between 1 and 3" << std::endl;
exit(EXIT_FAILURE);
}
// 如果该矩阵的元素数组没有被引用过,则初始化为1,否则将引用指针指向传入指针
if (ref_count == nullptr)
{
this->ref_count = new size_t[1];
this->ref_count[0] = 1;
}
else
{
this->ref_count = ref_count;
}
// 对ROI的边界的合法性进行检查
if (ROIBeginRow + ROIRow > row || ROIBeginCol + ROICol > col)
{
std::cerr << "In default constructor" << std::endl;
std::cerr << "The region of interest should not exceed the region of the matrix." << std::endl;
exit(EXIT_FAILURE);
}
// 赋值
this->row = row;
this->col = col;
this->channel = channel;
this->ROIBeginRow = ROIBeginRow;
this->ROIBeginCol = ROIBeginCol;
this->ROIRow = ROIRow;
this->ROICol = ROICol;
if (span == 0)
{
this->span = channel * col;
}
else
{
this->span = span;
}
// 根据通道数创建元素数组
if (data == nullptr)
{
this->data = new T[row * col * channel];
}
else
{
size_t element = row * col * channel;
this->data = new T[element];
for (size_t i = 0; i < element; i++)
{
this->data[i] = data[i];
}
}
}
// soft copy
template <typename T>
Matrix<T>::Matrix(const Matrix<T> &mat)
{
this->row = mat.row;
this->col = mat.col;
this->channel = mat.channel;
this->ROIBeginRow = mat.ROIBeginRow;
this->ROIBeginCol = mat.ROIBeginCol;
this->ROIRow = mat.ROIRow;
this->ROICol = mat.ROICol;
this->ref_count = mat.ref_count;
this->span = mat.span;
this->ref_count[0]++;
this->data = mat.data;
}
// 析构函数
template <typename T>
Matrix<T>::~Matrix()
{
if (this->ref_count[0] == 1)
{
// printf("In ref_count\n");
delete[] this->ref_count;
this->ref_count = nullptr;
// printf("After deleting ref_count pointer.\n");
delete[] this->data;
this->data = nullptr;
// std::cout << "The element array has been free.\n";
}
else
{
this->ref_count[0]--;
// std::cout << this -> ref_count[0] << " matrices share the element array.\n";
}
}
// 赋值的重载(soft copy)
template <typename T>
Matrix<T> &Matrix<T>::operator=(const Matrix<T> &mat)
{
// 防止自赋值
if (this == &mat)
{
return *this;
}
// 如果不是自赋值,则释放原有对象成员指针指向的内存并将非指针变量一一赋值
if (this->ref_count[0] == 1)
{
delete[] this->data;
this->data = nullptr;
delete[] this->ref_count;
this->ref_count = nullptr;
// std::cout << "The original element array has been free.\n";
}
else
{
this->ref_count[0]--;
}
this->row = mat.row;
this->col = mat.col;
this->channel = mat.channel;
this->ROIBeginRow = mat.ROIBeginRow;
this->ROIBeginCol = mat.ROIBeginCol;
this->ROIRow = mat.ROIRow;
this->ROICol = mat.ROICol;
this->span = mat.span;
this->ref_count = mat.ref_count;
this->ref_count[0]++;
// std::cout << this -> ref_count[0] << " matrices share the element array.\n";
this->data = mat.data;
return *this;
}
// 重载()便于访问元素
template <typename T>
T &Matrix<T>::operator()(size_t row, size_t col, size_t ch)
{
// 参数合法性检查
if (row < 0 || col < 0)
{
std::cerr << "The input of row and column should not be less than 0." << std::endl;
exit(EXIT_FAILURE);
}
if (ch < 1 || ch > channel)
{
std::cerr << "The input of channel should between 1 and 3" << std::endl;
exit(EXIT_FAILURE);
}
return data[row * this->span + col * this->channel + ch - 1];
}
// 重载<<运算符
template <class T>
std::ostream &operator<<(std::ostream &os, Matrix<T> &mat)
{
size_t ch = mat.channel;
size_t r = mat.row;
size_t c = mat.col;
os << "[";
for (size_t i = 0; i < r; i++)
{
for (size_t j = 0; j < c; j++)
{
os << "{";
for (size_t k = 1; k <= ch; k++)
{
os << mat(i, j, k);
if (k != ch)
os << " ";
}
os << "}";
if (j != c - 1)
os << ", ";
}
if (i != r - 1)
os << "\n";
}
os << "]";
return os;
}
// 加法运算符重载
template <typename T>
Matrix<T> Matrix<T>::operator+(T data) const
{
Matrix<T> mat;
mat.row = this->row;
mat.col = this->col;
mat.channel = this->channel;
mat.ROIBeginRow = this->ROIBeginRow;
mat.ROIBeginCol = this->ROIBeginCol;
mat.ROIRow = this->ROIRow;
mat.ROICol = this->ROICol;
mat.span = this->span;
size_t element = this->row * this->col * this->channel;
mat.data = new T[element];
for (size_t i = 0; i < element; i++)
{
mat.data[i] = this->data[i] + data;
}
return mat;
}
template <typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T> &addend) const
{
// 参数合法性检查
if (row != addend.row || col != addend.col || channel != addend.channel)
{
std::cerr << "In '+' operator overloading..." << std::endl;
std::cerr << "The size of the two matrix should be the same." << std::endl;
exit(EXIT_FAILURE);
}
Matrix<T> mat;
mat.row = this->row;
mat.col = this->col;
mat.ROIBeginRow = this->ROIBeginRow;
mat.ROIBeginCol = this->ROIBeginCol;
mat.ROIRow = this->ROIRow;
mat.ROICol = this->ROICol;
mat.channel = this->channel;
mat.span = this->span;
size_t element = this->row * this->col * this->channel;
mat.data = new T[element];
for (size_t i = 0; i < element; i++)
{
mat.data[i] = this->data[i] + addend.data[i];
}
return mat;
}
// 减法运算符重载
template <typename T>
Matrix<T> Matrix<T>::operator-(T data) const
{
Matrix<T> mat;
mat.row = this->row;
mat.col = this->col;
mat.channel = this->channel;
mat.ROIBeginRow = this->ROIBeginRow;
mat.ROIBeginCol = this->ROIBeginCol;
mat.ROIRow = this->ROIRow;
mat.ROICol = this->ROICol;
mat.span = this->span;
size_t element = this->row * this->col * this->channel;
mat.data = new T[element];
for (size_t i = 0; i < element; i++)
{
mat.data[i] = this->data[i] - data;
}
return mat;
}
template <typename T>
Matrix<T> Matrix<T>::operator-(const Matrix<T> &subtrahend) const
{
// 参数合法性检查
if (row != subtrahend.row || col != subtrahend.col || channel != subtrahend.channel)
{
std::cerr << "In '-' operator overloading..." << std::endl;
std::cerr << "The size of the two matrix should be the same." << std::endl;
exit(EXIT_FAILURE);
}
Matrix<T> mat;
mat.row = this->row;
mat.col = this->col;
mat.ROIBeginRow = this->ROIBeginRow;
mat.ROIBeginCol = this->ROIBeginCol;
mat.ROIRow = this->ROIRow;
mat.ROICol = this->ROICol;
mat.channel = this->channel;
mat.span = this->span;
size_t element = this->row * this->col * this->channel;
mat.data = new T[element];
for (size_t i = 0; i < element; i++)
{
mat.data[i] = this->data[i] - subtrahend.data[i];
}
return mat;
}
// 乘法运算符重载
template <typename T>
Matrix<T> Matrix<T>::operator*(T data) const
{
Matrix mat;
mat.row = this->row;
mat.col = this->col;
mat.channel = this->channel;
mat.ROIBeginRow = this->ROIBeginRow;
mat.ROIBeginCol = this->ROIBeginCol;
mat.ROIRow = this->ROIRow;
mat.ROICol = this->ROICol;
mat.span = this->span;
size_t element = this->row * this->col * this->channel;
mat.data = new T[element];
for (size_t i = 0; i < element; i++)
{
mat.data[i] = this->data[i] * data;
}
return mat;
}
template <typename T>
Matrix<T> Matrix<T>::operator*(Matrix<T> &multiplier)
{
// 判断参数合法性
if (multiplier.row != this->col || multiplier.channel != channel)
{
std::cerr << "In operator * overloading..." << std::endl;
std::cerr << "The two matrices' sizes and channel number should be the same." << std::endl;
exit(EXIT_FAILURE);
}
Matrix<T> product;
product.row = this->row;
product.col = multiplier.col;
product.channel = this->channel;
product.ROIBeginRow = 0;
product.ROIBeginCol = 0;
product.ROIRow = 0;
product.ROICol = 0;
product.span = product.channel * product.col;
size_t element = product.row * product.col * product.channel;
product.data = new T[element];
#pragma omp parallel for schedule(dynamic)
for (size_t k = 0; k < multiplier.row; k++)
{
for (size_t i = 0; i < this->row; i++)
{
for (size_t j = 0; j < multiplier.col; j++)
{
for (size_t ch = 1; ch <= this->channel; ch++)
{
product(i, j, ch) += (*this)(i, k, ch) * multiplier(k, j, ch);
}
}
}
}
return product;
}
// 相等运算符重载
template <typename T>
bool Matrix<T>::operator==(Matrix<T> &mat)
{
if (row != mat.row || col != mat.col || ROIBeginRow != mat.ROIBeginRow || ROIBeginCol != mat.ROIBeginCol || ROIRow != mat.ROIRow || ROICol != mat.ROIRow || channel != mat.channel)
{
return false;
}
size_t element = this->row * this->col * this->channel;
for (size_t i = 0; i < element; i++)
{
if (this->data[i] != mat.data[i])
{
return false;
}
}
return true;
}
// 矩阵转置运算符
template <typename T>
Matrix<T> Matrix<T>::operator!() const
{
Matrix<T> mat;
mat.row = this->col;
mat.col = this->row;
mat.ROIBeginRow = this->ROIBeginCol;
mat.ROIBeginCol = this->ROIBeginRow;
mat.ROIRow = this->ROICol;
mat.ROICol = this->ROIRow;
mat.channel = this->channel;
size_t element = mat.row * mat.col * mat.channel;
mat.data = new T[element];
mat.span = mat.col * mat.channel;
for (size_t i = 0; i < mat.row; i++)
{
for (size_t j = 0; j < mat.col; j++)
{
for (size_t k = 1; k <= mat.channel; k++)
{
mat(i, j, k) = this->data[j * this->span + i * this->channel + k - 1];
}
}
}
return mat;
}
// ROI相关操作
// 返回子矩阵
template <typename T>
Matrix<T> Matrix<T>::ROI() const
{
// 对ROI的边界的合法性进行检查
if (ROIBeginRow + ROIRow > row || ROIBeginCol + ROICol > col)
{
std::cerr << "The region of interest should not exceed the region of the matrix." << std::endl;
exit(EXIT_FAILURE);
}
Matrix mat;
mat.row = this->ROIRow;
mat.col = this->ROICol;
mat.channel = this->channel;
mat.data = this->data + ROIBeginRow * col * channel + ROIBeginCol * channel;
mat.ref_count = this->ref_count;
mat.ref_count[0]++;
mat.span = this->col * this->channel;
return mat;
}
template <typename T>
void Matrix<T>::setROI(size_t row, size_t col, size_t ROIRow, size_t ROICol)
{
// 参数正确性检查
if (row < 0 || col < 0 || ROIRow < 0 || ROICol < 0)
{
std::cerr << "In function setROI..." << std::endl;
std::cerr << "The input of row and column should not be less than 0." << std::endl;
exit(EXIT_FAILURE);
}
this->ROIBeginCol = col;
this->ROIBeginRow = row;
this->ROIRow = ROIRow;
this->ROICol = ROICol;
}
template <typename T>
void Matrix<T>::setROIPosition(size_t row, size_t col)
{
// 参数正确性检查
if (row < 0 || col < 0)
{
std::cerr << "In function setROIPosition..." << std::endl;
std::cerr << "The input of row and column should not be less than 0." << std::endl;
exit(EXIT_FAILURE);
}
if (row > this->row || col > this->col)
{
std::cerr << "In function setROIPosition..." << std::endl;
std::cerr << "The input of row and column should not be larger than the row number or colomn number of the source matrix." << std::endl;
exit(EXIT_FAILURE);
}
// 赋值
this->ROIBeginRow = row;
this->ROIBeginCol = col;
}
template <typename T>
void Matrix<T>::setROISize(size_t row, size_t col)
{
// 参数正确性检查
if (row < 0 || col < 0)
{
std::cerr << "In function setROISize..." << std::endl;
std::cerr << "The input of row and column should not be less than 0." << std::endl;
exit(EXIT_FAILURE);
}
this->ROIRow = row;
this->ROICol = col;
};
#endif