2
0

irrArray.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright (C) 2002-2005 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
  4. #ifndef __IRR_ARRAY_H_INCLUDED__
  5. #define __IRR_ARRAY_H_INCLUDED__
  6. #include "irrTypes.h"
  7. #include "heapsort.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. //! Self reallocating template array (like stl vector) with additional features.
  13. /** Some features are: Heap sorting, binary search methods, easier debugging.
  14. */
  15. template <class T>
  16. class array
  17. {
  18. public:
  19. array()
  20. : data(0), allocated(0), used(0),
  21. free_when_destroyed(true), is_sorted(true)
  22. {
  23. }
  24. //! Constructs a array and allocates an initial chunk of memory.
  25. //! \param start_count: Amount of elements to allocate.
  26. array(u32 start_count)
  27. : data(0), allocated(0), used(0),
  28. free_when_destroyed(true), is_sorted(true)
  29. {
  30. reallocate(start_count);
  31. }
  32. //! Copy constructor
  33. array(const array<T>& other)
  34. : data(0)
  35. {
  36. *this = other;
  37. }
  38. //! Destructor. Frees allocated memory, if set_free_when_destroyed
  39. //! was not set to false by the user before.
  40. ~array()
  41. {
  42. if (free_when_destroyed)
  43. delete [] data;
  44. }
  45. //! Reallocates the array, make it bigger or smaller.
  46. //! \param new_size: New size of array.
  47. void reallocate(u32 new_size)
  48. {
  49. T* old_data = data;
  50. data = new T[new_size];
  51. allocated = new_size;
  52. s32 end = used < new_size ? used : new_size;
  53. for (s32 i=0; i<end; ++i)
  54. data[i] = old_data[i];
  55. if (allocated < used)
  56. used = allocated;
  57. delete [] old_data;
  58. }
  59. //! Adds an element at back of array. If the array is to small to
  60. //! add this new element, the array is made bigger.
  61. //! \param element: Element to add at the back of the array.
  62. void push_back(const T& element)
  63. {
  64. if (used + 1 > allocated)
  65. {
  66. // reallocate(used * 2 +1);
  67. // this doesn't work if the element is in the same array. So
  68. // we'll copy the element first to be sure we'll get no data
  69. // corruption
  70. T e;
  71. e = element; // copy element
  72. reallocate(used * 2 +1); // increase data block
  73. data[used++] = e; // push_back
  74. is_sorted = false;
  75. return;
  76. }
  77. data[used++] = element;
  78. is_sorted = false;
  79. }
  80. //! Adds an element at the front of the array. If the array is to small to
  81. //! add this new element, the array is made bigger. Please note that this
  82. //! is slow, because the whole array needs to be copied for this.
  83. //! \param element: Element to add at the back of the array.
  84. void push_front(const T& element)
  85. {
  86. if (used + 1 > allocated)
  87. reallocate(used * 2 +1);
  88. for (int i=(int)used; i>0; --i)
  89. data[i] = data[i-1];
  90. data[0] = element;
  91. is_sorted = false;
  92. ++used;
  93. }
  94. //! Insert item into array at specified position. Please use this
  95. //! only if you know what you are doing (possible performance loss).
  96. //! The preferred method of adding elements should be push_back().
  97. //! \param element: Element to be inserted
  98. //! \param index: Where position to insert the new element.
  99. void insert(const T& element, u32 index=0)
  100. {
  101. _IRR_DEBUG_BREAK_IF(index>used) // access violation
  102. if (used + 1 > allocated)
  103. reallocate(used * 2 +1);
  104. for (u32 i=used++; i>index; i--)
  105. data[i] = data[i-1];
  106. data[index] = element;
  107. is_sorted = false;
  108. }
  109. //! Clears the array and deletes all allocated memory.
  110. void clear()
  111. {
  112. delete [] data;
  113. data = 0;
  114. used = 0;
  115. allocated = 0;
  116. is_sorted = true;
  117. }
  118. //! Sets pointer to new array, using this as new workspace.
  119. //! \param newPointer: Pointer to new array of elements.
  120. //! \param size: Size of the new array.
  121. void set_pointer(T* newPointer, u32 size)
  122. {
  123. delete [] data;
  124. data = newPointer;
  125. allocated = size;
  126. used = size;
  127. is_sorted = false;
  128. }
  129. //! Sets if the array should delete the memory it used.
  130. //! \param f: If true, the array frees the allocated memory in its
  131. //! destructor, otherwise not. The default is true.
  132. void set_free_when_destroyed(bool f)
  133. {
  134. free_when_destroyed = f;
  135. }
  136. //! Sets the size of the array.
  137. //! \param usedNow: Amount of elements now used.
  138. void set_used(u32 usedNow)
  139. {
  140. if (allocated < usedNow)
  141. reallocate(usedNow);
  142. used = usedNow;
  143. }
  144. //! Assignement operator
  145. void operator=(const array<T>& other)
  146. {
  147. if (data)
  148. delete [] data;
  149. //if (allocated < other.allocated)
  150. if (other.allocated == 0)
  151. data = 0;
  152. else
  153. data = new T[other.allocated];
  154. used = other.used;
  155. free_when_destroyed = other.free_when_destroyed;
  156. is_sorted = other.is_sorted;
  157. allocated = other.allocated;
  158. for (u32 i=0; i<other.used; ++i)
  159. data[i] = other.data[i];
  160. }
  161. //! Direct access operator
  162. T& operator [](u32 index)
  163. {
  164. _IRR_DEBUG_BREAK_IF(index>=used) // access violation
  165. return data[index];
  166. }
  167. //! Direct access operator
  168. const T& operator [](u32 index) const
  169. {
  170. _IRR_DEBUG_BREAK_IF(index>=used) // access violation
  171. return data[index];
  172. }
  173. //! Gets last frame
  174. const T& getLast() const
  175. {
  176. _IRR_DEBUG_BREAK_IF(!used) // access violation
  177. return data[used-1];
  178. }
  179. //! Gets last frame
  180. T& getLast()
  181. {
  182. _IRR_DEBUG_BREAK_IF(!used) // access violation
  183. return data[used-1];
  184. }
  185. //! Returns a pointer to the array.
  186. //! \return Pointer to the array.
  187. T* pointer()
  188. {
  189. return data;
  190. }
  191. //! Returns a const pointer to the array.
  192. //! \return Pointer to the array.
  193. const T* const_pointer() const
  194. {
  195. return data;
  196. }
  197. //! Returns size of used array.
  198. //! \return Size of elements in the array.
  199. u32 size() const
  200. {
  201. return used;
  202. }
  203. //! Returns amount memory allocated.
  204. //! \return Returns amount of memory allocated. The amount of bytes
  205. //! allocated would be allocated_size() * sizeof(ElementsUsed);
  206. u32 allocated_size() const
  207. {
  208. return allocated;
  209. }
  210. //! Returns true if array is empty
  211. //! \return True if the array is empty, false if not.
  212. bool empty() const
  213. {
  214. return used == 0;
  215. }
  216. //! Sorts the array using heapsort. There is no additional memory waste and
  217. //! the algorithm performs (O) n log n in worst case.
  218. void sort()
  219. {
  220. if (is_sorted || used<2)
  221. return;
  222. heapsort(data, used);
  223. is_sorted = true;
  224. }
  225. //! Performs a binary search for an element, returns -1 if not found.
  226. //! The array will be sorted before the binary search if it is not
  227. //! already sorted.
  228. //! \param element: Element to search for.
  229. //! \return Returns position of the searched element if it was found,
  230. //! otherwise -1 is returned.
  231. s32 binary_search(const T& element)
  232. {
  233. return binary_search(element, 0, used-1);
  234. }
  235. //! Performs a binary search for an element, returns -1 if not found.
  236. //! The array will be sorted before the binary search if it is not
  237. //! already sorted.
  238. //! \param element: Element to search for.
  239. //! \param left: First left index
  240. //! \param right: Last right index.
  241. //! \return Returns position of the searched element if it was found,
  242. //! otherwise -1 is returned.
  243. s32 binary_search(const T& element, s32 left, s32 right)
  244. {
  245. if (!used)
  246. return -1;
  247. sort();
  248. s32 m;
  249. do
  250. {
  251. m = (left+right)>>1;
  252. if (element < data[m])
  253. right = m - 1;
  254. else
  255. left = m + 1;
  256. } while((element < data[m] || data[m] < element) && left<=right);
  257. // this last line equals to:
  258. // " while((element != array[m]) && left<=right);"
  259. // but we only want to use the '<' operator.
  260. // the same in next line, it is "(element == array[m])"
  261. if (!(element < data[m]) && !(data[m] < element))
  262. return m;
  263. return -1;
  264. }
  265. //! Finds an element in linear time, which is very slow. Use
  266. //! binary_search for faster finding. Only works if =operator is implemented.
  267. //! \param element: Element to search for.
  268. //! \return Returns position of the searched element if it was found,
  269. //! otherwise -1 is returned.
  270. s32 linear_search(T& element)
  271. {
  272. for (u32 i=0; i<used; ++i)
  273. if (!(element < data[i]) && !(data[i] < element))
  274. return (s32)i;
  275. return -1;
  276. }
  277. //! Finds an element in linear time, which is very slow. Use
  278. //! binary_search for faster finding. Only works if =operator is implemented.
  279. //! \param element: Element to search for.
  280. //! \return Returns position of the searched element if it was found,
  281. //! otherwise -1 is returned.
  282. s32 linear_reverse_search(T& element)
  283. {
  284. for (s32 i=used-1; i>=0; --i)
  285. if (data[i] == element)
  286. return (s32)i;
  287. return -1;
  288. }
  289. //! Erases an element from the array. May be slow, because all elements
  290. //! following after the erased element have to be copied.
  291. //! \param index: Index of element to be erased.
  292. void erase(u32 index)
  293. {
  294. _IRR_DEBUG_BREAK_IF(index>=used || index<0) // access violation
  295. for (u32 i=index+1; i<used; ++i)
  296. data[i-1] = data[i];
  297. --used;
  298. }
  299. //! Erases some elements from the array. may be slow, because all elements
  300. //! following after the erased element have to be copied.
  301. //! \param index: Index of the first element to be erased.
  302. //! \param count: Amount of elements to be erased.
  303. void erase(u32 index, s32 count)
  304. {
  305. _IRR_DEBUG_BREAK_IF(index>=used || index<0 || count<1 || index+count>used) // access violation
  306. for (u32 i=index+count; i<used; ++i)
  307. data[i-count] = data[i];
  308. used-= count;
  309. }
  310. //! Sets if the array is sorted
  311. void set_sorted(bool _is_sorted)
  312. {
  313. is_sorted = _is_sorted;
  314. }
  315. private:
  316. T* data;
  317. u32 allocated;
  318. u32 used;
  319. bool free_when_destroyed;
  320. bool is_sorted;
  321. };
  322. } // end namespace core
  323. } // end namespace irr
  324. #endif