irrArray.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // Copyright (C) 2002-2012 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. #pragma once
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <vector>
  8. #include "irrTypes.h"
  9. #include "irrMath.h"
  10. namespace irr
  11. {
  12. namespace core
  13. {
  14. //! Self reallocating template array (like stl vector) with additional features.
  15. /** Some features are: Heap sorting, binary search methods, easier debugging.
  16. */
  17. template <class T>
  18. class array
  19. {
  20. public:
  21. static_assert(!std::is_same<T, bool>::value,
  22. "irr::core::array<T> with T = bool not supported. Use std::vector instead.");
  23. //! Default constructor for empty array.
  24. array() :
  25. m_data(), is_sorted(true)
  26. {
  27. }
  28. //! Constructs an array and allocates an initial chunk of memory.
  29. /** \param start_count Amount of elements to pre-allocate. */
  30. explicit array(u32 start_count) :
  31. m_data(), is_sorted(true)
  32. {
  33. m_data.reserve(start_count);
  34. }
  35. //! Copy constructor
  36. array(const array<T> &other) :
  37. m_data(other.m_data), is_sorted(other.is_sorted)
  38. {
  39. }
  40. //! Reallocates the array, make it bigger or smaller.
  41. /** \param new_size New size of array.
  42. \param canShrink Specifies whether the array is reallocated even if
  43. enough space is available. Setting this flag to false can speed up
  44. array usage, but may use more memory than required by the data.
  45. */
  46. void reallocate(u32 new_size, bool canShrink = true)
  47. {
  48. size_t allocated = m_data.capacity();
  49. if (new_size < allocated) {
  50. if (canShrink)
  51. m_data.resize(new_size);
  52. } else {
  53. m_data.reserve(new_size);
  54. }
  55. }
  56. //! Adds an element at back of array.
  57. /** If the array is too small to add this new element it is made bigger.
  58. \param element: Element to add at the back of the array. */
  59. void push_back(const T &element)
  60. {
  61. m_data.push_back(element);
  62. is_sorted = false;
  63. }
  64. void push_back(T &&element)
  65. {
  66. m_data.push_back(std::move(element));
  67. is_sorted = false;
  68. }
  69. //! Adds an element at the front of the array.
  70. /** If the array is to small to add this new element, the array is
  71. made bigger. Please note that this is slow, because the whole array
  72. needs to be copied for this.
  73. \param element Element to add at the back of the array. */
  74. void push_front(const T &element)
  75. {
  76. m_data.insert(m_data.begin(), element);
  77. is_sorted = false;
  78. }
  79. void push_front(T &&element)
  80. {
  81. m_data.insert(m_data.begin(), std::move(element));
  82. is_sorted = false;
  83. }
  84. //! Insert item into array at specified position.
  85. /**
  86. \param element: Element to be inserted
  87. \param index: Where position to insert the new element. */
  88. void insert(const T &element, u32 index = 0)
  89. {
  90. _IRR_DEBUG_BREAK_IF(index > m_data.size()) // access violation
  91. auto pos = std::next(m_data.begin(), index);
  92. m_data.insert(pos, element);
  93. is_sorted = false;
  94. }
  95. //! Clears the array and deletes all allocated memory.
  96. void clear()
  97. {
  98. // vector::clear() reduces the size to 0, but doesn't free memory.
  99. // This swap is guaranteed to delete the allocated memory.
  100. std::vector<T>().swap(m_data);
  101. is_sorted = true;
  102. }
  103. //! Set (copy) data from given memory block
  104. /** \param newData data to set, must have newSize elements
  105. \param newSize Amount of elements in newData
  106. \param canShrink When true we reallocate the array even it can shrink.
  107. May reduce memory usage, but call is more whenever size changes.
  108. \param newDataIsSorted Info if you pass sorted/unsorted data
  109. */
  110. void set_data(const T *newData, u32 newSize, bool newDataIsSorted = false, bool canShrink = false)
  111. {
  112. m_data.resize(newSize);
  113. if (canShrink) {
  114. m_data.shrink_to_fit();
  115. }
  116. std::copy(newData, newData + newSize, m_data.begin());
  117. is_sorted = newDataIsSorted;
  118. }
  119. //! Compare if given data block is identical to the data in our array
  120. /** Like operator ==, but without the need to create the array
  121. \param otherData Address to data against which we compare, must contain size elements
  122. \param size Amount of elements in otherData */
  123. bool equals(const T *otherData, u32 size) const
  124. {
  125. if (m_data.size() != size)
  126. return false;
  127. return std::equal(m_data.begin(), m_data.end(), otherData);
  128. }
  129. //! Sets the size of the array and allocates new elements if necessary.
  130. /** \param usedNow Amount of elements now used. */
  131. void set_used(u32 usedNow)
  132. {
  133. m_data.resize(usedNow);
  134. }
  135. //! Assignment operator
  136. const array<T> &operator=(const array<T> &other)
  137. {
  138. if (this == &other)
  139. return *this;
  140. m_data = other.m_data;
  141. is_sorted = other.is_sorted;
  142. return *this;
  143. }
  144. array<T> &operator=(const std::vector<T> &other)
  145. {
  146. m_data = other;
  147. is_sorted = false;
  148. return *this;
  149. }
  150. array<T> &operator=(std::vector<T> &&other)
  151. {
  152. m_data = std::move(other);
  153. is_sorted = false;
  154. return *this;
  155. }
  156. //! Equality operator
  157. bool operator==(const array<T> &other) const
  158. {
  159. return equals(other.const_pointer(), other.size());
  160. }
  161. //! Inequality operator
  162. bool operator!=(const array<T> &other) const
  163. {
  164. return !(*this == other);
  165. }
  166. //! Direct access operator
  167. T &operator[](u32 index)
  168. {
  169. _IRR_DEBUG_BREAK_IF(index >= m_data.size()) // access violation
  170. return m_data[index];
  171. }
  172. //! Direct const access operator
  173. const T &operator[](u32 index) const
  174. {
  175. _IRR_DEBUG_BREAK_IF(index >= m_data.size()) // access violation
  176. return m_data[index];
  177. }
  178. //! Gets last element.
  179. T &getLast()
  180. {
  181. _IRR_DEBUG_BREAK_IF(m_data.empty()) // access violation
  182. return m_data.back();
  183. }
  184. //! Gets last element
  185. const T &getLast() const
  186. {
  187. _IRR_DEBUG_BREAK_IF(m_data.empty()) // access violation
  188. return m_data.back();
  189. }
  190. //! Gets a pointer to the array.
  191. /** \return Pointer to the array. */
  192. T *pointer()
  193. {
  194. return m_data.empty() ? nullptr : &m_data[0];
  195. }
  196. //! Gets a const pointer to the array.
  197. /** \return Pointer to the array. */
  198. const T *const_pointer() const
  199. {
  200. return m_data.empty() ? nullptr : &m_data[0];
  201. }
  202. //! Get number of occupied elements of the array.
  203. /** \return Size of elements in the array which are actually occupied. */
  204. u32 size() const
  205. {
  206. return static_cast<u32>(m_data.size());
  207. }
  208. //! Get amount of memory allocated.
  209. /** \return Amount of memory allocated. The amount of bytes
  210. allocated would be allocated_size() * sizeof(ElementTypeUsed); */
  211. u32 allocated_size() const
  212. {
  213. return m_data.capacity();
  214. }
  215. //! Check if array is empty.
  216. /** \return True if the array is empty false if not. */
  217. bool empty() const
  218. {
  219. return m_data.empty();
  220. }
  221. //! Sorts the array using heapsort.
  222. /** There is no additional memory waste and the algorithm performs
  223. O(n*log n) in worst case. */
  224. void sort()
  225. {
  226. if (!is_sorted) {
  227. std::sort(m_data.begin(), m_data.end());
  228. is_sorted = true;
  229. }
  230. }
  231. //! Performs a binary search for an element, returns -1 if not found.
  232. /** The array will be sorted before the binary search if it is not
  233. already sorted. Caution is advised! Be careful not to call this on
  234. unsorted const arrays, or the slower method will be used.
  235. \param element Element to search for.
  236. \return Position of the searched element if it was found,
  237. otherwise -1 is returned. */
  238. s32 binary_search(const T &element)
  239. {
  240. sort();
  241. return binary_search(element, 0, (s32)m_data.size() - 1);
  242. }
  243. //! Performs a binary search for an element if possible, returns -1 if not found.
  244. /** This method is for const arrays and so cannot call sort(), if the array is
  245. not sorted then linear_search will be used instead. Potentially very slow!
  246. \param element Element to search for.
  247. \return Position of the searched element if it was found,
  248. otherwise -1 is returned. */
  249. s32 binary_search(const T &element) const
  250. {
  251. if (is_sorted)
  252. return binary_search(element, 0, (s32)m_data.size() - 1);
  253. else
  254. return linear_search(element);
  255. }
  256. //! Performs a binary search for an element, returns -1 if not found.
  257. /** \param element: Element to search for.
  258. \param left First left index
  259. \param right Last right index.
  260. \return Position of the searched element if it was found, otherwise -1
  261. is returned. */
  262. s32 binary_search(const T &element, s32 left, s32 right) const
  263. {
  264. if (left > right)
  265. return -1;
  266. auto lpos = std::next(m_data.begin(), left);
  267. auto rpos = std::next(m_data.begin(), right);
  268. auto it = std::lower_bound(lpos, rpos, element);
  269. // *it = first element in [first, last) that is >= element, or last if not found.
  270. if (*it < element || element < *it)
  271. return -1;
  272. return static_cast<u32>(it - m_data.begin());
  273. }
  274. //! Performs a binary search for an element, returns -1 if not found.
  275. //! it is used for searching a multiset
  276. /** The array will be sorted before the binary search if it is not
  277. already sorted.
  278. \param element Element to search for.
  279. \param &last return lastIndex of equal elements
  280. \return Position of the first searched element if it was found,
  281. otherwise -1 is returned. */
  282. s32 binary_search_multi(const T &element, s32 &last)
  283. {
  284. sort();
  285. auto iters = std::equal_range(m_data.begin(), m_data.end(), element);
  286. if (iters.first == iters.second)
  287. return -1;
  288. last = static_cast<s32>((iters.second - m_data.begin()) - 1);
  289. return static_cast<s32>(iters.first - m_data.begin());
  290. }
  291. //! Finds an element in linear time, which is very slow.
  292. /** Use binary_search for faster finding. Only works if ==operator is
  293. implemented.
  294. \param element Element to search for.
  295. \return Position of the searched element if it was found, otherwise -1
  296. is returned. */
  297. s32 linear_search(const T &element) const
  298. {
  299. auto it = std::find(m_data.begin(), m_data.end(), element);
  300. if (it == m_data.end())
  301. return -1;
  302. return static_cast<u32>(it - m_data.begin());
  303. }
  304. //! Finds an element in linear time, which is very slow.
  305. /** Use binary_search for faster finding. Only works if ==operator is
  306. implemented.
  307. \param element: Element to search for.
  308. \return Position of the searched element if it was found, otherwise -1
  309. is returned. */
  310. s32 linear_reverse_search(const T &element) const
  311. {
  312. auto it = std::find(m_data.rbegin(), m_data.rend(), element);
  313. if (it == m_data.rend())
  314. return -1;
  315. size_t offset = it - m_data.rbegin();
  316. return m_data.size() - offset - 1;
  317. }
  318. //! Erases an element from the array.
  319. /** May be slow, because all elements following after the erased
  320. element have to be copied.
  321. \param index: Index of element to be erased. */
  322. void erase(u32 index)
  323. {
  324. _IRR_DEBUG_BREAK_IF(index >= m_data.size()) // access violation
  325. auto it = std::next(m_data.begin(), index);
  326. m_data.erase(it);
  327. }
  328. //! Erases some elements from the array.
  329. /** May be slow, because all elements following after the erased
  330. element have to be copied.
  331. \param index: Index of the first element to be erased.
  332. \param count: Amount of elements to be erased. */
  333. void erase(u32 index, s32 count)
  334. {
  335. if (index >= m_data.size() || count < 1)
  336. return;
  337. count = core::min_(count, (s32)m_data.size() - (s32)index);
  338. auto first = std::next(m_data.begin(), index);
  339. auto last = std::next(first, count);
  340. m_data.erase(first, last);
  341. }
  342. //! Sets if the array is sorted
  343. void set_sorted(bool _is_sorted)
  344. {
  345. is_sorted = _is_sorted;
  346. }
  347. //! Swap the content of this array container with the content of another array
  348. /** Afterward this object will contain the content of the other object and the other
  349. object will contain the content of this object.
  350. \param other Swap content with this object */
  351. void swap(array<T> &other)
  352. {
  353. m_data.swap(other.m_data);
  354. std::swap(is_sorted, other.is_sorted);
  355. }
  356. //! Pull the contents of this array as a vector.
  357. // The array is left empty.
  358. std::vector<T> steal()
  359. {
  360. std::vector<T> ret = std::move(m_data);
  361. m_data.clear();
  362. is_sorted = true;
  363. return ret;
  364. }
  365. typedef T value_type;
  366. typedef u32 size_type;
  367. private:
  368. std::vector<T> m_data;
  369. bool is_sorted;
  370. };
  371. } // end namespace core
  372. } // end namespace irr