template<class vec_t>
class o2scl::table< vec_t >
Summary
A class to contain and manipulate several equally-sized columns of data. The purpose of this class is to provide a structure which allows one to refer to the columns using a name represented by a string. Thus for a table object named t
with 3 columns (named "colx", "coly" and "colz") and three rows, one could do the following:
t.set("colx",0,1.0);
t.set("colz",1,2.0);
t.set("coly",2,4.0);
cout << t.get("colz",1) << endl;
Note that the rows are numbered starting with 0 instead of starting with 1. To output all the rows of entire column, one can use
for(size_t i=0;i<t.get_nlines();i++) {
cout << i << " " << t.get("colx",i) << endl;
}
To output all the columns of an entire row (in the following example it is the second row), labeled by their column name, one can use:
for(size_t i=0;i<t.get_ncolumns();i++) {
cout << t.get_column_name(i) << " ";
}
cout << endl;
for(size_t i=0;i<t.get_ncolumns();i++) {
cout << t.get(i,1) << " ";
}
cout << endl;
Methods are provided for interpolating columns, sorting columns, finding data points, and several other manipulations of the data.
Column size
The columns grow automatically (similar to the STL <vector>) in reponse to an attempt to call set() for a row that does not presently exist or in a call to line_of_data() when the table is already full. However, this forces memory rearrangments that are expensive, O(R*C). If the user has a good estimate of the number of rows beforehand, it is best to either specify this in the constructor, or in an explicit call to inc_maxlines().
Lookup, differentiation, integration, and interpolation
Lookup, differentiation, integration, and interpolation are automatically implemented using splines from the class interp_vec. A caching mechanism is implemented so that successive interpolations, derivative evaluations or integrations over the same two columns are fast.
Sorting
The columns are automatically sorted by name for speed, the results can be accessed from get_sorted_name(). Individual columns can be sorted (sort_column() ), or the entire table can be sorted by one column (sort_table() ).
Data representation
Each individual column is just a vector object. The columns can be referred to in one of two ways:
- A numerical index from 0 to C-1 (where C is the number of columns). For example, data can be accessed through table::get() and table::set(size_t c, size_t r, double val), or the overloaded [] operator,
table[c][r]
.
- A name of the column which is a string. For example, data can be accessed with table::get(string cname, int r) and table::set(string cname, int r, double val).
The columns are organized in a both a <map> and a <vector> structure so that finding a column by its index, using either of
takes only constant time, and finding a column by its name using either of
const ubvector &
get_column(std::string col)
const;
is O(log(C)). Insertion of a column ( new_column() ) is O(log(C)), but deletion ( delete_column() ) is O(C). Adding a row of data can be either O(1) or O(C), but row insertion and deletion is slow, since the all of the rows must be shifted accordingly.
Because of the structure, this class is not suitable for the matrix manipulation.
Thread-safety
Generally, the member functions are only thread-safe if they are const
.
I/O and command-line manipulation
When data from an object of type table is output to a file through the hdf_output() function
in o2scl_hdf, the table can be manipulated on the command-line through the acol
utility (see The 'acol' Command-line Utility).
There is an example for the usage of this class given in examples/ex_table.cpp
.
- Todo:
- Specify somewhere what kind of vector types can be used for the template parameter. ublas objects work, but what about armadillo and Eigen vectors? The main reason the default type is std::vector is because of HDF5 I/O.
- Idea for Future:
- Create a delete_columns(std::string) function
- Return the empty column in the operator[] functions as is done for the get_column() functions.
- A "delete rows" method to delete a range of several rows
- The present structure,
std::map<std::string,col,string_comp>
atree and std::vector<aiter>
alist; could be replaced with std::vector<col>
list and std::map<std::string,int>
tree where the map just stores the index of the the column in the list.
- Rewrite check_synchro into a full is_valid()-like sanity check
Definition at line 47 of file table.h.
|
|
| table (size_t cmaxlines=0) |
| Create a new table with space for nlines<=cmaxlines.
|
|
virtual | ~table () |
|
| table (const table &t) |
| Copy constructor.
|
|
table & | operator= (const table &t) |
| Copy constructor.
|
|
|
void | set (std::string scol, size_t row, double val) |
| Set row row of column named col to value val . . More...
|
|
void | set (size_t icol, size_t row, double val) |
| Set row row of column number icol to value val . .
|
|
double | get (std::string scol, size_t row) const |
| Get value from row row of column named col . .
|
|
double | get (size_t icol, size_t row) const |
| Get value from row row of column number icol . .
|
|
size_t | get_ncolumns () const |
| Return the number of columns.
|
|
|
size_t | get_nlines () const |
| Return the number of lines.
|
|
void | set_nlines (size_t il) |
| Set the number of lines. More...
|
|
size_t | get_maxlines () |
| Return the maximum number of lines before a reallocation is required.
|
|
template<class vec2_t > |
void | get_row (std::string scol, double val, vec2_t &row) const |
| Returns a copy of the row with value val in column col . . More...
|
|
template<class vec2_t > |
void | get_row (size_t irow, vec2_t &row) const |
| Returns a copy of row number irow . . More...
|
|
void | set_nlines_auto (size_t il) |
| Set the number of lines, increasing the size more agressively. More...
|
|
void | inc_maxlines (size_t llines) |
| Manually increase the maximum number of lines.
|
|
|
const vec_t & | get_column (std::string scol) const |
| Returns a reference to the column named col . .
|
|
const vec_t & | operator[] (size_t icol) const |
| Returns the column of index icol (const version). . More...
|
|
const vec_t & | operator[] (std::string scol) const |
| Returns the column named scol (const version). . More...
|
|
void | new_column (std::string head) |
| Add a new column owned by the table . More...
|
|
template<class vec2_t > |
int | new_column (std::string name, size_t sz, vec2_t &v) |
| Add a new column by copying data from another vector. More...
|
|
std::string | get_column_name (size_t icol) const |
| Returns the name of column col .
|
|
virtual void | swap_column_data (std::string scol, vec_t &v) |
| Swap the data in column scol with that in vector v . More...
|
|
virtual void | rename_column (std::string src, std::string dest) |
| Rename column named src to dest .
|
|
virtual void | delete_column (std::string scol) |
| Delete column named scol . More...
|
|
std::string | get_sorted_name (size_t icol) const |
| Returns the name of column col in sorted order. .
|
|
void | init_column (std::string scol, double val) |
| Initialize all values of column named scol to val . More...
|
|
bool | is_column (std::string scol) const |
| Return true if scol is a column in the current table. More...
|
|
size_t | lookup_column (std::string lname) const |
| Find the index for column named name . More...
|
|
virtual void | copy_column (std::string src, std::string dest) |
| Copy data from column named src to column named dest , creating a new column if necessary .
|
|
template<class resize_vec_t > |
void | column_to_vector (std::string scol, resize_vec_t &v) const |
| Copy a column to a generic vector object. More...
|
|
template<class vec2_t > |
void | copy_to_column (vec2_t &v, std::string scol) |
| Copy to a column from a generic vector object.
|
|
template<class vec2_t > |
void | add_col_from_table (table< vec2_t > &source, std::string src_index, std::string src_col, std::string dest_index, std::string dest_col="") |
| Insert a column from a separate table, interpolating it into a new column. More...
|
|
|
void | new_row (size_t n) |
| Insert a row before row n . More...
|
|
void | copy_row (size_t src, size_t dest) |
| Copy the data in row src to row dest .
|
|
void | delete_row (std::string scol, double val) |
| Delete the row with the entry closest to the value val in column scol .
|
|
void | delete_row (size_t irow) |
| Delete the row of index irow .
|
|
void | delete_rows (std::string func) |
| Delete all rows where func evaluates to a number greater than 0.5 . More...
|
|
void | delete_rows (size_t row_start, size_t row_end) |
| Delete all rows between row_start and row_end . More...
|
|
void | line_of_names (std::string newheads) |
| Read a new set of names from newheads . More...
|
|
template<class vec2_t > |
void | line_of_data (size_t nv, const vec2_t &v) |
| Read a line of data from an array.
|
|
|
size_t | ordered_lookup (std::string scol, double val) const |
| Look for a value in an ordered column . More...
|
|
size_t | lookup (std::string scol, double val) const |
| Exhaustively search column col for the value val .
|
|
double | lookup_val (std::string scol, double val, std::string scol2) const |
| Search column col for the value val and return value in col2 .
|
|
size_t | lookup (int icol, double val) const |
| Exhaustively search column col for the value val .
|
|
size_t | mlookup (std::string scol, double val, std::vector< size_t > &results, double threshold=0.0) const |
| Exhaustively search column col for many occurences of val .
|
|
|
void | set_interp_type (size_t interp_type) |
| Set the base interpolation objects.
|
|
size_t | get_interp_type () const |
| Get the interpolation type.
|
|
double | interp (std::string sx, double x0, std::string sy) |
| Interpolate x0 from sx into sy . More...
|
|
double | interp_const (std::string sx, double x0, std::string sy) const |
| Interpolate x0 from sx into sy . More...
|
|
double | interp (size_t ix, double x0, size_t iy) |
| Interpolate x0 from ix into iy .
|
|
double | interp_const (size_t ix, double x0, size_t iy) const |
| Interpolate x0 from ix into iy .
|
|
void | deriv (std::string x, std::string y, std::string yp) |
| Make a new column yp which is the derivative . .
|
|
double | deriv (std::string sx, double x0, std::string sy) |
| The first derivative of the function sy(sx) at sx=x0. More...
|
|
double | deriv_const (std::string sx, double x0, std::string sy) const |
| The first derivative of the function sy(sx) at sx=x0. More...
|
|
double | deriv (size_t ix, double x0, size_t iy) |
| The first derivative of the function iy(ix) at ix=x0. More...
|
|
double | deriv_const (size_t ix, double x0, size_t iy) const |
| The first derivative of the function iy(ix) at ix=x0. More...
|
|
void | deriv2 (std::string x, std::string y, std::string yp) |
| Make a new column yp which is - O(log(C)*R).
|
|
double | deriv2 (std::string sx, double x0, std::string sy) |
| The second derivative of the function sy(sx) at sx=x0. More...
|
|
double | deriv2_const (std::string sx, double x0, std::string sy) const |
| The second derivative of the function sy(sx) at sx=x0. More...
|
|
double | deriv2 (size_t ix, double x0, size_t iy) |
| The second derivative of the function iy(ix) at ix=x0. More...
|
|
double | deriv2_const (size_t ix, double x0, size_t iy) const |
| The second derivative of the function iy(ix) at ix=x0. More...
|
|
double | integ (std::string sx, double x1, double x2, std::string sy) |
| The integral of the function sy(sx) from sx=x1 to sx=x2. More...
|
|
double | integ_const (std::string sx, double x1, double x2, std::string sy) const |
| The integral of the function sy(sx) from sx=x1 to sx=x2. More...
|
|
double | integ (size_t ix, double x1, double x2, size_t iy) |
| The integral of the function iy(ix) from ix=x1 to ix=x2. More...
|
|
double | integ_const (size_t ix, double x1, double x2, size_t iy) const |
| The integral of the function iy(ix) from ix=x1 to ix=x2. More...
|
|
void | integ (std::string x, std::string y, std::string ynew) |
| The integral of the function iy(ix) More...
|
|
double | max (std::string scol) const |
| Return column maximum. Makes no assumptions about ordering .
|
|
double | min (std::string scol) const |
| Return column minimum. Makes no assumptions about ordering .
|
|
|
table< vec_t > * | subtable (std::string list, size_t top, size_t bottom) const |
| Make a subtable. More...
|
|
|
void | zero_table () |
| Zero the data entries but keep the column names and nlines fixed.
|
|
void | clear_all () |
| Clear everything.
|
|
void | clear_table () |
| Clear the table and the column names (but leave constants)
|
|
void | clear_data () |
| Remove all of the data by setting the number of lines to zero. More...
|
|
void | clear_constants () |
| CLear all constants.
|
|
|
void | sort_table (std::string scol) |
| Sort the entire table by the column scol . More...
|
|
void | sort_column (std::string scol) |
| Individually sort the column scol . More...
|
|
|
virtual void | summary (std::ostream *out, size_t ncol=79) const |
| Output a summary of the information stored. More...
|
|
|
virtual void | add_constant (std::string name, double val) |
| Add a constant, or if the constant already exists, change its value.
|
|
virtual int | set_constant (std::string name, double val, bool err_on_notfound=true) |
| Add a constant.
|
|
virtual double | get_constant (std::string name) const |
| Get a constant.
|
|
virtual size_t | get_nconsts () const |
| Get the number of constants.
|
|
virtual void | get_constant (size_t ix, std::string &name, double &val) const |
| Get a constant by index.
|
|
virtual void | remove_constant (std::string name) |
| Remove a constant.
|
|
|
virtual int | read_generic (std::istream &fin, int verbose=0) |
| Clear the current table and read from a generic data file.
|
|
void | check_synchro () const |
| Return 0 if the tree and list are properly synchronized.
|
|
virtual const char * | type () |
| Return the type, "table" .
|
|
|
void | functions_columns (std::string list) |
| Create new columns or recompute from a list of functions. More...
|
|
void | function_column (std::string function, std::string scol) |
| Make a column from the function specified in function and add it to the table. More...
|
|
template<class vec2_t > |
void | function_vector (std::string function, vec2_t &vec, bool throw_on_err=true) |
| Compute a column from a function specified in a string.
|
|
double | row_function (std::string function, size_t row) const |
| Compute a value by applying a function to a row.
|
|
size_t | function_find_row (std::string function) const |
| Compute a value by applying a function to a row. More...
|
|