List.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Allocator.h"
  25. #include "Types.h"
  26. #include "Assert.h"
  27. #include <cstring>
  28. namespace crown
  29. {
  30. /// Dynamic array of POD items.
  31. /// @note
  32. /// Does not call constructors/destructors so it is not very suitable for non-POD items.
  33. template <typename T>
  34. class List
  35. {
  36. public:
  37. /// Does not allocate memory.
  38. List(Allocator& allocator);
  39. /// Allocates capacity * sizeof(T) bytes.
  40. List(Allocator& allocator, uint32_t capacity);
  41. List(const List<T>& list);
  42. ~List();
  43. /// Random access by index
  44. T& operator[](uint32_t index);
  45. /// Random access by index
  46. const T& operator[](uint32_t index) const;
  47. /// Returns whether the list is empty
  48. bool empty() const;
  49. /// Returns the number of items in the list
  50. uint32_t size() const;
  51. /// Returns the maximum number of items the array can hold
  52. uint32_t capacity() const;
  53. /// Resizes the list to the given @a size.
  54. /// @note
  55. /// Old items will be copied to the newly created list.
  56. /// If the new capacity is smaller than the previous one, the
  57. /// list will be truncated.
  58. void resize(uint32_t size);
  59. /// Reserves space in the list for at least @a capacity items.
  60. void reserve(uint32_t capacity);
  61. /// Sets the list capacity
  62. void set_capacity(uint32_t capacity);
  63. /// Grows the list to contain at least @a min_capacity items
  64. void grow(uint32_t min_capacity);
  65. /// Condenses the array so that the capacity matches the actual number
  66. /// of items in the list.
  67. void condense();
  68. /// Appends an item to the list and returns its index.
  69. uint32_t push_back(const T& item);
  70. /// Removes the last item from the list.
  71. void pop_back();
  72. /// Appends @a count @a items to the list and returns the number
  73. /// of items in the list after the append operation.
  74. uint32_t push(const T* items, uint32_t count);
  75. /// Clears the content of the list.
  76. /// @note
  77. /// Does not free memory nor call destructors, it only zeroes
  78. /// the number of items in the list for efficiency.
  79. void clear();
  80. /// Copies the content of the @a other list into this one.
  81. const List<T>& operator=(const List<T>& other);
  82. T* begin();
  83. const T* begin() const;
  84. T* end();
  85. const T* end() const;
  86. T& front();
  87. const T& front() const;
  88. T& back();
  89. const T& back() const;
  90. private:
  91. Allocator& m_allocator;
  92. uint32_t m_capacity;
  93. uint32_t m_size;
  94. T* m_array;
  95. };
  96. //-----------------------------------------------------------------------------
  97. template <typename T>
  98. inline List<T>::List(Allocator& allocator) :
  99. m_allocator(allocator),
  100. m_capacity(0),
  101. m_size(0),
  102. m_array(NULL)
  103. {
  104. }
  105. //-----------------------------------------------------------------------------
  106. template <typename T>
  107. inline List<T>::List(Allocator& allocator, uint32_t capacity) :
  108. m_allocator(allocator),
  109. m_capacity(0),
  110. m_size(0),
  111. m_array(NULL)
  112. {
  113. resize(capacity);
  114. }
  115. //-----------------------------------------------------------------------------
  116. template <typename T>
  117. inline List<T>::List(const List<T>& list) :
  118. m_capacity(0),
  119. m_size(0),
  120. m_array(NULL)
  121. {
  122. *this = list;
  123. }
  124. //-----------------------------------------------------------------------------
  125. template <typename T>
  126. inline List<T>::~List()
  127. {
  128. if (m_array)
  129. {
  130. m_allocator.deallocate(m_array);
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. template <typename T>
  135. inline T& List<T>::operator[](uint32_t index)
  136. {
  137. CE_ASSERT(index < m_size, "Index out of bounds");
  138. return m_array[index];
  139. }
  140. //-----------------------------------------------------------------------------
  141. template <typename T>
  142. inline const T& List<T>::operator[](uint32_t index) const
  143. {
  144. CE_ASSERT(index < m_size, "Index out of bounds");
  145. return m_array[index];
  146. }
  147. //-----------------------------------------------------------------------------
  148. template <typename T>
  149. inline bool List<T>::empty() const
  150. {
  151. return m_size == 0;
  152. }
  153. //-----------------------------------------------------------------------------
  154. template <typename T>
  155. inline uint32_t List<T>::size() const
  156. {
  157. return m_size;
  158. }
  159. //-----------------------------------------------------------------------------
  160. template <typename T>
  161. inline uint32_t List<T>::capacity() const
  162. {
  163. return m_capacity;
  164. }
  165. //-----------------------------------------------------------------------------
  166. template <typename T>
  167. inline void List<T>::resize(uint32_t size)
  168. {
  169. if (size > m_capacity)
  170. {
  171. set_capacity(size);
  172. }
  173. m_size = size;
  174. }
  175. //-----------------------------------------------------------------------------
  176. template <typename T>
  177. inline void List<T>::reserve(uint32_t capacity)
  178. {
  179. if (capacity > m_capacity)
  180. {
  181. grow(capacity);
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. template <typename T>
  186. inline void List<T>::set_capacity(uint32_t capacity)
  187. {
  188. if (capacity == m_capacity)
  189. {
  190. return;
  191. }
  192. if (capacity < m_size)
  193. {
  194. resize(capacity);
  195. }
  196. if (capacity > 0)
  197. {
  198. T* tmp = m_array;
  199. m_capacity = capacity;
  200. m_array = (T*)m_allocator.allocate(capacity * sizeof(T), CE_ALIGNOF(T));
  201. memcpy(m_array, tmp, m_size * sizeof(T));
  202. if (tmp)
  203. {
  204. m_allocator.deallocate(tmp);
  205. }
  206. }
  207. }
  208. //-----------------------------------------------------------------------------
  209. template <typename T>
  210. inline void List<T>::grow(uint32_t min_capacity)
  211. {
  212. uint32_t new_capacity = m_capacity * 2 + 1;
  213. if (new_capacity < min_capacity)
  214. {
  215. new_capacity = min_capacity;
  216. }
  217. set_capacity(new_capacity);
  218. }
  219. //-----------------------------------------------------------------------------
  220. template <typename T>
  221. inline void List<T>::condense()
  222. {
  223. resize(m_size);
  224. }
  225. //-----------------------------------------------------------------------------
  226. template <typename T>
  227. inline uint32_t List<T>::push_back(const T& item)
  228. {
  229. if (m_capacity == m_size)
  230. {
  231. grow(0);
  232. }
  233. m_array[m_size] = item;
  234. return m_size++;
  235. }
  236. //-----------------------------------------------------------------------------
  237. template <typename T>
  238. inline void List<T>::pop_back()
  239. {
  240. CE_ASSERT(m_size > 0, "The list is empty");
  241. m_size--;
  242. }
  243. //-----------------------------------------------------------------------------
  244. template <typename T>
  245. inline uint32_t List<T>::push(const T* items, uint32_t count)
  246. {
  247. if (m_capacity <= m_size + count)
  248. {
  249. grow(m_size + count);
  250. }
  251. memcpy(&m_array[m_size], items, sizeof(T) * count);
  252. m_size += count;
  253. return m_size;
  254. }
  255. //-----------------------------------------------------------------------------
  256. template <typename T>
  257. inline void List<T>::clear()
  258. {
  259. m_size = 0;
  260. }
  261. //-----------------------------------------------------------------------------
  262. template <typename T>
  263. inline const List<T>& List<T>::operator=(const List<T>& other)
  264. {
  265. if (m_array)
  266. {
  267. m_allocator.deallocate(m_array);
  268. }
  269. m_size = other.m_size;
  270. m_capacity = other.m_capacity;
  271. if (m_capacity)
  272. {
  273. m_array = (T*)m_allocator.allocate(m_capacity * sizeof(T), CE_ALIGNOF(T));
  274. memcpy(m_array, other.m_array, m_size * sizeof(T));
  275. }
  276. return *this;
  277. }
  278. //-----------------------------------------------------------------------------
  279. template <typename T>
  280. inline const T* List<T>::begin() const
  281. {
  282. return m_array;
  283. }
  284. //-----------------------------------------------------------------------------
  285. template <typename T>
  286. inline T* List<T>::begin()
  287. {
  288. return m_array;
  289. }
  290. //-----------------------------------------------------------------------------
  291. template <typename T>
  292. inline const T* List<T>::end() const
  293. {
  294. return m_array + m_size;
  295. }
  296. //-----------------------------------------------------------------------------
  297. template <typename T>
  298. inline T* List<T>::end()
  299. {
  300. return m_array + m_size;
  301. }
  302. //-----------------------------------------------------------------------------
  303. template <typename T>
  304. inline T& List<T>::front()
  305. {
  306. CE_ASSERT(m_size > 0, "The list is empty");
  307. return m_array[0];
  308. }
  309. //-----------------------------------------------------------------------------
  310. template <typename T>
  311. inline const T& List<T>::front() const
  312. {
  313. CE_ASSERT(m_size > 0, "The list is empty");
  314. return m_array[0];
  315. }
  316. //-----------------------------------------------------------------------------
  317. template <typename T>
  318. inline T& List<T>::back()
  319. {
  320. CE_ASSERT(m_size > 0, "The list is empty");
  321. return m_array[m_size - 1];
  322. }
  323. //-----------------------------------------------------------------------------
  324. template <typename T>
  325. inline const T& List<T>::back() const
  326. {
  327. CE_ASSERT(m_size > 0, "The list is empty");
  328. return m_array[m_size - 1];
  329. }
  330. } // namespace crown