-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmatrix.h
51 lines (44 loc) · 1.24 KB
/
bmatrix.h
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
#ifndef BMATRIX_H
#define BMATRIX_H
#include <memory>
template<typename Type>
struct Matrix {
Matrix(int width_, int height_, Type * values)
: width(width_), height(height_) {
matrix = new Type[width*height];
std::memcpy(matrix, values, width*height*sizeof(Type));
}
~Matrix() {
delete matrix;
matrix = 0;
}
Matrix(const Matrix<Type> & matrix) {
width = matrix.width;
height = matrix.height;
this->matrix = new Type[width*height];
std::memcpy(this->matrix, matrix.matrix, width*height*sizeof(Type));
}
Matrix & operator= (const Matrix & matrix) {
if (this != &matrix) {
delete this->matrix;
width = matrix.width;
height = matrix.height;
this->matrix = new Type[width*height];
std::memcpy(this->matrix, matrix.matrix, width*height*sizeof(Type));
}
return *this;
}
Type Get(int row, int col) const {
return matrix[row*width + col];
}
void operator *=(const Type value) {
for (int i = 0; i < width*height; ++i) {
matrix[i] = matrix[i] * value;
}
}
int width;
int height;
private:
Type * matrix;
};
#endif // BMATRIX_H