FlexibleArray< T, COUNT > Struct Template Reference

#include <flexible_array.h>

Detailed Description

template<typename T, typename COUNT = Int>
struct maxon::FlexibleArray< T, COUNT >

FlexibleArray can be used instead of e.g. BaseArray when you don't need dynamic reallocation but make sure in advance to provide enough memory. The class itself uses no allocation, the memory for the count and all elements is within the class itself. This leads to better performance due to improved cache locality.

There are two typical use cases:

  • You can add a FlexibleArray as the last member of a class which is dynamically allocated. Then when you allocate memory for an object of that class, you have to make sure that the allocation adds enough memory at the end to hold all array elements. The GetSize function helps to compute the required memory size.
  • You can allocate and construct a stand-alone FlexibleArray with the Create functions.
    Template Parameters
    TType of the array elements.
    COUNTThe class to use for the count member. Int by default.

Public Types

using ValueType = T
 
using Iterator = BaseIterator< FlexibleArray, false >
 
using ConstIterator = BaseIterator< const FlexibleArray, false >
 

Public Member Functions

 FlexibleArray (Int cnt)
 
 FlexibleArray (const Block< const T > &src)
 
 FlexibleArray (const MoveBlock< T > &src)
 
 FlexibleArray (Int cnt, ENUM_DONT_INITIALIZE)
 
 ~FlexibleArray ()
 
 MAXON_DISALLOW_COPY_AND_ASSIGN (FlexibleArray)
 
Block< const T > ToBlock () const
 
Block< T > ToBlock ()
 
 operator Block< const T > () const
 
 operator Block< T > ()
 
Block< const T > Slice (Int start, Int end) const
 
Block< T > Slice (Int start, Int end)
 
Block< const T > Slice (Int start) const
 
Block< T > Slice (Int start)
 
Int GetCount () const
 
Bool IsEmpty () const
 
Bool IsPopulated () const
 
void Shrink (Int cnt)
 
void Erase (Int index)
 
T & operator[] (Int index)
 
const T & operator[] (Int index) const
 
const T * GetFirst () const
 
T * GetFirst ()
 
const T * GetLast () const
 
T * GetLast ()
 
Iterator Begin ()
 
Iterator End ()
 
ConstIterator Begin () const
 
ConstIterator End () const
 
Bool operator== (const FlexibleArray &other) const
 
Bool operator!= (const FlexibleArray &other) const
 
HashInt GetHashCode () const
 
String ToString (const FormatStatement *fs=nullptr) const
 
const COUNT & PrivateGetCount () const
 
COUNT & PrivateGetCount ()
 

Static Public Member Functions

template<typename C = FlexibleArray>
static Int GetSize (Int cnt)
 
static ResultPtr< FlexibleArrayCreate (Int cnt)
 
static ResultPtr< FlexibleArrayCreate (Int cnt, Int capacity)
 
static ResultPtr< FlexibleArrayCreate (const Block< const T > &values)
 
static ResultPtr< FlexibleArrayCreate (const MoveBlock< T > &values)
 
static MAXON_ATTRIBUTE_FORCE_INLINE const FlexibleArrayNullValue ()
 

Private Attributes

COUNT _count
 
_elements []
 

Member Typedef Documentation

◆ ValueType

using ValueType = T

The element type.

◆ Iterator

◆ ConstIterator

using ConstIterator = BaseIterator<const FlexibleArray, false>

Constructor & Destructor Documentation

◆ FlexibleArray() [1/4]

FlexibleArray ( Int  cnt)
explicit

Constructs a FlexibleArray with cnt elements. All elements will be default-initialized. The storage memory used for the constructor call needs to be large enough to hold those elements.

Parameters
[in]cntNumber of elements.

◆ FlexibleArray() [2/4]

FlexibleArray ( const Block< const T > &  src)
explicit

Constructs a FlexibleArray with a copy of src. All elements will be copied from src. The storage memory used for the constructor call needs to be large enough to hold those elements.

