5 #ifndef __IRR_ARRAY_H_INCLUDED__ 6 #define __IRR_ARRAY_H_INCLUDED__ 21 template <
class T,
typename TAlloc = irrAllocator<T> >
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 const s32 end = used < new_size ? used : new_size;
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 if (used + 1 > allocated)
148 newAlloc = used + 5 + (allocated < 500 ? used : used >> 2);
162 allocator.destruct(&
data[i]);
163 allocator.construct(&
data[i],
data[i-1]);
176 allocator.construct(&
data[used],
data[used-1]);
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);
230 is_sorted = _is_sorted;
231 free_when_destroyed=_free_when_destroyed;
245 free_when_destroyed =
f;
255 if (allocated < usedNow)
267 strategy = other.strategy;
273 if (other.allocated == 0)
276 data = allocator.allocate(other.allocated);
279 free_when_destroyed =
true;
280 is_sorted = other.is_sorted;
281 allocated = other.allocated;
283 for (
u32 i=0; i<other.used; ++i)
284 allocator.construct(&
data[i], other.data[i]);
293 if (used != other.used)
296 for (
u32 i=0; i<other.used; ++i)
297 if (
data[i] != other[i])
306 return !(*
this==other);
392 if (!is_sorted && used>1)
444 if (element <
data[
m])
456 if (!(element <
data[
m]) && !(
data[
m] < element))
487 while ( last < (
s32) used - 1 && !(element <
data[last + 1]) && !(
data[last + 1] < element) )
504 for (
u32 i=0; i<used; ++i)
505 if (element ==
data[i])
520 for (
s32 i=used-1; i>=0; --i)
521 if (
data[i] == element)
538 allocator.destruct(&
data[i-1]);
539 allocator.construct(&
data[i-1],
data[i]);
542 allocator.destruct(&
data[used-1]);
562 allocator.destruct(&
data[i]);
572 allocator.destruct(&
data[i]);
582 is_sorted = _is_sorted;
597 strategy = other.strategy;
598 other.strategy = helper_strategy;
599 bool helper_free_when_destroyed(free_when_destroyed);
600 free_when_destroyed = other.free_when_destroyed;
601 other.free_when_destroyed = helper_free_when_destroyed;
602 bool helper_is_sorted(is_sorted);
603 is_sorted = other.is_sorted;
604 other.is_sorted = helper_is_sorted;
617 bool free_when_destroyed:1;
void set_used(u32 usedNow)
Sets the size of the array and allocates new elements if necessary.
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
s32 binary_search(const T &element) const
Performs a binary search for an element if possible, returns -1 if not found.
array()
Default constructor for empty array.
bool operator==(const array< T, TAlloc > &other) const
Equality operator.
GLuint GLuint GLsizei count
const T * const_pointer() const
Gets a const pointer to the array.
void sort()
Sorts the array using heapsort.
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
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.
Everything in the Irrlicht Engine can be found in this namespace.
u32 allocated_size() const
Get amount of memory allocated.
T & getLast()
Gets last element.
bool operator !=(const array< T, TAlloc > &other) const
Inequality operator.
void setAllocStrategy(eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
set a new allocation strategy
s32 binary_search(const T &element, s32 left, s32 right) const
Performs a binary search for an element, returns -1 if not found.
const array< T, TAlloc > & operator=(const array< T, TAlloc > &other)
Assignment operator.
void push_back(const T &element)
Adds an element at back of array.
bool empty() const
Check if array is empty.
eAllocStrategy
defines an allocation strategy (used only by irr::array so far)
void set_sorted(bool _is_sorted)
Sets if the array is sorted.
s32 linear_reverse_search(const T &element) const
Finds an element in linear time, which is very slow.
T & operator [](u32 index)
Direct access operator.
s32 binary_search_multi(const T &element, s32 &last)
signed int s32
32 bit signed variable.
void erase(u32 index, s32 count)
Erases some elements from the array.
unsigned int u32
32 bit unsigned variable.
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
void heapsort(T *array_, s32 size)
Sorts an array with size 'size' using heapsort.
u32 size() const
Get number of occupied elements of the array.
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
void swap(array< T, TAlloc > &other)
Swap the content of this array container with the content of another array.
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
void erase(u32 index)
Erases an element from the array.
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
s32 linear_search(const T &element) const
Finds an element in linear time, which is very slow.
const T & operator [](u32 index) const
Direct const access operator.
Self reallocating template array (like stl vector) with additional features.
void clear()
Clears the array and deletes all allocated memory.
array(u32 start_count)
Constructs an array and allocates an initial chunk of memory.
void push_front(const T &element)
Adds an element at the front of the array.
array(const array< T, TAlloc > &other)
Copy constructor.
const T & getLast() const
Gets last element.
T * pointer()
Gets a pointer to the array.
void set_free_when_destroyed(bool f)
Sets if the array should delete the memory it uses upon destruction.