Multi Type Matrix

API Reference

template<typename Traits>
class multi_type_matrix

Matrix that can store numeric, integer, boolean, empty and string types. The string and integer types can be specified in the matrix trait template parameter. To use std::string as the string type and int as the integer type, use mdds::mtm::std_string_traits.

Internally it uses mdds::multi_type_vector as its value store. The element values are linearly stored in column-major order.

Public Types

typedef traits_type::string_element_block string_block_type
typedef traits_type::integer_element_block integer_block_type
typedef string_block_type::value_type string_type
typedef integer_block_type::value_type integer_type
typedef size_t size_type
typedef store_type::position_type position_type
typedef store_type::const_position_type const_position_type
typedef store_type::element_block_type element_block_type
typedef mtv::boolean_element_block boolean_block_type
typedef mtv::double_element_block numeric_block_type

Public Functions

multi_type_matrix()

Default constructor.

multi_type_matrix(size_type rows, size_type cols)

Construct a matrix of specified size.

Parameters:
  • rows – size of rows.

  • cols – size of columns.

template<typename _T>
multi_type_matrix(size_type rows, size_type cols, const _T &value)

Construct a matrix of specified size and initialize all its elements with specified value.

Parameters:
  • rows – size of rows.

  • cols – size of columns.

  • value – value to initialize all its elements with.

template<typename _T>
multi_type_matrix(size_type rows, size_type cols, const _T &it_begin, const _T &it_end)

Construct a matrix of specified size and initialize its elements with specified values. The values are assigned to 2-dimensional matrix layout in column-major order. The size of the value array must equal rows x cols.

Parameters:
  • rows – size of rows.

  • cols – size of columns.

  • it_begin – iterator that points to the value of the first element.

  • it_end – iterator that points to the position after the last element value.

multi_type_matrix(const multi_type_matrix &r)

Copy constructor.

~multi_type_matrix()

Destructor.

bool operator==(const multi_type_matrix &other) const
bool operator!=(const multi_type_matrix &other) const
multi_type_matrix &operator=(const multi_type_matrix &r)
position_type position(size_type row, size_type col)

Get a mutable reference of an element (position object) at specified position. The position object can then be passed to an additional method to get the type or value of the element it references, or set a new value to it.

Parameters:
  • row – row position of the referenced element.

  • col – column position of the referenced element.

Returns:

reference object of element at specified position.

position_type position(const position_type &pos_hint, size_type row, size_type col)

Get a mutable reference of an element (position object) at specified position. The position object can then be passed to an additional method to get the type or value of the element it references, or set a new value to it.

Parameters:
  • pos_hint – position object to be used as a position hint for faster lookup.

  • row – row position of the referenced element.

  • col – column position of the referenced element.

Returns:

reference object of element at specified position.

const_position_type position(size_type row, size_type col) const

Get an immutable reference of an element (position object) at specified position. The position object can then be passed to an additional method to get the type or value of the element it references.

Parameters:
  • row – row position of the referenced element.

  • col – column position of the referenced element.

Returns:

reference object of element at specified position.

const_position_type position(const const_position_type &pos_hint, size_type row, size_type col) const

Get an immutable reference of an element (position object) at specified position. The position object can then be passed to an additional method to get the type or value of the element it references.

Parameters:
  • pos_hint – position object to be used as a position hint for faster lookup.

  • row – row position of the referenced element.

  • col – column position of the referenced element.

Returns:

reference object of element at specified position.

size_pair_type matrix_position(const const_position_type &pos) const

Get the row and column positions of the current element from a position object.

Parameters:

pos – position object.

Returns:

0-based row and column positions.

position_type end_position()

Return a position type that represents an end position. This can be used to compare with another position object to see if it is past the last element position.

Returns:

end position object.

const_position_type end_position() const

Return a position type that represents an end position. This can be used to compare with another position object to see if it is past the last element position.

Returns:

end position object.

mtm::element_t get_type(const const_position_type &pos) const

Get the type of element from a position object. The type can be one of empty, string, numeric, or boolean.

Parameters:

pos – position object of an element

Returns:

element type.

mtm::element_t get_type(size_type row, size_type col) const

Get the type of element specified by its position. The type can be one of empty, string, numeric, or boolean.

Returns:

element type.

double get_numeric(size_type row, size_type col) const

Get a numeric representation of the element. If the element is of numeric type, its value is returned. If it’s of boolean type, either 1 or 0 is returned depending on whether it’s true or false. If it’s of empty or string type, 0 is returned.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

Returns:

numeric representation of the element.

double get_numeric(const const_position_type &pos) const

Get a numeric representation of the element from a position object. If the element is of numeric type, its value is returned. If it’s of boolean type, either 1 or 0 is returned depending on whether it’s true or false. If it’s of empty or string type, 0 is returned.

Parameters:

pos – position object of an element

Returns:

numeric representation of the element.

integer_type get_integer(size_type row, size_type col) const

Get an integer representation of the element. If the element is of integer type, its value is returned. If it’s of boolean type, either 1 or 0 is returned depending on whether it’s true or false. If it’s of empty or string type, 0 is returned.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

Returns:

integer representation of the element.

integer_type get_integer(const const_position_type &pos) const

Get an integer representation of the element. If the element is of integer type, its value is returned. If it’s of boolean type, either 1 or 0 is returned depending on whether it’s true or false. If it’s of empty or string type, 0 is returned.

Parameters:

pos – position object of an element

Returns:

integer representation of the element.

bool get_boolean(size_type row, size_type col) const

Get a boolean representation of the element. If the element is of numeric type, true is returned if it’s non-zero, otherwise false is returned. If it’s of boolean type, its value is returned. If it’s of empty or string type, false is returned.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

Returns:

boolean representation of the element.

bool get_boolean(const const_position_type &pos) const

Get a boolean representation of the element from a position object. If the element is of numeric type, true is returned if it’s non-zero, otherwise false is returned. If it’s of boolean type, its value is returned. If it’s of empty or string type, false is returned.

Parameters:

pos – position object of an element

Returns:

boolean representation of the element.

const string_type &get_string(size_type row, size_type col) const

Get the value of a string element. If the element is not of string type, it throws an exception.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

Returns:

value of the element.

const string_type &get_string(const const_position_type &pos) const

Get the value of a string element from a position object. If the element is not of string type, it throws an exception.

Parameters:

pos – position object of an element

Returns:

value of the element.

template<typename _T>
_T get(size_type row, size_type col) const

Get the value of element at specified position. The caller must explicitly specify the return type. If the element is not of the specified type, it throws an exception.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

Returns:

value of the element.

void set_empty(size_type row, size_type col)

Set specified element position empty.

Parameters:
  • row – row position of the element.

  • col – column position of the element.

void set_empty(size_type row, size_type col, size_type length)

Set a range of elements empty. The range starts from the position specified by the row and col, and extends downward first then to the right.

Parameters:
  • row – row position of the first element.

  • col – column position of the first element.

  • length – length of the range to set empty. When the length is greater than 1, the range extends downward first then to the right.

position_type set_empty(const position_type &pos)

Set element referenced by the position object empty.

Parameters:

pos – position object that references element.

Returns:

position of the element that has just been made empty.

void set_column_empty(size_type col)

Set the entire column empty.

Parameters:

col – index of the column to empty.

void set_row_empty(size_type row)

Set the entire row empty.

Parameters:

row – index of the row to empty.

void set(size_type row, size_type col, double val)

Set a numeric value to an element at specified position.

Parameters:
  • row – row index of the element.

  • col – column index of the element.

  • val – new value to set.

position_type set(const position_type &pos, double val)

Set a numeric value to an element at specified position.

Parameters:
  • pos – position of the element to update.

  • val – new value to set.

Returns:

position of the element block where the new value has been set.

void set(size_type row, size_type col, bool val)

Set a boolean value to an element at specified position.

Parameters:
  • row – row index of the element.

  • col – column index of the element.

  • val – new value to set.

position_type set(const position_type &pos, bool val)

Set a boolean value to an element at specified position.

Parameters:
  • pos – position of the element to update.

  • val – new value to set.

Returns:

position of the element where the new value has been set.

void set(size_type row, size_type col, const string_type &str)

Set a string value to an element at specified position.

Parameters:
  • row – row index of the element.

  • col – column index of the element.

  • str – new value to set.

position_type set(const position_type &pos, const string_type &str)

Set a string value to an element at specified position.

Parameters:
  • pos – position of the element to update.

  • str – new value to set.

Returns:

position of the element block where the new value has been set.

void set(size_type row, size_type col, integer_type val)

Set an integer value to an element at specified position.

Parameters:
  • row – row index of the element.

  • col – column index of the element.

  • val – new value to set.

position_type set(const position_type &pos, integer_type val)

Set an integer value to an element at specified position.

Parameters:
  • pos – position of the element to update.

  • val – new value to set.

Returns:

position of the element block where the new value has been set.

template<typename _T>
void set(size_type row, size_type col, const _T &it_begin, const _T &it_end)

Set values of multiple elements at once, starting at specified element position following the direction of columns. When the new value series does not fit in the first column, it gets wrapped into the next column(s).

The method will throw an std::out_of_range exception if the specified position is outside the current container range.

Parameters:
  • row – row position of the start element.

  • col – column position of the start element.

  • it_begin – iterator that points to the begin position of the values being set.

  • it_end – iterator that points to the end position of the values being set.

template<typename _T>
position_type set(const position_type &pos, const _T &it_begin, const _T &it_end)

Set values of multiple elements at once, starting at specified element position following the direction of columns. When the new value series does not fit in the first column, it gets wrapped into the next column(s).

Parameters:
  • pos – position of the first element.

  • it_begin – iterator that points to the begin position of the values being set.

  • it_end – iterator that points to the end position of the values being set.

Returns:

position of the first element that has been modified.

template<typename _T>
void set_column(size_type col, const _T &it_begin, const _T &it_end)

Set values of multiple elements at once in a single column. When the length of passed elements exceeds that of the column, it gets truncated to the column size.

Parameters:
  • col – column position

  • it_begin – iterator that points to the begin position of the values being set.

  • it_end – iterator that points to the end position of the values being set.

size_pair_type size() const

Return the size of matrix as a pair. The first value is the row size, while the second value is the column size.

Returns:

matrix size as a value pair.

multi_type_matrix &transpose()

Transpose the stored matrix data.

Returns:

reference to this matrix instance.

void copy(const multi_type_matrix &src)

Copy values from the passed matrix instance. If the size of the passed matrix is smaller, then the element values are copied by their positions, while the rest of the elements that fall outside the size of the passed matrix instance will remain unmodified. If the size of the passed matrix instance is larger, then only the elements within the size of this matrix instance will get copied.

Parameters:

src – passed matrix object to copy element values from.

template<typename _T>
void copy(size_type rows, size_type cols, const _T &it_begin, const _T &it_end)

Copy values from an array or array-like container, to a specified sub-matrix range. The length of the array must match the number of elements in the destination range, else it will throw a mdds::size_error.

Parameters:
  • rows – row size of the destination range.

  • cols – column size of the destination range.

  • it_begin – iterator pointing to the beginning of the input array.

  • it_end – iterator pointing to the end position of the input array.

void resize(size_type rows, size_type cols)

Resize the matrix to specified size. This method supports resizing to zero-sized matrix; however, either specifying the row or column size to zero will resize the matrix to 0 x 0. When resizing the matrix larger, empty elements will be inserted in the region where no elements existed prior to the call.

Parameters:
  • rows – new row size

  • cols – new column size

