BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > Class Template Reference

#include <basearray.h>

Inheritance diagram for BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR >:

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 .

Classes

class  IteratorTemplate
 

Public Types

typedef T ValueType
 
typedef ALLOCATOR AllocatorType
 
typedef IteratorTemplate< false > Iterator
 
typedef IteratorTemplate< true > ConstIterator
 

Public Member Functions

 BaseArray ()
 
 BaseArray (const ALLOCATOR &a)
 
 ~BaseArray ()
 
 BaseArray (BaseArray &&src)
 
BaseArrayoperator= (BaseArray &&src)
 
void Reset ()
 
void Flush ()
 
Int GetCount () const
 
Int GetCapacityCount () const
 
const T & operator[] (Int idx) const
 
T & operator[] (Int idx)
 
T * Append ()
 
T * Append (const T &x)
 
T * Append (T &&x)
 
T * Insert (Int position)
 
Iterator Insert (Iterator position)
 
T * Insert (Int position, const T &x)
 
Iterator Insert (Iterator position, const T &x)
 
T * Insert (Int position, T &&x)
 
Iterator Insert (Iterator position, T &&x)
 
T * Insert (Int position, const T *x, Int insertCnt)
 
Iterator Insert (Iterator position, const T *x, Int insertCnt)
 
T * Erase (Int position, Int eraseCnt=1)
 
Iterator Erase (Iterator position, Int eraseCnt=1)
 
const T * GetFirst () const
 
T * GetFirst ()
 
const T * GetLast () const
 
T * GetLast ()
 
Bool Resize (Int newCnt, BASEARRAYRESIZEFLAGS resizeFlags=BASEARRAYRESIZEFLAGS_DEFAULT)
 
Bool Pop (T *dst=nullptr)
 
Int GetIndex (const T &x) const
 
Bool EnsureCapacity (Int requestedCapacity)
 
template<typename SourceArray >
Bool CopyFrom (const SourceArray &src, Bool fitToSize=true)
 
Bool CopyFrom (const BaseArray &src, Bool fitToSize)
 
Bool CopyFrom (const BaseArray &src)
 
void Swap (Iterator a, Iterator b)
 
ConstIterator Begin () const
 
ConstIterator begin () const
 
Iterator Begin ()
 
Iterator begin ()
 
ConstIterator End () const
 
ConstIterator end () const
 
Iterator End ()
 
Iterator end ()
 
Bool MoveAndShrink (BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &dst, Int position, Int moveCnt)
 
T * Disconnect (Int *dst=nullptr)
 
ALLOCATOR & GetAllocator ()
 
T * AppendWithoutConstructor (Int increment=1)
 
T * InsertWithoutConstructor (Int idx, Int increment=1)
 

Protected Member Functions

Bool FitToSize (Int newCapacity)
 

Protected Attributes

T * _ptr
 
Int _cnt
 
Int _capacity
 

Private Types

typedef BaseArrayHelper< T, ALLOCATOR, MEMFLAGS==BASEARRAYFLAGS_MOVEANDCOPYOBJECTSHelper
 

Private Member Functions

 BaseArray (const BaseArray &)
 
void operator= (const BaseArray &)
 

Member Typedef Documentation

§ Helper

typedef BaseArrayHelper<T, ALLOCATOR, MEMFLAGS == BASEARRAYFLAGS_MOVEANDCOPYOBJECTS> Helper
private

§ ValueType

typedef T ValueType

§ AllocatorType

typedef ALLOCATOR AllocatorType

§ Iterator

typedef IteratorTemplate<false> Iterator

Iterator allows read and write access to array elements.

§ ConstIterator

Iterator for read-only access to array elements.

Constructor & Destructor Documentation

§ BaseArray() [1/4]

BaseArray ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  )
private

§ BaseArray() [2/4]

BaseArray ( )

§ BaseArray() [3/4]

BaseArray ( const ALLOCATOR &  a)
explicit

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

§ ~BaseArray()

~BaseArray ( )

§ BaseArray() [4/4]

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

move constructor

Member Function Documentation

§ operator=() [1/2]

void operator= ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  )
private

§ operator=() [2/2]

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

move assignment operator

§ Reset()

void Reset ( void  )

Deletes all elements (calls destructors and frees memory).

§ Flush()

void Flush ( void  )

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

§ GetCount()

Int GetCount ( void  ) const

Gets the number of array elements.

Returns
Number of array elements.

§ GetCapacityCount()

Int GetCapacityCount ( ) const

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.

§ operator[]() [1/2]

const T& operator[] ( Int  idx) const

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.

§ operator[]() [2/2]

T& operator[] ( Int  idx)

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.

