Public Member Functions | Protected Member Functions

Vector Class Reference

A m x 1 or 1 x n Matrix. More...

#include <keyvalue/value/Vector.h>

Inheritance diagram for Vector:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Vector (size_t size=1, bool isRow=false)
 Constructs a Vector with a given size.
template<typename ElementType >
 Vector (const vector< ElementType > &data, bool isRow=false)
 Builds a Vector from a std::vector<ElementType>
size_t getSize () const
 Gets Vector size.
void resize (size_t size)
 Resizes the vector.
const Variantoperator() (size_t i) const
 Provides access to the i-th element.
Variantoperator() (size_t i)
 Non const operator()().
void transpose ()
 Transposes the vector.
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.

Detailed Description

A m x 1 or 1 x n Matrix.

See Matrix documentation for more details.


Constructor & Destructor Documentation

Vector ( size_t  size = 1,
bool  isRow = false 
) [explicit]

Constructs a Vector with a given size.

Debug build throws an exception when size is zero. Release build has undefined behavior in that circumstance.

Parameters:
size : Vector size;
isRow : When this parameter is true (default), a 1 x dim Matrix is built. Otherwise a dim x 1 Matrix is built.
Exceptions:
LogicError : (Debug build only) When dim = 0.
Vector ( const vector< ElementType > &  data,
bool  isRow = false 
)

Builds a Vector from a std::vector<ElementType>

ElementType must be one of the following types,

  • bool;
  • double;
  • ptime;
  • string.
Parameters:
ElementType : (template parameter) The input vector element type;
data : A vector defining the content.
isRow : When this parameter is true (default), a 1 x dim Matrix is built. Otherwise a dim x 1 Matrix is built.

Member Function Documentation

size_t getSize (  )  const

Gets Vector size.

Returns:
The size.
void resize ( size_t  size  ) 

Resizes the vector.

The size of a vector 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.

Parameters:
size : New size.
Exceptions:
LogicError : (Debug build only) If the new dimension is greater than the original one.
const Variant& operator() ( size_t  i  )  const

Provides access to the i-th element.

Debug build performs bound check and throws an exception when the check fail. Release build has undefined behavior in that circumstance.

Parameters:
i : Element's index.
Returns:
A const reference to the i-th element.
Exceptions:
LogicError : (Debug build only) When i is not smaller than vector's dimension.
Variant& operator() ( size_t  i  ) 

Non const operator()().

void transpose (  ) 

Transposes the vector.

size_t getNRows (  )  const [inherited]

Gets number of rows.

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

Gets number of columns.

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

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 [inherited]

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 
) [inherited]

Non const operator()().

bool operator== ( const Matrix rhs  )  const [inherited]

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, inherited]

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.