template<typename _T>
void resize(size_type rows, size_type cols, const _T &value)

Resize the matrix to specified size and initial value. The initial value will be applied to new elements created when the specified size is larger than the current one.

Parameters:
  • rows – new row size

  • cols – new column size

  • value – initial value for new elements

void clear()

Empty the matrix.

bool numeric() const

Check whether or not this matrix is numeric. A numeric matrix contains only numeric or boolean elements. An empty matrix is not numeric.

Returns:

true if the matrix contains only numeric or boolean elements, or false otherwise.

bool empty() const

Check whether or not this matrix is empty.

Returns:

true if this matrix is empty, or false otherwise.

void swap(multi_type_matrix &r)

Swap the content of the matrix with another instance.

template<typename FuncT>
FuncT walk(FuncT func) const

Walk all element blocks that consist of the matrix.

Parameters:

func – function object whose operator() gets called on each element block.

Returns:

function object passed to this method.

template<typename FuncT>
FuncT walk(FuncT func, const size_pair_type &start, const size_pair_type &end) const

Walk through the element blocks in a sub-matrix range defined by start and end positions passed to this method.

Parameters:
  • func – function object whose operator() gets called on the element block.

  • start – the column/row position of the upper-left corner of the sub-matrix.

  • end – the column/row position of the lower-right corner of the sub-matrix. Both column and row must be greater or equal to those of the start position.

Returns:

function object passed to this method.

template<typename FuncT>
FuncT walk(FuncT func, const multi_type_matrix &right) const

Walk through all element blocks in parallel with another matrix instance. It stops at the block boundaries of both matrix instances during the walk.

Parameters:
  • func – function object whose operator() gets called on each element block.

  • right – another matrix instance to parallel-walk with.

template<typename FuncT>
FuncT walk(FuncT func, const multi_type_matrix &right, const size_pair_type &start, const size_pair_type &end) const

Walk through the element blocks in a sub-matrix range in parallel with another matrix instance. It stops at the block boundaries of both matrix instances during the walk. The sub-matrix range is defined by start and end positions passed to this method.

Parameters:
  • func – function object whose operator() gets called on each element block.

  • right – another matrix instance to parallel-walk with.

  • start – the column/row position of the upper-left corner of the sub-matrix.

  • end – the column/row position of the lower-right corner of the sub-matrix. Both column and row must be greater or equal to those of the start position.

Public Static Functions

static inline mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
static position_type next_position(const position_type &pos)

Move to the next logical position. The movement is in the top-to-bottom then left-to-right direction.

Parameters:

pos – position object.

Returns:

position object that references the element at the next logical position.

static const_position_type next_position(const const_position_type &pos)

Move to the next logical position. The movement is in the top-to-bottom then left-to-right direction.

Parameters:

pos – position object.

Returns:

non-mutable position object that references the element at the next logical position.

struct element_block_node_type

Public Functions

element_block_node_type()
element_block_node_type(const element_block_node_type &other)
template<typename _Blk>
_Blk::const_iterator begin() const
template<typename _Blk>
_Blk::const_iterator end() const

Public Members

mtm::element_t type
size_type offset
size_type size
const element_block_type *data

Friends

friend class multi_type_matrix
struct size_pair_type

Public Functions

inline size_pair_type()
inline size_pair_type(size_type _row, size_type _column)
inline size_pair_type(std::initializer_list<size_type> vs)
inline bool operator==(const size_pair_type &r) const
inline bool operator!=(const size_pair_type &r) const

Public Members

size_type row
size_type column
struct std_string_traits

Default matrix trait that uses std::string as its string type.

Public Types

typedef mdds::mtv::int32_element_block integer_element_block
typedef mdds::mtv::string_element_block string_element_block
enum mdds::mtm::element_t

Element type for multi_type_matrix.

Values:

enumerator element_empty
enumerator element_boolean
enumerator element_string
enumerator element_numeric
enumerator element_integer