UTL

Collection of self-contained header-only libraries for C++17

View on GitHub

utl::table

<- to README.md

<- to implementation.hpp

utl::table implements LaTeX-like table drawing methods.

Useful in benchmarks and prototyping of algorithms to represent results as a table.

Definitions

using uint = std::streamsize;

// Table setup
void create(std::initializer_list<uint> &&widths);

// (optional)
void set_formats(std::initializer_list<ColumnFormat> &&formats);
void set_ostream(std::ostream &new_ostream);
void set_latex_mode(bool toggle);

// Drawing
template <class T, class... Types>
void cell(const T& value, const Types&... other_values); // table cells

void hline(); // horizontal line

// Format flags
ColumnFormat NONE;                          // default
ColumnFormat FIXED(     uint decimals = 3); // fixed      floats with given precision
ColumnFormat DEFAULT(   uint decimals = 6); // default    floats with given precision
ColumnFormat SCIENTIFIC(uint decimals = 3); // scientific floats with given precision
ColumnFormat BOOL;                          // bools as text

Methods

Table setup

void create(std::initializer_list<uint> &&widths);

Sets up table with given column widths. Similar to LaTeX |c{1cm}|c{1cm}|c{1cm}| syntax.

void set_formats(std::initializer_list<ColumnFormat> &&formats);

Sets up column std::ios flags. Mainly used with build-in table:: flags to change float formatting.

void set_ostream(std::ostream &new_ostream);

Redirects output to given std::ostream. By default std::cout is used.

void set_latex_mode(bool toggle);

Enables/disables LaTeX-compatible formatting.

Tables rendered with this option on will use LaTeX formatting and automatically wrap numbers in formula blocks.

This is useful for exporting tables that can be copy-pasted into a LaTeX document.

Drawing

template <class T, class... Types>
void cell(const T& value, const Types&... other_values);

Draws cells with given values, accepts any number of arguments and can be used to draw entire rows in a single line (see examples). Similar to LaTeX val1 & val2 & val3 \\ except line breaks are placed automatically based on the table width.

void hline();

Draws a horizontal line. Similar to LaTeX \hline.

Format flags

ColumnFormat NONE;
ColumnFormat FIXED(     uint decimals = 3);
ColumnFormat DEFAULT(   uint decimals = 6);
ColumnFormat SCIENTIFIC(uint decimals = 3);
ColumnFormat BOOL;

Predefined format flags. NONE sets no flags. FIXED(n), DEFAULT(n) and SCIENTIFIC(n) set corresponding float representations and precision. BOOL makes booleans render as true & false.

Examples

Drawing a table

[ Run this code ]

using namespace utl;

table::create({ 16, 16, 16, 16, 20 });
table::set_formats({ table::NONE, table::DEFAULT(), table::FIXED(2),table::SCIENTIFIC(3), table::BOOL });

table::hline();
table::cell("Method", "Threads", "Speedup", "Error", "Err. within range");
table::hline();
table::cell("Gauss",      16, 11.845236, 1.96e-4, false);
table::cell("Jacobi",     16, 15.512512, 1.37e-5, false);
table::cell("Seidel",     16, 13.412321, 1.74e-6, true );
table::cell("Relaxation", 16, 13.926783, 1.17e-6, true );
table::hline();

Output:

|----------------|----------------|----------------|----------------|--------------------|
|          Method|         Threads|         Speedup|           Error|   Err. within range|
|----------------|----------------|----------------|----------------|--------------------|
|           Gauss|              16|           11.85|       1.960e-04|               false|
|          Jacobi|              16|           15.51|       1.370e-05|               false|
|          Seidel|              16|           13.41|       1.740e-06|                true|
|      Relaxation|              16|           13.93|       1.170e-06|                true|
|----------------|----------------|----------------|----------------|--------------------|

LaTeX-compatible table

[ Run this code ]

using namespace utl;

table::create({ 10, 8, 8, 20, 18 });
table::set_formats({ table::NONE, table::DEFAULT(), table::FIXED(2),table::SCIENTIFIC(3), table::BOOL });

table::set_latex_mode(true);
// <- adding this line makes table render in LaTeX format

table::hline();
table::cell("Method", "Threads", "Speedup", "Error", "Err. within range");
table::hline();
table::cell("Gauss",      16, 11.845236, 1.96e-4, false);
table::cell("Jacobi",     16, 15.512512, 1.37e-5, false);
table::cell("Seidel",     16, 13.412321, 1.74e-6, true );
table::cell("Relaxation", 16, 13.926783, 1.17e-6, true );
table::hline();

Output:

\hline
    Method &  Threads &  Speedup &                Error &  Err. within range \\
\hline
     Gauss &     $16$ &  $11.85$ & $1.96 \cdot 10^{-4}$ &              false \\
    Jacobi &     $16$ &  $15.51$ & $1.37 \cdot 10^{-5}$ &              false \\
    Seidel &     $16$ &  $13.41$ & $1.74 \cdot 10^{-6}$ &               true \\
Relaxation &     $16$ &  $13.93$ & $1.17 \cdot 10^{-6}$ &               true \\
\hline