5#ifndef NIRT_ARRAY_HPP_INCLUDED
6#define NIRT_ARRAY_HPP_INCLUDED
8#include <nirtcpp/core/engine/nirt_types.hpp>
9#include <nirtcpp/core/engine/heapsort.hpp>
10#include <nirtcpp/core/engine/irrAllocator.hpp>
11#include <nirtcpp/core/engine/irrMath.hpp>
21template <
class T,
typename TAlloc = irrAllocator<T> >
28 array() : data(0), allocated(0), used(0),
29 strategy(ALLOC_STRATEGY_DOUBLE), free_when_destroyed(
true), is_sorted(
true)
37 strategy(ALLOC_STRATEGY_DOUBLE),
38 free_when_destroyed(
true), is_sorted(
true)
81 for (
s32 i=0; i<end; ++i)
84 allocator.construct(&data[i],
old_data[i]);
134 NIRT_DEBUG_BREAK_IF(index>used)
136 if (used + 1 > allocated)
147 case ALLOC_STRATEGY_DOUBLE:
151 case ALLOC_STRATEGY_SAFE:
159 for (
u32 i=used; i>index; --i)
162 allocator.destruct(&data[i]);
163 allocator.construct(&data[i], data[i-1]);
167 allocator.destruct(&data[index]);
168 allocator.construct(&data[index],
e);
176 allocator.construct(&data[used], data[used-1]);
179 for (
u32 i=used-1; i>index; --i)
189 allocator.construct(&data[index],
element);
201 if (free_when_destroyed)
203 for (
u32 i=0; i<used; ++i)
204 allocator.destruct(&data[i]);
206 allocator.deallocate(data);
280 free_when_destroyed = f;
301 strategy =
other.strategy;
309 free_when_destroyed =
true;
310 is_sorted =
other.is_sorted;
311 allocated =
other.allocated;
313 if (
other.allocated == 0)
319 data = allocator.allocate(
other.allocated);
322 allocator.construct(&data[i],
other.data[i]);
339 return !(*
this==
other);
346 NIRT_DEBUG_BREAK_IF(index>=used)
355 NIRT_DEBUG_BREAK_IF(index>=used)
364 NIRT_DEBUG_BREAK_IF(!used)
373 NIRT_DEBUG_BREAK_IF(!used)
425 if (!is_sorted && used>1)
515 while ( index > 0 && !(
element < data[index - 1]) && !(data[index - 1] <
element) )
538 for (
u32 i=0; i<used; ++i)
555 for (
s32 i=used-1; i>=0; --i)
569 NIRT_DEBUG_BREAK_IF(index>=used)
571 for (
u32 i=index+1; i<used; ++i)
573 allocator.destruct(&data[i-1]);
574 allocator.construct(&data[i-1], data[i]);
577 allocator.destruct(&data[used-1]);
590 if (index>=used || count<1)
592 if (index+count>used)
596 for (i=index; i<index+count; ++i)
597 allocator.destruct(&data[i]);
599 for (i=index+count; i<used; ++i)
601 if (i-count >= index+count)
602 allocator.destruct(&data[i-count]);
604 allocator.construct(&data[i-count], data[i]);
607 allocator.destruct(&data[i]);
632 strategy =
other.strategy;
635 free_when_destroyed =
other.free_when_destroyed;
638 is_sorted =
other.is_sorted;
642 using allocator_type =
TAlloc;
643 using value_type =
T;
644 using size_type =
u32;
652 bool free_when_destroyed:1;
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.hpp:23
u32 size() const
Get number of occupied elements of the array.
Definition irrArray.hpp:397
u32 allocated_size() const
Get amount of memory allocated.
Definition irrArray.hpp:406
s32 binary_search(const T &element) const
Performs a binary search for an element if possible, returns -1 if not found.
Definition irrArray.hpp:451
void set_sorted(bool _is_sorted)
Sets if the array is sorted.
Definition irrArray.hpp:615
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
Definition irrArray.hpp:132
const array< T, TAlloc > & operator=(const array< T, TAlloc > &other)
Assignment operator.
Definition irrArray.hpp:297
bool operator==(const array< T, TAlloc > &other) const
Equality operator.
Definition irrArray.hpp:330
void swap(array< T, TAlloc > &other)
Swap the content of this array container with the content of another array.
Definition irrArray.hpp:625
void push_back(const T &element)
Adds an element at back of array.
Definition irrArray.hpp:111
array(u32 start_count)
Constructs an array and allocates an initial chunk of memory.
Definition irrArray.hpp:36
T & getLast()
Gets last element.
Definition irrArray.hpp:362
void set_data(const T *newData, u32 newSize, bool newDataIsSorted=false, bool canShrink=false)
Set (copy) data from given memory block.
Definition irrArray.hpp:242
s32 linear_reverse_search(const E &element) const
Finds an element by searching linearly from array end to start.
Definition irrArray.hpp:553
void erase(u32 index, s32 count)
Erases some elements from the array.
Definition irrArray.hpp:588
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition irrArray.hpp:438
void erase(u32 index)
Erases an element from the array.
Definition irrArray.hpp:567
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition irrArray.hpp:66
array(const array< T, TAlloc > &other)
Copy constructor.
Definition irrArray.hpp:45
const T * const_pointer() const
Gets a const pointer to the array.
Definition irrArray.hpp:389
void push_front(const T &element)
Adds an element at the front of the array.
Definition irrArray.hpp:122
bool empty() const
Check if array is empty.
Definition irrArray.hpp:414
T * pointer()
Gets a pointer to the array.
Definition irrArray.hpp:381
void set_used(u32 usedNow)
Sets the size of the array and allocates new elements if necessary.
Definition irrArray.hpp:288
~array()
Destructor.
Definition irrArray.hpp:54
array()
Default constructor for empty array.
Definition irrArray.hpp:28
const T & getLast() const
Gets last element.
Definition irrArray.hpp:371
void sort()
Sorts the array using heapsort.
Definition irrArray.hpp:423
bool operator!=(const array< T, TAlloc > &other) const
Inequality operator.
Definition irrArray.hpp:337
void setAllocStrategy(eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
set a new allocation strategy
Definition irrArray.hpp:102
T & operator[](u32 index)
Direct access operator.
Definition irrArray.hpp:344
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.
Definition irrArray.hpp:224
s32 binary_search(const T &element, s32 left, s32 right) const
Performs a binary search for an element, returns -1 if not found.
Definition irrArray.hpp:466
void set_free_when_destroyed(bool f)
Sets if the array should delete the memory it uses upon destruction.
Definition irrArray.hpp:278
s32 linear_search(const E &element) const
Finds an element by searching linearly from array start to end.
Definition irrArray.hpp:536
bool equals(const T *otherData, u32 size) const
Compare if given data block is identical to the data in our array.
Definition irrArray.hpp:257
s32 binary_search_multi(const T &element, s32 &last)
Definition irrArray.hpp:504
void clear()
Clears the array and deletes all allocated memory.
Definition irrArray.hpp:199
eAllocStrategy
defines an allocation strategy (used only by nirt::array so far)
Definition irrAllocator.hpp:113
void heapsort(T *array_, s32 size)
Sorts an array with size 'size' using heapsort.
Definition heapsort.hpp:41
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.hpp:175
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64