Eidolon
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Matrix< T > Class Template Reference

#include <RenderTypes.h>

Inheritance diagram for Matrix< T >:
Inheritance graph
Collaboration diagram for Matrix< T >:
Collaboration graph

Public Member Functions

 Matrix (const char *name, sval n, sval m=1, bool isShared=false) throw (MemException)
 Constructs a matrix named `name' of `n' rows and `m' columns, local if `isShared' is false and shared otherwise. More...
 
 Matrix (const char *name, const char *type, sval n, sval m=1, bool isShared=false) throw (MemException)
 Constructs a matrix named `name' with type `type' of `n' rows and `m' columns, local if `isShared' is false and shared otherwise. More...
 
 Matrix (const char *name, const char *type, const char *sharedname, const char *serialmeta, sval n, sval m) throw (MemException)
 Constructor for unpickling only, do not use. More...
 
 Matrix (const char *name, const char *type, const T *array, sval n, sval m, bool isShared=false) throw (MemException)
 Constructor for converting a memory pointer into a Matrix, this will copy n*m values from `array'. More...
 
virtual ~Matrix ()
 
T * dataPtr () const
 
Matrix< T > * clone (const char *newname=NULL, bool isShared=false) const
 Copy the contents of this matrix into a newly allocated one (which can be shared if isShared is true), using `newname' if not NULL and getName() otherwise. More...
 
const char * getName () const
 
const char * getSharedName () const
 
const char * getType () const
 
void setName (const char *name)
 
void setType (const char *type)
 
bool isShared () const
 Returns true if the matrix is allocated in shared memory. More...
 
void setShared (bool val) throw (MemException)
 
void clear () throw (MemException)
 
sval n () const
 Get the number of rows. More...
 
sval m () const
 Get the number of columns. More...
 
sval memSize () const
 Get the total memory usage in bytes. More...
 
void fill (const T &t)
 Set every cell of the matrix to the given value. More...
 
template<typename R >
void copyFrom (const Matrix< R > *r)
 Copy the data bitwise from `r', the number of bytes copied is the minimum or either matrices' size. More...
 
Matrix< T > * subMatrix (const char *name, sval n, sval m=1, sval noff=0, sval moff=0, bool isShared=false) const throw (MemException)
 Create a submatrix from this one of dimensions (n,m), starting at row `noff' and column `moff'. More...
 
Matrix< T > * reshape (const char *name, sval n, sval m, bool isShared=false) const throw (MemException)
 
