Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Table.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_TABLE_HPP
11#define TEUCHOS_TABLE_HPP
12
13#include "Teuchos_TableDecl.hpp"
14#include "Teuchos_Assert.hpp"
15#include "Teuchos_vector.hpp"
16
17namespace Teuchos {
18
19/* pretty simple 2D array */
20template <typename T>
21Table<T>::Table() {
22}
23
24template <typename T>
25Table<T>::Table(int ncols_init, int nrows_reserve):ncols(ncols_init) {
26 TEUCHOS_ASSERT(0 <= ncols_init);
27 reserve(data, ncols * nrows_reserve);
28}
29
30template <typename T>
31void swap(Table<T>& a, Table<T>& b) {
32 using std::swap;
33 swap(a.data, b.data);
34 swap(a.ncols, b.ncols);
35}
36
37template <typename T>
38int get_nrows(Table<T> const& t) {
39 TEUCHOS_DEBUG_ASSERT(t.ncols > 0);
40 TEUCHOS_DEBUG_ASSERT(Teuchos::size(t.data) % t.ncols == 0);
41 return Teuchos::size(t.data) / t.ncols;
42}
43
44template <typename T>
45int get_ncols(Table<T> const& t) { return t.ncols; }
46
47template <typename T>
48void resize(Table<T>& t, int new_nrows, int new_ncols) {
49 TEUCHOS_ASSERT(new_ncols == t.ncols); // pretty specialized right now
50 Teuchos::resize(t.data, new_nrows * t.ncols);
51}
52
53template <typename T>
54typename Table<T>::Ref at(Table<T>& 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
62template <typename T>
63typename Table<T>::ConstRef at(Table<T> const& t, int row, int col) {
64 TEUCHOS_DEBUG_ASSERT(0 <= col);
65 TEUCHOS_DEBUG_ASSERT(col < t.ncols);
66 TEUCHOS_DEBUG_ASSERT(0 <= row);
67 TEUCHOS_DEBUG_ASSERT(row < get_nrows(t));
68 return Teuchos::at(t.data, row * t.ncols + col);
69}
70
71}
72
73#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,...