Array.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <cstring>
  25. #include "Assert.h"
  26. #include "ContainerTypes.h"
  27. namespace crown
  28. {
  29. namespace array
  30. {
  31. /// Returns whether the list is empty
  32. template <typename T> bool empty(const Array<T>& a);
  33. /// Returns the number of items in the list
  34. template <typename T> uint32_t size(const Array<T>& a);
  35. /// Returns the maximum number of items the array can hold
  36. template <typename T> uint32_t capacity(const Array<T>& a);
  37. /// Resizes the list to the given @a size.
  38. /// @note
  39. /// Old items will be copied to the newly created list.
  40. /// If the new capacity is smaller than the previous one, the
  41. /// list will be truncated.
  42. template <typename T> void resize(Array<T>& a, uint32_t size);
  43. /// Reserves space in the list for at least @a capacity items.
  44. template <typename T> void reserve(uint32_t capacity);
  45. /// Sets the list capacity
  46. template <typename T> void set_capacity(Array<T>& a, uint32_t capacity);
  47. /// Grows the list to contain at least @a min_capacity items
  48. template <typename T> void grow(Array<T>& a, uint32_t min_capacity);
  49. /// Condenses the array so that the capacity matches the actual number
  50. /// of items in the list.
  51. template <typename T> void condense(Array<T>& a);
  52. /// Appends an item to the list and returns its index.
  53. template <typename T> uint32_t push_back(Array<T>& a, const T& item);
  54. /// Removes the last item from the list.
  55. template <typename T> void pop_back(Array<T>& a);
  56. /// Appends @a count @a items to the list and returns the number
  57. /// of items in the list after the append operation.
  58. template <typename T> uint32_t push(Array<T>& a, const T* items, uint32_t count);
  59. /// Clears the content of the list.
  60. /// @note
  61. /// Does not free memory nor call destructors, it only zeroes
  62. /// the number of items in the list for efficiency.
  63. template <typename T> void clear(Array<T>& a);
  64. /// Copies the content of the @a other list into this one.
  65. template <typename T> T* begin(Array<T>& a);
  66. template <typename T> const T* begin(const Array<T>& a);
  67. template <typename T> T* end(Array<T>& a);
  68. template <typename T> const T* end(const Array<T>& a);
  69. template <typename T> T& front(Array<T>& a);
  70. template <typename T> const T& front(const Array<T>& a);
  71. template <typename T> T& back(Array<T>& a);
  72. template <typename T> const T& back(const Array<T>& a);
  73. } // namespace array
  74. namespace array
  75. {
  76. template <typename T>
  77. inline bool empty(const Array<T>& a)
  78. {
  79. return a.m_size == 0;
  80. }
  81. template <typename T>
  82. inline uint32_t size(const Array<T>& a)
  83. {
  84. return a.m_size;
  85. }
  86. template <typename T>
  87. inline uint32_t capacity(const Array<T>& a)
  88. {
  89. return a.m_capacity;
  90. }
  91. template <typename T>
  92. inline void resize(Array<T>& a, uint32_t size)
  93. {
  94. if (size > a.m_capacity)
  95. {
  96. set_capacity(a, size);
  97. }
  98. a.m_size = size;
  99. }
  100. template <typename T>
  101. inline void reserve(Array<T>& a, uint32_t capacity)
  102. {
  103. if (capacity > a.m_capacity)
  104. {
  105. grow(a, capacity);
  106. }
  107. }
  108. template <typename T>
  109. inline void set_capacity(Array<T>& a, uint32_t capacity)
  110. {
  111. if (capacity == a.m_capacity)
  112. {
  113. return;
  114. }
  115. if (capacity < a.m_size)
  116. {
  117. resize(a, capacity);
  118. }
  119. if (capacity > 0)
  120. {
  121. T* tmp = a.m_array;
  122. a.m_capacity = capacity;
  123. a.m_array = (T*)a.m_allocator->allocate(capacity * sizeof(T), CE_ALIGNOF(T));
  124. memcpy(a.m_array, tmp, a.m_size * sizeof(T));
  125. if (tmp)
  126. {
  127. a.m_allocator->deallocate(tmp);
  128. }
  129. }
  130. }
  131. template <typename T>
  132. inline void grow(Array<T>& a, uint32_t min_capacity)
  133. {
  134. uint32_t new_capacity = a.m_capacity * 2 + 1;
  135. if (new_capacity < min_capacity)
  136. {
  137. new_capacity = min_capacity;
  138. }
  139. set_capacity(a, new_capacity);
  140. }
  141. template <typename T>
  142. inline void condense(Array<T>& a)
  143. {
  144. resize(a, a.m_size);
  145. }
  146. template <typename T>
  147. inline uint32_t push_back(Array<T>& a, const T& item)
  148. {
  149. if (a.m_capacity == a.m_size)
  150. {
  151. grow(a, 0);
  152. }
  153. a.m_array[a.m_size] = item;
  154. return a.m_size++;
  155. }
  156. template <typename T>
  157. inline void pop_back(Array<T>& a)
  158. {
  159. CE_ASSERT(a.m_size > 0, "The list is empty");
  160. a.m_size--;
  161. }
  162. template <typename T>
  163. inline uint32_t push(Array<T>& a, const T* items, uint32_t count)
  164. {
  165. if (a.m_capacity <= a.m_size + count)
  166. {
  167. grow(a, a.m_size + count);
  168. }
  169. memcpy(&a.m_array[a.m_size], items, sizeof(T) * count);
  170. a.m_size += count;
  171. return a.m_size;
  172. }
  173. template <typename T>
  174. inline void clear(Array<T>& a)
  175. {
  176. a.m_size = 0;
  177. }
  178. template <typename T>
  179. inline const T* begin(const Array<T>& a)
  180. {
  181. return a.m_array;
  182. }
  183. template <typename T>
  184. inline T* begin(Array<T>& a)
  185. {
  186. return a.m_array;
  187. }
  188. template <typename T>
  189. inline const T* end(const Array<T>& a)
  190. {
  191. return a.m_array + a.m_size;
  192. }
  193. template <typename T>
  194. inline T* end(Array<T>& a)
  195. {
  196. return a.m_array + a.m_size;
  197. }
  198. template <typename T>
  199. inline T& front(Array<T>& a)
  200. {
  201. CE_ASSERT(a.m_size > 0, "The list is empty");
  202. return a.m_array[0];
  203. }
  204. template <typename T>
  205. inline const T& front(const Array<T>& a)
  206. {
  207. CE_ASSERT(a.m_size > 0, "The list is empty");
  208. return a.m_array[0];
  209. }
  210. template <typename T>
  211. inline T& back(Array<T>& a)
  212. {
  213. CE_ASSERT(a.m_size > 0, "The list is empty");
  214. return a.m_array[a.m_size - 1];
  215. }
  216. template <typename T>
  217. inline const T& back(const Array<T>& a)
  218. {
  219. CE_ASSERT(a.m_size > 0, "The list is empty");
  220. return a.m_array[a.m_size - 1];
  221. }
  222. } // namespace array
  223. template <typename T>
  224. inline Array<T>::Array(Allocator& allocator)
  225. : m_allocator(&allocator), m_capacity(0), m_size(0), m_array(NULL)
  226. {
  227. }
  228. template <typename T>
  229. inline Array<T>::Array(Allocator& allocator, uint32_t capacity)
  230. : m_allocator(&allocator), m_capacity(0), m_size(0), m_array(NULL)
  231. {
  232. array::resize(*this, capacity);
  233. }
  234. template <typename T>
  235. inline Array<T>::Array(const Array<T>& other)
  236. : m_allocator(other.m_allocator), m_capacity(0), m_size(0), m_array(NULL)
  237. {
  238. *this = other;
  239. }
  240. template <typename T>
  241. inline Array<T>::~Array()
  242. {
  243. if (m_array)
  244. {
  245. m_allocator->deallocate(m_array);
  246. }
  247. }
  248. template <typename T>
  249. inline T& Array<T>::operator[](uint32_t index)
  250. {
  251. CE_ASSERT(index < m_size, "Index out of bounds");
  252. return m_array[index];
  253. }
  254. template <typename T>
  255. inline const T& Array<T>::operator[](uint32_t index) const
  256. {
  257. CE_ASSERT(index < m_size, "Index out of bounds");
  258. return m_array[index];
  259. }
  260. template <typename T>
  261. inline Array<T>& Array<T>::operator=(const Array<T>& other)
  262. {
  263. const uint32_t size = other.m_size;
  264. array::resize(*this, size);
  265. memcpy(m_array, other.m_array, sizeof(T) * size);
  266. return *this;
  267. }
  268. } // namespace crown