Skip to content

var::table

2D array class More...

#include "table.hpp"

Public Functions

Name
table()
Construct a new table object.
table(int r, int c)
Construct a new table object.
table(std::initializer_list< std::initializer_list< D >> a)
Construct a new table object.
int row()
Get the row size.
int col()
Get the col size.
int size()
D & at(int i, int j)
Returns reference to element (i, j)
std::vector< std::string > get_col_names()
Get the col names.
std::vector< D > get_col(int j)
Get the colum based on index.
std::vector< D > get_col(std::string name)
Get the column vector based on header name.
table get_col_table(int j)
Get the col as table
table get_col_table(std::string name)
Get the col as table
std::vector< std::string > get_row_names()
Get the row names.
std::vector< D > get_row(int i)
Get the row based on index.
std::vector< D > get_row(std::string name)
Get the row vector based on header name.
table get_row_table(int i)
Get the row as table.
table get_row_table(std::string name)
Get the row as table.
void set_col_names(std::vector< std::string > names)
Set the col names.
void set_col_name(int j, std::string name)
Set the col name.
void insert_col(std::vector< D > a, int j, std::string name =std::string())
Inserts new column into given index.
void push_col(std::vector< D > a, std::string name =std::string())
Pushes column at the end of the table.
void swap_col(int j1, int j2)
Swaps two column.
void replace_col(int j, std::vector< D > a, std::string name =std::string())
Replaces given column index.
void join_col(table other)
Combines the columns of another table.
void erase_col(int j)
Erases given index column.
void pop_col()
Removes last column.
void set_row_names(std::vector< std::string > names)
Set the row names.
void set_row_name(int i, std::string names)
Set the row name.
void insert_row(std::vector< D > a, int i, std::string name =std::string())
Inserts new row into given index.
void push_row(std::vector< D > a, std::string name =std::string())
Pushes row at the end of the table.
void swap_row(int i1, int i2)
Swaps two rows.
void replace_row(int i, std::vector< D > a, std::string name =std::string())
Replaces given rowumn index.
void join_row(table other)
Combines the rows of another table.
void erase_row(int i)
Erases given index rowumn.
void pop_row()
Removes last rowumn.
void resize(int r, int c)
Resizes the table.
void turn_to(D n)
Converts all elements to n.
D sum()
Sum of all elements.
bool read_csv(std::string filename)
Reads from csv file.
bool save_csv(std::string filename)
Reads from csv file.
void show(int r)
Shows a certain number of rows in table.
void show()
shows all of the table
void show_header()
Shows col_names only.
std::vector< D > get_avgs()
Extract average of each column into a vector.
std::vector< D > get_stds()
Extract STD of each column into a vector.
std::vector< D > get_vars()
Extract variance of each column into a vector.
std::vector< QR< D > > get_qrs()
Extract QR of each column into a vector.
std::vector< D > get_sums()
Extract sum of each column into a vector.
table describe_all()
Statistical summary of all colums in table.

Protected Functions

Name
int get_index(int i, int j)
Get the index (maps 1D vector to 2D vector)
int check_col_name(std::string name)
Returns column index of given column name.
int check_row_name(std::string name)
Returns row index of given row name.
std::string generate_line(int l)
Generates line to seperate rows.
void generate_rows()
Clears current row names and generates new ones.
void generate_cols()
Clears current column names and generates new ones.
std::string center(const std::string s, const int w)
Re-centers a string based on given width.
std::string prd(D x, int width)
Fills the string with given width with empty space.
std::string prd(std::string x, int width)
Gives spacing to a string.

Protected Attributes

Name
std::vector< D > data
the data container
std::vector< std::string > col_names
Stores the column names.
std::vector< std::string > row_names
Stores the row names.
int _row
Row size.
int _col
Colum size.
int sz
Spacing size for print.

Detailed Description

template <typename D >
class var::table;

2D array class

Public Functions Documentation

function table

inline table()

Construct a new table object.

  • Usage:
var::table<int> t;

function table

inline table(
    int r,
    int c
)

Construct a new table object.

Parameters:

  • r number of rows
  • c number of colums

Usage:

var::table<int> t(2, 3); // 2 rows, 3 colums of all 0

function table

inline table(
    std::initializer_list< std::initializer_list< D >> a
)

Construct a new table object.

Usage:

var::table<int> m = {
   {1, 2, 3},
   {4, 5, 6},
   {7, 8, 9}
};

General getters

ainitializer list

function row

inline int row()

Get the row size.

Return: int

function col

inline int col()

Get the col size.

Return: int

function size

inline int size()

function at

inline D & at(
    int i,
    int j
)

Returns reference to element (i, j)

Column getters

irow index

jcolumn index

D& element std::invalid_argumentif invalid indexes

function get_col_names

inline std::vector< std::string > get_col_names()

Get the col names.

Return: std::vector<std::string>

function get_col

