irrArray.h 11 KB

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