#include <grow_array.h>
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... | |
T | 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... |
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:
Elements are added to the end of the array via the append function.
Elements in the array can be accessed via the [] operator.
If the elements in the array are dynamicly allocated, the user is responsible for deallocating these elements.
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]; }
T | the type of the array elements |
|
The GrowableArray initially starts with array_size = StartArraySize.
00095 { StartArraySize = 128 } bogus; |
|
00142 { 00143 pArray = new T[ StartArraySize ]; 00144 num_elem = 0; 00145 array_size = StartArraySize; 00146 } // GrowableArray constructor |
|
00149 { 00150 if (pArray != NULL) { 00151 delete [] pArray; 00152 } 00153 } // GrowableArray destructor |
|
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 |
|
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 |
|
00179 { return pArray; } |
|
00156 { return num_elem; } |
|
RHS [] operator.
00173 { 00174 assert( i < num_elem ); 00175 return pArray[ i ]; 00176 } |
|
LHS [] operator.
00166 { 00167 assert( i < num_elem ); 00168 return pArray[ i ]; 00169 } |
|
Remove one item from the end of the array.
00220 { 00221 if (num_elem > 0) 00222 num_elem--; 00223 } |
|
set array to zero length.
00160 { 00161 num_elem = 0; 00162 } |
|
Double the amount of memory allocated for the array.
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 |
|
Number of possible slots for data elements.
|
|
Number of data elements in the array.
|
|
internal data storage array.
|