inline std::vector< D > get_col(
    int j
)

Get the colum based on index.

Parameters:

  • j index of that colum

Exceptions:

  • std::invalid_argument in case index is invalid

Return: std::vector<D>

function get_col

inline std::vector< D > get_col(
    std::string name
)

Get the column vector based on header name.

Row getters

namename of the column

std::vector<D>

std::invalid_argumentin case name is invalid

function get_col_table

inline table get_col_table(
    int j
)

Get the col as table

Parameters:

  • j index ofo that column

Exceptions:

  • std::invalid_argument in case index is invalid

Return: table

function get_col_table

inline table get_col_table(
    std::string name
)

Get the col as table

Parameters:

  • name column name

Exceptions:

  • std::invalid_argument in case name is invalid

Return: table

function get_row_names

inline std::vector< std::string > get_row_names()

Get the row names.

Return: std::vector<std::string>

function get_row

inline std::vector< D > get_row(
    int i
)

Get the row based on index.

Parameters:

  • i index of that row

Exceptions:

  • std::invalid_argument in case index is invalid

Return: std::vector<&D>

function get_row

inline std::vector< D > get_row(
    std::string name
)

Get the row vector based on header name.

Parameters:

  • name name of the row

Exceptions:

  • std::invalid_argument in case name is invalid

Return: std::vector<D>: Empty if nothing exits

function get_row_table

inline table get_row_table(
    int i
)

Get the row as table.

Parameters:

  • i

Exceptions:

  • std::invalid_argument in case index is invalid

Return: table

function get_row_table

inline table get_row_table(
    std::string name
)

Get the row as table.

Column setters

name

table

std::invalid_argumentin case name is invalid

function set_col_names

inline void set_col_names(
    std::vector< std::string > names
)

Set the col names.

Parameters:

  • names vector of column names

Exceptions:

  • std::invalid_argument size of vector names doesnt match column size

function set_col_name

inline void set_col_name(
    int j,
    std::string name
)

Set the col name.

Parameters:

  • j index of column
  • name name of column

Exceptions:

  • std::invalid_argument invalid index j
  • std::runtime_error column names are not set properly

function insert_col

inline void insert_col(
    std::vector< D > a,
    int j,
    std::string name =std::string()
)

Inserts new column into given index.

Parameters:

  • a column to be inserted
  • j column index to insert at
  • name name of the column

Exceptions:

  • std::invalid_argument invalid column index
  • std::invalid_argument a.size() \(\neq\) row size

function push_col

inline void push_col(
    std::vector< D > a,
    std::string name =std::string()
)

Pushes column at the end of the table.

Parameters:

  • a column to be inserted
  • name name of the column

Exceptions:

  • std::invalid_argument a.size() \(\neq\) row size

function swap_col

inline void swap_col(
    int j1,
    int j2
)

Swaps two column.

Parameters:

  • j1 index 1
  • j2 index 2

Exceptions:

  • std::invalid_argument invalid index

function replace_col

inline void replace_col(
    int j,
    std::vector< D > a,
    std::string name =std::string()
)

Replaces given column index.

Parameters:

  • j column index
  • a new column

Exceptions:

  • std::invalid_argument invalid j index
  • std::invalid_argument a.size() \(\neq\) row size

function join_col

inline void join_col(
    table other
)

Combines the columns of another table.

Parameters:

  • other table

Exceptions:

  • std::invalid_argument other doesn't have same number of rows

function erase_col

inline void erase_col(
    int j
)

Erases given index column.

Parameters:

  • j column index

Exceptions:

  • std::invalid_argument invalid index
  • std::runtime_error table is empty

function pop_col

inline void pop_col()

Removes last column.

Row setters

std::runtime_errortable is empty

function set_row_names

inline void set_row_names(
    std::vector< std::string > names
)

Set the row names.

Parameters:

  • names vector of row names

Exceptions:

  • std::invalid_argument if names.size() doesnt match row size

function set_row_name

inline void set_row_name(
    int i,
    std::string names
)

Set the row name.

Parameters:

  • i index of row
  • name name of row

Exceptions:

  • std::invalid_argument invalid index i
  • std::runtime_errorrow names are not set properly

function insert_row

inline void insert_row(
    std::vector< D > a,
    int i,
    std::string name =std::string()
)

Inserts new row into given index.

Parameters:

  • a row to be inserted
  • i row index to insert at
  • name name of the row

Exceptions:

  • std::invalid_argument invalid row index
  • std::invalid_argument a.size() \(\neq\) column size

function push_row

inline void push_row(
    std::vector< D > a,
    std::string name =std::string()
)

Pushes row at the end of the table.

Parameters:

  • a row to be inserted
  • name name of the row

Exceptions:

  • std::invalid_argument a.size() \(\neq\) column size

function swap_row

inline void swap_row(
    int i1,
    int i2
)

Swaps two rows.

