SPUC  3.0
Public Types | Public Member Functions | List of all members
SPUC::array2d< T > Class Template Reference

#include <array2d.h>

Public Types

typedef T value_type
 

Public Member Functions

 array2d ()
 
 array2d (int m, int n)
 
 array2d (int m, int n, T *a)
 
 array2d (int m, int n, const T &a)
 
 array2d (const array2d &A)
 
array2doperator= (const T &a)
 
array2doperator= (const array2d &A)
 
array2dref (const array2d &A)
 
array2d copy () const
 
array2dinject (const array2d &A)
 
T * operator[] (int i)
 
const T * operator[] (int i) const
 
int dim1 () const
 
int dim2 () const
 
int ref_count () const
 
 ~array2d ()
 
int cols () const
 
int rows () const
 
array2d< T > & newsize (int m, int n)
 
array2d< T > & set_size (int m, int n)
 

Detailed Description

template<class T>
class SPUC::array2d< T >

Templated two-dimensional, numerical array which looks like a conventional C multiarray. Storage corresponds to C (row-major) ordering. Elements are accessed via A[i][j] notation.

Array assignment is by reference (i.e. shallow assignment). That is, B=A implies that the A and B point to the same array, so modifications to the elements of A will be reflected in B. If an independent copy is required, then B = A.copy() can be used. Note that this facilitates returning arrays from functions without relying on compiler optimizations to eliminate extensive data copying.

The indexing and layout of this array object makes it compatible with C and C++ algorithms that utilize the familiar C[i][j] notation. This includes numerous textbooks, such as Numercial Recipes, and various public domain codes.

This class employs its own garbage collection via the use of reference counts. That is, whenever an internal array storage no longer has any references to it, it is destoryed.

Member Typedef Documentation

template<class T>
typedef T SPUC::array2d< T >::value_type

Constructor & Destructor Documentation

template<class T >
SPUC::array2d< T >::array2d ( )

Create a null (0x0) array.

template<class T >
SPUC::array2d< T >::array2d ( int  m,
int  n 
)

Create a new (m x n) array, WIHOUT initializing array elements. To create an initialized array of constants, see array2d(m,n,value).

This version avoids the O(m*n) initialization overhead and is used just before manual assignment.

Parameters
mthe first (row) dimension of the new matrix.
nthe second (column) dimension of the new matrix.
template<class T >
SPUC::array2d< T >::array2d ( int  m,
int  n,
T *  a 
)

Create a new (m x n) array, as a view of an existing one-dimensional array stored in C order, i.e. right-most dimension varying fastest. (Often referred to as "row-major" ordering.) Note that the storage for this pre-existing array will never be garbage collected by the array2d class.

Parameters
mthe first (row) dimension of the new matrix.
nthe second (column) dimension of the new matrix.
athe one dimensional C array to use as data storage for the array.
template<class T >
SPUC::array2d< T >::array2d ( int  m,
int  n,
const T &  val 
)

Create a new (m x n) array, initializing array elements to constant specified by argument. Most often used to create an array of zeros, as in A(m, n, 0.0).

Parameters
mthe first (row) dimension of the new matrix.
nthe second (column) dimension of the new matrix.
valthe constant value to set all elements of the new array to.
template<class T >
SPUC::array2d< T >::array2d ( const array2d< T > &  A)
inline

Copy constructor. Array data is NOT copied, but shared. Thus, in array2d B(A), subsequent changes to A will be reflected in B. For an indepent copy of A, use array2d B(A.copy()), or B = A.copy(), instead.

template<class T >
SPUC::array2d< T >::~array2d ( )

Member Function Documentation

template<class T >
int SPUC::array2d< T >::cols ( ) const
inline
template<class T >
array2d< T > SPUC::array2d< T >::copy ( ) const

Create a new of existing matrix. Used in B = A.copy() or in the construction of B, e.g. array2d B(A.copy()), to create a new array that does not share data.

template<class T >
int SPUC::array2d< T >::dim1 ( ) const
inline
Returns
the size of the first dimension of the array, i.e. the number of rows.

Referenced by SPUC::array2d< T >::set_size().

template<class T >
int SPUC::array2d< T >::dim2 ( ) const
inline
Returns
the size of the second dimension of the array, i.e. the number of columns.

Referenced by SPUC::array2d< T >::set_size().

template<class T >
array2d< T > & SPUC::array2d< T >::inject ( const array2d< T > &  A)

Copy the elements to from one array to another, in place. That is B.inject(A), both A and B must conform (i.e. have identical row and column dimensions).

This differs from B = A.copy() in that references to B before this assignment are also affected. That is, if we have

        array2d A(m,n);
        array2d C(m,n);
        array2d B(C);        // elements of B and C are shared.

then B.inject(A) affects both and C, while B=A.copy() creates a new array B which shares no data with C or A.

Parameters
Athe array from elements will be copied
Returns
an instance of the modifed array. That is, in B.inject(A), it returns B. If A and B are not conformat, no modifications to B are made.
template<class T>
array2d<T>& SPUC::array2d< T >::newsize ( int  m,
int  n 
)
inline

References SPUC::array2d< T >::set_size().

Here is the call graph for this function:

template<class T >
array2d< T > & SPUC::array2d< T >::operator= ( const T &  a)
inline

Assign all elemnts of A to a constant scalar.

template<class T >
array2d< T > & SPUC::array2d< T >::operator= ( const array2d< T > &  A)
inline

B = A is shorthand notation for B.ref(A).

template<class T >
T * SPUC::array2d< T >::operator[] ( int  i)
inline

Used for A[i][j] indexing. The first [] operator returns a conventional pointer which can be dereferenced using the same [] notation.

If TNT_BOUNDS_CHECK macro is define, the left-most index (row index) is checked that it falls within the array bounds (via the assert() macro.) Note that bounds checking can occur in the row dimension, but the not column, since this is just a C pointer.

template<class T >
const T * SPUC::array2d< T >::operator[] ( int  i) const
inline
template<class T >
array2d< T > & SPUC::array2d< T >::ref ( const array2d< T > &  A)
inline

Create a reference (shallow assignment) to another existing array. In B.ref(A), B and A shared the same data and subsequent changes to the array elements of one will be reflected in the other.

This is what operator= calls, and B=A and B.ref(A) are equivalent operations.

Returns
The new referenced array: in B.ref(A), it returns B.
template<class T >
int SPUC::array2d< T >::ref_count ( ) const
inline
Returns
the number of arrays that share the same storage area as this one. (Must be at least one.)
template<class T >
int SPUC::array2d< T >::rows ( ) const
inline
template<class T>
array2d<T>& SPUC::array2d< T >::set_size ( int  m,
int  n 
)
inline

References SPUC::array2d< T >::dim1(), and SPUC::array2d< T >::dim2().

Referenced by SPUC::array2d< T >::newsize().

Here is the call graph for this function:


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