arsa  2.7
Public Types | Public Member Functions | List of all members
irr::core::array< T, TAlloc > Class Template Reference

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

#include <irrArray.h>

Public Types

typedef TAlloc allocator_type
 
typedef T value_type
 
typedef u32 size_type
 

Public Member Functions

 array ()
 Default constructor for empty array. More...
 
 array (u32 start_count)
 Constructs an array and allocates an initial chunk of memory. More...
 
 array (const array< T, TAlloc > &other)
 Copy constructor. More...
 
 ~array ()
 Destructor. More...
 
void reallocate (u32 new_size, bool canShrink=true)
 Reallocates the array, make it bigger or smaller. More...
 
void setAllocStrategy (eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
 set a new allocation strategy More...
 
void push_back (const T &element)
 Adds an element at back of array. More...
 
void push_front (const T &element)
 Adds an element at the front of the array. More...
 
void insert (const T &element, u32 index=0)
 Insert item into array at specified position. More...
 
void clear ()
 Clears the array and deletes all allocated memory. More...
 
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. More...
 
void set_free_when_destroyed (bool f)
 Sets if the array should delete the memory it uses upon destruction. More...
 
void set_used (u32 usedNow)
 Sets the size of the array and allocates new elements if necessary. More...
 
const array< T, TAlloc > & operator= (const array< T, TAlloc > &other)
 Assignment operator. More...
 
bool operator== (const array< T, TAlloc > &other) const
 Equality operator. More...
 
bool operator != (const array< T, TAlloc > &other) const
 Inequality operator. More...
 
T & operator [] (u32 index)
 Direct access operator. More...
 
const T & operator [] (u32 index) const
 Direct const access operator. More...
 
T & getLast ()
 Gets last element. More...
 
const T & getLast () const
 Gets last element. More...
 
T * pointer ()
 Gets a pointer to the array. More...
 
const T * const_pointer () const
 Gets a const pointer to the array. More...
 
u32 size () const
 Get number of occupied elements of the array. More...
 
u32 allocated_size () const
 Get amount of memory allocated. More...
 
bool empty () const
 Check if array is empty. More...
 
void sort ()
 Sorts the array using heapsort. More...
 
s32 binary_search (const T &element)
 Performs a binary search for an element, returns -1 if not found. More...
 
s32 binary_search (const T &element) const
 Performs a binary search for an element if possible, returns -1 if not found. More...
 
s32 binary_search (const T &element, s32 left, s32 right) const
 Performs a binary search for an element, returns -1 if not found. More...
 
s32 binary_search_multi (const T &element, s32 &last)
 
s32 linear_search (const T &element) const
 Finds an element in linear time, which is very slow. More...
 
s32 linear_reverse_search (const T &element) const
 Finds an element in linear time, which is very slow. More...
 
void erase (u32 index)
 Erases an element from the array. More...
 
void erase (u32 index, s32 count)
 Erases some elements from the array. More...
 
void set_sorted (bool _is_sorted)
 Sets if the array is sorted. More...
 
void swap (array< T, TAlloc > &other)
 Swap the content of this array container with the content of another array. More...
 

Detailed Description

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

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

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

Definition at line 22 of file irrArray.h.

Member Typedef Documentation

◆ allocator_type

template<class T, typename TAlloc = irrAllocator<T>>
typedef TAlloc irr::core::array< T, TAlloc >::allocator_type

Definition at line 607 of file irrArray.h.

◆ size_type

template<class T, typename TAlloc = irrAllocator<T>>
typedef u32 irr::core::array< T, TAlloc >::size_type

Definition at line 609 of file irrArray.h.

◆ value_type

template<class T, typename TAlloc = irrAllocator<T>>
typedef T irr::core::array< T, TAlloc >::value_type

Definition at line 608 of file irrArray.h.

Constructor & Destructor Documentation

◆ array() [1/3]

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

Default constructor for empty array.

Definition at line 28 of file irrArray.h.

28  : data(0), allocated(0), used(0),
29  strategy(ALLOC_STRATEGY_DOUBLE), free_when_destroyed(true), is_sorted(true)
30  {
31  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974

◆ array() [2/3]

template<class T, typename TAlloc = irrAllocator<T>>
irr::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.

Definition at line 36 of file irrArray.h.

36  : data(0), allocated(0), used(0),
37  strategy(ALLOC_STRATEGY_DOUBLE),
38  free_when_destroyed(true), is_sorted(true)
39  {
40  reallocate(start_count);
41  }
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition: irrArray.h:66
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974

◆ array() [3/3]

template<class T, typename TAlloc = irrAllocator<T>>
irr::core::array< T, TAlloc >::array ( const array< T, TAlloc > &  other)
inline

Copy constructor.

Definition at line 45 of file irrArray.h.

45  : data(0)
46  {
47  *this = other;
48  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974

◆ ~array()

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

Destructor.

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

Definition at line 54 of file irrArray.h.

55  {
56  clear();
57  }
void clear()
Clears the array and deletes all allocated memory.
Definition: irrArray.h:199

Member Function Documentation

◆ allocated_size()

template<class T, typename TAlloc = irrAllocator<T>>
u32 irr::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);

Definition at line 373 of file irrArray.h.

374  {
375  return allocated;
376  }

◆ binary_search() [1/3]

template<class T, typename TAlloc = irrAllocator<T>>
s32 irr::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.

Definition at line 405 of file irrArray.h.

406  {
407  sort();
408  return binary_search(element, 0, used-1);
409  }
void sort()
Sorts the array using heapsort.
Definition: irrArray.h:390
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition: irrArray.h:405

◆ binary_search() [2/3]

template<class T, typename TAlloc = irrAllocator<T>>
s32 irr::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.

Definition at line 418 of file irrArray.h.

419  {
420  if (is_sorted)
421  return binary_search(element, 0, used-1);
422  else
423  return linear_search(element);
424  }
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition: irrArray.h:405
s32 linear_search(const T &element) const
Finds an element in linear time, which is very slow.
Definition: irrArray.h:502

◆ binary_search() [3/3]

template<class T, typename TAlloc = irrAllocator<T>>
s32 irr::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.

Definition at line 433 of file irrArray.h.

434  {
435  if (!used)
436  return -1;
437 
438  s32 m;
439 
440  do
441  {
442  m = (left+right)>>1;
443 
444  if (element < data[m])
445  right = m - 1;
446  else
447  left = m + 1;
448 
449  } while((element < data[m] || data[m] < element) && left<=right);
450  // this last line equals to:
451  // " while((element != array[m]) && left<=right);"
452  // but we only want to use the '<' operator.
453  // the same in next line, it is "(element == array[m])"
454 
455 
456  if (!(element < data[m]) && !(data[m] < element))
457  return m;
458 
459  return -1;
460  }
const GLfloat * m
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
GLint left
GLdouble GLdouble right

◆ binary_search_multi()

template<class T, typename TAlloc = irrAllocator<T>>
s32 irr::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.

Definition at line 471 of file irrArray.h.

472  {
473  sort();
474  s32 index = binary_search(element, 0, used-1);
475  if ( index < 0 )
476  return index;
477 
478  // The search can be somewhere in the middle of the set
479  // look linear previous and past the index
480  last = index;
481 
482  while ( index > 0 && !(element < data[index - 1]) && !(data[index - 1] < element) )
483  {
484  index -= 1;
485  }
486  // look linear up
487  while ( last < (s32) used - 1 && !(element < data[last + 1]) && !(data[last + 1] < element) )
488  {
489  last += 1;
490  }
491 
492  return index;
493  }
void sort()
Sorts the array using heapsort.
Definition: irrArray.h:390
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition: irrArray.h:405
GLuint index

◆ clear()

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

Clears the array and deletes all allocated memory.

Definition at line 199 of file irrArray.h.

200  {
201  if (free_when_destroyed)
202  {
203  for (u32 i=0; i<used; ++i)
204  allocator.destruct(&data[i]);
205 
206  allocator.deallocate(data); // delete [] data;
207  }
208  data = 0;
209  used = 0;
210  allocated = 0;
211  is_sorted = true;
212  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62

◆ const_pointer()

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

Gets a const pointer to the array.

Returns
Pointer to the array.

Definition at line 356 of file irrArray.h.

357  {
358  return data;
359  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974

◆ empty()

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

Check if array is empty.

Returns
True if the array is empty false if not.

Definition at line 381 of file irrArray.h.

382  {
383  return used == 0;
384  }

◆ erase() [1/2]

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 532 of file irrArray.h.

533  {
534  _IRR_DEBUG_BREAK_IF(index>=used) // access violation
535 
536  for (u32 i=index+1; i<used; ++i)
537  {
538  allocator.destruct(&data[i-1]);
539  allocator.construct(&data[i-1], data[i]); // data[i-1] = data[i];
540  }
541 
542  allocator.destruct(&data[used-1]);
543 
544  --used;
545  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
GLuint index

◆ erase() [2/2]

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 553 of file irrArray.h.

554  {
555  if (index>=used || count<1)
556  return;
557  if (index+count>used)
558  count = used-index;
559 
560  u32 i;
561  for (i=index; i<index+count; ++i)
562  allocator.destruct(&data[i]);
563 
564  for (i=index+count; i<used; ++i)
565  {
566  if (i-count >= index+count) // not already destructed before loop
567  allocator.destruct(&data[i-count]);
568 
569  allocator.construct(&data[i-count], data[i]); // data[i-count] = data[i];
570 
571  if (i >= used-count) // those which are not overwritten
572  allocator.destruct(&data[i]);
573  }
574 
575  used-= count;
576  }
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
GLuint index

◆ getLast() [1/2]

template<class T, typename TAlloc = irrAllocator<T>>
T& irr::core::array< T, TAlloc >::getLast ( )
inline

Gets last element.

Definition at line 329 of file irrArray.h.

330  {
331  _IRR_DEBUG_BREAK_IF(!used) // access violation
332 
333  return data[used-1];
334  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185

◆ getLast() [2/2]

template<class T, typename TAlloc = irrAllocator<T>>
const T& irr::core::array< T, TAlloc >::getLast ( ) const
inline

Gets last element.

Definition at line 338 of file irrArray.h.

339  {
340  _IRR_DEBUG_BREAK_IF(!used) // access violation
341 
342  return data[used-1];
343  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185

◆ insert()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 132 of file irrArray.h.

133  {
134  _IRR_DEBUG_BREAK_IF(index>used) // access violation
135 
136  if (used + 1 > allocated)
137  {
138  // this doesn't work if the element is in the same
139  // array. So we'll copy the element first to be sure
140  // we'll get no data corruption
141  const T e(element);
142 
143  // increase data block
144  u32 newAlloc;
145  switch ( strategy )
146  {
148  newAlloc = used + 5 + (allocated < 500 ? used : used >> 2);
149  break;
150  default:
151  case ALLOC_STRATEGY_SAFE:
152  newAlloc = used + 1;
153  break;
154  }
155  reallocate( newAlloc);
156 
157  // move array content and construct new element
158  // first move end one up
159  for (u32 i=used; i>index; --i)
160  {
161  if (i<used)
162  allocator.destruct(&data[i]);
163  allocator.construct(&data[i], data[i-1]); // data[i] = data[i-1];
164  }
165  // then add new element
166  if (used > index)
167  allocator.destruct(&data[index]);
168  allocator.construct(&data[index], e); // data[index] = e;
169  }
170  else
171  {
172  // element inserted not at end
173  if ( used > index )
174  {
175  // create one new element at the end
176  allocator.construct(&data[used], data[used-1]);
177 
178  // move the rest of the array content
179  for (u32 i=used-1; i>index; --i)
180  {
181  data[i] = data[i-1];
182  }
183  // insert the new element
184  data[index] = element;
185  }
186  else
187  {
188  // insert the new element to the end
189  allocator.construct(&data[index], element);
190  }
191  }
192  // set to false as we don't know if we have the comparison operators
193  is_sorted = false;
194  ++used;
195  }
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition: irrArray.h:66
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
GLuint index

◆ linear_reverse_search()

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

Finds an element in linear time, which is very slow.

Use binary_search for faster finding. Only works if ==operator is implemented.

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

Definition at line 518 of file irrArray.h.

519  {
520  for (s32 i=used-1; i>=0; --i)
521  if (data[i] == element)
522  return i;
523 
524  return -1;
525  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70

◆ linear_search()

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

Finds an element in linear time, which is very slow.

Use binary_search for faster finding. Only works if ==operator is implemented.

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

Definition at line 502 of file irrArray.h.

503  {
504  for (u32 i=0; i<used; ++i)
505  if (element == data[i])
506  return (s32)i;
507 
508  return -1;
509  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62

◆ operator !=()

template<class T, typename TAlloc = irrAllocator<T>>
bool irr::core::array< T, TAlloc >::operator != ( const array< T, TAlloc > &  other) const
inline

Inequality operator.

Definition at line 304 of file irrArray.h.

305  {
306  return !(*this==other);
307  }

◆ operator []() [1/2]

template<class T, typename TAlloc = irrAllocator<T>>
T& irr::core::array< T, TAlloc >::operator [] ( u32  index)
inline

Direct access operator.

Definition at line 311 of file irrArray.h.

312  {
313  _IRR_DEBUG_BREAK_IF(index>=used) // access violation
314 
315  return data[index];
316  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
GLuint index

◆ operator []() [2/2]

template<class T, typename TAlloc = irrAllocator<T>>
const T& irr::core::array< T, TAlloc >::operator [] ( u32  index) const
inline

Direct const access operator.

Definition at line 320 of file irrArray.h.

321  {
322  _IRR_DEBUG_BREAK_IF(index>=used) // access violation
323 
324  return data[index];
325  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
GLuint index

◆ operator=()

template<class T, typename TAlloc = irrAllocator<T>>
const array<T, TAlloc>& irr::core::array< T, TAlloc >::operator= ( const array< T, TAlloc > &  other)
inline

Assignment operator.

Definition at line 263 of file irrArray.h.

264  {
265  if (this == &other)
266  return *this;
267  strategy = other.strategy;
268 
269  if (data)
270  clear();
271 
272  //if (allocated < other.allocated)
273  if (other.allocated == 0)
274  data = 0;
275  else
276  data = allocator.allocate(other.allocated); // new T[other.allocated];
277 
278  used = other.used;
279  free_when_destroyed = true;
280  is_sorted = other.is_sorted;
281  allocated = other.allocated;
282 
283  for (u32 i=0; i<other.used; ++i)
284  allocator.construct(&data[i], other.data[i]); // data[i] = other.data[i];
285 
286  return *this;
287  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
void clear()
Clears the array and deletes all allocated memory.
Definition: irrArray.h:199

◆ operator==()

template<class T, typename TAlloc = irrAllocator<T>>
bool irr::core::array< T, TAlloc >::operator== ( const array< T, TAlloc > &  other) const
inline

Equality operator.

Definition at line 291 of file irrArray.h.

292  {
293  if (used != other.used)
294  return false;
295 
296  for (u32 i=0; i<other.used; ++i)
297  if (data[i] != other[i])
298  return false;
299  return true;
300  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62

◆ pointer()

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

Gets a pointer to the array.

Returns
Pointer to the array.

Definition at line 348 of file irrArray.h.

349  {
350  return data;
351  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974

◆ push_back()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 111 of file irrArray.h.

112  {
113  insert(element, used);
114  }
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
Definition: irrArray.h:132

◆ push_front()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 122 of file irrArray.h.

123  {
124  insert(element);
125  }
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
Definition: irrArray.h:132

◆ reallocate()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 66 of file irrArray.h.

67  {
68  if (allocated==new_size)
69  return;
70  if (!canShrink && (new_size < allocated))
71  return;
72 
73  T* old_data = data;
74 
75  data = allocator.allocate(new_size); //new T[new_size];
76  allocated = new_size;
77 
78  // copy old data
79  const s32 end = used < new_size ? used : new_size;
80 
81  for (s32 i=0; i<end; ++i)
82  {
83  // data[i] = old_data[i];
84  allocator.construct(&data[i], old_data[i]);
85  }
86 
87  // destruct old data
88  for (u32 j=0; j<used; ++j)
89  allocator.destruct(&old_data[j]);
90 
91  if (allocated < used)
92  used = allocated;
93 
94  allocator.deallocate(old_data); //delete [] old_data;
95  }
GLuint GLuint end
Definition: SDL_opengl.h:1571
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62

◆ set_free_when_destroyed()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 243 of file irrArray.h.

244  {
245  free_when_destroyed = f;
246  }
GLfloat f

◆ set_pointer()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 224 of file irrArray.h.

225  {
226  clear();
227  data = newPointer;
228  allocated = size;
229  used = size;
230  is_sorted = _is_sorted;
231  free_when_destroyed=_free_when_destroyed;
232  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
u32 size() const
Get number of occupied elements of the array.
Definition: irrArray.h:364
void clear()
Clears the array and deletes all allocated memory.
Definition: irrArray.h:199

◆ set_sorted()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::core::array< T, TAlloc >::set_sorted ( bool  _is_sorted)
inline

Sets if the array is sorted.

Definition at line 580 of file irrArray.h.

581  {
582  is_sorted = _is_sorted;
583  }

◆ set_used()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 253 of file irrArray.h.

254  {
255  if (allocated < usedNow)
256  reallocate(usedNow);
257 
258  used = usedNow;
259  }
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition: irrArray.h:66

◆ setAllocStrategy()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 102 of file irrArray.h.

103  {
104  strategy = newStrategy;
105  }

◆ size()

template<class T, typename TAlloc = irrAllocator<T>>
u32 irr::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.

Definition at line 364 of file irrArray.h.

365  {
366  return used;
367  }

◆ sort()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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.

Definition at line 390 of file irrArray.h.

391  {
392  if (!is_sorted && used>1)
393  heapsort(data, used);
394  is_sorted = true;
395  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
void heapsort(T *array_, s32 size)
Sorts an array with size 'size' using heapsort.
Definition: heapsort.h:41

◆ swap()

template<class T, typename TAlloc = irrAllocator<T>>
void irr::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

Definition at line 590 of file irrArray.h.

591  {
592  core::swap(data, other.data);
593  core::swap(allocated, other.allocated);
594  core::swap(used, other.used);
595  core::swap(allocator, other.allocator); // memory is still released by the same allocator used for allocation
596  eAllocStrategy helper_strategy(strategy); // can't use core::swap with bitfields
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;
605  }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
eAllocStrategy
defines an allocation strategy (used only by irr::array so far)
Definition: irrAllocator.h:112
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition: irrMath.h:178

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