Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
mbl_table Class Reference

Container for tabulated data suitable for reading/writing to delimited text files. More...

#include <mbl_table.h>

List of all members.

Public Member Functions

 mbl_table (const char delim='\t')
 Constructor.
 mbl_table (const char delim, const vcl_vector< vcl_string > &headers)
 Constructor.
unsigned num_cols () const
 Return the number of columns.
unsigned num_rows () const
 Return the number of rows.
bool get_column (const vcl_string &header, vcl_vector< double > &column) const
 Get the column of data corresponding to a particular heading.
bool column_exists (const vcl_string &header) const
 Returns true if column exists.
bool get_row (const unsigned &r, vcl_vector< double > &row) const
 Get a specified row of data.
void get_column_headers (vcl_vector< vcl_string > &headers) const
 Get the list of column headers (in column order).
bool append_column (const vcl_string &header, const vcl_vector< double > &column)
 Append a column of data with its own heading.
bool append_column (const vcl_string &header, const double val=0)
 Append an empty column with its own heading.
bool append_row (const vcl_vector< double > &row)
 Append a row of data.
bool append_row (const double val=0)
 Append an empty row.
bool set_element (const vcl_string &header, const unsigned r, const double value)
 Set the value of an existing element.
double get_element (const vcl_string &header, const unsigned r, bool *success=0) const
 Get the value of an existing element.
bool read (vcl_istream &is)
 Load this table's data from specified text stream.
void write (vcl_ostream &os) const
 Save this table's data to specified text stream.
bool subtable (mbl_table &new_table, const vcl_vector< vcl_string > &headers) const
 Create a new table of subset of columns defined by headers.
bool operator== (const mbl_table &rhs) const
 Is another table identical to this one?.
bool operator!= (const mbl_table &rhs) const
 Is another table different from this one?.

Static Public Member Functions

static void set_tolerance (const double &tol, const bool &fract=false)
 Set the tolerance used to determine whether table entries are equal.
static void set_verbosity (const int &v)
 Set the level of verbosity used for error output.

Protected Member Functions

bool read_delimited_string (vcl_istream &is, vcl_string &str, bool &eol, bool &eof)
 Read a series of characters from the stream until a delimiter character or eol.

Protected Attributes

char delimiter_
 The character delimiting each column.
vcl_vector< vcl_string > column_headers_
 The column headers (in order).
vcl_map< vcl_string, unsigned > header_to_column_index_
 Map a column header string to column index.
vcl_vector< vcl_vector< double > > columns_
 The table data, arranged as column vectors of double data.

Detailed Description

Container for tabulated data suitable for reading/writing to delimited text files.

Table comprises a number of columns of double-precision data. Each column is headed by a text string (which may contain whitespace but not end-of-line). Column headers must be unique. Columns are separated by a delimiter character (e.g. "\t"). The delimiter character must not occur in any of the entries! The delimiter character is optional at the end of a row. Each row must be terminated by an end-of-line (including the last row). Each column of data is stored as a vector of doubles and associated with the column header string.

Definition at line 24 of file mbl_table.h.


Constructor & Destructor Documentation

mbl_table::mbl_table ( const char  delim = '\t')

Constructor.

Parameters:
delimThe delimiter character.

Definition at line 26 of file mbl_table.cxx.

mbl_table::mbl_table ( const char  delim,
const vcl_vector< vcl_string > &  headers 
)

Constructor.

Parameters:
delimThe delimiter character.
headersThe column headers (in order).

Definition at line 34 of file mbl_table.cxx.


Member Function Documentation

bool mbl_table::append_column ( const vcl_string &  header,
const vcl_vector< double > &  column 
)

Append a column of data with its own heading.

Parameters:
headerString identifying the column.
columnA vector containing the values of the column.
Returns:
true If the column was added.
Note:
The new column must be the same length as existing columns.

Definition at line 236 of file mbl_table.cxx.

bool mbl_table::append_column ( const vcl_string &  header,
const double  val = 0 
)

Append an empty column with its own heading.

Parameters:
headerString identifying the column.
valDefault value to initialize all elements of the new column.
Returns:
true If the column was added.
Note:
The new column will be the same length as existing columns.

Definition at line 270 of file mbl_table.cxx.

bool mbl_table::append_row ( const vcl_vector< double > &  row)

Append a row of data.

Returns:
true if the row was added.
Parameters:
rowA vector containing the values of the new row.
Note:
The new row must be the same length as existing rows.

Definition at line 297 of file mbl_table.cxx.

bool mbl_table::append_row ( const double  val = 0)

Append an empty row.

Returns:
true If the row was added.
Parameters:
valDefault value to initialize all elements of the new row.
Note:
The new row will be the same length as existing rows.

Definition at line 322 of file mbl_table.cxx.

bool mbl_table::column_exists ( const vcl_string &  header) const

Returns true if column exists.

Definition at line 76 of file mbl_table.cxx.

bool mbl_table::get_column ( const vcl_string &  header,
vcl_vector< double > &  column 
) const

Get the column of data corresponding to a particular heading.

