Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
nirt::core::array< T, TAlloc > Class Template Reference

Self reallocating template array (like stl vector) with additional features. More...

#include <nirtcpp/core/engine/irrArray.hpp>

Public Types

using allocator_type = TAlloc
 
using value_type = T
 
using size_type = u32
 

Public Member Functions

 array ()
 Default constructor for empty array.
 
 array (u32 start_count)
 Constructs an array and allocates an initial chunk of memory.
 
 array (const array< T, TAlloc > &other)
 Copy constructor.
 
 ~array ()
 Destructor.
 
void reallocate (u32 new_size, bool canShrink=true)
 Reallocates the array, make it bigger or smaller.
 
void setAllocStrategy (eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
 set a new allocation strategy
 
void push_back (const T &element)
 Adds an element at back of array.
 
void push_front (const T &element)
 Adds an element at the front of the array.
 
void insert (const T &element, u32 index=0)
 Insert item into array at specified position.
 
void clear ()
 Clears the array and deletes all allocated memory.
 
void set_pointer (T *newPointer, u32 size, bool _is_sorted=false, bool _free_when_destroyed=true)
 Sets pointer to new array, using this as new workspace.
 
void set_data (const T *newData, u32 newSize, bool newDataIsSorted=false, bool canShrink=false)
 Set (copy) data from given memory block.
 
bool equals (const T *otherData, u32 size) const
 Compare if given data block is identical to the data in our array.
 
void set_free_when_destroyed (bool f)
 Sets if the array should delete the memory it uses upon destruction.
 
void set_used (u32 usedNow)
 Sets the size of the array and allocates new elements if necessary.
 
const array< T, TAlloc > & operator= (const array< T, TAlloc > &other)
 Assignment operator.
 
bool operator== (const array< T, TAlloc > &other) const
 Equality operator.
 
bool operator!= (const array< T, TAlloc > &other) const
 Inequality operator.
 
Toperator[] (u32 index)
 Direct access operator.
 
const Toperator[] (u32 index) const
 Direct const access operator.
 
TgetLast ()
 Gets last element.
 
const TgetLast () const
 Gets last element.
 
Tpointer ()
 Gets a pointer to the array.
 
const Tconst_pointer () const
 Gets a const pointer to the array.
 
u32 size () const
 Get number of occupied elements of the array.
 
u32 allocated_size () const
 Get amount of memory allocated.
 
bool empty () const
 Check if array is empty.
 
void sort ()
 Sorts the array using heapsort.
 
s32 binary_search (const T &element)
 Performs a binary search for an element, returns -1 if not found.
 
s32 binary_search (const T &element) const
 Performs a binary search for an element if possible, returns -1 if not found.
 
s32 binary_search (const T &element, s32 left, s32 right) const
 Performs a binary search for an element, returns -1 if not found.
 
s32 binary_search_multi (const T &element, s32 &last)
 
template<class E >
s32 linear_search (const E &element) const
 Finds an element by searching linearly from array start to end.
 
template<class E >
s32 linear_reverse_search (const E &element) const
 Finds an element by searching linearly from array end to start.
 
void erase (u32 index)
 Erases an element from the array.
 
void erase (u32 index, s32 count)
 Erases some elements from the array.
 
void set_sorted (bool _is_sorted)
 Sets if the array is sorted.
 
void swap (array< T, TAlloc > &other)
 Swap the content of this array container with the content of another array.
 

Detailed Description

template<class T, typename TAlloc = irrAllocator<T>>
class nirt::core::array< T, TAlloc >

Self reallocating template array (like stl vector) with additional features.

Some features are: Heap sorting, binary search methods, easier debugging.

Constructor & Destructor Documentation

◆ array()

template<class T , typename TAlloc = irrAllocator<T>>
nirt::core::array< T, TAlloc >::array ( u32  start_count)
inlineexplicit

Constructs an array and allocates an initial chunk of memory.

Parameters
start_countAmount of elements to pre-allocate.

◆ ~array()

template<class T , typename TAlloc = irrAllocator<T>>
nirt::core::array< T, TAlloc >::~array ( )
inline

Destructor.

Frees allocated memory, if set_free_when_destroyed was not set to false by the user before.

Member Function Documentation

◆ allocated_size()

template<class T , typename TAlloc = irrAllocator<T>>
u32 nirt::core::array< T, TAlloc >::allocated_size ( ) const
inline

Get amount of memory allocated.

Returns
Amount of memory allocated. The amount of bytes allocated would be allocated_size() * sizeof(ElementTypeUsed);

◆ binary_search() [1/3]

template<class T , typename TAlloc = irrAllocator<T>>
s32 nirt::core::array< T, TAlloc >::binary_search ( const T element)
inline

Performs a binary search for an element, returns -1 if not found.

The array will be sorted before the binary search if it is not already sorted. Caution is advised! Be careful not to call this on unsorted const arrays, or the slower method will be used.

Parameters
elementElement to search for.
Returns
Position of the searched element if it was found, otherwise -1 is returned.

◆ binary_search() [2/3]

template<class T , typename TAlloc = irrAllocator<T>>
s32 nirt::core::array< T, TAlloc >::binary_search ( const T element) const
inline

Performs a binary search for an element if possible, returns -1 if not found.

This method is for const arrays and so cannot call sort(), if the array is not sorted then linear_search will be used instead. Potentially very slow!

Parameters
elementElement to search for.
Returns
Position of the searched element if it was found, otherwise -1 is returned.

◆ binary_search() [3/3]

template<class T , typename TAlloc = irrAllocator<T>>
s32 nirt::core::array< T, TAlloc >::binary_search ( const T element,
s32  left,
s32  right 
) const
inline

Performs a binary search for an element, returns -1 if not found.

Parameters
elementElement to search for.
leftFirst left index
rightLast right index.
Returns
Position of the searched element if it was found, otherwise -1 is returned.

◆ binary_search_multi()

template<class T , typename TAlloc = irrAllocator<T>>
s32 nirt::core::array< T, TAlloc >::binary_search_multi ( const T element,
s32 last 
)
inline

Performs a binary search for an element, returns -1 if not found. it is used for searching a multiset The array will be sorted before the binary search if it is not already sorted.

Parameters
elementElement to search for.
&lastreturn lastIndex of equal elements
Returns
Position of the first searched element if it was found, otherwise -1 is returned.

◆ const_pointer()

template<class T , typename TAlloc = irrAllocator<T>>
const T * nirt::core::array< T, TAlloc >::const_pointer ( ) const
inline

Gets a const pointer to the array.

Returns
Pointer to the array.

◆ empty()

template<class T , typename TAlloc = irrAllocator<T>>
bool nirt::core::array< T, TAlloc >::empty ( ) const
inline

Check if array is empty.

Returns
True if the array is empty false if not.

◆ equals()

template<class T , typename TAlloc = irrAllocator<T>>
bool nirt::core::array< T, TAlloc >::equals ( const T otherData,
u32  size 
) const
inline

Compare if given data block is identical to the data in our array.

Like operator ==, but without the need to create the array

Parameters
otherDataAddress to data against which we compare, must contain size elements
sizeAmount of elements in otherData

◆ erase() [1/2]

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::erase ( u32  index)
inline

Erases an element from the array.

May be slow, because all elements following after the erased element have to be copied.

Parameters
indexIndex of element to be erased.

◆ erase() [2/2]

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::erase ( u32  index,
s32  count 
)
inline

Erases some elements from the array.

May be slow, because all elements following after the erased element have to be copied.

Parameters
indexIndex of the first element to be erased.
countAmount of elements to be erased.

◆ insert()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::insert ( const T element,
u32  index = 0 
)
inline

Insert item into array at specified position.

Parameters
elementElement to be inserted
indexWhere position to insert the new element.

◆ linear_reverse_search()

template<class T , typename TAlloc = irrAllocator<T>>
template<class E >
s32 nirt::core::array< T, TAlloc >::linear_reverse_search ( const E element) const
inline

Finds an element by searching linearly from array end to start.

Can be slow with large arrays, try binary_search for those. Only works if corresponding operator== is implemented.

Parameters
elementElement to search for.
Returns
Position of the searched element if it was found, otherwise -1 is returned.

◆ linear_search()

template<class T , typename TAlloc = irrAllocator<T>>
template<class E >
s32 nirt::core::array< T, TAlloc >::linear_search ( const E element) const
inline

Finds an element by searching linearly from array start to end.

Can be slow with large arrays, try binary_search for those. Only works if corresponding operator== is implemented.

Parameters
elementElement to search for.
Returns
Position of the searched element if it was found, otherwise -1 is returned.

◆ pointer()

template<class T , typename TAlloc = irrAllocator<T>>
T * nirt::core::array< T, TAlloc >::pointer ( )
inline

Gets a pointer to the array.

Returns
Pointer to the array.

◆ push_back()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::push_back ( const T element)
inline

Adds an element at back of array.

If the array is too small to add this new element it is made bigger.

Parameters
elementElement to add at the back of the array.

◆ push_front()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::push_front ( const T element)
inline

Adds an element at the front of the array.

If the array is to small to add this new element, the array is made bigger. Please note that this is slow, because the whole array needs to be copied for this.

Parameters
elementElement to add at the back of the array.

◆ reallocate()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::reallocate ( u32  new_size,
bool  canShrink = true 
)
inline

Reallocates the array, make it bigger or smaller.

Parameters
new_sizeNew size of array.
canShrinkSpecifies whether the array is reallocated even if enough space is available. Setting this flag to false can speed up array usage, but may use more memory than required by the data.

◆ set_data()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::set_data ( const T newData,
u32  newSize,
bool  newDataIsSorted = false,
bool  canShrink = false 
)
inline

Set (copy) data from given memory block.

Parameters
newDatadata to set, must have newSize elements
newSizeAmount of elements in newData
newDataIsSortedInfo if you pass sorted/unsorted data
canShrinkSpecifies whether the array is reallocated even if enough space is available. Setting this flag to false can speed up array usage, but may use more memory than required by the data.

◆ set_free_when_destroyed()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::set_free_when_destroyed ( bool  f)
inline

Sets if the array should delete the memory it uses upon destruction.

Also clear and set_pointer will only delete the (original) memory area if this flag is set to true, which is also the default. The methods reallocate, set_used, push_back, push_front, insert, and erase will still try to deallocate the original memory, which might cause troubles depending on the intended use of the memory area.

Parameters
fIf true, the array frees the allocated memory in its destructor, otherwise not. The default is true.

◆ set_pointer()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::set_pointer ( T newPointer,
u32  size,
bool  _is_sorted = false,
bool  _free_when_destroyed = true 
)
inline

Sets pointer to new array, using this as new workspace.

Make sure that set_free_when_destroyed is used properly.

Parameters
newPointerPointer to new array of elements.
sizeSize of the new array.
_is_sortedFlag which tells whether the new array is already sorted.
_free_when_destroyedSets whether the new memory area shall be freed by the array upon destruction, or if this will be up to the user application.

◆ set_used()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::set_used ( u32  usedNow)
inline

Sets the size of the array and allocates new elements if necessary.

Please note: This is only secure when using it with simple types, because no default constructor will be called for the added elements.

Parameters
usedNowAmount of elements now used.

◆ setAllocStrategy()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::setAllocStrategy ( eAllocStrategy  newStrategy = ALLOC_STRATEGY_DOUBLE)
inline

set a new allocation strategy

if the maximum size of the array is unknown, you can define how big the allocation should happen.

Parameters
newStrategyNew strategy to apply to this array.

◆ size()

template<class T , typename TAlloc = irrAllocator<T>>
u32 nirt::core::array< T, TAlloc >::size ( ) const
inline

Get number of occupied elements of the array.

Returns
Size of elements in the array which are actually occupied.

◆ sort()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::sort ( )
inline

Sorts the array using heapsort.

There is no additional memory waste and the algorithm performs O(n*log n) in worst case.

◆ swap()

template<class T , typename TAlloc = irrAllocator<T>>
void nirt::core::array< T, TAlloc >::swap ( array< T, TAlloc > &  other)
inline

Swap the content of this array container with the content of another array.

Afterward this object will contain the content of the other object and the other object will contain the content of this object.

Parameters
otherSwap content with this object

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

Nirtcpp    @cppfx.xyz

Esvcpp    esv::print