nCine 2025.06.r503-ff15d8d
A cross-platform 2D game engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
nctl::Array< T > Class Template Reference

A dynamic array based on templates that stores elements in the heap. More...

#include <Array.h>

Public Types

using Iterator = ArrayIterator< T, false >
 Iterator type.
 
using ConstIterator = ArrayIterator< T, true >
 Constant iterator type.
 
using ReverseIterator = nctl::ReverseIterator< Iterator >
 Reverse iterator type.
 
using ConstReverseIterator = nctl::ReverseIterator< ConstIterator >
 Reverse constant iterator type.
 

Public Member Functions

 Array ()
 Constructs an array without allocating memory.
 
 Array (unsigned int capacity)
 Constructs an array with explicit capacity.
 
 Array (unsigned int capacity, ArrayMode mode)
 Constructs an array with explicit capacity and the option for it to be fixed.
 
 Array (const Array &other)
 Copy constructor.
 
 Array (Array &&other)
 Move constructor.
 
Arrayoperator= (const Array &other)
 Assignment operator.
 
Arrayoperator= (Array &&other)
 Move assignment operator.
 
void swap (Array &first, Array &second)
 Swaps two arrays without copying their data.
 
Iterator begin ()
 Returns an iterator to the first element.
 
ReverseIterator rBegin ()
 Returns a reverse iterator to the last element.
 
Iterator end ()
 Returns an iterator to past the last element.
 
ReverseIterator rEnd ()
 Returns a reverse iterator to prior the first element.
 
ConstIterator begin () const
 Returns a constant iterator to the first element.
 
ConstReverseIterator rBegin () const
 Returns a constant reverse iterator to the last element.
 
ConstIterator end () const
 Returns a constant iterator to past the last lement.
 
ConstReverseIterator rEnd () const
 Returns a constant reverse iterator to prior the first element.
 
ConstIterator cBegin () const
 Returns a constant iterator to the first element.
 
ConstReverseIterator crBegin () const
 Returns a constant reverse iterator to the last element.
 
ConstIterator cEnd () const
 Returns a constant iterator to past the last lement.
 
ConstReverseIterator crEnd () const
 Returns a constant reverse iterator to prior the first element.
 
bool isEmpty () const
 Returns true if the array is empty.
 
unsigned int size () const
 Returns the array size.
 
unsigned int capacity () const
 Returns the array capacity.
 
void setSize (unsigned int newSize)
 Sets a new size for the array (allowing for "holes")
 
void setCapacity (unsigned int newCapacity)
 Sets a new capacity for the array (can be bigger or smaller than the current one)
 
void shrinkToFit ()
 Decreases the capacity to match the current size of the array.
 
void clear ()
 Clears the array.
 
const T & front () const
 Returns a constant reference to the first element in constant time.
 
T & front ()
 Returns a reference to the first element in constant time.
 
const T & back () const
 Returns a constant reference to the last element in constant time.
 
T & back ()
 Returns a reference to the last element in constant time.
 
void pushBack (const T &element)
 Appends a new element in constant time, the element is copied into the array.
 
void pushBack (T &&element)
 Appends a new element in constant time, the element is moved into the array.
 
template<typename... Args>
void emplaceBack (Args &&... args)
 Constructs a new element at the end of the array.
 
void popBack ()
 Removes the last element in constant time.
 
T * insertRange (unsigned int index, const T *firstPtr, const T *lastPtr)
 Inserts new elements at the specified position from a source range, last not included (shifting elements around)
 
T * insertAt (unsigned int index, const T &element)
 Inserts a new element at a specified position (shifting elements around)
 
T * insertAt (unsigned int index, T &&element)
 Move inserts a new element at a specified position (shifting elements around)
 
template<typename... Args>
T * emplaceAt (unsigned int index, Args &&... args)
 Constructs a new element at the position specified by the index.
 
Iterator insert (Iterator position, const T &value)
 Inserts a new element at the position specified by the iterator (shifting elements around)
 
Iterator insert (Iterator position, T &&value)
 Move inserts a new element at the position specified by the iterator (shifting elements around)
 
Iterator insert (Iterator position, Iterator first, Iterator last)
 Inserts new elements from a source at the position specified by the iterator (shifting elements around)
 
template<typename... Args>
Iterator emplace (Iterator position, Args &&... args)
 Constructs a new element at the position specified by the iterator.
 
T * removeRange (unsigned int firstIndex, unsigned int lastIndex)
 Removes the specified range of elements, last not included (shifting elements around)
 
Iterator removeAt (unsigned int index)
 Removes an element at a specified position (shifting elements around)
 
Iterator erase (Iterator position)
 Removes the element pointed by the iterator (shifting elements around)
 
Iterator erase (Iterator first, const Iterator last)
 Removes the elements in the range, last not included (shifting elements around)
 
T * unorderedRemoveRange (unsigned int firstIndex, unsigned int lastIndex)
 Removes the specified range of elements, last not included (moving tail elements in place)
 
Iterator unorderedRemoveAt (unsigned int index)
 Removes an element at a specified position (moving the last element in place)
 
Iterator unorderedErase (Iterator position)
 Removes the element pointed by the iterator (moving the last element in place)
 
Iterator unorderedErase (Iterator first, const Iterator last)
 Removes the elements in the range, last not included (moving tail elements in place)
 
const T & at (unsigned int index) const
 Read-only access to the specified element (with bounds checking)
 
T & at (unsigned int index)
 Access to the specified element (with bounds checking)
 
const T & operator[] (unsigned int index) const
 Read-only subscript operator.
 
T & operator[] (unsigned int index)
 Subscript operator.
 
const T * data () const
 Returns a constant pointer to the allocated memory.
 
T * data ()
 Returns a pointer to the allocated memory.
 

Detailed Description

template<class T>
class nctl::Array< T >

A dynamic array based on templates that stores elements in the heap.

Member Function Documentation

◆ capacity()

template<class T >
unsigned int nctl::Array< T >::capacity ( ) const
inline

Returns the array capacity.

The array has memory allocated to store until the Capacity()-1 element.

◆ clear()

template<class T >
void nctl::Array< T >::clear ( )

Clears the array.

Note
Size will be set to zero but capacity remains unmodified.

◆ data()

template<class T >
T * nctl::Array< T >::data ( )
inline

Returns a pointer to the allocated memory.

When adding new elements through a pointer the size field is not updated, like with std::vector.

◆ size()

template<class T >
unsigned int nctl::Array< T >::size ( ) const
inline

Returns the array size.

The array is filled without gaps until the Size()-1 element.

◆ unorderedErase() [1/2]

template<class T >
Array< T >::Iterator nctl::Array< T >::unorderedErase ( Iterator  first,
const Iterator  last 
)

Removes the elements in the range, last not included (moving tail elements in place)

Note
This method is faster than erase() but it will not preserve the array order

◆ unorderedErase() [2/2]

template<class T >
Array< T >::Iterator nctl::Array< T >::unorderedErase ( Iterator  position)

Removes the element pointed by the iterator (moving the last element in place)

Note
This method is faster than erase() but it will not preserve the array order

◆ unorderedRemoveRange()

template<class T >
T * nctl::Array< T >::unorderedRemoveRange ( unsigned int  firstIndex,
unsigned int  lastIndex 
)

Removes the specified range of elements, last not included (moving tail elements in place)

Note
This method is faster than removeRange() but it will not preserve the array order

The documentation for this class was generated from the following file: