ug4
ug::Table< T > Class Template Reference

Useful for printing a table to the terminal or into a file. More...

#include <table.h>

Public Member Functions

void add_cols (size_t num)
 
void add_rows (size_t num)
 
void clear ()
 
const T & get (size_t rowInd, size_t colInd) const
 uses operator() to get a value More...
 
char get_col_alignment (size_t col) const
 
char get_col_sep (size_t col) const
 
char get_row_sep (size_t row) const
 
size_t num_cols () const
 
size_t num_rows () const
 
T & operator() (size_t rowInd, size_t colInd)
 Returns a reference to the given entry. More...
 
const T & operator() (size_t rowInd, size_t colInd) const
 Returns a reference to the given entry. More...
 
void set (size_t rowInd, size_t colInd, T value)
 uses operator() to set an entry to a value More...
 
void set_col_alignment (size_t i_col, const char *c)
 
void set_col_alignments (std::string s)
 
void set_col_seperator (size_t i_col, const char *c)
 
void set_col_seperators (std::string s)
 
void set_default_col_alignment (const char *c)
 
void set_default_col_seperator (const char *c)
 
void set_default_row_seperator (const char *c)
 
void set_row_seperator (size_t i_row, const char *c)
 
void set_row_seperators (std::string s)
 
std::ostream & stream (std::ostream &os) const
 
 Table ()
 
 Table (size_t numRows, size_t numCols)
 
std::string to_csv (const char *seperator) const
 
std::string to_latex () const
 
std::string to_string () const
 
void transpose ()
 
 ~Table ()
 

Private Types

typedef std::vector< std::vector< T * > > DataVec
 

Private Attributes

std::vector< char > m_colAlign
 
std::vector< char > m_colSep
 
DataVec m_data
 
char m_defaultColAlignment
 
char m_defaultColSeperator
 
char m_defaultRowSeperator
 
size_t m_numCols
 
size_t m_numRows
 
std::vector< char > m_rowSep
 

Detailed Description

template<class T>
class ug::Table< T >

Useful for printing a table to the terminal or into a file.

This class is for output purposes only. It automatically adds spacings, so that all entries in a column are indeed displayed in the same column. Note that the implementation is not optimized in regards to speed and efficiency. The class thus shouldn't be used in performance critical sections of your code. If it is however used in output related code, the introduced overhead should be fine and probably even negligible.

The class is most commonly used as a string table (Table<std::string>) or as a stringstream table (Table<std::stringstream>). If you want to use it for your own types, you may specialize the template method

template <class T> std::string EntryToString(const Table<T>& table, size_t rowInd, size_t colInd);
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 default implementation of EntryToString may already be suited for most cases.

Here's an example on how to use the class:

...
ug::Table<std::stringstream> table;
table(0, 0) << "num rows:"; table(0, 1) << table.num_rows();
table(1, 0) << "num columns:"; table(1, 1) << table.num_cols();
std::cout << table;

And this is what the output looks like:

    num rows:  2
    num columns:  2
* 

Note that several typedefs exist: StringTable, StringStreamTable

Todo:
different alignments for different columns / rows / fields

Member Typedef Documentation

◆ DataVec

template<class T >
typedef std::vector<std::vector<T*> > ug::Table< T >::DataVec
private

Constructor & Destructor Documentation

◆ Table() [1/2]

◆ Table() [2/2]

◆ ~Table()

template<class T >
ug::Table< T >::~Table

Member Function Documentation

◆ add_cols()

template<class T >
void ug::Table< T >::add_cols ( size_t  num)

Referenced by ug::Table< T >::Table().

◆ add_rows()

template<class T >
void ug::Table< T >::add_rows ( size_t  num)

Referenced by ug::Table< T >::Table().

◆ clear()

template<class T >
void ug::Table< T >::clear

◆ get()

template<class T >
const T& ug::Table< T >::get ( size_t  rowInd,
size_t  colInd 
) const
inline

uses operator() to get a value

References ug::Table< T >::operator()().

◆ get_col_alignment()

