Array< T > Class Template Reference

This template class defines a data structure for general arrays of arbitrary types. More...

#include <Array.h>

Inheritance diagram for Array< T >:
ArrayBase ArrayReference< T >

List of all members.

Public Types

typedef T * iterator
 Pointer to a single array element.
typedef T * const_iterator
 Constant pointer to a single array element.

Public Member Functions

 Array ()
 Creates a new empty Array object.
 Array (unsigned i)
 Creates a new empty one-dimensional array of size "i".
 Array (unsigned i, unsigned j)
 Creates a new empty two-dimensional array.
 Array (unsigned i, unsigned j, unsigned k)
 Creates a new empty three-dimensional array.
 Array (std::vector< T > &v)
 Creates a new one-dimensional array with the content of "v".
 Array (Array< T > &v, bool)
 Creates a new array with the structure and content of array "v".
 Array (const Array< T > &v)
 Creates a new array with the structure and content of the constant array "v".
 ~Array ()
 Destructs the current Array object.
unsigned int rows () const
 Returns the number of rows.
unsigned int cols () const
 Returns the number of columns.
T & operator() ()
 Returns the first and only element of the current array.
const T & operator() () const
 Returns the first and only element of the current array as constant.
T & operator() (unsigned i)
 Returns the i-th element of the one-dimensional array.
const T & operator() (unsigned i) const
 Returns the i-th element of the one-dimensional array as constant.
T & operator() (unsigned i, unsigned j)
 Returns the element at position "i" of dimension one and position "j" at dimension two of the two-dimensional array.
const T & operator() (unsigned i, unsigned j) const
 Returns the element at position "i" of dimension one and position "j" at dimension two of the two-dimensional array as constant.
T & operator() (unsigned i, unsigned j, unsigned k)
 Returns the element at position "i" of dimension one, position "j" at dimension two and position "k" of dimension three of the three-dimensional array.
const T & operator() (unsigned i, unsigned j, unsigned k) const
 Returns the element at position "i" of dimension one, position "j" at dimension two and position "k" of dimension three of the three-dimensional array as constant.
T & operator() (const std::vector< unsigned > &i)
 Returns the element identified by the dimension-positions in vector "i" from the current array.
const T & operator() (const std::vector< unsigned > &i) const
 Returns the element identified by the dimension-positions in vector "i" from the current array as constant.
ArrayReference< T > operator[] (unsigned i)
 Returns a reference to subarray "i" of the current array.
const ArrayReference< T > operator[] (unsigned i) const
 Returns a constant reference to subarray "i" of the current array.
Array< T > & operator= (const T &v)
 Assigns the value "v" to all positions of the current array.
Array< T > & operator= (const std::vector< T > &v)
 Assigns the values of vector "v" to the current array.
Array< T > & operator= (const Array< T > &v)
 Assigns the values of array "v" to the current array.
T & elem (unsigned i)
 Returns the i-th element of the arrays element vector Array::e.
const T & elem (unsigned i) const
 Returns the i-th element of the arrays element vector Array::e as constant.
T * elemvec ()
 Returns the element vector Array::e of the array.
const T * elemvec () const
 Returns the element vector Array::e of the array as constant.
Array< T > & append_elem (const T &w)
 Appends value "w" to the current one-dimensional or empty array.
Array< T > & append_elems (const Array< T > &w)
 Appends the values in "w" to the current one-dimensional or empty array.
Array< T > & append_rows (const Array< T > &y)
 Appends the array "y" after the last row of the current array.
Array< T > & remove_row (unsigned i)
 Removes row "i" from the current non-empty array.
Array< T > remove_col (unsigned k) const
 Removes column "k" from the current non-empty array.
Array< T > & remove_cols (const Array< unsigned > idx)
 Removes the columns with the indices stored in array "idx" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j)
 Removes the two columns with the indices "i" and "j" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k)
 Removes the three columns with the indices "i", "j" and "k" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k, unsigned l)
 Removes the four columns with the indices "i", "j", "k" and "l" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m)
 Removes the five columns with the indices "i", "j", "k", "l" and "m" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n)
 Removes the six columns with the indices "i", "j", "k", "l", "m" and "n" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o)
 Removes the seven columns with the indices "i", "j", "k", "l", "m", "n" and "o" from the current non-empty array.
