Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
table_impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__TABLE_IMPL__
34#define __H__TABLE_IMPL__
35
36#include <iomanip>
37#include <cassert>
38#include "common/error.h"
39#include "common/log.h"
40#include "string_util.h"
41
42namespace ug{
43
44template <class T>
46 m_numRows(0),
47 m_numCols(0)
48{
52}
53
54template <class T>
55Table<T>::Table(size_t numRows, size_t numCols) :
56 m_numRows(0),
57 m_numCols(0)
58{
62 add_rows(numRows);
63 add_cols(numCols);
64}
65
66template <class T>
68{
69 clear();
70}
71
72template <class T>
74{
75 for(size_t i_row = 0; i_row < m_numRows; ++i_row){
76 for(size_t i_col = 0; i_col < m_numCols; ++i_col){
77 delete m_data[i_row][i_col];
78 }
79 }
80 m_numRows = m_numCols = 0;
81 m_data.clear();
82}
83
84template <class T>
85void Table<T>::add_rows(size_t num)
87 if(num > 0){
88 size_t newSize = m_numRows + num;
89 m_data.resize(newSize);
90
91 if(m_numCols > 0){
92 for(size_t i_row = m_numRows; i_row < newSize; ++i_row){
93 m_data[i_row].resize(m_numCols);
94 for(size_t i_col = 0; i_col < m_numCols; ++i_col)
95 m_data[i_row][i_col] = new T;
96 }
97 }
98 m_numRows = newSize;
99 }
100}
101
102template <class T>
103void Table<T>::add_cols(size_t num)
104{
105 if(num > 0){
106 size_t newSize = m_numCols + num;
107 for(size_t i_row = 0; i_row < m_numRows; ++i_row){
108 m_data[i_row].resize(newSize);
109 for(size_t i_col = m_numCols; i_col < newSize; ++i_col)
110 m_data[i_row][i_col] = new T;
111 }
112 m_numCols = newSize;
113 }
114}
115
116template <class T>
117T& Table<T>::operator() (size_t rowInd, size_t colInd)
118{
119 if(rowInd >= num_rows())
120 add_rows((rowInd + 1) - num_rows());
121
122 if(colInd >= num_cols())
123 add_cols((colInd + 1) - num_cols());
124
125 return *m_data[rowInd][colInd];
126}
127
128template <class T>
129const T& Table<T>::operator() (size_t rowInd, size_t colInd) const
130{
131 UG_COND_THROW(rowInd >= num_rows(), "Bad row index: " << rowInd << "! Only " << num_rows() << " rows exist.");
132 UG_COND_THROW(colInd >= num_cols(), "Bad col index: " << colInd << "! Only " << num_cols() << " cols exist.");
133 return *m_data[rowInd][colInd];
134}
135
136template <class T>
137size_t Table<T>::num_rows() const
138{
139 return m_numRows;
140}
141
142template <class T>
143size_t Table<T>::num_cols() const
144{
145 return m_numCols;
146}
147
148
149
150
151
152template <class T>
153std::string Table<T>::to_string() const
154{
155 std::stringstream ssOut;
156 ssOut << *this;
157 return ssOut.str();
158}
159
160
161
162template<typename T>
163std::string Table<T>::to_latex() const
164{
165 Table<std::string> strTable(num_rows(), num_cols());
166
167 std::stringstream os;
168 os << "\\begin{table}\n\\centering\n";
169 os << "\\begin{tabular}{";
170 for(size_t i=0; i < num_cols(); i++)
171 os << get_col_sep(i) << get_col_alignment(i);
172 os << get_col_sep(num_cols());
173 os << "}\n";
174
175
176 for(size_t i_row = 0; i_row < num_rows(); ++i_row)
177 {
178 if(get_row_sep(i_row) != ' ')
179 os << "\\hline \n";
180
181 for(size_t i_col = 0; i_col < num_cols(); ++i_col)
182 {
183 if(i_col != 0) os << " & ";
184 os << EntryToString(*this, i_row, i_col);
185 }
186
187 os << " \\\\\n";
188 }
189 if(get_row_sep(num_rows()) != ' ')
190 os << "\\hline \n";
191 os << "\\end{tabular}\n";
192 os << "\\end{table}\n";
193 return os.str();
194}
195
196
197template<typename T>
198std::string Table<T>::to_csv(const char *seperator) const
199{
200 std::stringstream os;
201 for(size_t i_row = 0; i_row < num_rows(); ++i_row)
202 {
203 for(size_t i_col = 0; i_col < num_cols(); ++i_col)
204 {
205 if(i_col != 0) os << " " << seperator << " ";
206 os << EntryToString(*this, i_row, i_col);
207 }
208
209 os << "\n";
210 }
211 return os.str();
212}
213
214template <class T>
215std::ostream& operator << (std::ostream& os, const Table<T>& table)
216{
217 return table.stream(os);
218}
219
220template <class T>
221std::ostream& Table<T>::stream(std::ostream& os) const
222{
223 const Table<T> &table = *this;
224// we'll fill a fresh table with strings of the given table
225// At the same time we'll find the max-width of each column
226 Table<std::string> strTable(num_rows(), num_cols());
227 std::vector<size_t> colSizes(num_cols(), 0);
228
229 for(size_t i_row = 0; i_row < num_rows(); ++i_row){
230 for(size_t i_col = 0; i_col < num_cols(); ++i_col){
231 strTable(i_row, i_col) = EntryToString(table, i_row, i_col);
232 colSizes[i_col] = std::max(colSizes[i_col], strTable(i_row, i_col).size());
233 }
234 }
235
236 size_t totalRowLength=0;
237 for(size_t i_col = 0; i_col < num_cols(); ++i_col)
238 {
239 totalRowLength++;
240 totalRowLength += colSizes[i_col] + 2;
241 }
242 totalRowLength++;
243
244// now print each row
245 for(size_t i_row = 0; i_row < num_rows(); ++i_row)
246 {
247 if(get_row_sep(i_row) != 0x00 && get_row_sep(i_row) != ' ')
248 os << repeat(get_row_sep(i_row), totalRowLength) << "\n";
249 for(size_t i_col = 0; i_col < num_cols(); ++i_col)
250 {
251 os << get_col_sep(i_col);
252 size_t l = colSizes[i_col] + 1;
253 size_t s = strTable(i_row, i_col).size();
254 os << " ";
255 if(get_col_alignment(i_col) == 'r')
256 os << std::setw(l) << std::right << strTable(i_row, i_col);
257 else if(get_col_alignment(i_col) == 'l')
258 os << std::setw(l) << std::left << strTable(i_row, i_col);
259 else
260 os << repeat(' ', (l-s)/2) << std::setw(l-(l-s)/2) << std::left << strTable(i_row, i_col);
261 }
262 os << get_col_sep(num_cols()) << std::endl;
263 }
264 if(get_row_sep(num_cols()) != 0x00 && get_row_sep(num_cols()) != ' ')
265 os << repeat(get_row_sep(num_cols()), totalRowLength) << "\n";
266 return os;
267}
268
269
270
271
272template <class T>
273inline
274std::string EntryToString(const Table<T>& table, size_t rowInd, size_t colInd)
275{
276 std::stringstream ss;
277 ss << table(rowInd, colInd);
278 return ss.str();
279}
280
281
282inline
283std::string EntryToString(const Table<std::string>& table,
284 size_t rowInd, size_t colInd)
285{
286 return table(rowInd, colInd);
287}
288
289inline
291 size_t rowInd, size_t colInd)
292{
293 return table(rowInd, colInd).str();
294}
295
296}// end of namespace
297
298#endif
parameterString s
Useful for printing a table to the terminal or into a file.
Definition table.h:84
~Table()
Definition table_impl.hpp:67
void add_rows(size_t num)
Definition table_impl.hpp:85
size_t num_rows() const
Definition table_impl.hpp:137
char m_defaultColAlignment
Definition table.h:215
Table()
Definition table_impl.hpp:45
std::string to_string() const
Definition table_impl.hpp:153
std::ostream & stream(std::ostream &os) const
Definition table_impl.hpp:221
void clear()
Definition table_impl.hpp:73
std::string to_latex() const
Definition table_impl.hpp:163
T & operator()(size_t rowInd, size_t colInd)
Returns a reference to the given entry.
Definition table_impl.hpp:117
char m_defaultRowSeperator
Definition table.h:215
std::string to_csv(const char *seperator) const
Definition table_impl.hpp:198
size_t num_cols() const
Definition table_impl.hpp:143
char m_defaultColSeperator
Definition table.h:215
void add_cols(size_t num)
Definition table_impl.hpp:103
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition math_matrix.cpp:38
std::string EntryToString(const Table< T > &table, size_t rowInd, size_t colInd)
Returns a string-representation of the current entry.
Definition table_impl.hpp:274
string repeat(char c, int nr)
Builds a string with specified repetitions of given character.
Definition string_util.cpp:346
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition error.h:61
the ug namespace
size_t num_rows() const
Definition sparsematrix_interface.h:38
size_t num_cols() const
Definition sparsematrix_interface.h:39