#include <grow_array.h>
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... | |
T | 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 |
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:
Definition at line 69 of file grow_array.h.
|
Definition at line 71 of file grow_array.h. 00071 { StartArraySize = 128 } bogus; |
|
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 |
|
destructor.
Definition at line 120 of file grow_array.h. 00121 { 00122 if (pArray != NULL) { 00123 delete [] pArray; 00124 } 00125 } // GrowableArray destructor |
|
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 |
|
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 |
|
Get a pointer to the internal data array.
Definition at line 152 of file grow_array.h. 00152 { return pArray; } |
|
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; } |
|
RHS [] operator.
Definition at line 145 of file grow_array.h. 00146 { 00147 assert( i < num_elem ); 00148 return pArray[ i ]; 00149 } |
|
LHS [] operator.
Definition at line 138 of file grow_array.h. 00139 { 00140 assert( i < num_elem ); 00141 return pArray[ i ]; 00142 } |
|
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 } |
|
set array to zero length.
Definition at line 132 of file grow_array.h. 00133 { 00134 num_elem = 0; 00135 } |
|
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 |
|
Array size (always <= num_elem).
Definition at line 75 of file grow_array.h. |
|
number of data elements.
Definition at line 73 of file grow_array.h. |
|
Definition at line 76 of file grow_array.h. |