-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.hpp
151 lines (121 loc) · 3.44 KB
/
matrix.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
*
* Copyright (c) 2016, Lutz, Clemens <[email protected]>
*/
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <vector>
#include <memory>
#include <limits>
#ifdef MATRIX_BOUNDSCHECK
#include <iostream>
#endif
namespace cle {
template <typename T, typename Talloc, typename INT, bool COL_MAJOR = true>
class Matrix {
public:
using iterator = typename std::vector<T, Talloc>::iterator;
Matrix()
:
x_dim_(0), y_dim_(0)
{}
Matrix(std::vector<T, Talloc>&& matrix, INT const x_dim, INT const y_dim)
:
raw_(std::move(matrix)), x_dim_(x_dim), y_dim_(y_dim)
{}
Matrix(Matrix<T, Talloc, INT, COL_MAJOR>& other)
:
raw_(other.raw_), x_dim_(other.x_dim_), y_dim_(other.y_dim_)
{}
Matrix(Matrix<T, Talloc, INT, COL_MAJOR>&& other)
:
raw_(std::move(other.raw_)),
x_dim_(other.x_dim_), y_dim_(other.y_dim_)
{}
Matrix<T, Talloc, INT, COL_MAJOR>& operator= (Matrix<T, Talloc, INT, COL_MAJOR>&& other) {
raw_ = std::move(other.raw_);
x_dim_ = other.x_dim_;
y_dim_ = other.y_dim_;
return *this;
}
Matrix<T, Talloc, INT, COL_MAJOR>& operator= (Matrix<T, Talloc, INT, COL_MAJOR>& other) {
raw_ = other.raw_;
x_dim_ = other.x_dim_;
y_dim_ = other.y_dim_;
return *this;
}
inline iterator begin() {
return raw_.begin();
}
inline iterator end() {
return raw_.end();
}
void resize(INT const x_dim, INT const y_dim) {
size_t total_size = (size_t) x_dim * y_dim;
#ifdef MATRIX_BOUNDSCHECK
if (total_size > std::numeric_limits<INT>::max()) {
std::cerr << "Warning: Matrix size exceeds integer type max value"
<< std::endl;
}
#endif
raw_.resize(total_size);
x_dim_ = x_dim;
y_dim_ = y_dim;
}
T* data() {
return raw_.data();
}
T const* data() const {
return raw_.data();
}
std::vector<T, Talloc>& get_data() {
return raw_;
}
std::vector<T, Talloc> const& get_data() const {
return raw_;
}
std::vector<T, Talloc>&& move_data() {
return std::move(raw_);
}
inline T& operator() (INT const x, INT const y) {
#ifdef MATRIX_BOUNDSCHECK
if (x >= x_dim_ || y >= y_dim_) {
std::cerr << "Warning: Matrix out of bounds access: ("
<< x << "," << y << ") in ("
<< x_dim_ << "," << y_dim_ << ") matrix"
<< std::endl;
}
#endif
return raw_[x_dim_ * y + x];
}
inline T const& operator() (INT const x, INT const y) const {
#ifdef MATRIX_BOUNDSCHECK
if (x >= x_dim_ || y >= y_dim_) {
std::cerr << "Warning: Matrix out of bounds access: ("
<< x << "," << y << ") in ("
<< x_dim_ << "," << y_dim_ << ") matrix"
<< std::endl;
}
#endif
return raw_[x_dim_ * y + x];
}
inline INT size() const {
return raw_.size();
}
inline INT rows() const {
return x_dim_;
}
inline INT cols() const {
return y_dim_;
}
private:
std::vector<T, Talloc> raw_;
INT x_dim_;
INT y_dim_;
};
}
#endif /* MATRIX_HPP */