Parameters:
headerString identifying the desired column.
Returns:
true if there is a column with the specified heading.
Return values:
columnA vector containing the values of the requested column.

Definition at line 88 of file mbl_table.cxx.

void mbl_table::get_column_headers ( vcl_vector< vcl_string > &  headers) const

Get the list of column headers (in column order).

Return values:
headersThe list of column headers.

Definition at line 146 of file mbl_table.cxx.

double mbl_table::get_element ( const vcl_string &  header,
const unsigned  r,
bool *  success = 0 
) const

Get the value of an existing element.

Parameters:
headerThe string identifying the column of interest.
rThe row index of the element of interest.
Returns:
The value of the requested element (undefined if the element does not exist).
Return values:
successIf provided, will be used to indicate whether the specified element existed.
Note:
This function is intended only for existing elements.
It is recommended that you provide and check the parameter success.

Definition at line 194 of file mbl_table.cxx.

bool mbl_table::get_row ( const unsigned &  r,
vcl_vector< double > &  row 
) const

Get a specified row of data.

Parameters:
rIndex of the desired row.
Returns:
true if there is a row with the specified index.
Return values:
rowA vector containing the values of the requested row.

Definition at line 117 of file mbl_table.cxx.

unsigned mbl_table::num_cols ( ) const

Return the number of columns.

Definition at line 55 of file mbl_table.cxx.

unsigned mbl_table::num_rows ( ) const

Return the number of rows.

Definition at line 64 of file mbl_table.cxx.

bool mbl_table::operator!= ( const mbl_table rhs) const

Is another table different from this one?.

See also:
operator==()

Definition at line 593 of file mbl_table.cxx.

bool mbl_table::operator== ( const mbl_table rhs) const

Is another table identical to this one?.

Note:
The normal behaviour of this function is to return false as soon as a discrepancy is found. However, if verbosity>=2 this function will compare all corresponding elements (providing that the table dimensions match).

Definition at line 501 of file mbl_table.cxx.

bool mbl_table::read ( vcl_istream &  is)

Load this table's data from specified text stream.

Any existing data is lost.

Returns:
true if table was read successfully from the stream.

Definition at line 338 of file mbl_table.cxx.

bool mbl_table::read_delimited_string ( vcl_istream &  is,
vcl_string &  str,
bool &  eol,
bool &  eof 
) [protected]

Read a series of characters from the stream until a delimiter character or eol.

Parameters:
isThe input stream.
Returns:
true if a non-empty string was successfully read.
Return values:
strThe string which was read.
eolWhether the stream is at end-of-line after reading the string.
eofWhether the stream is at end-of-file after reading the string.

Definition at line 456 of file mbl_table.cxx.

bool mbl_table::set_element ( const vcl_string &  header,
const unsigned  r,
const double  value 
)

Set the value of an existing element.

Parameters:
headerThe string identifying the column to be modified.
rThe row index of the element to be modified.
valueThe new value to use for the modified element.
Returns:
False if the column does not exist or the row index is not valid.
Note:
This function is intended only for existing elements.

Definition at line 155 of file mbl_table.cxx.

void mbl_table::set_tolerance ( const double &  tol,
const bool &  fract = false 
) [static]

Set the tolerance used to determine whether table entries are equal.

Parameters:
tolThe tolerance should be a small positive number, eg 1e-19.
fracWhether the tolerance is applied as a fractional difference.

Definition at line 602 of file mbl_table.cxx.

void mbl_table::set_verbosity ( const int &  v) [static]

Set the level of verbosity used for error output.

Parameters:
vThe verbosity should be a small integer, eg -3 to 3. Larger values cause more detailed output. Default level is 0.
Note:
Currently only levels 1 and 2 are defined: level 1 provides some feedback for equality testing; level 2 also causes the equality operator to check all corresponding elements of a table.

Definition at line 613 of file mbl_table.cxx.

bool mbl_table::subtable ( mbl_table new_table,
const vcl_vector< vcl_string > &  headers 
) const

Create a new table of subset of columns defined by headers.

Create a new table with as subset of columns defined by headers.

Return values:
new_tablea subtable
Parameters:
headerssubset of column headers
Returns:
true if all columns in headers existed in "this" and were copied

Definition at line 425 of file mbl_table.cxx.

void mbl_table::write ( vcl_ostream &  os) const

Save this table's data to specified text stream.

Definition at line 398 of file mbl_table.cxx.


Member Data Documentation

vcl_vector<vcl_string> mbl_table::column_headers_ [protected]

The column headers (in order).

Definition at line 193 of file mbl_table.h.

vcl_vector<vcl_vector<double> > mbl_table::columns_ [protected]

The table data, arranged as column vectors of double data.

Definition at line 199 of file mbl_table.h.

char mbl_table::delimiter_ [protected]

The character delimiting each column.

Definition at line 190 of file mbl_table.h.

vcl_map<vcl_string, unsigned> mbl_table::header_to_column_index_ [protected]

Map a column header string to column index.

Definition at line 196 of file mbl_table.h.


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