DynamicArray.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/MemoryPool.h>
  7. #include <AnKi/Util/Functions.h>
  8. #include <AnKi/Util/Forward.h>
  9. namespace anki {
  10. /// @addtogroup util_containers
  11. /// @{
  12. /// Dynamic array with manual destruction. It doesn't hold the allocator and that makes it compact. At the same time
  13. /// that requires manual destruction. Used in permanent classes.
  14. /// @tparam T The type this array will hold.
  15. /// @tparam TSize The type that denotes the maximum number of elements of the array.
  16. template<typename T, typename TSize>
  17. class DynamicArray
  18. {
  19. public:
  20. using Value = T;
  21. using Iterator = Value*;
  22. using ConstIterator = const Value*;
  23. using Reference = Value&;
  24. using ConstReference = const Value&;
  25. using Size = TSize;
  26. static constexpr F32 kGrowScale = 2.0f;
  27. static constexpr F32 kShrinkScale = 2.0f;
  28. DynamicArray()
  29. : m_data(nullptr)
  30. , m_size(0)
  31. , m_capacity(0)
  32. {
  33. }
  34. // Non-copyable
  35. DynamicArray(const DynamicArray& b) = delete;
  36. /// Move.
  37. DynamicArray(DynamicArray&& b)
  38. : DynamicArray()
  39. {
  40. *this = std::move(b);
  41. }
  42. ~DynamicArray()
  43. {
  44. ANKI_ASSERT(m_data == nullptr && m_size == 0 && m_capacity == 0 && "Requires manual destruction");
  45. }
  46. /// Move.
  47. DynamicArray& operator=(DynamicArray&& b)
  48. {
  49. ANKI_ASSERT(m_data == nullptr && m_size == 0 && "Cannot move before destroying");
  50. m_data = b.m_data;
  51. b.m_data = nullptr;
  52. m_size = b.m_size;
  53. b.m_size = 0;
  54. m_capacity = b.m_capacity;
  55. b.m_capacity = 0;
  56. return *this;
  57. }
  58. /// Move DynamicArrayRaii to this.
  59. template<typename TMemPool>
  60. DynamicArray& operator=(DynamicArrayRaii<T, TSize, TMemPool>&& b);
  61. // Non-copyable
  62. DynamicArray& operator=(const DynamicArray& b) = delete;
  63. Reference operator[](const Size n)
  64. {
  65. ANKI_ASSERT(n < m_size);
  66. return m_data[n];
  67. }
  68. ConstReference operator[](const Size n) const
  69. {
  70. ANKI_ASSERT(n < m_size);
  71. return m_data[n];
  72. }
  73. Iterator getBegin()
  74. {
  75. return m_data;
  76. }
  77. ConstIterator getBegin() const
  78. {
  79. return m_data;
  80. }
  81. Iterator getEnd()
  82. {
  83. return m_data + m_size;
  84. }
  85. ConstIterator getEnd() const
  86. {
  87. return m_data + m_size;
  88. }
  89. /// Make it compatible with the C++11 range based for loop.
  90. Iterator begin()
  91. {
  92. return getBegin();
  93. }
  94. /// Make it compatible with the C++11 range based for loop.
  95. ConstIterator begin() const
  96. {
  97. return getBegin();
  98. }
  99. /// Make it compatible with the C++11 range based for loop.
  100. Iterator end()
  101. {
  102. return getEnd();
  103. }
  104. /// Make it compatible with the C++11 range based for loop.
  105. ConstIterator end() const
  106. {
  107. return getEnd();
  108. }
  109. /// Get first element.
  110. Reference getFront()
  111. {
  112. ANKI_ASSERT(!isEmpty());
  113. return m_data[0];
  114. }
  115. /// Get first element.
  116. ConstReference getFront() const
  117. {
  118. ANKI_ASSERT(!isEmpty());
  119. return m_data[0];
  120. }
  121. /// Get last element.
  122. Reference getBack()
  123. {
  124. ANKI_ASSERT(!isEmpty());
  125. return m_data[m_size - 1];
  126. }
  127. /// Get last element.
  128. ConstReference getBack() const
  129. {
  130. ANKI_ASSERT(!isEmpty());
  131. return m_data[m_size - 1];
  132. }
  133. Size getSize() const
  134. {
  135. return m_size;
  136. }
  137. Bool isEmpty() const
  138. {
  139. return m_size == 0;
  140. }
  141. PtrSize getSizeInBytes() const
  142. {
  143. return m_size * sizeof(Value);
  144. }
  145. /// Only create the array. Useful if @a T is non-copyable or movable .
  146. template<typename TMemPool>
  147. void create(TMemPool& pool, Size size)
  148. {
  149. ANKI_ASSERT(m_data == nullptr && m_size == 0 && m_capacity == 0);
  150. if(size > 0)
  151. {
  152. m_data = newArray<Value>(pool, size);
  153. m_size = size;
  154. m_capacity = size;
  155. }
  156. }
  157. /// Only create the array. Useful if @a T is non-copyable or movable .
  158. template<typename TMemPool>
  159. void create(TMemPool& pool, Size size, const Value& v)
  160. {
  161. ANKI_ASSERT(m_data == nullptr && m_size == 0 && m_capacity == 0);
  162. if(size > 0)
  163. {
  164. m_data = newArray<Value>(pool, size, v);
  165. m_size = size;
  166. m_capacity = size;
  167. }
  168. }
  169. /// Destroy the array.
  170. template<typename TMemPool>
  171. void destroy(TMemPool& pool)
  172. {
  173. if(m_data)
  174. {
  175. ANKI_ASSERT(m_size > 0);
  176. ANKI_ASSERT(m_capacity > 0);
  177. deleteArray(pool, m_data, m_size);
  178. m_data = nullptr;
  179. m_size = 0;
  180. m_capacity = 0;
  181. }
  182. ANKI_ASSERT(m_data == nullptr && m_size == 0 && m_capacity == 0);
  183. }
  184. /// Grow or create the array. @a T needs to be copyable and moveable.
  185. template<typename TMemPool>
  186. void resize(TMemPool& pool, Size size, const Value& v);
  187. /// Grow or create the array. @a T needs to be copyable, moveable and default constructible.
  188. template<typename TMemPool>
  189. void resize(TMemPool& pool, Size size);
  190. /// Push back value.
  191. template<typename TMemPool, typename... TArgs>
  192. Iterator emplaceBack(TMemPool& pool, TArgs&&... args)
  193. {
  194. resizeStorage(pool, m_size + 1);
  195. callConstructor(m_data[m_size], std::forward<TArgs>(args)...);
  196. ++m_size;
  197. return &m_data[m_size - 1];
  198. }
  199. /// Remove the last value.
  200. template<typename TMemPool>
  201. void popBack(TMemPool& pool)
  202. {
  203. if(m_size > 0)
  204. {
  205. resizeStorage(pool, m_size - 1);
  206. }
  207. }
  208. /// Emplace a new element at a specific position. @a T needs to be movable and default constructible.
  209. /// @param pool The memory pool.
  210. /// @param where Points to the position to emplace. Should be less or equal to what getEnd() returns.
  211. /// @param args Constructor arguments.
  212. template<typename TMemPool, typename... TArgs>
  213. Iterator emplaceAt(TMemPool& pool, ConstIterator where, TArgs&&... args);
  214. /// Removes the (first, last] elements.
  215. /// @param pool The memory pool.
  216. /// @param first Points to the position of the first element to remove.
  217. /// @param last Points to the position of the last element to remove minus one.
  218. template<typename TMemPool>
  219. void erase(TMemPool& pool, ConstIterator first, ConstIterator last);
  220. /// Removes one element.
  221. /// @param pool The memory pool.
  222. /// @param at Points to the position of the element to remove.
  223. template<typename TMemPool>
  224. void erase(TMemPool& pool, ConstIterator at)
  225. {
  226. erase(pool, at, at + 1);
  227. }
  228. /// Validate it. Will only work when assertions are enabled.
  229. void validate() const
  230. {
  231. if(m_data)
  232. {
  233. ANKI_ASSERT(m_size > 0 && m_capacity > 0);
  234. ANKI_ASSERT(m_size <= m_capacity);
  235. }
  236. else
  237. {
  238. ANKI_ASSERT(m_size == 0 && m_capacity == 0);
  239. }
  240. }
  241. /// Move the data from this object. It's like moving (operator or constructor) but instead of moving to another
  242. /// object of the same type it moves to 3 values.
  243. void moveAndReset(Value*& data, Size& size, Size& storageSize)
  244. {
  245. data = m_data;
  246. size = m_size;
  247. storageSize = m_capacity;
  248. m_data = nullptr;
  249. m_size = 0;
  250. m_capacity = 0;
  251. }
  252. /// @copydoc moveAndReset
  253. void moveAndReset(WeakArray<Value, Size>& array)
  254. {
  255. Value* data;
  256. Size size;
  257. Size storageSize;
  258. moveAndReset(data, size, storageSize);
  259. array.setArray(data, size);
  260. }
  261. /// Resizes the storage but DOESN'T CONSTRUCT ANY ELEMENTS. It only moves or destroys.
  262. template<typename TMemPool>
  263. void resizeStorage(TMemPool& pool, Size newSize);
  264. protected:
  265. Value* m_data;
  266. Size m_size;
  267. Size m_capacity = 0;
  268. };
  269. /// Dynamic array with automatic destruction. It's the same as DynamicArray but it holds the memory pool in order to
  270. /// perform automatic destruction. Use it for temp operations and on transient classes.
  271. template<typename T, typename TSize = U32, typename TMemPool = MemoryPoolPtrWrapper<BaseMemoryPool>>
  272. class DynamicArrayRaii : public DynamicArray<T, TSize>
  273. {
  274. public:
  275. using Base = DynamicArray<T, TSize>;
  276. using Base::m_capacity;
  277. using Base::m_data;
  278. using Base::m_size;
  279. using typename Base::Value;
  280. using typename Base::Iterator;
  281. using typename Base::ConstIterator;
  282. using typename Base::Size;
  283. using MemoryPool = TMemPool;
  284. DynamicArrayRaii(const MemoryPool& pool = MemoryPool())
  285. : Base()
  286. , m_pool(pool)
  287. {
  288. }
  289. /// And resize
  290. DynamicArrayRaii(Size size, const MemoryPool& pool = MemoryPool())
  291. : Base()
  292. , m_pool(pool)
  293. {
  294. resize(size);
  295. }
  296. /// With default value
  297. DynamicArrayRaii(Size size, const T& v, const MemoryPool& pool = MemoryPool())
  298. : Base()
  299. , m_pool(pool)
  300. {
  301. create(size, v);
  302. }
  303. /// Move.
  304. DynamicArrayRaii(DynamicArrayRaii&& b)
  305. : Base()
  306. {
  307. *this = std::move(b);
  308. }
  309. ~DynamicArrayRaii()
  310. {
  311. Base::destroy(m_pool);
  312. }
  313. /// Copy.
  314. DynamicArrayRaii(const DynamicArrayRaii& b)
  315. : Base()
  316. , m_pool(b.m_pool)
  317. {
  318. if(b.getSize())
  319. {
  320. create(b.getSize());
  321. for(Size i = 0; i < b.getSize(); ++i)
  322. {
  323. (*this)[i] = b[i];
  324. }
  325. }
  326. }
  327. /// Move.
  328. DynamicArrayRaii& operator=(DynamicArrayRaii&& b)
  329. {
  330. Base::destroy(m_pool);
  331. m_data = b.m_data;
  332. b.m_data = nullptr;
  333. m_size = b.m_size;
  334. b.m_size = 0;
  335. m_capacity = b.m_capacity;
  336. b.m_capacity = 0;
  337. m_pool = std::move(b.m_pool);
  338. b.m_pool = {};
  339. return *this;
  340. }
  341. /// Copy.
  342. DynamicArrayRaii& operator=(const DynamicArrayRaii& b)
  343. {
  344. destroy();
  345. if(b.getSize())
  346. {
  347. create(b.getSize());
  348. for(Size i = 0; i < b.getSize(); ++i)
  349. {
  350. (*this)[i] = b[i];
  351. }
  352. }
  353. return *this;
  354. }
  355. /// @copydoc DynamicArray::create
  356. void create(Size size)
  357. {
  358. Base::create(m_pool, size);
  359. }
  360. /// @copydoc DynamicArray::create
  361. void create(Size size, const Value& v)
  362. {
  363. Base::create(m_pool, size, v);
  364. }
  365. /// @copydoc DynamicArray::destroy
  366. void destroy()
  367. {
  368. Base::destroy(m_pool);
  369. }
  370. /// @copydoc DynamicArray::resize
  371. void resize(Size size, const Value& v)
  372. {
  373. Base::resize(m_pool, size, v);
  374. }
  375. /// @copydoc DynamicArray::resize
  376. void resize(Size size)
  377. {
  378. Base::resize(m_pool, size);
  379. }
  380. /// @copydoc DynamicArray::emplaceBack
  381. template<typename... TArgs>
  382. Iterator emplaceBack(TArgs&&... args)
  383. {
  384. return Base::emplaceBack(m_pool, std::forward<TArgs>(args)...);
  385. }
  386. /// @copydoc DynamicArray::popBack
  387. void popBack()
  388. {
  389. Base::popBack(m_pool);
  390. }
  391. /// @copydoc DynamicArray::emplaceAt
  392. template<typename... TArgs>
  393. Iterator emplaceAt(ConstIterator where, TArgs&&... args)
  394. {
  395. return Base::emplaceAt(m_pool, where, std::forward<TArgs>(args)...);
  396. }
  397. /// @copydoc DynamicArray::erase
  398. void erase(ConstIterator first, ConstIterator last)
  399. {
  400. return Base::erase(m_pool, first, last);
  401. }
  402. /// @copydoc DynamicArray::erase
  403. void erase(ConstIterator at)
  404. {
  405. return Base::erase(m_pool, at);
  406. }
  407. /// @copydoc DynamicArray::moveAndReset
  408. void moveAndReset(Value*& data, Size& size, Size& storageSize)
  409. {
  410. Base::moveAndReset(data, size, storageSize);
  411. // Don't touch the m_pool
  412. }
  413. /// @copydoc DynamicArray::moveAndReset
  414. void moveAndReset(WeakArray<Value, Size>& array)
  415. {
  416. Base::moveAndReset(array);
  417. // Don't touch the m_pool
  418. }
  419. /// @copydoc DynamicArray::resizeStorage
  420. void resizeStorage(Size newSize)
  421. {
  422. Base::resizeStorage(m_pool, newSize);
  423. }
  424. /// Get the pool.
  425. const MemoryPool& getMemoryPool() const
  426. {
  427. return m_pool;
  428. }
  429. /// Get the pool.
  430. MemoryPool& getMemoryPool()
  431. {
  432. return m_pool;
  433. }
  434. private:
  435. MemoryPool m_pool;
  436. };
  437. /// @}
  438. } // end namespace anki
  439. #include <AnKi/Util/DynamicArray.inl.h>