Class columnify (o2scl)

O2scl : Class List

class columnify

Create nicely formatted columns from a table of strings.

This is a brute-force approach of order \( \mathrm{ncols} \times \mathrm{nrows} \). The column widths and spacings of are computed by exhaustively examining all strings in every column.

Todo

  • Future: Create a function which accepts delimited strings (e.g. like csv) instead of vector<vector<string>>.

  • Future: Consider a function which takes a ref o2scl::table object as input?

  • Future: Make a LaTeX output?

Public Functions

inline columnify()
template<class mat_string_t, class vec_string_t, class vec_int_t>
inline int align(const mat_string_t &table, size_t ncols, size_t nrows, vec_string_t &ctable, vec_int_t &align_spec, size_t n_headers = 0)

Take table and create a new object ctable with appropriately formatted columns.

This function takes the input table and aligns all of the
columns according to the specifications in \c align_spec
and then places the strings for all of the final rows
in ctable.
The table of strings should be stored in table in “column-major” order (table[ncols][nrows]), so that table has the interpretation of a set of columns to be aligned. Before calling align(), ctable should be allocated so that at least the first nrows entries can be assigned, and align_spec should contain ncols entries specifying the style of alignment for each column.

The first argument, of type mat_string_t, can be any type which is accessible using two applications of operator[], such as string **, vector<string>[], or vector<vector<string>>. The fourth type must be a type which can be resized with one resize() call (of size ncols) and then for the first index and then an additional ncols calls to resize() of size nrows for example vector<string>. The fifth type, vec_int_t, may be any integer array type.

template<class vec_string_t>
inline int add_spaces_one(const vec_string_t &col_in, int align_spec, vec_string_t &col_out, size_t n_headers = 0)

For input col_in, add spaces to each entry to ensure that each string has the same width, and store the result in col_out.

template<class mat_string_t, class mat_string2_t, class vec_int_t>
inline int add_spaces(const mat_string_t &table_in, size_t ncols, size_t nrows, vec_int_t &align_spec, mat_string2_t &table_out, size_t n_headers = 0)

Add enough spaces to ensure all columns have the same width.

The first argument, table_in, can be any type which is accessible using two applications of operator[], such as string **, vector<string>[], or vector<vector<string>>. The type for the fourth argument, align_spec, can be any integer array type, but it must have size greater than or equal to ncols. The type for the fifth argument, table_out, must be a type which can be resized with one resize() call (of size ncols) and then for the first index and then an additional ncols calls to resize() (of size nrows) (for example vector<vector<string>>).

This function makes two passes through the input table.
The first pass constructs the maximum width for each
column. The second pass adds spaces sufficient to ensure
each row has the same width.

Public Members

int table_lines

Specification for table lines (experimental; default 0)

If table_lines is 1, then ascii characters are used to create table lines. If table_lines is 2, then terminal sequences are used to create graphical lines.

int verbose

Verbosity parameter (default 0)

Public Static Attributes

static const int align_left = 1

Align the left-hand sides.

static const int align_right = 2

Align the right-hand sides.

static const int align_lmid = 3

Center, slightly to the left if spacing is uneven.

static const int align_rmid = 4

Center, slightly to the right if spacing is uneven.

static const int align_dp = 5

Align entries with decimal points. For header rows, align to the left.

static const int align_lnum = 6

Align negative numbers to the left and use a space for positive numbers. Align headers to the left-most digit.

static const int align_auto = 7

Pre-process the columns to decide their alignment.

This option uses align_left for columns with text, align_lnum for columns of all floating point numbers, align_right for integers, and align_dp otherwise. Header rows are ignored when deciding the alignment.