maxon
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > Class Template Reference

Basic array template. More...

#include <basearray.h>

Inherits ALLOCATOR.

Classes

class  IteratorTemplate
 The BaseArray iterator internally is a pointer and using it to iterate over an array or parts of it is as efficient as using a real pointer (for more ease of use you may want to invoke this via AutoIterator). More...
 

Public Types

typedef T ValueType
 
typedef ALLOCATOR AllocatorType
 
typedef IteratorTemplate< false > Iterator
 Iterator allows read and write access to array elements. More...
 
typedef IteratorTemplate< true > ConstIterator
 Iterator for read-only access to array elements. More...
 

Public Member Functions

 BaseArray (const ALLOCATOR &a)
 this constructor has to be used if an array should use a custom allocator with member variables More...
 
 BaseArray (BaseArray &&src)
 move constructor More...
 
BaseArrayoperator= (BaseArray &&)
 move assignment operator More...
 
void Reset ()
 Deletes all elements (calls destructors and frees memory) More...
 
void Flush ()
 Deletes all elements, but doesn't free memory (calls destructors though) More...
 
Int GetCount () const
 Gets the number of array elements. More...
 
Int GetCapacityCount () const
 Gets the number of elements for which memory has been allocated (this is usually bigger than GetCount()) More...
 
const T & operator[] (Int idx) const
 Array (subscript) operator for const objects. More...
 
T & operator[] (Int idx)
 Array (subscript) operator for non-const objects. More...
 
T * Append ()
 Adds a new element at the end of the array. More...
 
T * Append (const T &x)
 Adds a new element at the end of the array and initializes it with a copy of x. More...
 
T * Append (T &&x)
 Adds a new element at the end of the array and moves the content of x to it. More...
 
T * Insert (Int position)
 Inserts a new default element at index position. More...
 
Iterator Insert (Iterator position)
 Inserts a new default element at iterator position. More...
 
T * Insert (Int position, const T &x)
 Inserts a new element at index position and initializes it with a copy of x. More...
 
Iterator Insert (Iterator position, const T &x)
 Inserts a new element at iterator position and initializes it with a copy of x. More...
 
T * Insert (Int position, T &&x)
 Inserts a new element at index position and moves the content of x to it. More...
 
Iterator Insert (Iterator position, T &&x)
 Inserts a new element at iterator position and moves the content of x to it. More...
 
T * Insert (Int position, const T *x, Int insertCnt)
 Inserts new elements at index position (all elements from position on are moved by insertCnt) More...
 
Iterator Insert (Iterator position, const T *x, Int insertCnt)
 Inserts new elements at iterator position (all elements from position on are moved by insertCnt) More...
 
T * Erase (Int position, Int eraseCnt=1)
 Erases (removes and deletes) elements. More...
 
Iterator Erase (Iterator position, Int eraseCnt=1)
 Erases (removes and deletes) elements. More...
 
const T * GetFirst () const
 Returns the first element of the array. More...
 
T * GetFirst ()
 Returns the first element of the array. More...
 
const T * GetLast () const
 Returns the last element of the array. More...
 
T * GetLast ()
 Returns the last element of the array. More...
 
Bool Resize (Int newCnt, BASEARRAYRESIZEFLAGS resizeFlags=BASEARRAYRESIZEFLAGS_DEFAULT)
 Resizes the array to contain newCnt elements If newCnt is smaller than GetCount() all extra elements are being deleted. More...
 
Bool Pop (T *dst=nullptr)
 Deletes the last element. More...
 
Int GetIndex (const T &x) const
 Gets the index of the element. More...
 
Bool EnsureCapacity (Int requestedCapacity)
 Increases the internal capacity of this array (if necessary) so that it can hold at least the given number of elements without further memory allocations. More...
 
template<typename SourceArray >
Bool CopyFrom (const SourceArray &src, Bool fitToSize=true)
 Copies an array. More...
 
Bool CopyFrom (const BaseArray &src, Bool fitToSize)
 Copies an array: specialization for BaseArray. More...
 
Bool CopyFrom (const BaseArray &src)
 Default CopyFrom() method. More...
 
