-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat.hpp
76 lines (61 loc) · 1.16 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
#ifndef MAT_HPP_
#define MAT_HPP_
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <ostream>
#include <vector>
#include "debug.hpp"
template <typename T>
class mat
: public std::vector<T>
{
private:
typedef std::vector<T> vec;
public:
const size_t m;
const size_t n;
mat(size_t m_, size_t n_, const T& val = T())
: vec(m_ * n_, val), m(m_), n(n_)
{
}
T& operator()(size_t i, size_t j)
{
check_range(i, j);
return (*this)[i + j * m];
}
const T& operator()(size_t i, size_t j) const
{
check_range(i, j);
return (*this)[i + j * m];
}
private:
void check_range(size_t i, size_t j) const
{
assert(0 <= i && i < m);
assert(0 <= j && j < n);
}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const mat<T>& m)
{
const size_t WIDTH = 6;
const size_t PRECISION = 3;
os << std::setw(WIDTH) << "X";
for (size_t j = 0; j < m.n; ++j)
{
os << std::setw(WIDTH) << j;
}
os << std::endl;
for (size_t i = 0; i < m.m; ++i)
{
os << std::setw(WIDTH) << i;
for (size_t j = 0; j < m.n; ++j)
{
os << std::setprecision(PRECISION) << std::setw(WIDTH) << m(i, j);
}
os << std::endl;
}
return os;
}
#endif