Parameters:

  • i1 index 1
  • i2 index 2

Exceptions:

  • std::invalid_argument invalid index

function replace_row

inline void replace_row(
    int i,
    std::vector< D > a,
    std::string name =std::string()
)

Replaces given rowumn index.

Parameters:

  • j rowumn index
  • a new rowumn

Exceptions:

  • std::invalid_argument invalid j index
  • std::invalid_argument a.size() \(\neq\) row size

function join_row

inline void join_row(
    table other
)

Combines the rows of another table.

Parameters:

  • other table

Exceptions:

  • std::invalid_argument Size mismatch

function erase_row

inline void erase_row(
    int i
)

Erases given index rowumn.

Parameters:

  • i rowumn index

function pop_row

inline void pop_row()

Removes last rowumn.

Data manipualtors

function resize

inline void resize(
    int r,
    int c
)

Resizes the table.

Parameters:

  • r number of rows
  • c number of colums

Warning

This can impact performance as it calls std::vector::resize

function turn_to

inline void turn_to(
    D n
)

Converts all elements to n.

Parameters:

  • n the specifeid variable

Usage:

t.turn_to(1); // turns all elements to 1

function sum

inline D sum()

Sum of all elements.

Return: D

Usage:

auto sum = m.sum();

function read_csv

inline bool read_csv(
    std::string filename
)

Reads from csv file.

Parameters:

  • file file name

Return:

  • true : if read is success
  • false: if read did not complete

function save_csv

inline bool save_csv(
    std::string filename
)

Reads from csv file.

Parameters:

  • file

Return:

  • true: if read is success
  • false: if read did not complete

function show

inline void show(
    int r
)

Shows a certain number of rows in table.

Parameters:

  • r

function show

inline void show()

shows all of the table

function show_header

inline void show_header()

Shows col_names only.

function get_avgs

inline std::vector< D > get_avgs()

Extract average of each column into a vector.

Return: std::vector<D>: same size as number of columns

Used in describe_all()

function get_stds

inline std::vector< D > get_stds()

Extract STD of each column into a vector.

Return: std::vector<D> same size as number of columns

Used in describe_all()

function get_vars

inline std::vector< D > get_vars()

Extract variance of each column into a vector.

Return: std::vector<D>: same size as numebr of columns

Used in describe_all()

function get_qrs

inline std::vector< QR< D > > get_qrs()

Extract QR of each column into a vector.

Return: std::vector<QR>: same size as number of colums

Used in describe_all()

function get_sums

inline std::vector< D > get_sums()

Extract sum of each column into a vector.

Return: std::vector<D> size is same size as number of colums

Used in describe_all()

function describe_all

inline table describe_all()

Statistical summary of all colums in table.

Return: table<D>

  • Example:
Avg STD VAR Min Q1 Q2 Q3 Max IQR Sum
col-0 .. .. .. .. .. .. .. .. .. ..
col-1 .. .. .. .. .. .. .. .. .. ..

Protected Functions Documentation

function get_index

inline int get_index(
    int i,
    int j
)

Get the index (maps 1D vector to 2D vector)

Parameters:

  • i row index
  • j column index

Return: int used for data index

function check_col_name

inline int check_col_name(
    std::string name
)

Returns column index of given column name.

Parameters:

  • name string of column name

Return:

  • int \([-1]\): index not found
  • int \([\geq 0]\): index found

function check_row_name

inline int check_row_name(
    std::string name
)

Returns row index of given row name.

Parameters:

  • name string of row name

Return:

  • int \([-1]\): index not found
  • int \([\geq 0]\): index found

function generate_line

inline std::string generate_line(
    int l
)

Generates line to seperate rows.

Parameters:

  • l number of "―" in the generated string

Return: std::string

function generate_rows

inline void generate_rows()

Clears current row names and generates new ones.

function generate_cols

inline void generate_cols()

Clears current column names and generates new ones.

Constructors

function center

static inline std::string center(
    const std::string s,
    const int w
)

Re-centers a string based on given width.

Parameters:

  • s string given
  • w width to center the string to

Return: std::string

function prd

static inline std::string prd(
    D x,
    int width
)

Fills the string with given width with empty space.

Parameters:

  • x string given
  • width width to center the string to

Return: std::string

function prd

static inline std::string prd(
    std::string x,
    int width
)

Gives spacing to a string.

Parameters:

  • x string given
  • width width to center the string to

See: Similar to var::table::prd(D, int)

Return: std::string

Protected Attributes Documentation

variable data

std::vector< D > data;

the data container

variable col_names

std::vector< std::string > col_names;

Stores the column names.

variable row_names

std::vector< std::string > row_names;

Stores the row names.

variable _row

int _row;

Row size.

variable _col

int _col;

Colum size.

variable sz

int sz = 10;

Spacing size for print.


Updated on 4 September 2022 at 18:30:41 EEST