-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
105 lines (89 loc) · 1.95 KB
/
matrix.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
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
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <cstdint>
#include <stdlib.h>
#include <new>
template <typename T>
class matrix
{
public:
matrix()
: _data(NULL)
, _rows(0)
, _cols(0)
, _border(0)
{
}
~matrix()
{
_free();
}
public:
bool create(std::int32_t rows, std::int32_t cols, std::int32_t border = 0, const T& v = T())
{
_free();
_data = (T*)malloc(sizeof(T) * (rows + border * 2) * (cols + border * 2));
if (_data == NULL)
{
return false;
}
for (std::int32_t i = 0; i < (rows + border * 2) * (cols + border * 2); i++)
{
new(_data + i) T(v);
}
_rows = rows;
_cols = cols;
_border = border;
return true;
}
inline T& at(std::int32_t row, std::int32_t col)
{
return _data[(row + _border) * (_cols + _border * 2) + col + _border];
}
inline const T& at(std::int32_t row, std::int32_t col) const
{
return _data[(row + _border) * (_cols + _border * 2) + col + _border];
}
inline T* data()
{
return _data;
}
inline const T* data() const
{
return _data;
}
inline std::int32_t rows() const
{
return _rows;
}
inline std::int32_t cols() const
{
return _cols;
}
inline std::int32_t border_size()
{
return _border;
}
private:
void _free()
{
if (_data)
{
for (std::int32_t i = 0; i < (_rows + _border * 2) * (_cols + _border * 2); i++)
{
((T*)(_data + i))->~T();
}
free(_data);
_data = 0;
_rows = 0;
_cols = 0;
_border = 0;
}
}
private:
T *_data;
std::int32_t _rows;
std::int32_t _cols;
std::int32_t _border;
};
#endif