-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatrix.h
54 lines (45 loc) · 1.05 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
#ifndef MATRIX_H
#define MATRIX_H
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
/**
* A very simple N x M matrix class.
*/
template<typename T>
class Matrix {
public:
Matrix(std::size_t N, std::size_t M) :
m_data(N * M, T()),
m_rows(N),
m_cols(M) {}
inline T* operator[](int i) {
return &m_data[i * m_cols];
}
inline T const* operator[](int i) const {
return &m_data[i * m_cols];
}
inline std::size_t rows() const {
return m_rows;
}
inline std::size_t cols() const {
return m_cols;
}
private:
std::vector<T> m_data;
std::size_t m_rows;
std::size_t m_cols;
};
// Stream output operator.
template<typename T>
std::ostream& operator<<(std::ostream& os, const Matrix<T>& matrix) {
for (std::size_t i = 0; i < matrix.rows(); ++i) {
for (std::size_t j = 0; j < matrix.cols(); ++j) {
os << std::left << std::setw(3) << matrix[i][j];
}
os << std::endl;
}
return os;
}
#endif // MATRIX_H