void Swap (Iterator a, Iterator b)
 Swaps elements a and b (equivalent to global Swap(array[a], array[b]) More...
 
ConstIterator Begin () const
 Gets an iterator for the first element When you modify the array Begin() will change, it is not a constant value. More...
 
Iterator Begin ()
 Gets an iterator for the first element When you modify the array Begin() will change, it is not a constant value. More...
 
ConstIterator End () const
 Gets an iterator for the end (End() - 1 is the last element if the array is not empty) When you modify the array End() will change, it is not a constant value. More...
 
Iterator End ()
 Gets an iterator for the end (End() - 1 is the last element if the array is not empty) When you modify the array End() will change, it is not a constant value. More...
 
Bool MoveAndShrink (BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &dst, Int position, Int moveCnt)
 
T * Disconnect (Int *dst=nullptr)
 Disconnects the array's memory buffer and returns its address and size. More...
 
ALLOCATOR & GetAllocator ()
 Returns the allocator as reference. More...
 
T * AppendWithoutConstructor (Int increment=1)
 BaseArray specific: Appends uninitialized element(s) at the end of the array. More...
 
T * InsertWithoutConstructor (Int idx, Int increment=1)
 BaseArray specific: Inserts uninitialized element(s) at the specified index. More...
 

Protected Member Functions

Bool FitToSize (Int newCapacity)
 

Protected Attributes

T * _ptr
 
Int _cnt
 
Int _capacity
 

Detailed Description

template<typename T, Int MINCHUNKSIZE = BASEARRAY_DEFAULT_CHUNK_SIZE, BASEARRAYFLAGS MEMFLAGS = BASEARRAYFLAGS_0, typename ALLOCATOR = DefaultAllocator>
class maxon::BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR >

Basic array template.

The array consists of one continuous block of memory. The block will have a minimum size of MINCHUNKSIZE elements of type T as soon as the first element is added.

The elements may be copied and change their memory address when the array grows. If your objects cannot be copied or need a constant address use the BlockArray template.

If you need a specific alignment you may have to use a non-default allocator. C4D's allocators align on a 8 byte boundary for 32 bit code and on a 16 byte boundary for 64 bit code. If you need a different alignment or custom resize behaviour (default is grow rate of 1.5) you may use your own allocator instead.

Template Parameters
TType of the array elements.
MINCHUNKSIZEThe minimum number of elements upon array creation.
MEMFLAGSUse BASEARRAYFLAGS_0 unless you know the object can be moved and/or copied.
ALLOCATORClass for memory allocation.
Note
Note that the array element class has special requirements regarding copy and move constructors .

Member Typedef Documentation

Iterator for read-only access to array elements.

typedef IteratorTemplate<false> Iterator

Iterator allows read and write access to array elements.

Constructor & Destructor Documentation

BaseArray ( const ALLOCATOR &  a)
inlineexplicit

this constructor has to be used if an array should use a custom allocator with member variables

BaseArray ( BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &&  src)
inline

move constructor

Member Function Documentation

T* Append ( )
inline

Adds a new element at the end of the array.

Returns
Element pointer or nullptr (allocation failed)
T* Append ( const T &  x)
inline

Adds a new element at the end of the array and initializes it with a copy of x.

Parameters
[in]xValue to be copied.
Returns
Element pointer or nullptr (allocation failed)
T* Append ( T &&  x)
inline

Adds a new element at the end of the array and moves the content of x to it.

Parameters
[in]xValue to be moved.
Returns
Element pointer or nullptr (allocation failed)
T* AppendWithoutConstructor ( Int  increment = 1)
inline

BaseArray specific: Appends uninitialized element(s) at the end of the array.

This does not call the constructor! Use Append() unless you deal with PODs that shall not be initialized for a very good reason!

Parameters
[in]incrementNumber of elements to be appended.
Returns
Pointer to the element (the constructor hasn't been called yet) or nullptr.
ConstIterator Begin ( ) const
inline

Gets an iterator for the first element When you modify the array Begin() will change, it is not a constant value.

Returns
Iterator for the first element (equal to End() if the array is empty)
Iterator Begin ( )
inline

Gets an iterator for the first element When you modify the array Begin() will change, it is not a constant value.

Returns
Iterator for the first element (equal to End() if the array is empty)
Bool CopyFrom ( const SourceArray &  src,
Bool  fitToSize = true 
)
inline

Copies an array.

Parameters
[in]srcSource array.
[in]fitToSizeIf true the array will be copied to a new memory block that is as small as possible to hold the data.
Returns
True if successful.
Bool CopyFrom ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  src,
Bool  fitToSize 
)
inline

Copies an array: specialization for BaseArray.

Parameters
[in]srcSource array.
[in]fitToSizeIf true the array will be copied to a new memory block that is as small as possible to hold the data.
Returns
True if successful.
Bool CopyFrom ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  src)
inline

