Nebula
|
#include <array.h>
Nebula's dynamic array class.
This class is also used by most other collection classes.
The default constructor will not pre-allocate elements, so no space is wasted as long as no elements are added. As soon as the first element is added to the array, an initial buffer of 16 elements is created. Whenever the element buffer would overflow, a new buffer of twice the size of the previous buffer is created and the existing elements are then copied over to the new buffer. The element buffer will never shrink, the only way to reclaim unused memory is to copy the Array to a new Array object. This is usually not a problem since most arrays will oscillate around some specific size, so once the array has reached this specific size, no costly memory free or allocs will be performed.
It is possible to sort the array using the Sort() method, this uses std::sort (one of the very few exceptions where the STL is used in Nebula).
One should generally be careful with costly copy operators, the Array class (and the other container classes using Array) may do some heavy element shuffling in some situations (especially when sorting and erasing elements).
Inherited by Util::PinnedArray< MAX_ALLOCS, TYPE >, and Util::TrivialArray< TYPE >.
Public Types | |
typedef TYPE * | Iterator |
define iterator | |
typedef const TYPE * | ConstIterator |
using | ArrayT = Array<TYPE, SMALL_VECTOR_SIZE> |
Public Member Functions | |
Array () | |
constructor with default parameters | |
Array (SizeT initialCapacity, SizeT initialGrow) | |
constuctor with initial size and grow size | |
Array (SizeT initialSize, SizeT initialGrow, const TYPE &initialValue) | |
constructor with initial size, grow size and initial values | |
Array (const ArrayT &rhs) | |
copy constructor | |
Array (ArrayT &&rhs) noexcept | |
move constructor | |
Array (std::initializer_list< TYPE > list) | |
constructor from initializer list | |
Array (std::nullptr_t) | |
construct an empty fixed array | |
Array (const TYPE *const buf, SizeT num) | |
constructor from TYPE pointer and size. | |
~Array () | |
destructor | |
void | operator= (const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) |
assignment operator | |
void | operator= (Array< TYPE, SMALL_VECTOR_SIZE > &&rhs) noexcept |
move operator | |
TYPE & | operator[] (IndexT index) const |
[] operator | |
TYPE & | operator[] (IndexT index) |
[] operator | |
bool | operator== (const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) const |
equality operator | |
bool | operator!= (const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) const |
inequality operator | |
template<typename T > | |
T | As () const |
convert to "anything" | |
TYPE & | Get (IndexT index) const |
Get element (same as operator[] but as a function) | |
template<typename ... ELEM_TYPE> | |
void | Append (const TYPE &first, const ELEM_TYPE &... elements) |
Append multiple elements to the end of the array. | |
void | Append (const TYPE &elm) |
append element to end of array | |
void | Append (TYPE &&elm) |
append an element which is being forwarded | |
void | AppendArray (const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) |
append the contents of an array to this array | |
void | AppendArray (const TYPE *arr, const SizeT count) |
append from C array | |
TYPE & | Emplace () |
Emplace item (create new item and return reference) | |
TYPE * | EmplaceArray (const SizeT count) |
Emplace range of items and return pointer to first. | |
void | Reserve (SizeT num) |
increase capacity to fit N more elements into the array. | |
const SizeT | Size () const |
get number of elements in array | |
const SizeT | ByteSize () const |
return the byte size of the array. | |
const SizeT | Capacity () const |
get overall allocated size of array in number of elements | |
TYPE & | Front () const |
return reference to first element | |
TYPE & | Back () const |
return reference to last element | |
bool | IsEmpty () const |
return true if array empty | |
bool | IsValidIndex (IndexT index) const |
check if index is valid | |
void | EraseIndex (IndexT index) |
erase element at index, keep sorting intact | |
Iterator | Erase (Iterator iter) |
erase element pointed to by iterator, keep sorting intact | |
void | EraseIndexSwap (IndexT index) |
erase element at index, fill gap by swapping in last element, destroys sorting! | |
Iterator | EraseSwap (Iterator iter) |
erase element at iterator, fill gap by swapping in last element, destroys sorting! | |
void | EraseRange (IndexT start, IndexT end) |
erase range, excluding the element at end | |
void | EraseBack () |
erase back | |
void | EraseFront () |
erase front | |
TYPE | PopFront () |
Pop front. | |
TYPE | PopBack () |
Pop back. | |
void | Insert (IndexT index, const TYPE &elm) |
insert element before element at index | |
IndexT | InsertSorted (const TYPE &elm) |
insert element into sorted array, return index where element was included | |
IndexT | InsertAtEndOfIdenticalRange (IndexT startIndex, const TYPE &elm) |
insert element at the first non-identical position, return index of inclusion position | |
bool | IsSorted () const |
test if the array is sorted, this is a slow operation! | |
void | Clear () |
clear array (calls destructors) | |
void | Reset () |
reset array (does NOT call destructors) | |
void | Free () |
free memory and reset size | |
Iterator | Begin () const |
return iterator to beginning of array | |
ConstIterator | ConstBegin () const |
return const iterator to beginning of array | |
Iterator | End () const |
return iterator to end of array | |
ConstIterator | ConstEnd () const |
return const iterator to end of array | |
Iterator | Find (const TYPE &elm, const IndexT start=0) const |
find identical element in array, return iterator | |
IndexT | FindIndex (const TYPE &elm, const IndexT start=0) const |
find identical element in array, return index, InvalidIndex if not found | |
template<typename KEYTYPE > | |
IndexT | FindIndex (typename std::enable_if< true, const KEYTYPE & >::type elm, const IndexT start=0) const |
find identical element using a specific key type | |
void | Fill (IndexT first, SizeT num, const TYPE &elm) |
fill array range with element | |
void | Realloc (SizeT capacity, SizeT grow) |
clear contents and preallocate with new attributes | |
ArrayT | Difference (const Array< TYPE, SMALL_VECTOR_SIZE > &rhs) |
returns new array with elements which are not in rhs (slow!) | |
void | Sort () |
sort the array | |
void | QuickSort () |
quick sort the array | |
void | SortWithFunc (bool(*func)(const TYPE &lhs, const TYPE &rhs)) |
sort with custom function | |
void | QuickSortWithFunc (int(*func)(const void *lhs, const void *rhs)) |
quick sort the array | |
IndexT | BinarySearchIndex (const TYPE &elm) const |
do a binary search, requires a sorted array | |
template<typename KEYTYPE > | |
IndexT | BinarySearchIndex (typename std::enable_if< true, const KEYTYPE & >::type elm) const |
do a binary search using a specific key type | |
void | Resize (SizeT num) |
Set size. Grows array if num is greater than capacity. Calls destroy on all objects at index > num! | |
template<typename ... ARGS> | |
void | Resize (SizeT num, ARGS... args) |
Resize and fill new elements with arguments. | |
void | Extend (SizeT num) |
Resize to fit the provided value, but don't shrink if the new size is smaller. | |
void | Fit () |
Fit the size of the array to the amount of elements. | |
constexpr SizeT | TypeSize () const |
Returns sizeof(TYPE) | |
Iterator | begin () const |
for range-based iteration | |
Iterator | end () const |
size_t | size () const |
void | resize (size_t size) |
void | clear () noexcept |
void | push_back (const TYPE &item) |
void | Grow () |
grow array with grow value | |
Protected Member Functions | |
void | Destroy (TYPE *elm) |
destroy an element (call destructor without freeing memory) | |
void | Copy (const Array< TYPE, SMALL_VECTOR_SIZE > &src) |
copy content | |
void | Delete () |
delete content | |
void | GrowTo (SizeT newCapacity) |
grow array to target size | |
void | Move (IndexT fromIndex, IndexT toIndex) |
move elements, grows array if needed | |
void | DestroyRange (IndexT fromIndex, IndexT toIndex) |
destroy range of elements | |
void | CopyRange (TYPE *to, TYPE *from, SizeT num) |
copy range | |
void | MoveRange (TYPE *to, TYPE *from, SizeT num) |
move range | |
Protected Attributes | |
SizeT | grow |
SizeT | capacity |
SizeT | count |
TYPE * | elements |
_smallvector< TYPE, SMALL_VECTOR_SIZE > | stackElements |
Static Protected Attributes | |
static const SizeT | MinGrowSize = 16 |
static const SizeT | MaxGrowSize = 65536 |
Friends | |
template<class T , bool S> | |
class | FixedArray |
using Util::Array< TYPE, SMALL_VECTOR_SIZE >::ArrayT = Array<TYPE, SMALL_VECTOR_SIZE> |
const TYPE* Util::Array< TYPE, SMALL_VECTOR_SIZE >::ConstIterator |
TYPE* Util::Array< TYPE, SMALL_VECTOR_SIZE >::Iterator |
define iterator
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | ) |
constructor with default parameters
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | SizeT | initialCapacity, |
SizeT | initialGrow ) |
constuctor with initial size and grow size
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | SizeT | initialSize, |
SizeT | initialGrow, | ||
const TYPE & | initialValue ) |
constructor with initial size, grow size and initial values
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | const ArrayT & | rhs | ) |
copy constructor
|
noexcept |
move constructor
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | std::initializer_list< TYPE > | list | ) |
constructor from initializer list
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | std::nullptr_t | ) |
construct an empty fixed array
Util::Array< TYPE, SMALL_VECTOR_SIZE >::Array | ( | const TYPE *const | buf, |
SizeT | num ) |
constructor from TYPE pointer and size.
Util::Array< TYPE, SMALL_VECTOR_SIZE >::~Array | ( | ) |
destructor
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Append | ( | const TYPE & | elm | ) |
append element to end of array
|
inline |
Append multiple elements to the end of the array.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Append | ( | TYPE && | elm | ) |
append an element which is being forwarded
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::AppendArray | ( | const Array< TYPE, SMALL_VECTOR_SIZE > & | rhs | ) |
append the contents of an array to this array
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::AppendArray | ( | const TYPE * | arr, |
const SizeT | count ) |
append from C array
T Util::Array< TYPE, SMALL_VECTOR_SIZE >::As | ( | ) | const |
convert to "anything"
TYPE & Util::Array< TYPE, SMALL_VECTOR_SIZE >::Back | ( | ) | const |
return reference to last element
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::Begin | ( | ) | const |
return iterator to beginning of array
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::begin | ( | ) | const |
for range-based iteration
IndexT Util::Array< TYPE, SMALL_VECTOR_SIZE >::BinarySearchIndex | ( | const TYPE & | elm | ) | const |
do a binary search, requires a sorted array
Does a binary search on the array, returns the index of the identical element, or InvalidIndex if not found.
|
inline |
do a binary search using a specific key type
Template type is used to force a specific type comparison.
This might mitigate some expensive implicit constructions to TYPE.
This templated method requires a explicit template type, which is enforced by using typename to put the template type in a non-deducable context. The enable_if does nothing except allow us to use typename.
const SizeT Util::Array< TYPE, SMALL_VECTOR_SIZE >::ByteSize | ( | ) | const |
return the byte size of the array.
const SizeT Util::Array< TYPE, SMALL_VECTOR_SIZE >::Capacity | ( | ) | const |
get overall allocated size of array in number of elements
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Clear | ( | ) |
clear array (calls destructors)
The current implementation of this method does not shrink the preallocated space.
It simply sets the array _size to 0.
|
noexcept |
Array< TYPE, SMALL_VECTOR_SIZE >::ConstIterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::ConstBegin | ( | ) | const |
return const iterator to beginning of array
Array< TYPE, SMALL_VECTOR_SIZE >::ConstIterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::ConstEnd | ( | ) | const |
return const iterator to end of array
|
protected |
copy content
|
inlineprotected |
copy range
|
protected |
delete content
|
protected |
destroy an element (call destructor without freeing memory)
|
inlineprotected |
destroy range of elements
Array< TYPE, SMALL_VECTOR_SIZE > Util::Array< TYPE, SMALL_VECTOR_SIZE >::Difference | ( | const Array< TYPE, SMALL_VECTOR_SIZE > & | rhs | ) |
returns new array with elements which are not in rhs (slow!)
Returns a new array with all element which are in rhs, but not in this.
Carefull, this method may be very slow with large arrays!
TYPE & Util::Array< TYPE, SMALL_VECTOR_SIZE >::Emplace | ( | ) |
Emplace item (create new item and return reference)
TYPE * Util::Array< TYPE, SMALL_VECTOR_SIZE >::EmplaceArray | ( | const SizeT | count | ) |
Emplace range of items and return pointer to first.
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::End | ( | ) | const |
return iterator to end of array
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::end | ( | ) | const |
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::Erase | ( | Iterator | iter | ) |
erase element pointed to by iterator, keep sorting intact
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseBack | ( | ) |
erase back
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseFront | ( | ) |
erase front
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseIndex | ( | IndexT | index | ) |
erase element at index, keep sorting intact
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseIndexSwap | ( | IndexT | index | ) |
erase element at index, fill gap by swapping in last element, destroys sorting!
NOTE: this method is fast but destroys the sorting order!
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseRange | ( | IndexT | start, |
IndexT | end ) |
erase range, excluding the element at end
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::EraseSwap | ( | Iterator | iter | ) |
erase element at iterator, fill gap by swapping in last element, destroys sorting!
NOTE: this method is fast but destroys the sorting order!
|
inline |
Resize to fit the provided value, but don't shrink if the new size is smaller.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Fill | ( | IndexT | first, |
SizeT | num, | ||
const TYPE & | elm ) |
fill array range with element
Fills an array range with the given element value.
Will grow the array if necessary
first | index of first element to start fill |
num | num elements to fill |
elm | fill value |
Array< TYPE, SMALL_VECTOR_SIZE >::Iterator Util::Array< TYPE, SMALL_VECTOR_SIZE >::Find | ( | const TYPE & | elm, |
const IndexT | start = 0 ) const |
find identical element in array, return iterator
Find element in array, return iterator, or 0 if element not found.
elm | element to find |
IndexT Util::Array< TYPE, SMALL_VECTOR_SIZE >::FindIndex | ( | const TYPE & | elm, |
const IndexT | start = 0 ) const |
find identical element in array, return index, InvalidIndex if not found
Find element in array, return element index, or InvalidIndex if element not found.
elm | element to find |
|
inline |
find identical element using a specific key type
Find element in array, return element index, or InvalidIndex if element not found.
Template type is used to force a specific type comparison. This might mitigate some expensive implicit constructions to TYPE.
This templated method requires a explicit template type, which is enforced by using typename to put the template type in a non-deducable context. The enable_if does nothing except allow us to use typename.
elm | element to find |
|
inline |
Fit the size of the array to the amount of elements.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Free | ( | ) |
free memory and reset size
Free up memory and reset the grow.
TYPE & Util::Array< TYPE, SMALL_VECTOR_SIZE >::Front | ( | ) | const |
return reference to first element
|
inline |
Get element (same as operator[] but as a function)
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Grow | ( | ) |
grow array with grow value
|
protected |
grow array to target size
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Insert | ( | IndexT | index, |
const TYPE & | elm ) |
insert element before element at index
IndexT Util::Array< TYPE, SMALL_VECTOR_SIZE >::InsertAtEndOfIdenticalRange | ( | IndexT | startIndex, |
const TYPE & | elm ) |
insert element at the first non-identical position, return index of inclusion position
This inserts an element at the end of a range of identical elements starting at a given index.
Performance is O(n). Returns the index at which the element was added.
IndexT Util::Array< TYPE, SMALL_VECTOR_SIZE >::InsertSorted | ( | const TYPE & | elm | ) |
insert element into sorted array, return index where element was included
This inserts the element into a sorted array.
Returns the index at which the element was inserted.
bool Util::Array< TYPE, SMALL_VECTOR_SIZE >::IsEmpty | ( | ) | const |
return true if array empty
bool Util::Array< TYPE, SMALL_VECTOR_SIZE >::IsSorted | ( | ) | const |
test if the array is sorted, this is a slow operation!
This tests, whether the array is sorted.
This is a slow operation O(n).
bool Util::Array< TYPE, SMALL_VECTOR_SIZE >::IsValidIndex | ( | IndexT | index | ) | const |
check if index is valid
|
protected |
move elements, grows array if needed
30-Jan-03 floh serious bugfixes! 07-Dec-04 jo bugfix: neededSize >= this->capacity => neededSize > capacity
|
inlineprotected |
move range
bool Util::Array< TYPE, SMALL_VECTOR_SIZE >::operator!= | ( | const Array< TYPE, SMALL_VECTOR_SIZE > & | rhs | ) | const |
inequality operator
The inequality operator returns true if at least one element in the array is different, or the array sizes are different.
|
noexcept |
move operator
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::operator= | ( | const Array< TYPE, SMALL_VECTOR_SIZE > & | rhs | ) |
assignment operator
bool Util::Array< TYPE, SMALL_VECTOR_SIZE >::operator== | ( | const Array< TYPE, SMALL_VECTOR_SIZE > & | rhs | ) | const |
equality operator
The equality operator returns true if all elements are identical.
The TYPE class must support the equality operator.
TYPE & Util::Array< TYPE, SMALL_VECTOR_SIZE >::operator[] | ( | IndexT | index | ) |
[] operator
Access an element.
This method will NOT grow the array, and instead do a range check, which may throw an assertion.
TYPE & Util::Array< TYPE, SMALL_VECTOR_SIZE >::operator[] | ( | IndexT | index | ) | const |
[] operator
Access an element.
This method will NOT grow the array, and instead do a range check, which may throw an assertion.
|
inline |
Pop back.
|
inline |
Pop front.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::push_back | ( | const TYPE & | item | ) |
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::QuickSort | ( | ) |
quick sort the array
Sorts the array using quick sort.
This just calls the STL sort algorithm.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::QuickSortWithFunc | ( | int(* | func )(const void *lhs, const void *rhs) | ) |
quick sort the array
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Realloc | ( | SizeT | capacity, |
SizeT | grow ) |
clear contents and preallocate with new attributes
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Reserve | ( | SizeT | num | ) |
increase capacity to fit N more elements into the array.
This increases the capacity to make room for N elements.
If the number of elements is known before appending the elements, this method can be used to prevent reallocation. If there is already enough room for N more elements, nothing will happen.
NOTE: the functionality of this method has been changed as of 26-Apr-08, it will now only change the capacity of the array, not its size.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Reset | ( | ) |
reset array (does NOT call destructors)
This is identical with Clear(), but does NOT call destructors (it just resets the _size member.
USE WITH CARE!
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Resize | ( | SizeT | num | ) |
Set size. Grows array if num is greater than capacity. Calls destroy on all objects at index > num!
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Resize | ( | SizeT | num, |
ARGS... | args ) |
Resize and fill new elements with arguments.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::resize | ( | size_t | size | ) |
const SizeT Util::Array< TYPE, SMALL_VECTOR_SIZE >::Size | ( | ) | const |
get number of elements in array
size_t Util::Array< TYPE, SMALL_VECTOR_SIZE >::size | ( | ) | const |
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::Sort | ( | ) |
sort the array
Sorts the array.
This just calls the STL sort algorithm.
void Util::Array< TYPE, SMALL_VECTOR_SIZE >::SortWithFunc | ( | bool(* | func )(const TYPE &lhs, const TYPE &rhs) | ) |
sort with custom function
|
inlineconstexpr |
Returns sizeof(TYPE)
|
friend |
|
protected |
|
protected |
|
protected |
|
protected |
|
staticprotected |
|
staticprotected |
|
protected |