Parameters
[in]srcA block of elements to copy.

◆ FlexibleArray() [3/4]

FlexibleArray ( const MoveBlock< T > &  src)
explicit

Constructs a FlexibleArray from src. All elements will be moved from src. The storage memory used for the constructor call needs to be large enough to hold those elements.

Parameters
[in]srcA block of elements to move.

◆ FlexibleArray() [4/4]

Constructs a FlexibleArray with cnt elements, but without any initialization of the elements. The storage memory used for the constructor call needs to be large enough to hold those elements.

Parameters
[in]cntNumber of elements.

◆ ~FlexibleArray()

Member Function Documentation

◆ MAXON_DISALLOW_COPY_AND_ASSIGN()

MAXON_DISALLOW_COPY_AND_ASSIGN ( FlexibleArray< T, COUNT >  )

◆ ToBlock() [1/2]

Block<const T> ToBlock ( ) const

Returns a Block<const T> which represents the elements of the array.

Returns
The content of this array as a block.

◆ ToBlock() [2/2]

Block<T> ToBlock ( )

Returns a Block<T> which represents the elements of the array.

Returns
The content of this array as a block.

◆ operator Block< const T >()

operator Block< const T > ( ) const

Converts this FlexibleArray to a Block<const T> which represents the elements of the array.

Returns
The content of this array as a block.

◆ operator Block< T >()

operator Block< T > ( )

Converts this FlexibleArray to a Block<T> which represents the elements of the array.

Returns
The content of this array as a block.

◆ Slice() [1/4]

Block<const T> Slice ( Int  start,
Int  end 
) const

Returns a sub block of this array with contains the elements from the given start index up to but not including the given end index.

Parameters
[in]startStart index of the sub block.
[in]endEnd index of the sub block. The end index is exclusive.
Returns
Sub block with elements from start up to end.

◆ Slice() [2/4]

Block<T> Slice ( Int  start,
Int  end 
)

Returns a sub block of this array with contains the elements from the given start index up to but not including the given end index.

Parameters
[in]startStart index of the sub block.
[in]endEnd index of the sub block. The end index is exclusive.
Returns
Sub block with elements from start up to end.

◆ Slice() [3/4]

Block<const T> Slice ( Int  start) const

Returns a sub block of this array with contains the elements from the given start index up to the end of the block.

Parameters
[in]startStart index of the sub block.
Returns
Sub block starting at start.

◆ Slice() [4/4]

Block<T> Slice ( Int  start)

Returns a sub block of this array with contains the elements from the given start index up to the end of the block.

Parameters
[in]startStart index of the sub block.
Returns
Sub block starting at start.

◆ GetCount()

Int GetCount ( ) const

Gets the number of array elements.

Returns
Number of array elements.

◆ IsEmpty()

Bool IsEmpty ( ) const

Checks if the array is empty. This is the same as {GetCount() == 0}.

Returns
True if the array does not contain any elements.

◆ IsPopulated()

Bool IsPopulated ( ) const

Checks if the array is populated. This is the same as {GetCount() != 0}.

Returns
True if the array contains any elements.

◆ Shrink()

void Shrink ( Int  cnt)

Shrinks this array by cnt elements. These are removed (and destructed) from the end.

Parameters
[in]cntNumber of elements to remove.

◆ Erase()

void Erase ( Int  index)

Erases the element at index. Elements behind index are moved by one.

Parameters
[in]indexIndex of the element to remove.

◆ GetSize()

static Int GetSize ( Int  cnt)
static

Computes the required size for an allocation of class C when C has FlexibleArray as last member and cnt element shall be held in the array.

Template Parameters
CClass with a FlexibleArray as last member.
Parameters
[in]cntNumber of elements to be held in the FlexibleArray.

◆ Create() [1/4]

static ResultPtr<FlexibleArray> Create ( Int  cnt)
static

Creates a FlexibleArray of the given size in dynamic memory. The elements are default-initialized.