Default CopyFrom() method.

Parameters
[in]srcSource array.
Returns
True if successful.
T* Disconnect ( Int *  dst = nullptr)
inline

Disconnects the array's memory buffer and returns its address and size.

Parameters
[out]dstNullptr or pointer to return value.
Returns
Allocator reference.
ConstIterator End ( ) const
inline

Gets an iterator for the end (End() - 1 is the last element if the array is not empty) When you modify the array End() will change, it is not a constant value.

Returns
Iterator for the array end (this is behind the last element)
Iterator End ( )
inline

Gets an iterator for the end (End() - 1 is the last element if the array is not empty) When you modify the array End() will change, it is not a constant value.

Returns
Iterator for the array end (this is behind the last element)
Bool EnsureCapacity ( Int  requestedCapacity)
inline

Increases the internal capacity of this array (if necessary) so that it can hold at least the given number of elements without further memory allocations.

Parameters
[in]requestedCapacityThe desired internal capacity.
Returns
False if allocation failed.
T* Erase ( Int  position,
Int  eraseCnt = 1 
)
inline

Erases (removes and deletes) elements.

Parameters
[in]positionErase index (Erase() will fail if out of bounds and return nullptr)
[in]eraseCntNumber of elements to be erased (if eraseCnt is higher than what is available at position Erase() will succeed, but remove only the number of available elements)
Returns
Pointer to the element that is now at position or nullptr (no more element at position, either because position is out of bounds or the last element was erased)
Iterator Erase ( Iterator  position,
Int  eraseCnt = 1 
)
inline

Erases (removes and deletes) elements.

Parameters
[in]positionErase position.
[in]eraseCntNumber of elements to be erased (if eraseCnt is higher than what is available at position Erase() will succeed, but remove only the number of available elements)
Returns
Iterator for the element that is now at position (IsValid() == false if something failed)
void Flush ( )
inline

Deletes all elements, but doesn't free memory (calls destructors though)

ALLOCATOR& GetAllocator ( )
inline

Returns the allocator as reference.

Typically this is used by the arrays and other base classes when multiple of them are "stitched" together as one big object all shall use one main allocator.

Returns
Allocator reference.
Int GetCapacityCount ( ) const
inline

Gets the number of elements for which memory has been allocated (this is usually bigger than GetCount())

Returns
Number of array elements for which memory has been allocated.
Int GetCount ( ) const
inline

Gets the number of array elements.

Returns
Number of array elements.
const T* GetFirst ( ) const
inline

Returns the first element of the array.

Returns
Pointer to the first element (null if the array is empty)
T* GetFirst ( )
inline

Returns the first element of the array.

Returns
Pointer to the first element (null if the array is empty)
Int GetIndex ( const T &  x) const
inline

Gets the index of the element.

The element must be part of the array, otherwise (e.g. if x is a copy of an array element) InvalidArrayIndex will be returned.

Returns
Index of element or InvalidArrayIndex (not element of this)
const T* GetLast ( ) const
inline

Returns the last element of the array.

Returns
Pointer to the last element (null if the array is empty)
T* GetLast ( )
inline

Returns the last element of the array.

Returns
Pointer to the last element (null if the array is empty)
T* Insert ( Int  position)
inline

Inserts a new default element at index position.

Parameters
[in]positionInsert index (the array size will increase and the existing elements are moved)
Returns
Element pointer or nullptr (allocation failed or position out of boundaries)
Iterator Insert ( Iterator  position)
inline

Inserts a new default element at iterator position.

Parameters
[in]positionInsert position.
Returns
Iterator for the new element (IsValid() == false if allocation failed)
T* Insert ( Int  position,
const T &  x 
)
inline

Inserts a new element at index position and initializes it with a copy of x.

Parameters
[in]positionInsert index (the array size will increase and the existing elements are moved)
[in]xValue to be copied.
Returns
Element pointer or nullptr (allocation failed or position out of boundaries)
Iterator Insert ( Iterator  position,
const T &  x 
)
inline

Inserts a new element at iterator position and initializes it with a copy of x.

Parameters
[in]positionInsert position.
[in]xValue to be copied.
Returns
Iterator for the new element (IsValid() == false if allocation failed)
T* Insert ( Int  position,
T &&  x 
)
inline

Inserts a new element at index position and moves the content of x to it.

Parameters
[in]positionInsert index (the array size will increase and the existing elements are moved)
[in]xValue to be moved.
Returns
Element pointer or nullptr (allocation failed or position out of boundaries)
Iterator Insert ( Iterator  position,
T &&  x 
)
inline

Inserts a new element at iterator position and moves the content of x to it.

Parameters
[in]positionInsert position.
[in]xValue to be moved.
Returns
Iterator for the new element (IsValid() == false if allocation failed)
T* Insert ( Int  position,
const T *  x,
Int  insertCnt 
)
inline

Inserts new elements at index position (all elements from position on are moved by insertCnt)

Parameters
[in]positionInsert index (the array size will increase and the existing elements are moved)
[in]xArray with values to be copied or nullptr (in this case you have to call the constructor manually)
[in]insertCntNumber of elements to be inserted.
Returns
Element pointer or nullptr (allocation failed or position out of boundaries)
Iterator Insert ( Iterator  position,
const T *  x,
Int  insertCnt 
)
inline

Inserts new elements at iterator position (all elements from position on are moved by insertCnt)

Parameters
[in]positionInsert position.
[in]xArray with values to be copied or nullptr (in this case you have to call the constructor manually)
[in]insertCntNumber of elements to be inserted.
Returns
Iterator for the new element (IsValid() == false if allocation failed)
T* InsertWithoutConstructor ( Int  idx,
Int  increment = 1 
)
inline

BaseArray specific: Inserts uninitialized element(s) at the specified index.

This does not call the constructor! Use Insert() unless you deal with PODs that shall not be initialized for a very good reason!

Parameters
[in]idxIndex at which elements shall be inserted (0 <= idx <= cnt)
[in]incrementNumber of elements to be inserted.
Returns
Pointer to the element (the constructor hasn't been called yet) or nullptr.
BaseArray& operator= ( BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &&  )

move assignment operator

const T& operator[] ( Int  idx) const
inline

Array (subscript) operator for const objects.

Parameters
[in]idxElement index (if it's out of bounds you will get an error in debug code only, otherwise it will crash)
Returns
Array element.
T& operator[] ( Int  idx)
inline

Array (subscript) operator for non-const objects.

Parameters
[in]idxElement index (if it's out of bounds you will get an error in debug code only, otherwise it will crash)
Returns
Array element.
Bool Pop ( T *  dst = nullptr)
inline

Deletes the last element.

Parameters
[out]dstNullptr or pointer to return value.
Returns
True if successful.
void Reset ( )
inline

Deletes all elements (calls destructors and frees memory)

Bool Resize ( Int  newCnt,
BASEARRAYRESIZEFLAGS  resizeFlags = BASEARRAYRESIZEFLAGS_DEFAULT 
)
inline

Resizes the array to contain newCnt elements If newCnt is smaller than GetCount() all extra elements are being deleted.

If it is greater the array is expanded and the default constructor is called for new elements.

Parameters
[in]newCntNew array size.
[in]resizeFlagsBaseArray specific: see BASEARRAYRESIZEFLAGS.
Returns
False if allocation failed.
void Swap ( Iterator  a,
Iterator  b 
)
inline

Swaps elements a and b (equivalent to global Swap(array[a], array[b])

Parameters
[in]aPosition of element to be swapped.
[in]bPosition of element to be swapped.