irrArray.h 11 KB

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