vtdata library
|
Public Member Functions | |
vtArray (uint size=0) | |
vtArray (const vtArray< E > &) | |
virtual | ~vtArray () |
uint | GetSize () const |
uint | GetMaxSize () const |
bool | SetSize (uint) |
bool | SetMaxSize (uint) |
uint | GetElemSize () const |
E * | GetData () const |
void | FreeData () |
bool | IsEmpty () const |
E & | GetAt (uint i) const |
bool | SetAt (uint i, E) |
vtArray< E > & | operator= (const vtArray< E > &) |
E & | operator[] (uint i) |
const E & | operator[] (uint i) const |
void | Clear () |
bool | RemoveAt (uint i, int n=1) |
int | Append (const E &) |
int | Append (const vtArray< E > &) |
int | Find (const E &) const |
Protected Member Functions | |
virtual bool | Grow (uint) |
virtual void | DestructItems (uint first, uint last) |
Protected Attributes | |
uint | m_Size |
uint | m_MaxSize |
E * | m_Data |
An Array template which automatically grows as you add or set entities.
Note that construction and destruction is not done automatically if the entities are class objects. You can provide this destruction yourself by overriding the DestructItems method, but it is easier to use this template for objects with simple value semantics such as basic types (int, float..), structs, and pointers. If you do create a subclass like this:
note that you will need to provide not only a DestructItems() implementation, but also a destructor. This is because the default vtArray destructor is not smart enough to call your DestructItems() method (it will call the base DestructItems() instead, which does nothing).
A full working example is:
Creates and initializes an empty array (array with no elements).
size | number of elements data area should initially make room for (initial value of [MaxSize]). If zero, little initial space is allocated but the array will grow dynamically as space is needed. |
Creates and initializes an array from another (of the same type).
a | An array to copy from. |
|
inline |
Appends one element onto the end of the array. If the array is dynamically managed, it is enlarged to accomodate another element if necessary.
<v> | value of the new element |
Concatenates the contents of the source array into the destination array. The destination array is enlarged if possible. When an object array is appended, the objects are multiply referenced (not duplicated).
<src> | Source array containing elements to be appended |
void vtArray< E >::Clear | ( | ) |
Removes the elements in the array but not the array data area. An array is considered empty if it has no elements.
|
inlineprotectedvirtual |
Called by the array implementation when array items are deleted. The default implementation does not call the destructors for the array items. This will not work if your array elements do memory deallocation in their destructors.
Override this function to explicitly call the destructors properly if you need this functionality.
first | Index of first element to destroy |
last | Index of last element to destroy |
int vtArray< E >::Find | ( | const E & | elem | ) | const |
Compares each element of the array to the input element and returns the index of the first one that matches. Comparison of elements is done using operator== (which must be defined for your element class if you want to use Find).
<elem> | value of the element to match |
void vtArray< E >::FreeData | ( | ) |
Complete free the data held by this array's data.
|
inline |
Gets the i'th element of the array.
i | 0 based index of element to get. Like C++ arrays, these arrays do not check the range of the index so your program will crash if you supply a number less than zero or greater than the array size as an index. |
|
protectedvirtual |
Enlarge the array to accomodate growto elements. If the array can already hold this many elements, nothing is done. Otherwise, the data area of the array is enlarged (reallocating if necessary) so that growto contiguous elements can be stored.
growto | Number of elements the array should be able to hold after it has grown |
|
inline |
Determines whether an array has elements or not
bool vtArray< E >::RemoveAt | ( | uint | i, |
int | n = 1 |
||
) |
Removes the i'th element of the array. The following elements are shuffled up to eliminate the unused space.
i | Index of element to remove (0 based) |
n | Number of elements to remove (default 1) |
bool vtArray< E >::SetAt | ( | uint | i, |
E | val | ||
) |
Sets the i'th element of the array to the given value. The number of bytes copied is determined by the element size of the array specified at initialization time. If the array is not large enough, it is extended to become 1 1/2 times its current size.
i | Index of new element (0 based) |
val | Value of the new element. |
bool vtArray< E >::SetMaxSize | ( | uint | s | ) |
If the array is user-managed, MaxSize establishes the maximum number of elements that can be stored. If the array is dynamically managed by the system, setting the maximum size enlarges its data area to accomodate the required number of elements.
s | Current maximum size of array (number of elements its data area can hold) |
|
inline |
Set the current array size. If the array is dynamically managed, it will be enlarged to accomodate the new size. If not, the array size cannot be set beyond the current maximum size. When the array size is enlarged, we should call constructors for the new empty elements. We don't do that yet.
s | Current number of elements contained in array |