Array< T > & remove_cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o, unsigned p)
 Removes the eight columns with the indices "i", "j", "k", "l", "m", "n", "o" and "p" from the current non-empty array.
Array< T > append_cols (const Array< T > &y) const
 Appends the array "y" after the last column of the current array.
Array< T > subarr (unsigned from, unsigned to) const
 Returns a subarray from position "from" to position "to" of the current array.
Array< unsigned > pos2idx (unsigned p)
 Given an absolute position "p" in the element vector Array::e, the positions relative to the single dimensions of the array are returned.
void pos2idx (unsigned p, Array< unsigned > &idx)
 Given an absolute position "p" in the element vector Array::e, the positions relative to the single dimensions are stored in "idx".
Array< T > row (unsigned i) const
 Returns the row number "i" of the current array.
Array< T > rows (const Array< unsigned > &idx) const
 Returns the array rows with the numbers defined in array "idx" from the current non-empty array.
Array< T > rows (unsigned i) const
 Returns the array row number "i" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j) const
 Returns the array rows with the numbers "i" and "j" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k) const
 Returns the array rows with the numbers "i", "j" and "k" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k, unsigned l) const
 Returns the array rows with the numbers "i", "j", "k" and "l" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m) const
 Returns the array rows with the numbers "i", "j", "k", "l" and "m" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n) const
 Returns the array rows with the numbers "i", "j", "k", "l", "m" and "n" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o) const
 Returns the array rows with the numbers "i", "j", "k", "l", "m", "n" and "o" from the current non-empty array.
Array< T > rows (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o, unsigned p) const
 Returns the array rows with the numbers "i", "j", "k", "l", "m", "n", "o" and "p" from the current non-empty array.
Array< T > col (unsigned i) const
 Returns the column number "i" of the current array.
Array< T > cols (const Array< unsigned > &idx) const
 Returns the array columns with the numbers defined in array "idx" from the current non-empty array.
Array< T > cols (unsigned i) const
 Returns the array column number "i" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j) const
 Returns the array columns with the numbers "i" and "j" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k) const
 Returns the array columns with the numbers "i", "j" and "k" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k, unsigned l) const
 Returns the array columns with the numbers "i", "j", "k" and "l" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m) const
 Returns the array columns with the numbers "i", "j", "k", "l" and "m" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n) const
 Returns the array columns with the numbers "i", "j", "k", "l", "m" and "n" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o) const
 Returns the array columns with the numbers "i", "j", "k", "l", "m", "n" and "o" from the current non-empty array.
Array< T > cols (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m, unsigned n, unsigned o, unsigned p) const
 Returns the array columns with the numbers "i", "j", "k", "l", "m", "n", "o" and "p" from the current non-empty array.
Array< T > & transpose ()
 Returns the transposition of the current array.
Array< T > & rotate_rows (int n)
 The last "n" rows of the array will become the first rows.
Array< T > & rotate_cols (int n)
 The last "n" columns of the array will become the first columns.
Array< unsigned > dimarr () const
 Returns an array that has the same content as the dimension vector ArrayBase::d of the current Array object.
ArrayBaseclone () const
 Returns an identical copy of this Array object.
ArrayBaseempty () const
 Returns an empty array with the same type as this array object.
iterator begin ()
 Returns an iterator to the first array element.
const const_iterator begin () const
 Returns a constant iterator to the first array element.
iterator end ()
 Returns an iterator to the last array element.
const const_iterator end () const
 Returns a constant iterator to the last array element.

Protected Member Functions

void resize_i (unsigned *_d, unsigned _nd, bool copy)
 Handles memory allocation for the other resize methods.
 Array (unsigned *_d, T *_e, unsigned _nd, unsigned _ne, bool _stat)
 Creates a new Array object with the structure and content given by the parameters.

Protected Attributes

T * e
 The element vector of the current array.
T * eEnd
 The last element of the element vector Array::e.

Friends

class ArrayReference< T >
class ArrayIndex

Detailed Description

template<class T>
class Array< T >

This template class defines a data structure for general arrays of arbitrary types.

The class is derived from class ArrayBase.
Here you will find element-specific methods for access of elements, assigning elements, adding and removing elements, changing the order of elements and resizing arrays.

Author:
M. Kreutz
Date:
1995-01-01
Changes:
none
Status:
stable
Examples:

mean-var.cpp.

Definition at line 988 of file Array.h.


Member Typedef Documentation

template<class T>
typedef T* Array< T >::const_iterator

Constant pointer to a single array element.

Used for compatibility with the other stdlib structures.

Definition at line 1001 of file Array.h.

template<class T>
typedef T* Array< T >::iterator

Pointer to a single array element.

Used for compatibility with the other stdlib structures.

Definition at line 997 of file Array.h.


Constructor & Destructor Documentation

template<class T>
Array< T >::Array (  )  [inline]

Creates a new empty Array object.

Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1021 of file Array.h.

template<class T>
Array< T >::Array ( unsigned  i  )  [inline, explicit]

Creates a new empty one-dimensional array of size "i".

Memory for an one-dimensional array of length i is allocated.

Parameters:
i size of the new array
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1048 of file Array.h.

template<class T>
Array< T >::Array ( unsigned  i,
unsigned  j 
) [inline]

Creates a new empty two-dimensional array.

Memory for an $i \times j$ array is allocated.

Parameters:
i size of the first dimension of the new array
j size of the second dimension of the new array
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1084 of file Array.h.

template<class T>
Array< T >::Array ( unsigned  i,
unsigned  j,
unsigned  k 
) [inline]

Creates a new empty three-dimensional array.

Memory for an $i \times j \times k$ array is allocated.

Parameters:
i size of the first dimension of the new array
j size of the second dimension of the new array
k size of the third dimension of the new array
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1119 of file Array.h.

template<class T>
Array< T >::Array ( std::vector< T > &  v  )  [inline]

Creates a new one-dimensional array with the content of "v".

Parameters:
v vector, whose content will be content of the new array
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1154 of file Array.h.

template<class T>
Array< T >::Array ( Array< T > &  v,
bool   
) [inline]

Creates a new array with the structure and content of array "v".

Parameters:
v array, whose content will be content of the new array, the content of v will be deleted and v will act as static reference to the new created array
... dummy value
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1191 of file Array.h.

template<class T>
Array< T >::Array ( const Array< T > &  v  )  [inline]

Creates a new array with the structure and content of the constant array "v".

Parameters:
v array, whose content will be copied to the new array
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1222 of file Array.h.

template<class T>
Array< T >::~Array (  )  [inline]

Destructs the current Array object.

Deletes the dimension and element vector Array::e and frees the corresponding memory.

Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable
See also:
ArrayBase::~ArrayBase()

Definition at line 1257 of file Array.h.

template<class T>
Array< T >::Array ( unsigned *  _d,
T *  _e,
unsigned  _nd,
unsigned  _ne,
bool  _stat 
) [inline, protected]

Creates a new Array object with the structure and content given by the parameters.

A new Array is created by using the parameterized values for the internal structure and content.
This method is for internal use only.

Parameters:
_d one-dimensional vector of array-dimensions
_e one-dimensional vector of array elements for all dimensions
_nd number of array-dimensions
_ne total number of array elements
_stat defines, whether the array object is a static reference or not
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4572 of file Array.h.


Member Function Documentation

template<class T>
const const_iterator Array< T >::begin (  )  const [inline]

Returns a constant iterator to the first array element.

A constant pointer to the first element of the element vector Array::e of the current array is returned as iterator.
This method is used for compatibility with the other stdlib structures.

Returns:
Constant iterator to the first array element.
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4279 of file Array.h.

template<class T>
iterator Array< T >::begin (  )  [inline]

Returns an iterator to the first array element.

A pointer to the first element of the element vector Array::e of the current array is returned as iterator.
This method is used for compatibility with the other stdlib structures.

Returns:
Iterator to the first array element.
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4252 of file Array.h.

Referenced by operator==(), and Array< T * >::remove_cols().

template<class T>
const T* Array< T >::elemvec (  )  const [inline]

Returns the element vector Array::e of the array as constant.

Returns:
the element vector of the current array
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1853 of file Array.h.

template<class T>
T* Array< T >::elemvec (  )  [inline]

Returns the element vector Array::e of the array.

Returns:
the element vector of the current array
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 1831 of file Array.h.

Referenced by Array< T * >::append_cols(), Array< T * >::append_rows(), Array< T * >::col(), Array< T * >::cols(), ArrayTable< T >::operator T **(), ArrayTable< T >::operator T ***(), Array< T * >::remove_col(), Array< T * >::remove_row(), Array< T * >::rows(), and Array< T * >::subarr().

template<class T>
ArrayBase* Array< T >::empty (  )  const [inline, virtual]

Returns an empty array with the same type as this array object.

A new empty Array object with the same type as the current array is created and a pointer to this new object returned.

Returns:
an empty copy of the current array
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable
See also:
ArrayBase::empty

Implements ArrayBase.

Definition at line 4199 of file Array.h.

template<class T>
const const_iterator Array< T >::end (  )  const [inline]

Returns a constant iterator to the last array element.

A constant pointer to the last element of the element vector Array::e of the current array is returned as iterator.
This method is used for compatibility with the other stdlib structures.

Returns:
Constant iterator to the last array element.
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4331 of file Array.h.

template<class T>
iterator Array< T >::end (  )  [inline]

Returns an iterator to the last array element.

A constant pointer to the last element of the element vector Array::e of the current array is returned as iterator.
This method is used for compatibility with the other stdlib structures.

Returns:
Iterator to the last array element.
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4305 of file Array.h.

Referenced by operator==(), and Array< T * >::remove_cols().

template<class T>
void Array< T >::pos2idx ( unsigned  p,
Array< unsigned > &  idx 
) [inline]

Given an absolute position "p" in the element vector Array::e, the positions relative to the single dimensions are stored in "idx".

Parameters:
p the absolute position
idx an array with the relative positions
Returns:
none
Exceptions:
SharkException the type of the exception will be "range check error" and indicates that p exceeds the number of elements of the current array
Example
Guess you have an $5 \times 4$ array, i.e. an array with a total number of 20 elements. You want to extract the element at the absolute position 13 of the element vector (containing elements with indices 0 to 19).
Transformed to the array as seen by the user, you will get the relative positions $(3,1)$, because $3 \ast 4 + 1 = 13$.
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 2845 of file Array.h.

template<class T>
void Array< T >::resize_i ( unsigned *  _d,
unsigned  _nd,
bool  copy 
) [inline, protected, virtual]

Handles memory allocation for the other resize methods.

This function handles memory allocation of the respective template type in case of resizing and is only for internal use.

Parameters:
_d one-dimensional vector of array-dimensions
_nd number of array-dimensions
copy flag which indicates whether existing elements of an array should be copied in case of resizing (as long as possible)
Returns:
none
Exceptions:
SharkException the type of the exception is "size mismatch" and indicates, that the current array is a static array reference. You can't change the size of an array reference, so it is checked, if the current number of dimensions is not equal to the new defined number of dimensions _nd and if the current total number of array elements is not equal to the new number of array elements. If the preprocessor directive "-DNDEBUG" is not used, then it is also checked, if at least one of the sizes of the single dimensions of the current array (reference) is not equal to the corresponding new defined size in _d.
Both cases are illegal for static array references, so the exception is thrown.
So the only chance to use this method on static array references is to change nothing, i.e. the new number of dimensions and dimension sizes are equal to the old ones (what makes not a real sense at all).
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable
See also:
ArrayBase::resize_i(unsigned*, unsigned, bool)

Implements ArrayBase.

Definition at line 4460 of file Array.h.

Referenced by Array< T * >::append_cols(), Array< T * >::col(), Array< T * >::cols(), Array< T * >::remove_col(), Array< T * >::remove_row(), Array< T * >::rows(), and Array< T * >::subarr().

template<class T>
Array<T>& Array< T >::rotate_cols ( int  n  )  [inline]

