array.h 7.6 KB

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