C4 Engine
C4 Engine API Documentation

class Array

Defined in:  TSArray.h
The Array class represents a dynamically resizable array of objects for which any entry can be accessed in constant time.
Definition

template <typename type, int32 baseCount = 0> class Array final : public ImmutableArray<type>

Member Functions
Array::GetArrayElementCount Returns the current size of an array.
Array::SetArrayElementCount Sets the current size of an array.
Array::AppendArrayElement Adds an object to the end of an array.
Array::InsertArrayElement Inserts an object into an array.
Array::RemoveArrayElement Removes an object from an array.
Array::RemoveLastArrayElement Removes the last object from an array.
Array::ClearArray Removes all objects from an array.
Array::PurgeArray Removes all objects from an array and deallocates storage.
Array::FindArrayElementIndex Finds a specific element in an array.
Template Parameters
type The type of the class that can be stored in the array.
baseCount The minimum number of array elements for which storage is available inside the Array object itself.
Constructor

explicit Array(int32 count = 0);

Parameters
count The number of array elements for which space is initially reserved in the array's storage.
Description
The Array class represents a homogeneous array of objects whose type is given by the type template parameter. Upon construction, the initial size of the array is zero, but space is reserved for the number of objects given by the count parameter. The array is stored contiguously in memory, allowing constant-time random access to its elements.

As elements are added to the array (using the Array::AppendArrayElement function), the storage size is automatically increased to a size somewhat larger than that needed to store the new element. The cost of adding an element is thus amortized linear time.

If the baseCount template parameter is zero (the default), then storage space for the array elements is always allocated on the heap separately from the Array object. If the value of baseCount is greater than zero, then space for that number of array elements is built into the structure of the Array object so that no separate allocations need to be made until the size of the array exceeds the value of baseCount.

The count parameter can only be specified if the baseCount template parameter is zero.

An Array object can be implicitly converted to a pointer to its first element. This allows the use of the [] operator to access individual elements of the array.

It is possible to iterate over the elements of an array using a range-based for loop. This is illustrated by the following code, where array is a variable of type Array<type>.
for (type& element : array)
{
    ...
}
Base Classes
ImmutableArray<type> Used internally.
See Also

List