template<class T >
char ug::Table< T >::get_col_alignment ( size_t  col) const
inline

◆ get_col_sep()

template<class T >
char ug::Table< T >::get_col_sep ( size_t  col) const
inline

◆ get_row_sep()

template<class T >
char ug::Table< T >::get_row_sep ( size_t  row) const
inline

◆ num_cols()

template<class T >
size_t ug::Table< T >::num_cols

◆ num_rows()

template<class T >
size_t ug::Table< T >::num_rows

◆ operator()() [1/2]

template<class T >
T & ug::Table< T >::operator() ( size_t  rowInd,
size_t  colInd 
)

Returns a reference to the given entry.

If an entry lies outside of the tables bounds, the table will automatically be resized accordingly.

Referenced by ug::Table< T >::get(), and ug::Table< T >::set().

◆ operator()() [2/2]

template<class T >
const T & ug::Table< T >::operator() ( size_t  rowInd,
size_t  colInd 
) const

Returns a reference to the given entry.

References num_cols(), num_rows(), and UG_COND_THROW.

◆ set()

template<class T >
void ug::Table< T >::set ( size_t  rowInd,
size_t  colInd,
value 
)
inline

uses operator() to set an entry to a value

References ug::Table< T >::operator()().

◆ set_col_alignment()

template<class T >
void ug::Table< T >::set_col_alignment ( size_t  i_col,
const char *  c 
)
inline

◆ set_col_alignments()

template<class T >
void ug::Table< T >::set_col_alignments ( std::string  s)
inline

References ug::Table< T >::m_colAlign, and s.

◆ set_col_seperator()

template<class T >
void ug::Table< T >::set_col_seperator ( size_t  i_col,
const char *  c 
)
inline

◆ set_col_seperators()

template<class T >
void ug::Table< T >::set_col_seperators ( std::string  s)
inline

References ug::Table< T >::m_colSep, and s.

◆ set_default_col_alignment()

template<class T >
void ug::Table< T >::set_default_col_alignment ( const char *  c)
inline

◆ set_default_col_seperator()

template<class T >
void ug::Table< T >::set_default_col_seperator ( const char *  c)
inline

◆ set_default_row_seperator()

template<class T >
void ug::Table< T >::set_default_row_seperator ( const char *  c)
inline

◆ set_row_seperator()

template<class T >
void ug::Table< T >::set_row_seperator ( size_t  i_row,
const char *  c 
)
inline

◆ set_row_seperators()

template<class T >
void ug::Table< T >::set_row_seperators ( std::string  s)
inline

References ug::Table< T >::m_rowSep, and s.

◆ stream()

template<class T >
std::ostream & ug::Table< T >::stream ( std::ostream &  os) const

◆ to_csv()

template<typename T >
std::string ug::Table< T >::to_csv ( const char *  seperator) const

◆ to_latex()

template<typename T >
std::string ug::Table< T >::to_latex

References num_cols(), and num_rows().

◆ to_string()

◆ transpose()

template<class T >
void ug::Table< T >::transpose ( )
inline

Member Data Documentation

◆ m_colAlign

template<class T >
std::vector<char> ug::Table< T >::m_colAlign
private

◆ m_colSep

template<class T >
std::vector<char> ug::Table< T >::m_colSep
private

◆ m_data

template<class T >
DataVec ug::Table< T >::m_data
private

◆ m_defaultColAlignment

template<class T >
char ug::Table< T >::m_defaultColAlignment
private

◆ m_defaultColSeperator

template<class T >
char ug::Table< T >::m_defaultColSeperator
private

◆ m_defaultRowSeperator

template<class T >
char ug::Table< T >::m_defaultRowSeperator
private

◆ m_numCols

template<class T >
size_t ug::Table< T >::m_numCols
private

◆ m_numRows

template<class T >
size_t ug::Table< T >::m_numRows
private

◆ m_rowSep

template<class T >
std::vector<char> ug::Table< T >::m_rowSep
private

The documentation for this class was generated from the following files: