-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsegen_table.hpp
60 lines (50 loc) · 1.42 KB
/
parsegen_table.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
#ifndef PARSEGEN_TABLE_HPP
#define PARSEGEN_TABLE_HPP
#include "parsegen_std_vector.hpp"
namespace parsegen {
/* pretty simple 2D array */
template <typename T>
struct table {
std::vector<T> data;
int ncols;
using reference = typename std::vector<T>::reference;
using const_reference = typename std::vector<T>::const_reference;
table() = default;
table(int ncols_init, int nrows_reserve) : ncols(ncols_init) {
assert(0 <= ncols_init);
reserve(data, ncols * nrows_reserve);
}
};
template <typename T>
int get_nrows(table<T> const& t) {
assert(t.ncols > 0);
assert(size(t.data) % t.ncols == 0);
return isize(t.data) / t.ncols;
}
template <typename T>
int get_ncols(table<T> const& t) {
return t.ncols;
}
template <typename T>
void resize(table<T>& t, int new_nrows, int new_ncols) {
assert(new_ncols == t.ncols); // pretty specialized right now
parsegen::resize(t.data, new_nrows * t.ncols);
}
template <typename T>
typename table<T>::reference at(table<T>& t, int row, int col) {
assert(0 <= col);
assert(col < t.ncols);
assert(0 <= row);
assert(row < get_nrows(t));
return parsegen::at(t.data, row * t.ncols + col);
}
template <typename T>
typename table<T>::const_reference at(table<T> const& t, int row, int col) {
assert(0 <= col);
assert(col < t.ncols);
assert(0 <= row);
assert(row < get_nrows(t));
return parsegen::at(t.data, row * t.ncols + col);
}
} // namespace parsegen
#endif