Matrix Class Reference

Matrix of Variants. More...

#include "keyvalue/value/Matrix.h"

Inheritance diagram for Matrix:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Matrix (size_t nRows=1, size_t nCols=1)
 Constructs a Matrix with a given dimension.
size_t getNRows () const
 Gets number of rows.
size_t getNCols () const
 Gets number of columns.
void resize (size_t nRows, size_t nCols)
 Resizes the matrix.
const Variantoperator() (size_t i, size_t j) const
 Provides access to the element in the i-th row and j-th column.
Variantoperator() (size_t i, size_t j)
 Non const operator()().
bool operator== (const Matrix &rhs) const
 Comparison operator.

Protected Member Functions

void transposeVector ()
 Transposes the matrix if one of its dimensions is 1.

Private Attributes

boost::intrusive_ptr< Impl > pimpl_

Friends

void intrusive_ptr_add_ref (Matrix::Impl *pimpl)
void intrusive_ptr_release (Matrix::Impl *pimpl)

Detailed Description

Matrix of Variants.

Why yet another matrix class? The reason is the following. Key-value pattern recognition (see pattern::Pattern) is simplified if vector is publicly derived from matrix (a vector is either a m x 1 or a 1 x n matrix) and single is publicly derived from vector (a single is a one-dimensional vector).

Furthermore, this class and all its derived classes implement reference semantics when copying. That is, when a Matrix M is copied to a Matrix N, then changing N also changes M and reciprocally.

The reference semantics is implemented through the PImpl idiom which also make this class cheap to copy.


Constructor & Destructor Documentation

Matrix ( size_t  nRows = 1,
size_t  nCols = 1 
) [explicit]

Constructs a Matrix with a given dimension.

If the number of rows or the number of columns is zero, then both are changed to 1. Hence, trying to build a 0 x n or m x 0 matrix ends up building a 1 x 1 matrix.

Parameters:
nRows : number of rows;
nCols : number of columns.

Member Function Documentation

size_t getNRows (  )  const

Gets number of rows.

Returns:
The number of rows.
size_t getNCols (  )  const

Gets number of columns.

Returns:
The number of columns.
void resize ( size_t  nRows,
size_t  nCols 
)

Resizes the matrix.

The size of a matrix (i.e., number of rows times number of columns) can not increase. Debug build check for this and an exception is thrown when the test fails. Release build has undefined behavior in this case.

The contents of a matrix after resizing is undefined.

Parameters:
nRows : number of rows;
nCols : number of columns.
Exceptions:
LogicError : (Debug build only) If the new size is greater than the original one.
const Variant& operator() ( size_t  i,
size_t  j 
) const

Provides access to the element in the i-th row and j-th column.

Debug build perform bound checks and throws an exception on failure. Release build has undefined behavior in that case.

Parameters:
i : Row index;
j : Column index.
Returns:
A const reference to (i, j)-th element.
Exceptions:
LogicError : (Debug build only) If i is not smaller than the number of rows or j is not smaller than the number of columns.
Variant& operator() ( size_t  i,
size_t  j 
)

Non const operator()().

bool operator== ( const Matrix rhs  )  const

Comparison operator.

Recall that Matrix has reference semantics. Consider the code:


Matrix m1(2,2);
m1(0,0) = m1(0,1) = m1(1,0) = m1(1,1) = 1.0;
Matrix m2(2,2); m2(0,0) = m2(0,1) = m2(1,0) = m2(1,1) = 1.0;
Matrix m3(m1);

Then m1 == m2 is false and m1 == m3 is true.

Parameters:
rhs : The Value to be compared against.
Returns:
This method returns true if *this and rhs refer to the same data. Otherwise it returns false.
void transposeVector (  )  [protected]

Transposes the matrix if one of its dimensions is 1.

In-place matrix transposition is a complicated stuff. See

http://en.wikipedia.org/wiki/In-place_matrix_transposition

However, since the storage is linear, the memory layouts of n x 1 and 1 x n matrix, are the same. Hence, we only need to swap dimensions. This method is intent to perform only this simple transposition and it is protected to be called by Vector which derives from this class.

Debug build checks if either of the dimensions is 1 and throws an exception if the test fails. Release build has undefined behaviour in that circumstance.

Exceptions:
LogicError : (Debug build only) If none of dimensions is 1.

Friends And Related Function Documentation

void intrusive_ptr_add_ref ( Matrix::Impl *  pimpl  )  [friend]

headerfile Matrix.h "keyvalue/value/Matrix.h"

brief Increment Matrix reference counting.

See documentation of intrusive_ptr in the Smart Ptr (Boost) library.

param Pointer to Matrix::Impl storing the counter.

void intrusive_ptr_release ( Matrix::Impl *  pimpl  )  [friend]

headerfile Matrix.h "keyvalue/value/Matrix.h"

brief Decrement Matrix reference counting.

See documentation of intrusive_ptr in the Smart Ptr (Boost) library.

param Pointer to Matrix::Impl storing the counter.


Generated on Sat Mar 20 15:08:31 2010 for KeyValue by  doxygen 1.6.1