1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
\r
5 #ifndef __IRR_ARRAY_H_INCLUDED__
\r
6 #define __IRR_ARRAY_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
9 #include "heapsort.h"
\r
10 #include "irrAllocator.h"
\r
11 #include "irrMath.h"
\r
18 //! Self reallocating template array (like stl vector) with additional features.
\r
19 /** Some features are: Heap sorting, binary search methods, easier debugging.
\r
21 template <class T, typename TAlloc = irrAllocator<T> >
\r
27 //! Default constructor for empty array.
\r
28 array() : data(0), allocated(0), used(0),
\r
29 strategy(ALLOC_STRATEGY_DOUBLE), free_when_destroyed(true), is_sorted(true)
\r
34 //! Constructs an array and allocates an initial chunk of memory.
\r
35 /** \param start_count Amount of elements to pre-allocate. */
\r
36 explicit array(u32 start_count) : data(0), allocated(0), used(0),
\r
37 strategy(ALLOC_STRATEGY_DOUBLE),
\r
38 free_when_destroyed(true), is_sorted(true)
\r
40 reallocate(start_count);
\r
44 //! Copy constructor
\r
45 array(const array<T, TAlloc>& other) : data(0)
\r
52 /** Frees allocated memory, if set_free_when_destroyed was not set to
\r
53 false by the user before. */
\r
60 //! Reallocates the array, make it bigger or smaller.
\r
61 /** \param new_size New size of array.
\r
62 \param canShrink Specifies whether the array is reallocated even if
\r
63 enough space is available. Setting this flag to false can speed up
\r
64 array usage, but may use more memory than required by the data.
\r
66 void reallocate(u32 new_size, bool canShrink=true)
\r
68 if (allocated==new_size)
\r
70 if (!canShrink && (new_size < allocated))
\r
75 data = allocator.allocate(new_size); //new T[new_size];
\r
76 allocated = new_size;
\r
79 const s32 end = used < new_size ? used : new_size;
\r
81 for (s32 i=0; i<end; ++i)
\r
83 // data[i] = old_data[i];
\r
84 allocator.construct(&data[i], old_data[i]);
\r
87 // destruct old data
\r
88 for (u32 j=0; j<used; ++j)
\r
89 allocator.destruct(&old_data[j]);
\r
91 if (allocated < used)
\r
94 allocator.deallocate(old_data); //delete [] old_data;
\r
98 //! set a new allocation strategy
\r
99 /** if the maximum size of the array is unknown, you can define how big the
\r
100 allocation should happen.
\r
101 \param newStrategy New strategy to apply to this array. */
\r
102 void setAllocStrategy ( eAllocStrategy newStrategy = ALLOC_STRATEGY_DOUBLE )
\r
104 strategy = newStrategy;
\r
108 //! Adds an element at back of array.
\r
109 /** If the array is too small to add this new element it is made bigger.
\r
110 \param element: Element to add at the back of the array. */
\r
111 void push_back(const T& element)
\r
113 insert(element, used);
\r
117 //! Adds an element at the front of the array.
\r
118 /** If the array is to small to add this new element, the array is
\r
119 made bigger. Please note that this is slow, because the whole array
\r
120 needs to be copied for this.
\r
121 \param element Element to add at the back of the array. */
\r
122 void push_front(const T& element)
\r
128 //! Insert item into array at specified position.
\r
130 \param element: Element to be inserted
\r
131 \param index: Where position to insert the new element. */
\r
132 void insert(const T& element, u32 index=0)
\r
134 _IRR_DEBUG_BREAK_IF(index>used) // access violation
\r
136 if (used + 1 > allocated)
\r
138 // this doesn't work if the element is in the same
\r
139 // array. So we'll copy the element first to be sure
\r
140 // we'll get no data corruption
\r
141 const T e(element);
\r
143 // increase data block
\r
145 switch ( strategy )
\r
147 case ALLOC_STRATEGY_DOUBLE:
\r
148 newAlloc = used + 5 + (allocated < 500 ? used : used >> 2);
\r
151 case ALLOC_STRATEGY_SAFE:
\r
152 newAlloc = used + 1;
\r
155 reallocate( newAlloc);
\r
157 // move array content and construct new element
\r
158 // first move end one up
\r
159 for (u32 i=used; i>index; --i)
\r
162 allocator.destruct(&data[i]);
\r
163 allocator.construct(&data[i], data[i-1]); // data[i] = data[i-1];
\r
165 // then add new element
\r
167 allocator.destruct(&data[index]);
\r
168 allocator.construct(&data[index], e); // data[index] = e;
\r
172 // element inserted not at end
\r
173 if ( used > index )
\r
175 // create one new element at the end
\r
176 allocator.construct(&data[used], data[used-1]);
\r
178 // move the rest of the array content
\r
179 for (u32 i=used-1; i>index; --i)
\r
181 data[i] = data[i-1];
\r
183 // insert the new element
\r
184 data[index] = element;
\r
188 // insert the new element to the end
\r
189 allocator.construct(&data[index], element);
\r
192 // set to false as we don't know if we have the comparison operators
\r
198 //! Clears the array and deletes all allocated memory.
\r
201 if (free_when_destroyed)
\r
203 for (u32 i=0; i<used; ++i)
\r
204 allocator.destruct(&data[i]);
\r
206 allocator.deallocate(data); // delete [] data;
\r
215 //! Sets pointer to new array, using this as new workspace.
\r
216 /** Make sure that set_free_when_destroyed is used properly.
\r
217 \param newPointer: Pointer to new array of elements.
\r
218 \param size: Size of the new array.
\r
219 \param _is_sorted Flag which tells whether the new array is already
\r
221 \param _free_when_destroyed Sets whether the new memory area shall be
\r
222 freed by the array upon destruction, or if this will be up to the user
\r
224 void set_pointer(T* newPointer, u32 size, bool _is_sorted=false, bool _free_when_destroyed=true)
\r
230 is_sorted = _is_sorted;
\r
231 free_when_destroyed=_free_when_destroyed;
\r
234 //! Set (copy) data from given memory block
\r
235 /** \param newData data to set, must have newSize elements
\r
236 \param newSize Amount of elements in newData
\r
237 \param canShrink When true we reallocate the array even it can shrink.
\r
238 May reduce memory usage, but call is more whenever size changes.
\r
239 \param newDataIsSorted Info if you pass sorted/unsorted data
\r
241 void set_data(const T* newData, u32 newSize, bool newDataIsSorted=false, bool canShrink=false)
\r
243 reallocate(newSize, canShrink);
\r
245 for ( u32 i=0; i<newSize; ++i)
\r
247 data[i] = newData[i];
\r
249 is_sorted = newDataIsSorted;
\r
252 //! Compare if given data block is identical to the data in our array
\r
253 /** Like operator ==, but without the need to create the array
\r
254 \param otherData Address to data against which we compare, must contain size elements
\r
255 \param size Amount of elements in otherData */
\r
256 bool equals(const T* otherData, u32 size) const
\r
261 for (u32 i=0; i<size; ++i)
\r
262 if (data[i] != otherData[i])
\r
269 //! Sets if the array should delete the memory it uses upon destruction.
\r
270 /** Also clear and set_pointer will only delete the (original) memory
\r
271 area if this flag is set to true, which is also the default. The
\r
272 methods reallocate, set_used, push_back, push_front, insert, and erase
\r
273 will still try to deallocate the original memory, which might cause
\r
274 troubles depending on the intended use of the memory area.
\r
275 \param f If true, the array frees the allocated memory in its
\r
276 destructor, otherwise not. The default is true. */
\r
277 void set_free_when_destroyed(bool f)
\r
279 free_when_destroyed = f;
\r
283 //! Sets the size of the array and allocates new elements if necessary.
\r
284 /** Please note: This is only secure when using it with simple types,
\r
285 because no default constructor will be called for the added elements.
\r
286 \param usedNow Amount of elements now used. */
\r
287 void set_used(u32 usedNow)
\r
289 if (allocated < usedNow)
\r
290 reallocate(usedNow);
\r
295 //! Assignment operator
\r
296 const array<T, TAlloc>& operator=(const array<T, TAlloc>& other)
\r
298 if (this == &other)
\r
300 strategy = other.strategy;
\r
305 //if (allocated < other.allocated)
\r
306 if (other.allocated == 0)
\r
309 data = allocator.allocate(other.allocated); // new T[other.allocated];
\r
312 free_when_destroyed = true;
\r
313 is_sorted = other.is_sorted;
\r
314 allocated = other.allocated;
\r
316 for (u32 i=0; i<other.used; ++i)
\r
317 allocator.construct(&data[i], other.data[i]); // data[i] = other.data[i];
\r
323 //! Equality operator
\r
324 bool operator == (const array<T, TAlloc>& other) const
\r
326 return equals(other.const_pointer(), other.size());
\r
330 //! Inequality operator
\r
331 bool operator != (const array<T, TAlloc>& other) const
\r
333 return !(*this==other);
\r
337 //! Direct access operator
\r
338 T& operator [](u32 index)
\r
340 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
\r
342 return data[index];
\r
346 //! Direct const access operator
\r
347 const T& operator [](u32 index) const
\r
349 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
\r
351 return data[index];
\r
355 //! Gets last element.
\r
358 _IRR_DEBUG_BREAK_IF(!used) // access violation
\r
360 return data[used-1];
\r
364 //! Gets last element
\r
365 const T& getLast() const
\r
367 _IRR_DEBUG_BREAK_IF(!used) // access violation
\r
369 return data[used-1];
\r
373 //! Gets a pointer to the array.
\r
374 /** \return Pointer to the array. */
\r
381 //! Gets a const pointer to the array.
\r
382 /** \return Pointer to the array. */
\r
383 const T* const_pointer() const
\r
389 //! Get number of occupied elements of the array.
\r
390 /** \return Size of elements in the array which are actually occupied. */
\r
397 //! Get amount of memory allocated.
\r
398 /** \return Amount of memory allocated. The amount of bytes
\r
399 allocated would be allocated_size() * sizeof(ElementTypeUsed); */
\r
400 u32 allocated_size() const
\r
406 //! Check if array is empty.
\r
407 /** \return True if the array is empty false if not. */
\r
414 //! Sorts the array using heapsort.
\r
415 /** There is no additional memory waste and the algorithm performs
\r
416 O(n*log n) in worst case. */
\r
419 if (!is_sorted && used>1)
\r
420 heapsort(data, used);
\r
425 //! Performs a binary search for an element, returns -1 if not found.
\r
426 /** The array will be sorted before the binary search if it is not
\r
427 already sorted. Caution is advised! Be careful not to call this on
\r
428 unsorted const arrays, or the slower method will be used.
\r
429 \param element Element to search for.
\r
430 \return Position of the searched element if it was found,
\r
431 otherwise -1 is returned. */
\r
432 s32 binary_search(const T& element)
\r
435 return binary_search(element, 0, used-1);
\r
439 //! Performs a binary search for an element if possible, returns -1 if not found.
\r
440 /** This method is for const arrays and so cannot call sort(), if the array is
\r
441 not sorted then linear_search will be used instead. Potentially very slow!
\r
442 \param element Element to search for.
\r
443 \return Position of the searched element if it was found,
\r
444 otherwise -1 is returned. */
\r
445 s32 binary_search(const T& element) const
\r
448 return binary_search(element, 0, used-1);
\r
450 return linear_search(element);
\r
454 //! Performs a binary search for an element, returns -1 if not found.
\r
455 /** \param element: Element to search for.
\r
456 \param left First left index
\r
457 \param right Last right index.
\r
458 \return Position of the searched element if it was found, otherwise -1
\r
460 s32 binary_search(const T& element, s32 left, s32 right) const
\r
469 m = (left+right)>>1;
\r
471 if (element < data[m])
\r
476 } while((element < data[m] || data[m] < element) && left<=right);
\r
477 // this last line equals to:
\r
478 // " while((element != array[m]) && left<=right);"
\r
479 // but we only want to use the '<' operator.
\r
480 // the same in next line, it is "(element == array[m])"
\r
483 if (!(element < data[m]) && !(data[m] < element))
\r
490 //! Performs a binary search for an element, returns -1 if not found.
\r
491 //! it is used for searching a multiset
\r
492 /** The array will be sorted before the binary search if it is not
\r
494 \param element Element to search for.
\r
495 \param &last return lastIndex of equal elements
\r
496 \return Position of the first searched element if it was found,
\r
497 otherwise -1 is returned. */
\r
498 s32 binary_search_multi(const T& element, s32 &last)
\r
501 s32 index = binary_search(element, 0, used-1);
\r
505 // The search can be somewhere in the middle of the set
\r
506 // look linear previous and past the index
\r
509 while ( index > 0 && !(element < data[index - 1]) && !(data[index - 1] < element) )
\r
514 while ( last < (s32) used - 1 && !(element < data[last + 1]) && !(data[last + 1] < element) )
\r
523 //! Finds an element in linear time, which is very slow.
\r
524 /** Use binary_search for faster finding. Only works if ==operator is
\r
526 \param element Element to search for.
\r
527 \return Position of the searched element if it was found, otherwise -1
\r
529 s32 linear_search(const T& element) const
\r
531 for (u32 i=0; i<used; ++i)
\r
532 if (element == data[i])
\r
539 //! Finds an element in linear time, which is very slow.
\r
540 /** Use binary_search for faster finding. Only works if ==operator is
\r
542 \param element: Element to search for.
\r
543 \return Position of the searched element if it was found, otherwise -1
\r
545 s32 linear_reverse_search(const T& element) const
\r
547 for (s32 i=used-1; i>=0; --i)
\r
548 if (data[i] == element)
\r
555 //! Erases an element from the array.
\r
556 /** May be slow, because all elements following after the erased
\r
557 element have to be copied.
\r
558 \param index: Index of element to be erased. */
\r
559 void erase(u32 index)
\r
561 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
\r
563 for (u32 i=index+1; i<used; ++i)
\r
565 allocator.destruct(&data[i-1]);
\r
566 allocator.construct(&data[i-1], data[i]); // data[i-1] = data[i];
\r
569 allocator.destruct(&data[used-1]);
\r
575 //! Erases some elements from the array.
\r
576 /** May be slow, because all elements following after the erased
\r
577 element have to be copied.
\r
578 \param index: Index of the first element to be erased.
\r
579 \param count: Amount of elements to be erased. */
\r
580 void erase(u32 index, s32 count)
\r
582 if (index>=used || count<1)
\r
584 if (index+count>used)
\r
585 count = used-index;
\r
588 for (i=index; i<index+count; ++i)
\r
589 allocator.destruct(&data[i]);
\r
591 for (i=index+count; i<used; ++i)
\r
593 if (i-count >= index+count) // not already destructed before loop
\r
594 allocator.destruct(&data[i-count]);
\r
596 allocator.construct(&data[i-count], data[i]); // data[i-count] = data[i];
\r
598 if (i >= used-count) // those which are not overwritten
\r
599 allocator.destruct(&data[i]);
\r
606 //! Sets if the array is sorted
\r
607 void set_sorted(bool _is_sorted)
\r
609 is_sorted = _is_sorted;
\r
613 //! Swap the content of this array container with the content of another array
\r
614 /** Afterward this object will contain the content of the other object and the other
\r
615 object will contain the content of this object.
\r
616 \param other Swap content with this object */
\r
617 void swap(array<T, TAlloc>& other)
\r
619 core::swap(data, other.data);
\r
620 core::swap(allocated, other.allocated);
\r
621 core::swap(used, other.used);
\r
622 core::swap(allocator, other.allocator); // memory is still released by the same allocator used for allocation
\r
623 eAllocStrategy helper_strategy(strategy); // can't use core::swap with bitfields
\r
624 strategy = other.strategy;
\r
625 other.strategy = helper_strategy;
\r
626 bool helper_free_when_destroyed(free_when_destroyed);
\r
627 free_when_destroyed = other.free_when_destroyed;
\r
628 other.free_when_destroyed = helper_free_when_destroyed;
\r
629 bool helper_is_sorted(is_sorted);
\r
630 is_sorted = other.is_sorted;
\r
631 other.is_sorted = helper_is_sorted;
\r
634 typedef TAlloc allocator_type;
\r
635 typedef T value_type;
\r
636 typedef u32 size_type;
\r
643 eAllocStrategy strategy:4;
\r
644 bool free_when_destroyed:1;
\r
649 } // end namespace core
\r
650 } // end namespace irr
\r