Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Table.hpp
1#ifndef TEUCHOS_TABLE_HPP
2#define TEUCHOS_TABLE_HPP
3
4#include "Teuchos_TableDecl.hpp"
5#include "Teuchos_Assert.hpp"
6#include "Teuchos_vector.hpp"
7
8namespace Teuchos {
9
10/* pretty simple 2D array */
11template <typename T>
12Table<T>::Table() {
13}
14
15template <typename T>
16Table<T>::Table(int ncols_init, int nrows_reserve):ncols(ncols_init) {
17 TEUCHOS_ASSERT(0 <= ncols_init);
18 reserve(data, ncols * nrows_reserve);
19}
20
21template <typename T>
22void swap(Table<T>& a, Table<T>& b) {
23 using std::swap;
24 swap(a.data, b.data);
25 swap(a.ncols, b.ncols);
26}
27
28template <typename T>
29int get_nrows(Table<T> const& t) {
30 TEUCHOS_DEBUG_ASSERT(t.ncols > 0);
31 TEUCHOS_DEBUG_ASSERT(Teuchos::size(t.data) % t.ncols == 0);
32 return Teuchos::size(t.data) / t.ncols;
33}
34
35template <typename T>
36int get_ncols(Table<T> const& t) { return t.ncols; }
37
38template <typename T>
39void resize(Table<T>& t, int new_nrows, int new_ncols) {
40 TEUCHOS_ASSERT(new_ncols == t.ncols); // pretty specialized right now
41 Teuchos::resize(t.data, new_nrows * t.ncols);
42}
43
44template <typename T>
45typename Table<T>::Ref at(Table<T>& t, int row, int col) {
46 TEUCHOS_DEBUG_ASSERT(0 <= col);
47 TEUCHOS_DEBUG_ASSERT(col < t.ncols);
48 TEUCHOS_DEBUG_ASSERT(0 <= row);
49 TEUCHOS_DEBUG_ASSERT(row < get_nrows(t));
50 return Teuchos::at(t.data, row * t.ncols + col);
51}
52
53template <typename T>
54typename Table<T>::ConstRef at(Table<T> const& t, int row, int col) {
55 TEUCHOS_DEBUG_ASSERT(0 <= col);
56 TEUCHOS_DEBUG_ASSERT(col < t.ncols);
57 TEUCHOS_DEBUG_ASSERT(0 <= row);
58 TEUCHOS_DEBUG_ASSERT(row < get_nrows(t));
59 return Teuchos::at(t.data, row * t.ncols + col);
60}
61
62}
63
64#endif
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...