template<typename Ctx >
void applyFunc (T(*op)(Ctx, const T &, sval, sval), Ctx ctx, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Apply the function `op' to each cell from (minrow,mincol) to (maxrow-1,maxcol-1), passing in `ctx' as the first argument for each call. More...
 
template<typename R , typename OpType >
void scalarop (const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix with `r' as the second operand. More...
 
template<typename R , typename OpType >
void matop (const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix with the equivalent cell in `mat' as the second operand. More...
 
template<typename R >
void add (const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Add `r' to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix. More...
 
template<typename R >
void sub (const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Subtract `r' from every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix. More...
 
template<typename R >
void mul (const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Multiple every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r'. More...
 
template<typename R >
void div (const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r'. More...
 
template<typename R >
void addm (const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Add every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat' to the same cell in the matrix. More...
 
template<typename R >
void subm (const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Subtract every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat' from the same cell in the matrix. More...
 
template<typename R >
void mulm (const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Multiply every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat'. More...
 
template<typename R >
void divm (const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
 Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat'. More...
 
void reorderColumns (const sval *orderinds) throw (IndexException)
 
void swapEndian ()
 
T & at (sval n, sval m=0) const
 Same as getAt except returning a reference and no bounds check. More...
 
const T & atc (sval n, sval m=0) const
 
void ats (sval n, sval m, const T &t)
 
T & operator() (sval n, sval m=0) const
 Same as getAt except returning a reference and no bounds check. More...
 
T & operator[] (sval n) const
 
getAt (sval n, sval m=0) const throw (IndexException)
 Get the element at (n,m) in the matrix, throws exception if `n' or `m' out of range. More...
 
void setAt (const T &t, sval n, sval m=0) throw (IndexException)
 Set the value at (n,m) to t. More...
 
void setN (sval _newn) throw (MemException)
 Resize the matrix to have _newn rows, throws exception if shared. More...
 
void setM (sval _newm) throw (MemException)
 Reshape to fit this many columns, does not allocate new columns but re-arranges existing ones and truncates the last row if necessary. More...
 
void addRows (sval num) throw (MemException)
 Add `num' rows, throws exception if shared. More...
 
void reserveRows (sval num) throw (MemException)
 Ensure that at least `num' rows are reserved in memory, throws exception if shared. More...
 
void append (const Matrix< T > &t) throw (MemException)
 Append `t' to the bottom of `this', throws exception if shared or if columns of `t' and `this' don't match. More...
 
void append (const T &t, sval m=0) throw (MemException)
 Append `t' to a new row, placing it in column `m'. More...
 
void removeRow (sval n) throw (MemException,IndexException)
 
void readBinaryFile (const char *filename, size_t offset) throw (MemException)
 Read a binary file of data into this matrix starting from byte `offset'. More...
 
void readTextFile (const char *filename, sval numHeaders) throw (MemException)
 Read a text file of data into this matrix which has with `numHeaders' values in the header line. More...
 
void storeBinaryFile (const char *filename, int *header, sval headerlen) throw (MemException)
 Store the header values `header' and then this matrix's contents to the file. More...
 
indexpair indexOf (const T &t, sval aftern=0, sval afterm=0) const
 Find the row-column pair in the matrix where `t' is found, or indexpair(n(),0) if not found (None in Python) More...
 
- Public Member Functions inherited from MetaType
bool hasMetaKey (const char *key) const
 Returns true if a key-value pair with the given key is present. More...
 
std::vector< std::string > getMetaKeys () const
 
std::string meta () const
 
const char * meta (const char *key) const
 Returns the value associated with the given key or the empty string if none is stored. More...
 
void meta (const char *key, const char *val)
 Add the given key-value pair to the metadata, or overwrite an existing if present. Does nothing if either argument is NULL. More...
 
void meta (const MetaType *m)
 Copy all the metadata from `m' to `this'. More...
 
std::string serializeMeta () const
 Turn all metadata name-value pairs into one string suitable for pickling. More...
 
void deserializeMeta (const std::string &s)
 Break a string generated by the above back into name-value pairs and store in `this'. More...
 

Protected Member Functions

sval getIndex (sval n, sval m) const throw (IndexException)
 Get the index corresponding to cell (n,m) More...
 
void checkIndex (const char *name, sval val, sval maxval) const throw (IndexException)
 
void checkNotShared () const throw (MemException)
 
void checkDimension (const char *name, sval dim) const throw (MemException)
 
void resize (sval reserveNum=0)
 
void chooseSharedName (int counter=0)
 Determine a shared name based on the name stored in this matrix and the counter value, used to determine a system-wide unique name. More...
 
T * createShared () throw (MemException)
 Create a shared segment and return a mapped pointer to it. More...
 
void closeShared (T *ptr) throw (MemException)
 Unmap the given shared segment, and delete it if this object created it. More...
 

Protected Attributes

std::string _name
 
std::string _type
 
std::string _sharedname
 
T * data
 
sval _n_actual
 
sval _n
 
sval _m
 
bool _isShared
 
- Protected Attributes inherited from MetaType
metamap _meta
 

Additional Inherited Members

- Protected Types inherited from MetaType
typedef std::map< std::string, std::string > metamap
 
typedef metamap::iterator iter
 
typedef metamap::const_iterator citer
 

Detailed Description

template<typename T>
class RenderTypes::Matrix< T >

This represents a 2-dimensional array of data elements of type T. There are four typedefs given below for T being vec3, color, indexval, and real. A number of methods are provided for doing arithmetic with all the elements of a matrix and with whole matrices. A facility is provided for defining matrices as shared memory segments suitable for communication between processes.

Matrix types are indexed in (row,column) or (Y,X) order since they are used to represent lists or entries, are thus expandable by adding rows but not columns, and are stored in row major order.

Constructor & Destructor Documentation

◆ Matrix() [1/4]

Matrix ( const char *  name,
sval  n,
sval  m = 1,
bool  isShared = false 
)
throw (MemException
)
inline

Constructs a matrix named `name' of `n' rows and `m' columns, local if `isShared' is false and shared otherwise.

◆ Matrix() [2/4]

Matrix ( const char *  name,
const char *  type,
sval  n,
sval  m = 1,
bool  isShared = false 
)
throw (MemException
)
inline

Constructs a matrix named `name' with type `type' of `n' rows and `m' columns, local if `isShared' is false and shared otherwise.

◆ Matrix() [3/4]

Matrix ( const char *  name,
const char *  type,
const char *  sharedname,
const char *  serialmeta,
sval  n,
sval  m 
)
throw (MemException
)
inline

Constructor for unpickling only, do not use.

◆ Matrix() [4/4]

Matrix ( const char *  name,
const char *  type,
const T *  array,
sval  n,
sval  m,
bool  isShared = false 
)
throw (MemException
)
inline

Constructor for converting a memory pointer into a Matrix, this will copy n*m values from `array'.

◆ ~Matrix()

virtual ~Matrix ( )
inlinevirtual

Member Function Documentation

◆ add()

void add ( const R &  r,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Add `r' to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix.

◆ addm()

void addm ( const Matrix< R > &  mat,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Add every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat' to the same cell in the matrix.

◆ addRows()

void addRows ( sval  num)
throw (MemException
)
inline

Add `num' rows, throws exception if shared.

◆ append() [1/2]

void append ( const Matrix< T > &  t)
throw (MemException
)
inline

Append `t' to the bottom of `this', throws exception if shared or if columns of `t' and `this' don't match.

Here is the caller graph for this function:

◆ append() [2/2]

void append ( const T &  t,
sval  m = 0 
)
throw (MemException
)
inline

Append `t' to a new row, placing it in column `m'.

Here is the caller graph for this function:

◆ applyFunc()

void applyFunc ( T(*)(Ctx, const T &, sval, sval op,
Ctx  ctx,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Apply the function `op' to each cell from (minrow,mincol) to (maxrow-1,maxcol-1), passing in `ctx' as the first argument for each call.

◆ at()

T& at ( sval  n,
sval  m = 0 
) const
inline

Same as getAt except returning a reference and no bounds check.

Here is the caller graph for this function:

◆ atc()

const T& atc ( sval  n,
sval  m = 0 
) const
inline
Here is the caller graph for this function:

◆ ats()

void ats ( sval  n,
sval  m,
const T &  t 
)
inline

◆ checkDimension()

void checkDimension ( const char *  name,
sval  dim 
) const
throw (MemException
)
inlineprotected

◆ checkIndex()

void checkIndex ( const char *  name,
sval  val,
sval  maxval 
) const
throw (IndexException
)
inlineprotected

◆ checkNotShared()

void checkNotShared ( ) const
throw (MemException
)
inlineprotected

◆ chooseSharedName()

void chooseSharedName ( int  counter = 0)
inlineprotected

Determine a shared name based on the name stored in this matrix and the counter value, used to determine a system-wide unique name.

◆ clear()

void clear ( )
throw (MemException
)
inline
Here is the caller graph for this function:

◆ clone()

Matrix<T>* clone ( const char *  newname = NULL,
bool  isShared = false 
) const
inline

Copy the contents of this matrix into a newly allocated one (which can be shared if isShared is true), using `newname' if not NULL and getName() otherwise.

Here is the caller graph for this function:

◆ closeShared()

void closeShared ( T *  ptr)
throw (MemException
)
inlineprotected

Unmap the given shared segment, and delete it if this object created it.

◆ copyFrom()

void copyFrom ( const Matrix< R > *  r)
inline

Copy the data bitwise from `r', the number of bytes copied is the minimum or either matrices' size.

◆ createShared()

T* createShared ( )
throw (MemException
)
inlineprotected

Create a shared segment and return a mapped pointer to it.

◆ dataPtr()

T* dataPtr ( ) const
inline
Here is the caller graph for this function:

◆ div()

void div ( const R &  r,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r'.

◆ divm()

void divm ( const Matrix< R > &  mat,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat'.

◆ fill()

void fill ( const T &  t)
inline

Set every cell of the matrix to the given value.

Here is the caller graph for this function:

◆ getAt()

T getAt ( sval  n,
sval  m = 0 
) const
throw (IndexException
)
inline

Get the element at (n,m) in the matrix, throws exception if `n' or `m' out of range.

Here is the caller graph for this function:

◆ getIndex()

sval getIndex ( sval  n,
sval  m 
) const
throw (IndexException
)
inlineprotected

Get the index corresponding to cell (n,m)

◆ getName()

const char* getName ( ) const
inline
Here is the caller graph for this function:

◆ getSharedName()

const char* getSharedName ( ) const
inline

◆ getType()

const char* getType ( ) const
inline

◆ indexOf()

indexpair indexOf ( const T &  t,
sval  aftern = 0,
sval  afterm = 0 
) const
inline

Find the row-column pair in the matrix where `t' is found, or indexpair(n(),0) if not found (None in Python)

◆ isShared()

bool isShared ( ) const
inline

Returns true if the matrix is allocated in shared memory.

◆ m()

sval m ( ) const
inline

Get the number of columns.

Here is the caller graph for this function:

◆ matop()

void matop ( const Matrix< R > &  mat,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix with the equivalent cell in `mat' as the second operand.

◆ memSize()

sval memSize ( ) const
inline

Get the total memory usage in bytes.

Here is the caller graph for this function:

◆ mul()

void mul ( const R &  r,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Multiple every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r'.

◆ mulm()

void mulm ( const Matrix< R > &  mat,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Multiply every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat'.

◆ n()

sval n ( ) const
inline

Get the number of rows.

Here is the caller graph for this function:

◆ operator()()

T& operator() ( sval  n,
sval  m = 0 
) const
inline

Same as getAt except returning a reference and no bounds check.

◆ operator[]()

T& operator[] ( sval  n) const
inline

◆ readBinaryFile()

void readBinaryFile ( const char *  filename,
size_t  offset 
)
throw (MemException
)
inline

Read a binary file of data into this matrix starting from byte `offset'.

◆ readTextFile()

void readTextFile ( const char *  filename,
sval  numHeaders 
)
throw (MemException
)
inline

Read a text file of data into this matrix which has with `numHeaders' values in the header line.

◆ removeRow()

void removeRow ( sval  n)
throw (MemException,
IndexException
)
inline
Here is the caller graph for this function:

◆ reorderColumns()

void reorderColumns ( const sval orderinds)
throw (IndexException
)
inline

◆ reserveRows()

void reserveRows ( sval  num)
throw (MemException
)
inline

Ensure that at least `num' rows are reserved in memory, throws exception if shared.

◆ reshape()

Matrix<T>* reshape ( const char *  name,
sval  n,
sval  m,
bool  isShared = false 
) const
throw (MemException
)
inline

Create a copy of this matrix with the given dimensions. If the new matrix is smaller, the data copied will be truncated, if larger then only as much data as available will be copied and the rest of the new matrix's memory will be left uninitialized.

◆ resize()

void resize ( sval  reserveNum = 0)
inlineprotected

Resize the allocated segment, or do nothing if shared or if _n+reservedNum is less than what is already allocated. To allocate a bigger segment, set _n to be what one wants, then call this method which allocates max(1000,(_n*1.5)+reserveNum) rows, copying over existing data as needed. When a matrix is first instantiated its buffer is exactly the size it needs to be, but whenever new rows are added with setN(), reserveRows(), or append(), the _n member is set to the needed size and this method is called.

Here is the caller graph for this function:

◆ scalarop()

void scalarop ( const R &  r,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix with `r' as the second operand.

◆ setAt()

void setAt ( const T &  t,
sval  n,
sval  m = 0 
)
throw (IndexException
)
inline

Set the value at (n,m) to t.

Here is the caller graph for this function:

◆ setM()

void setM ( sval  _newm)
throw (MemException
)
inline

Reshape to fit this many columns, does not allocate new columns but re-arranges existing ones and truncates the last row if necessary.

◆ setN()

void setN ( sval  _newn)
throw (MemException
)
inline

Resize the matrix to have _newn rows, throws exception if shared.

Here is the caller graph for this function:

◆ setName()

void setName ( const char *  name)
inline

◆ setShared()

void setShared ( bool  val)
throw (MemException
)
inline

Toggles whether this matrix is in local memory or shared. If this matrix is local and the given argument is true, then a new shared segment is created, the data is copied into it, and the local segment is deallocated. If the matrix is shared and the argument false, a new local segment is allocated. The shared segment is then released and, if this matrix is the creator of the segment, it removes it from the system. If the argument is the same as the shared status, nothing is done.

◆ setType()

void setType ( const char *  type)
inline

◆ storeBinaryFile()

void storeBinaryFile ( const char *  filename,
int *  header,
sval  headerlen 
)
throw (MemException
)
inline

Store the header values `header' and then this matrix's contents to the file.

◆ sub()

void sub ( const R &  r,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Subtract `r' from every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix.

◆ subm()

void subm ( const Matrix< R > &  mat,
sval  minrow = 0,
sval  mincol = 0,
sval  maxrow = -1,
sval  maxcol = -1 
)
inline

Subtract every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat' from the same cell in the matrix.

◆ subMatrix()

Matrix<T>* subMatrix ( const char *  name,
sval  n,
sval  m = 1,
sval  noff = 0,
sval  moff = 0,
bool  isShared = false 
) const
throw (MemException
)
inline

Create a submatrix from this one of dimensions (n,m), starting at row `noff' and column `moff'.

◆ swapEndian()

void swapEndian ( )
inline

Member Data Documentation

◆ _isShared

bool _isShared
protected

◆ _m

sval _m
protected

◆ _n

sval _n
protected

◆ _n_actual

sval _n_actual
protected

◆ _name

std::string _name
protected

◆ _sharedname

std::string _sharedname
protected

◆ _type

std::string _type
protected

◆ data

T* data
protected

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