5#ifndef __IRR_ARRAY_H_INCLUDED__
6#define __IRR_ARRAY_H_INCLUDED__
10#include "irrAllocator.h"
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)
36 array(
u32 start_count) : data(0), allocated(0), used(0),
37 strategy(ALLOC_STRATEGY_DOUBLE),
38 free_when_destroyed(true), is_sorted(true)
68 if (allocated==new_size)
70 if (!canShrink && (new_size < allocated))
75 data = allocator.allocate(new_size);
79 s32 end = used < new_size ? used : new_size;
81 for (
s32 i=0; i<end; ++i)
84 allocator.construct(&data[i], old_data[i]);
88 for (
u32 j=0; j<used; ++j)
89 allocator.destruct(&old_data[j]);
94 allocator.deallocate(old_data);
104 strategy = newStrategy;
136 _IRR_DEBUG_BREAK_IF(index>used)
138 if (used + 1 > allocated)
149 case ALLOC_STRATEGY_DOUBLE:
150 newAlloc = used + 1 + (allocated < 500 ?
151 (allocated < 5 ? 5 : used) : used >> 2);
154 case ALLOC_STRATEGY_SAFE:
162 for (
u32 i=used; i>index; --i)
165 allocator.destruct(&data[i]);
166 allocator.construct(&data[i], data[i-1]);
170 allocator.destruct(&data[index]);
171 allocator.construct(&data[index], e);
179 allocator.construct(&data[used], data[used-1]);
182 for (
u32 i=used-1; i>index; --i)
187 data[index] = element;
192 allocator.construct(&data[index], element);
204 if (free_when_destroyed)
206 for (
u32 i=0; i<used; ++i)
207 allocator.destruct(&data[i]);
209 allocator.deallocate(data);
233 is_sorted = _is_sorted;
234 free_when_destroyed=_free_when_destroyed;
248 free_when_destroyed = f;
258 if (allocated < usedNow)
270 strategy = other.strategy;
276 if (other.allocated == 0)
279 data = allocator.allocate(other.allocated);
282 free_when_destroyed =
true;
283 is_sorted = other.is_sorted;
284 allocated = other.allocated;
286 for (
u32 i=0; i<other.used; ++i)
287 allocator.construct(&data[i], other.data[i]);
296 if (used != other.used)
299 for (
u32 i=0; i<other.used; ++i)
300 if (data[i] != other[i])
309 return !(*
this==other);
316 _IRR_DEBUG_BREAK_IF(index>=used)
325 _IRR_DEBUG_BREAK_IF(index>=used)
334 _IRR_DEBUG_BREAK_IF(!used)
343 _IRR_DEBUG_BREAK_IF(!used)
395 if (!is_sorted && used>1)
447 if (element < data[m])
452 }
while((element < data[m] || data[m] < element) && left<=right);
459 if (!(element < data[m]) && !(data[m] < element))
485 while ( index > 0 && !(element < data[index - 1]) && !(data[index - 1] < element) )
490 while ( last < (
s32) used - 1 && !(element < data[last + 1]) && !(data[last + 1] < element) )
507 for (
u32 i=0; i<used; ++i)
508 if (element == data[i])
523 for (
s32 i=used-1; i>=0; --i)
524 if (data[i] == element)
537 _IRR_DEBUG_BREAK_IF(index>=used)
539 for (
u32 i=index+1; i<used; ++i)
541 allocator.destruct(&data[i-1]);
542 allocator.construct(&data[i-1], data[i]);
545 allocator.destruct(&data[used-1]);
558 if (index>=used || count<1)
560 if (index+count>used)
564 for (i=index; i<index+count; ++i)
565 allocator.destruct(&data[i]);
567 for (i=index+count; i<used; ++i)
569 if (i-count >= index+count)
570 allocator.destruct(&data[i-count]);
572 allocator.construct(&data[i-count], data[i]);
575 allocator.destruct(&data[i]);
585 is_sorted = _is_sorted;
600 strategy = other.strategy;
601 other.strategy = helper_strategy;
602 bool helper_free_when_destroyed(free_when_destroyed);
603 free_when_destroyed = other.free_when_destroyed;
604 other.free_when_destroyed = helper_free_when_destroyed;
605 bool helper_is_sorted(is_sorted);
606 is_sorted = other.is_sorted;
607 other.is_sorted = helper_is_sorted;
617 bool free_when_destroyed:1;
const array< T, TAlloc > & operator=(const array< T, TAlloc > &other)
Assignment operator.
Definition irrArray.h:266
u32 allocated_size() const
Get amount of memory allocated.
Definition irrArray.h:376
void clear()
Clears the array and deletes all allocated memory.
Definition irrArray.h:202
void push_front(const T &element)
Adds an element at the front of the array.
Definition irrArray.h:122
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition irrArray.h:408
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
Definition irrArray.h:134
s32 linear_search(const T &element) const
Finds an element in linear time, which is very slow.
Definition irrArray.h:505
array(const array< T, TAlloc > &other)
Copy constructor.
Definition irrArray.h:45
void erase(u32 index)
Erases an element from the array.
Definition irrArray.h:535
array()
Default constructor for empty array.
Definition irrArray.h:28
s32 binary_search_multi(const T &element, s32 &last)
Definition irrArray.h:474
void set_used(u32 usedNow)
Sets the size of the array and allocates new elements if necessary.
Definition irrArray.h:256
T & getLast()
Gets last element.
Definition irrArray.h:332
bool operator!=(const array< T, TAlloc > &other) const
Inequality operator.
Definition irrArray.h:307
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.h:227
bool operator==(const array< T, TAlloc > &other) const
Equality operator.
Definition irrArray.h:294
void setAllocStrategy(eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
set a new allocation strategy
Definition irrArray.h:102
void sort()
Sorts the array using heapsort.
Definition irrArray.h:393
void swap(array< T, TAlloc > &other)
Swap the content of this array container with the content of another array.
Definition irrArray.h:593
T & operator[](u32 index)
Direct access operator.
Definition irrArray.h:314
bool empty() const
Check if array is empty.
Definition irrArray.h:384
T * pointer()
Gets a pointer to the array.
Definition irrArray.h:351
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.h:436
const T & getLast() const
Gets last element.
Definition irrArray.h:341
~array()
Destructor.
Definition irrArray.h:54
u32 size() const
Get number of occupied elements of the array.
Definition irrArray.h:367
array(u32 start_count)
Constructs an array and allocates an initial chunk of memory.
Definition irrArray.h:36
void set_sorted(bool _is_sorted)
Sets if the array is sorted.
Definition irrArray.h:583
void erase(u32 index, s32 count)
Erases some elements from the array.
Definition irrArray.h:556
const T * const_pointer() const
Gets a const pointer to the array.
Definition irrArray.h:359
void push_back(const T &element)
Adds an element at back of array.
Definition irrArray.h:111
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition irrArray.h:66
s32 binary_search(const T &element) const
Performs a binary search for an element if possible, returns -1 if not found.
Definition irrArray.h:421
s32 linear_reverse_search(const T &element) const
Finds an element in linear time, which is very slow.
Definition irrArray.h:521
void set_free_when_destroyed(bool f)
Sets if the array should delete the memory it uses upon destruction.
Definition irrArray.h:246
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
eAllocStrategy
defines an allocation strategy
Definition irrAllocator.h:113
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.h:177
void heapsort(T *array_, s32 size)
Sorts an array with size 'size' using heapsort.
Definition heapsort.h:41
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66