§ Append() [1/3]

T* Append ( )

Adds a new element at the end of the array.

Returns
Element pointer or nullptr (allocation failed).

§ Append() [2/3]

T* Append ( const T &  x)

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).

§ Append() [3/3]

T* Append ( T &&  x)

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).

§ Insert() [1/8]

T* Insert ( Int  position)

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).

§ Insert() [2/8]

Iterator Insert ( Iterator  position)

Inserts a new default element at iterator position.

Parameters
[in]positionInsert position.
Returns
Iterator for the new element (IsValid() == false if allocation failed).

§ Insert() [3/8]

T* Insert ( Int  position,
const T &  x 
)

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).

§ Insert() [4/8]

Iterator Insert ( Iterator  position,
const T &  x 
)

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).

§ Insert() [5/8]

T* Insert ( Int  position,
T &&  x 
)

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).

§ Insert() [6/8]

Iterator Insert ( Iterator  position,
T &&  x 
)

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).

§ Insert() [7/8]

T* Insert ( Int  position,
const T *  x,
Int  insertCnt 
)

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).

§ Insert() [8/8]

Iterator Insert ( Iterator  position,
const T *  x,
Int  insertCnt 
)

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).

§ Erase() [1/2]

T* Erase ( Int  position,
Int  eraseCnt = 1 
)

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. If position equals the number of elements after Erase() a valid pointer is returned but you are not allowed to access it. A nullptr is only returned on failure (position was out of bounds).

§ Erase() [2/2]

Iterator Erase ( Iterator  position,
Int  eraseCnt = 1 
)

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).

§ GetFirst() [1/2]

const T* GetFirst ( void  ) const

Returns the first element of the array.

Returns
Pointer to the first element (nullptr if the array is empty).

§ GetFirst() [2/2]

T* GetFirst ( void  )

Returns the first element of the array.

Returns
Pointer to the first element (nullptr if the array is empty).

§ GetLast() [1/2]

const T* GetLast ( void  ) const

Returns the last element of the array.

Returns
Pointer to the last element (nullptr if the array is empty).

§ GetLast() [2/2]

T* GetLast ( void  )

Returns the last element of the array.

Returns
Pointer to the last element (nullptr if the array is empty).

§ Resize()

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. 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.

§ Pop()

Bool Pop ( T *  dst = nullptr)

Deletes the last element.

Parameters
[out]dstNullptr or pointer to return value.
Returns
True if there was at least one element.

§ GetIndex()

Int GetIndex ( const T &  x) const

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).

§ EnsureCapacity()

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.

Parameters
[in]requestedCapacityThe desired internal capacity.
Returns
False if allocation failed.

§ CopyFrom() [1/3]

Bool CopyFrom ( const SourceArray &  src,
Bool  fitToSize = true 
)

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.

§ CopyFrom() [2/3]

Bool CopyFrom ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  src,
Bool  fitToSize 
)

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.

§ CopyFrom() [3/3]

Bool CopyFrom ( const BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  src)

Default CopyFrom() method.

Parameters
[in]srcSource array.
Returns
True if successful.

§ Swap()

void Swap ( Iterator  a,
Iterator  b 
)

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.

§ Begin() [1/2]

ConstIterator Begin ( ) const

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).

§ begin() [1/2]

ConstIterator begin ( ) const

§ Begin() [2/2]

Iterator Begin ( )

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).

§ begin() [2/2]

Iterator begin ( )

§ End() [1/2]

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.

Returns
Iterator for the array end (points behind the last element).

§ end() [1/2]

ConstIterator end ( ) const

§ End() [2/2]

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.

Returns
Iterator for the array end (points behind the last element).

§ end() [2/2]

Iterator end ( )

§ MoveAndShrink()

Bool MoveAndShrink ( BaseArray< T, MINCHUNKSIZE, MEMFLAGS, ALLOCATOR > &  dst,
Int  position,
Int  moveCnt 
)

§ Disconnect()

T* Disconnect ( Int dst = nullptr)

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

Parameters
[out]dstNullptr or pointer to return value.
Returns
Allocator reference.

§ GetAllocator()

ALLOCATOR& GetAllocator ( )

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.

§ AppendWithoutConstructor()

T* AppendWithoutConstructor ( Int  increment = 1)

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.

§ InsertWithoutConstructor()

T* InsertWithoutConstructor ( Int  idx,
Int  increment = 1 
)

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.

§ FitToSize()

Bool FitToSize ( Int  newCapacity)
protected

Member Data Documentation

§ _ptr

T* _ptr
protected

§ _cnt

Int _cnt
protected

§ _capacity

Int _capacity
protected