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

GrowableArray Class Template Reference

A Generic Growable Array. More...

#include <grow_array.h>

List of all members.

Public Methods

 GrowableArray ()
 ~GrowableArray ()
const size_t length (void) const
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
void append (T item)
 append an item to the end of the array. More...

void expand (size_t amount)
 Expand the array by amount. More...

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


Private Types

enum  bogus { StartArraySize = 128 }
 The GrowableArray initially starts with array_size = StartArraySize. More...


Private Methods

bool twice ()
 Double the amount of memory allocated for the array. More...


Private Attributes

size_t num_elem
 Number of data elements in the array. More...

size_t array_size
 Number of possible slots for data elements. More...

T* pArray
 internal data storage array. More...


Detailed Description

template<class T> class GrowableArray

A Generic Growable Array.

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.

Example:


  GrowableArray< int > iArray;
  const int numInts = 20;
  
  for (int i = 0; i < numInts; i++)
    {
      iArray.append( i );
    }
  
  const int len = iArray.length();
  int sum = 0;
  
  for (i = 0; i < len; i++) {
    sum = sum + iArray[i];
  }

Parameters:
T   the type of the array elements


Member Enumeration Documentation

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

The GrowableArray initially starts with array_size = StartArraySize.

Enumeration values:
StartArraySize  
00095 { StartArraySize = 128 } bogus;


Constructor & Destructor Documentation

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

00142   {
00143     pArray = new T[ StartArraySize ];
00144     num_elem = 0;
00145     array_size = StartArraySize;
00146   } // GrowableArray constructor

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

00149   {
00150     if (pArray != NULL) {
00151       delete [] pArray;
00152     }
00153   } // GrowableArray destructor


Member Function Documentation

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

append an item to the end of the array.

00183   {
00184 
00185     if (num_elem == array_size) {
00186       bool allocOK = twice();
00187       assert( allocOK );
00188     }
00189 
00190     pArray[ num_elem ] = item;
00191     num_elem++;
00192   } // append

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

Expand the array by amount.

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.

00207   {
00208     bool allocOK = true;
00209 
00210     while (allocOK && num_elem + amount >= array_size) {
00211       allocOK = twice();
00212       assert( allocOK );
00213     }
00214     num_elem += amount;
00215   } // expand

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

Returns:
a pointer to the internal data
00179 { return pArray; }

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

Returns:
the amount of data in the array (number of elements)
00156 { return num_elem; }

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

RHS [] operator.

00173   {
00174     assert( i < num_elem );
00175     return pArray[ i ];
00176   }

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

LHS [] operator.

00166   {
00167     assert( i < num_elem );
00168     return pArray[ i ];
00169   }

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

Remove one item from the end of the array.

00220   {
00221     if (num_elem > 0)
00222       num_elem--;
00223   }

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

set array to zero length.

00160   {
00161     num_elem = 0;
00162   }

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

Double the amount of memory allocated for the array.

Returns:
true if memory allocation succeeded, false otherwise.
00115   {
00116     bool rslt;
00117 
00118     T *old_array = pArray;
00119     size_t new_size = array_size * 2;
00120     
00121     pArray = new T [ new_size ];
00122     if (pArray != 0) {
00123       rslt = true;
00124       for (int i = 0; i < array_size; i++) {
00125         pArray[i] = old_array[i];
00126       }
00127     
00128       delete [] old_array;
00129     
00130       array_size = new_size;
00131     }
00132     else {
00133       rslt = false;
00134     }
00135 
00136     return rslt;
00137   } // twice


Member Data Documentation

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

Number of possible slots for data elements.

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

Number of data elements in the array.

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

internal data storage array.


The documentation for this class was generated from the following file:
Generated at Thu Jun 14 21:06:18 2001 for C++ Templates: the power, the swamp by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001