omega_h
Reliable mesh adaptation
Omega_h_table.hpp
1 #ifndef OMEGA_H_TABLE_HPP
2 #define OMEGA_H_TABLE_HPP
3 
4 #include <Omega_h_std_vector.hpp>
5 
6 namespace Omega_h {
7 
8 /* pretty simple 2D array */
9 template <typename T>
10 struct Table {
11  std::vector<T> data;
12  int ncols;
13  using Ref = typename std::vector<T>::reference;
14  using ConstRef = typename std::vector<T>::const_reference;
15  Table() = default;
16  Table(int ncols_init, int nrows_reserve) : ncols(ncols_init) {
17  OMEGA_H_CHECK(0 <= ncols_init);
18  reserve(data, ncols * nrows_reserve);
19  }
20 };
21 
22 template <typename T>
23 int get_nrows(Table<T> const& t) {
24  OMEGA_H_CHECK(t.ncols > 0);
25  OMEGA_H_CHECK(size(t.data) % t.ncols == 0);
26  return size(t.data) / t.ncols;
27 }
28 
29 template <typename T>
30 int get_ncols(Table<T> const& t) {
31  return t.ncols;
32 }
33 
34 template <typename T>
35 void resize(Table<T>& t, int new_nrows, int new_ncols) {
36  OMEGA_H_CHECK(new_ncols == t.ncols); // pretty specialized right now
37  Omega_h::resize(t.data, new_nrows * t.ncols);
38 }
39 
40 template <typename T>
41 typename Table<T>::Ref at(Table<T>& t, int row, int col) {
42  OMEGA_H_CHECK(0 <= col);
43  OMEGA_H_CHECK(col < t.ncols);
44  OMEGA_H_CHECK(0 <= row);
45  OMEGA_H_CHECK(row < get_nrows(t));
46  return Omega_h::at(t.data, row * t.ncols + col);
47 }
48 
49 template <typename T>
50 typename Table<T>::ConstRef at(Table<T> const& t, int row, int col) {
51  OMEGA_H_CHECK(0 <= col);
52  OMEGA_H_CHECK(col < t.ncols);
53  OMEGA_H_CHECK(0 <= row);
54  OMEGA_H_CHECK(row < get_nrows(t));
55  return Omega_h::at(t.data, row * t.ncols + col);
56 }
57 
58 } // namespace Omega_h
59 
60 #endif
Definition: amr_mpi_test.cpp:6
Definition: Omega_h_table.hpp:10