The last "n" columns of the array will become the first columns.

Parameters:
n number of last columns that will become the n first columns
Returns:
the array with the rotated columns
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that the current array has no dimensions
Example
Given an array with the content

$ \left(\begin{array}{lll} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\\ 10 & 11 & 12 \end{array}\right) $

and $n = 2$ then the returned array will be

$ \left(\begin{array}{lll} 2 & 3 & 1\\ 5 & 6 & 4\\ 8 & 9 & 7\\ 11 & 12 & 10 \end{array}\right) $

Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Definition at line 4095 of file Array.h.

template<class T>
Array<T>& Array< T >::rotate_rows ( int  n  )  [inline]

The last "n" rows of the array will become the first rows.

Parameters:
n number of last rows that will become the n first rows
Returns:
the array with the rotated rows
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that the current array has no dimensions
Example
Given an array with the content

$ \left(\begin{array}{lll} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\\ 10 & 11 & 12 \end{array}\right) $

and $n = 2$ then the returned array will be

$ \left(\begin{array}{lll} 7 & 8 & 9\\ 10 & 11 & 12\\ 1 & 2 & 3\\ 4 & 5 & 6 \end{array}\right) $

Author:
M. Kreutz
Date:
1995-01-01

Definition at line 4026 of file Array.h.


Friends And Related Function Documentation

template<class T>
friend class ArrayIndex [friend]

Definition at line 991 of file Array.h.

template<class T>
friend class ArrayReference< T > [friend]

Definition at line 990 of file Array.h.


Member Data Documentation

template<class T>
T* Array< T >::e [protected]

The element vector of the current array.

Internally, the major information about an array is stored in two vectors, the element vector and the dimension vector ArrayBase::d.
The element vector stores all single elements of an array successively. Beginning with the first position in the first dimension of an array as seen by the user, go recursively down to the last dimension and list all elements there.
This will give you the order of the elements in the element vector.

Example
  #include "Array/Array.h"

  void main()
  {
      Array< unsigned >  test( 2, 2, 4 );  // 3-dimensional test array
      unsigned          *evec,             // element vector
                         no_elems,         // total number of array 
                                           // elements
                         curr_no;          // number of current element 

      // Fill test array with cotent:
      test( 0, 0, 0 ) = 1;
      test( 0, 0, 1 ) = 2;
      test( 0, 0, 2 ) = 3;
      test( 0, 0, 3 ) = 4;
      test( 0, 1, 0 ) = 5;
      test( 0, 1, 1 ) = 6;
      test( 0, 1, 2 ) = 7;
      test( 0, 1, 3 ) = 8;
      test( 1, 0, 0 ) = 9;
      test( 1, 0, 1 ) = 10;
      test( 1, 0, 2 ) = 11;
      test( 1, 0, 3 ) = 12;
      test( 1, 1, 0 ) = 13;
      test( 1, 1, 1 ) = 14;
      test( 1, 1, 2 ) = 15;
      test( 1, 1, 3 ) = 16;

      // Get total number of array elements:
      no_elems = test.nelem( );

      // Get element vector, output of each vector element:
      for ( curr_no = 0, evec = test.elemvec( ); 
            curr_no < no_elems; curr_no++, evec++ )
      {
          cout << *evec << ' ';
      }
  }

This program will produce the output:

$1\mbox{\ }2\mbox{\ }3\mbox{\ }4\mbox{\ }5\mbox{\ }6\mbox{\ }7\mbox{\ }8\mbox{\ }9\mbox{\ }10\mbox{\ }11\mbox{\ }12\mbox{\ }13\mbox{\ }14\mbox{\ }15\mbox{\ }16$

Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable
See also:
ArrayBase::resize_i(unsigned*, unsigned, bool)

Definition at line 4410 of file Array.h.

Referenced by Array< T * >::append_elems(), Array< T * >::Array(), ArrayReference< T >::copyReference(), Array< T * >::operator=(), and Array< T >::operator[]().

template<class T>
T* Array< T >::eEnd [protected]

The last element of the element vector Array::e.

Definition at line 4413 of file Array.h.


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