SAR<em>bayes</em>: SORAL Documentation
(SARBayes) Main Related Pages Class List Hierarchy Methods Files

Array2D Class Reference

#include <Array2D.h>

List of all members.


Detailed Description

Array2D is a dynamically allocated Matrix of doubles.

C and C++ 2-dimensional arrays must be dimensioned at compile-time. We need a 2-D array that is allocated dynamically. We could just use double**, but then there are a lot of potential memory leaks, so we've wrapped up the functionality in a small class that gives us nice initialization, constructor/destructor memory management, and the ability to change implementation later.

It is designed to be a simple, lightweight implementation that gives us just the functionality we need, which is basically construction and destruction for memory-management and initialization, plus square-bracket[][] indexing.

Generic code created by Michael and adapted by Andre both on 19/12/02 after phone and e-mail communication. * Thank you Michael :) *

Author : Michael Eldridge and Andre Oboler


Public Member Functions

 Array2D (const int p_rows, const int p_columns)
 Construct an empty 2D array of size p_rows x p_columns.

 Array2D (const int p_rows, const int p_columns, const valarray< double > p_array1D)
 Construct an array of size p_rows x p_columns, filled with passed values.

 Array2D (const Array2D &p_array2D)
 The Array2D copy constructor.

 Array2D (const Array2D &p_base, const Array2D &p_extension, const joinType p_joinTo)
 The constructor for appending one Array2D to another.

virtual ~Array2D ()
 Reclaims all memory in the 2D array.

void print (void) const
 Prints an Array2D.

double *const & operator[] (int index)
 The mutable version of the [] operator.

double *const & operator[] (int index) const
 The const version of the [] operator.

double operator= (int index)
const int getNumColumns () const
const int getNumRows () const


Constructor & Destructor Documentation

Array2D::Array2D const int  p_rows,
const int  p_columns
 

Construct an empty 2D array of size p_rows x p_columns.

Constructor takes the dimensions of the matrix and constructs a matrix with all values initialized to 0.

Array2D::Array2D const int  p_rows,
const int  p_columns,
const valarray< double >  p_array1D
 

Construct an array of size p_rows x p_columns, filled with passed values.

Creates a new array of size row x column, filled with the values in the supplied built-in array array1D.

array1D must be of size [rows*columns]. It is converted to the 2D array as: value[i][j] = array1D[i*columns + j]

At the moment, it allocates a new double** and then fills it value by value. That may change if we change our underlying implementation to valarrays rather than double**.

Todo:
Change assertion to proper exception.

Array2D::Array2D const Array2D p_array2D  ) 
 

The Array2D copy constructor.

Author: ASO

Array2D::Array2D const Array2D p_base,
const Array2D p_extension,
const joinType  p_joinTo
 

The constructor for appending one Array2D to another.

This constructor makes a new Array2D out of 2 old ones. The second Array2D is appended either below or to the right of the first, depending on the value of p_joinTo. p_joinTo can take values ROW or COLUMN. When ROW, the second array is appended below the first. When COLUMN, the second array is appended to the right of the first.

If the arrays do not share an edge of the same size, an exception should be thrown (not yet implemented).

Todo:
Throw an exception if arrays are the wrong size.

Array2D::~Array2D  )  [virtual]
 

Reclaims all memory in the 2D array.

This destructor reclaims memory from any allocated pointers. If we switch to valarrays, this will be unnecessary.


Member Function Documentation

double *const & Array2D::operator[] int  index  )  const
 

The const version of the [] operator.

Return the 1D array of all column values for the row provided.

As per the mutable version of this operator, but this version is invoked on const Array2D objects. On such objects you won't be able to do an assignment like myArray[i][j] = 3.5, though it is the const-ness of the array which prevents that, not anything in this operator.

double *const & Array2D::operator[] int  index  ) 
 

The mutable version of the [] operator.

Return the 1D array of all column values for the row provided.

This is not meant to be used alone. It is here to provide [][] indexing into the array, by returning a 1D array which provides its own [] operator.

This version is invoked on non-const Array2D objects. On such objects, you can assign directly to the indexed value. Thus myArray[i][j] = 3.5 will replace the value at (i,j).

If you use this alone to get a 1D slice, be aware that we may change the return type if we change implementations. We only guarantee that the return type will provide its own operator[]. It might be a valarray, a vector, an int[] array....

To avoid copying an entire 1D array every time someone uses [][], the current implementation hands you a reference. Obviously that reference is valid only so long as the Array2D still exists. There's no reason for you to hang on to the references from [] or [][] anyway. Since you've got the Array2D object, just [][] again.

void Array2D::print void   )  const
 

Prints an Array2D.

Loops through the Array2D giving a reasonably nice printout.


The documentation for this class was generated from the following files:
Generated on Tue Jul 29 03:09:38 2003 for SORAL by dOxygen 1.3.2
(SARBayes) Main Related Pages Class List Hierarchy Methods Files