ug4
table.h
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
34 #define __H__TABLE
35 
36 #include <sstream>
37 #include <string>
38 #include <vector>
39 #include <iostream>
40 
41 namespace ug{
42 
45 
47 
82 template <class T>
83 class Table
84 {
85  public:
86  Table();
87  Table(size_t numRows, size_t numCols);
88 
89  ~Table();
90 
91  void clear();
92 
93  void add_rows(size_t num);
94  void add_cols(size_t num);
95 
97 
99  T& operator() (size_t rowInd, size_t colInd);
100 
102  void set(size_t rowInd, size_t colInd, T value)
103  {
104  operator()(rowInd, colInd) = value;
105  }
106 
108  const T& operator() (size_t rowInd, size_t colInd) const;
109 
111  const T &get(size_t rowInd, size_t colInd) const
112  {
113  return operator()(rowInd, colInd);
114  }
115 
116  size_t num_rows() const;
117  size_t num_cols() const;
118 
119 
120 
121 
122  void set_default_row_seperator(const char *c)
123  {
125  }
126  void set_row_seperator(size_t i_row, const char *c)
127  {
128  if(m_rowSep.size() <= i_row) m_rowSep.resize(i_row+1, 0x00);
129  m_rowSep[i_row] = *c;
130  }
131  void set_row_seperators(std::string s)
132  {
133  if(m_rowSep.size() < s.size()) m_rowSep.resize(s.size(), 0x00);
134  for(size_t i=0; i<s.size(); i++)
135  m_rowSep[i] = s[i];
136  }
137  void set_default_col_seperator(const char *c)
138  {
139 
141  }
142  void set_col_seperator(size_t i_col, const char *c)
143  {
144  if(m_colSep.size() <= i_col) m_colSep.resize(i_col+1, 0x00);
145  m_colSep[i_col] = *c;
146  }
147  void set_col_seperators(std::string s)
148  {
149  if(m_colSep.size() < s.size()) m_colSep.resize(s.size(), 0x00);
150  for(size_t i=0; i<s.size(); i++)
151  m_colSep[i] = s[i];
152  }
153 
154  void set_default_col_alignment(const char *c)
155  {
157  }
158  void set_col_alignment(size_t i_col, const char *c)
159  {
160  if(m_colAlign.size() <= i_col) m_colAlign.resize(i_col+1, 0x00);
161  m_colAlign[i_col] = *c;
162  }
163  void set_col_alignments(std::string s)
164  {
165  if(m_colAlign.size() < s.size()) m_colAlign.resize(s.size(), 0x00);
166  for(size_t i=0; i<s.size(); i++)
167  m_colAlign[i] = s[i];
168  }
169 
170  std::ostream& stream(std::ostream& os) const;
171  std::string to_latex() const;
172  std::string to_string() const;
173  std::string to_csv(const char *seperator) const;
174 
175  char get_row_sep(size_t row) const
176  {
177  if(row >= m_rowSep.size()) return m_defaultRowSeperator;
178  return m_rowSep[row] != 0x00 ? m_rowSep[row] : m_defaultRowSeperator;
179  }
180 
181  char get_col_sep(size_t col) const
182  {
183  if(col >= m_colSep.size()) return m_defaultColSeperator;
184  return m_colSep[col] != 0x00 ? m_colSep[col] : m_defaultColSeperator;
185  }
186 
187  char get_col_alignment(size_t col) const
188  {
189  if(col >= m_colAlign.size()) return m_defaultColAlignment;
190  return m_colAlign[col] != 0x00 ? m_colAlign[col] : m_defaultColAlignment;
191  }
192 
193  void transpose()
194  {
195  DataVec newData;
196  newData.resize(m_numCols);
197  for(size_t irow = 0; irow < m_numRows; ++irow){
198  for(size_t icol = 0; icol < m_numCols; ++icol){
199  newData[icol].push_back(m_data[irow][icol]);
200  }
201  }
202  std::swap(m_numRows, m_numCols);
203  m_data.swap(newData);
204  }
205  private:
206  size_t m_numRows;
207  size_t m_numCols;
208 
209  typedef std::vector<std::vector<T*> > DataVec;
211 
212  std::vector<char> m_colAlign;
213  std::vector<char> m_colSep;
214  std::vector<char> m_rowSep;
216 
217 };
218 
220 
228 template <class T>
229 std::ostream& operator << (std::ostream& os, const Table<T>& table);
230 
231 
233 
236 template <class T>
237 std::string EntryToString(const Table<T>& table, size_t rowInd, size_t colInd);
238 
239 inline
240 std::string EntryToString(const Table<std::string>& table,
241  size_t rowInd, size_t colInd);
242 
243 inline
244 std::string EntryToString(const Table<std::stringstream>& table,
245  size_t rowInd, size_t colInd);
246 
247 
250 
251 // end group ugbase_common_types
253 
254 }// end of namespace
255 
257 // include implementation
258 #include "table_impl.hpp"
259 
260 #endif
parameterString s
Useful for printing a table to the terminal or into a file.
Definition: table.h:84
std::vector< char > m_colAlign
Definition: table.h:212
void set_col_alignments(std::string s)
Definition: table.h:163
~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
char get_col_alignment(size_t col) const
Definition: table.h:187
void set_col_alignment(size_t i_col, const char *c)
Definition: table.h:158
void transpose()
Definition: table.h:193
std::vector< std::vector< T * > > DataVec
Definition: table.h:209
Table()
Definition: table_impl.hpp:45
void set(size_t rowInd, size_t colInd, T value)
uses operator() to set an entry to a value
Definition: table.h:102
std::string to_string() const
Definition: table_impl.hpp:153
char get_row_sep(size_t row) const
Definition: table.h:175
void set_default_col_alignment(const char *c)
Definition: table.h:154
size_t m_numCols
Definition: table.h:207
std::ostream & stream(std::ostream &os) const
Definition: table_impl.hpp:221
void clear()
Definition: table_impl.hpp:73
DataVec m_data
Definition: table.h:210
void set_default_col_seperator(const char *c)
Definition: table.h:137
void set_col_seperators(std::string s)
Definition: table.h:147
std::vector< char > m_rowSep
Definition: table.h:214
std::string to_latex() const
Definition: table_impl.hpp:163
void set_row_seperators(std::string s)
Definition: table.h:131
T & operator()(size_t rowInd, size_t colInd)
Returns a reference to the given entry.
Definition: table_impl.hpp:117
void set_default_row_seperator(const char *c)
Definition: table.h:122
const T & get(size_t rowInd, size_t colInd) const
uses operator() to get a value
Definition: table.h:111
void set_col_seperator(size_t i_col, const char *c)
Definition: table.h:142
std::vector< char > m_colSep
Definition: table.h:213
void set_row_seperator(size_t i_row, const char *c)
Definition: table.h:126
char m_defaultRowSeperator
Definition: table.h:215
char get_col_sep(size_t col) const
Definition: table.h:181
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
size_t m_numRows
Definition: table.h:206
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition: math_matrix.cpp:38
Table< std::string > StringTable
Definition: table.h:248
Table< std::stringstream > StringStreamTable
Definition: table.h:249
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
the ug namespace