Parameters
[in]cntNumber of elements to be held in the FlexibleArray.
Returns
A newly allocated array of the given size. The caller owns the array.

◆ Create() [2/4]

static ResultPtr<FlexibleArray> Create ( Int  cnt,
Int  capacity 
)
static

Creates a FlexibleArray of the given size in dynamic memory. The elements are default-initialized.

Parameters
[in]cntInitial number of elements in the FlexibleArray.
[in]capacityMaximum number of elements to be held in the FlexibleArray.
Returns
A newly allocated array of the given size. The caller owns the array.

◆ Create() [3/4]

static ResultPtr<FlexibleArray> Create ( const Block< const T > &  values)
static

Creates a FlexibleArray of the given content in dynamic memory. All elements are copied from the values block.

Parameters
[in]valuesA block of initial values for the elements of the FlexibleArray.
Returns
A newly allocated array with the given content. The caller owns the array.

◆ Create() [4/4]

static ResultPtr<FlexibleArray> Create ( const MoveBlock< T > &  values)
static

Creates a FlexibleArray of the given content in dynamic memory. All elements are moved from the values block.

Parameters
[in]valuesA block of initial values for the elements of the FlexibleArray.
Returns
A newly allocated array with the given content. The caller owns the array.

◆ operator[]() [1/2]

T& operator[] ( Int  index)

Returns the element at the given index of this array.

Parameters
[in]indexIndex into the array.
Returns
Reference to the value at index.

◆ operator[]() [2/2]

const T& operator[] ( Int  index) const

Returns the element at the given index of this array.

Parameters
[in]indexIndex into the array.
Returns
Reference to the value at index.

◆ GetFirst() [1/2]

const T* GetFirst ( ) const

Returns the first element of this array. Even for an empty array this will return a non-null pointer.

Returns
Pointer to the first element of this array.

◆ GetFirst() [2/2]

T* GetFirst ( )

Returns the first element of this array. Even for an empty array this will return a non-null pointer.

Returns
Pointer to the first element of this array.

◆ GetLast() [1/2]

const T* GetLast ( ) const

Returns the last element of this array. For an empty array this will return nullptr.

Returns
Pointer to the last element of this array, or nullptr for an empty array.

◆ GetLast() [2/2]

T* GetLast ( )

Returns the last element of this array. For an empty array this will return nullptr.

Returns
Pointer to the last element of this array, or nullptr for an empty array.

◆ Begin() [1/2]

Iterator Begin ( )

◆ End() [1/2]

Iterator End ( )

◆ Begin() [2/2]

ConstIterator Begin ( ) const

◆ End() [2/2]

ConstIterator End ( ) const

◆ operator==()

Bool operator== ( const FlexibleArray< T, COUNT > &  other) const

Checks if this @CLASS equals other.

Parameters
[in]otherAnother @CLASS.
Returns
True if this equals other, false otherwise.

◆ operator!=()

Bool operator!= ( const FlexibleArray< T, COUNT > &  other) const

Checks if this @CLASS is not equal to other.

Parameters
[in]otherAnother @CLASS.
Returns
True if this is not equal to other, false otherwise.

◆ GetHashCode()

HashInt GetHashCode ( ) const

Returns the hash code of this @CLASS.

Returns
Hash code of this @CLASS.

◆ ToString()

String ToString ( const FormatStatement fs = nullptr) const

Returns a String representation of this @CLASS.

Parameters
[in]formatStatementNullptr or additional formatting instruction.
Returns
String representation of this @CLASS.

◆ NullValue()

static MAXON_ATTRIBUTE_FORCE_INLINE const FlexibleArray& NullValue ( )
static

◆ PrivateGetCount() [1/2]

const COUNT& PrivateGetCount ( ) const

◆ PrivateGetCount() [2/2]

COUNT& PrivateGetCount ( )

Member Data Documentation

◆ _count

COUNT _count
private

◆ _elements

T _elements[]
private