Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

GrowableArray Class Template Reference

This file defines an array template class that will grow as elements are added to the end of the array. More...

#include <grow_array.h>

List of all members.

Public Methods

 GrowableArray ()
 ~GrowableArray ()
 destructor. More...

const size_t length (void) const
 Length of the data (which is not necessarily the same as the length of the internal array. More...

void set_to_zero ()
 set array to zero length. More...

T& operator[] (const size_t i)
 LHS [] operator. More...

operator[] (const size_t i) const
 RHS [] operator. More...

const T* getData () const
 Get a pointer to the internal data array. More...

void append (T item)
 append an item to the end of the array. More...

void expand (size_t amount)
 expand. More...

void remove (void)
 Remove one item from the end of the array. More...


Private Types

enum  bogus { StartArraySize = 128 }

Private Methods

bool twice ()
 twice. More...


Private Attributes

size_t num_elem
 number of data elements. More...

size_t array_size
 Array size (always <= num_elem). More...

T* pArray


Detailed Description

template<class T> class GrowableArray

This file defines an array template class that will grow as elements are added to the end of the array.

This is similar to the STL <vector> class.

This array class is designed for dense arrays where the all elements of the array are used.

Usage:

A doubling algorithm is used when the data size is expanded because it minimizes the amount of copying that must be done. The array will quickly grow to a the size needed to accomodate the data set and no more copying will be necessary. For large arrays there is the drawback that more memory may be allocated than is needed, since the amount of memory used grows exponentially.

Author:
Ian Kaplan

Definition at line 69 of file grow_array.h.


Member Enumeration Documentation

template<class T>
enum GrowableArray<T>::bogus [private]
 

Enumeration values:
StartArraySize  

Definition at line 71 of file grow_array.h.

00071 { StartArraySize = 128 } bogus;


Constructor & Destructor Documentation

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

Definition at line 112 of file grow_array.h.

00113   {
00114     pArray = new T[ StartArraySize ];
00115     num_elem = 0;
00116     array_size = StartArraySize;
00117   } // GrowableArray constructor

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

destructor.

Definition at line 120 of file grow_array.h.

00121   {
00122     if (pArray != NULL) {
00123       delete [] pArray;
00124     }
00125   } // GrowableArray destructor


Member Function Documentation

template<class T>
void GrowableArray<T>::append ( T item ) [inline]
 

append an item to the end of the array.

Definition at line 155 of file grow_array.h.

Referenced by packfreq::findLevel().

00156   {
00157 
00158     if (num_elem == array_size) {
00159       bool allocOK = twice();
00160       assert( allocOK );
00161     }
00162 
00163     pArray[ num_elem ] = item;
00164     num_elem++;
00165   } // append

template<class T>
void GrowableArray<T>::expand ( size_t amount ) [inline]
 

expand.

Expand the number of array data slots by "amount" elements. Note that "array_size" is the total amount of storage available for data slots. "num_elem" is the number of data slots. The bounds over which the array can be indexed is governed by num_elem. Note that after expand() is called the new data elements can be read, but their value is undefined until they are initialized.

Definition at line 179 of file grow_array.h.

00180   {
00181     bool allocOK = true;
00182 
00183     while (allocOK && num_elem + amount >= array_size) {
00184       allocOK = twice();
00185       assert( allocOK );
00186     }
00187     num_elem += amount;
00188   } // expand

template<class T>
const T * GrowableArray<T>::getData ( ) const [inline]
 

Get a pointer to the internal data array.

Definition at line 152 of file grow_array.h.

00152 { return pArray; }

template<class T>
const size_t GrowableArray<T>::length ( void ) const [inline]
 

Length of the data (which is not necessarily the same as the length of the internal array.

Definition at line 129 of file grow_array.h.

Referenced by packfreq::plotMat(), and packfreq::prMat().

00129 { return num_elem; }

template<class T>
T GrowableArray<T>::operator[] ( const size_t i ) const [inline]
 

RHS [] operator.

Definition at line 145 of file grow_array.h.

00146   {
00147     assert( i < num_elem );
00148     return pArray[ i ];
00149   }

template<class T>
T & GrowableArray<T>::operator[] ( const size_t i ) [inline]
 

LHS [] operator.

Definition at line 138 of file grow_array.h.

00139   {
00140     assert( i < num_elem );
00141     return pArray[ i ];
00142   }

template<class T>
void GrowableArray<T>::remove ( void ) [inline]
 

Remove one item from the end of the array.

Definition at line 192 of file grow_array.h.

00193   {
00194     if (num_elem > 0)
00195       num_elem--;
00196   }

template<class T>
void GrowableArray<T>::set_to_zero ( ) [inline]
 

set array to zero length.

Definition at line 132 of file grow_array.h.

00133   {
00134     num_elem = 0;
00135   }

template<class T>
bool GrowableArray<T>::twice ( ) [inline, private]
 

twice.

Double the amount of memory allocated for the array. Return true if memory allocation succeeded, false otherwise.

Definition at line 86 of file grow_array.h.

Referenced by append(), and expand().

00087   {
00088     bool rslt;
00089     T *old_array = pArray;
00090     size_t new_size = array_size * 2;
00091     
00092     pArray = new T [ new_size ];
00093     if (pArray != 0) {
00094       rslt = true;
00095       for (int i = 0; i < array_size; i++) {
00096         pArray[i] = old_array[i];
00097       }
00098     
00099       delete [] old_array;
00100     
00101       array_size = new_size;
00102     }
00103     else {
00104       rslt = false;
00105     }
00106 
00107     return rslt;
00108   } // twice


Member Data Documentation

template<class T>
size_t GrowableArray<T>::array_size [private]
 

Array size (always <= num_elem).

Definition at line 75 of file grow_array.h.

template<class T>
size_t GrowableArray<T>::num_elem [private]
 

number of data elements.

Definition at line 73 of file grow_array.h.

template<class T>
T * GrowableArray<T>::pArray [private]
 

Definition at line 76 of file grow_array.h.


The documentation for this class was generated from the following file:
Generated at Tue May 27 21:56:17 2003 for Wavelet compression, determinism and time series forecasting by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001