Vector.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. namespace crown
  28. {
  29. /// Dynamic array of objects.
  30. /// @note
  31. /// Calls constructors and destructors, not suitable for performance-critical stuff.
  32. /// If your data is POD, use List<T> instead.
  33. template <typename T>
  34. class Vector
  35. {
  36. public:
  37. /// Does not allocate memory.
  38. Vector(Allocator& allocator);
  39. /// Allocates capacity * sizeof(T) bytes.
  40. Vector(Allocator& allocator, uint32_t capacity);
  41. Vector(const Vector<T>& other);
  42. ~Vector();
  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 vector is empty
  48. bool empty() const;
  49. /// Returns the number of items in the vector
  50. uint32_t size() const;
  51. /// Returns the maximum number of items the array can hold
  52. uint32_t capacity() const;
  53. /// Resizes the vector to the given @a size.
  54. /// @note
  55. /// Old items will be copied to the newly created vector.
  56. /// If the new capacity is smaller than the previous one, the
  57. /// vector will be truncated.
  58. void resize(uint32_t size);
  59. /// Reserves space in the vector for at least @a capacity items.
  60. void reserve(uint32_t capacity);
  61. /// Sets the vector capacity
  62. void set_capacity(uint32_t capacity);
  63. /// Grows the vector 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 vector.
  67. void condense();
  68. /// Appends an item to the vector and returns its index.
  69. uint32_t push_back(const T& item);
  70. /// Removes the last item from the vector.
  71. void pop_back();
  72. /// Appends @a count @a items to the vector and returns the number
  73. /// of items in the vector after the append operation.
  74. uint32_t push(const T* items, uint32_t count);
  75. /// Clears the content of the vector.
  76. /// @note
  77. /// Does not free memory nor call destructors, it only zeroes
  78. /// the number of items in the vector for efficiency.
  79. void clear();
  80. /// Copies the content of the @a other vector into this one.
  81. const Vector<T>& operator=(const Vector<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 Vector<T>::Vector(Allocator& allocator)
  99. : m_allocator(&allocator), m_capacity(0), m_size(0), m_array(NULL)
  100. {
  101. }
  102. //-----------------------------------------------------------------------------
  103. template <typename T>
  104. inline Vector<T>::Vector(Allocator& allocator, uint32_t capacity)
  105. : m_allocator(&allocator), m_capacity(0), m_size(0), m_array(NULL)
  106. {
  107. resize(capacity);
  108. }
  109. //-----------------------------------------------------------------------------
  110. template <typename T>
  111. inline Vector<T>::Vector(const Vector<T>& other)
  112. : m_allocator(other.m_allocator), m_capacity(0), m_size(0), m_array(NULL)
  113. {
  114. *this = other;
  115. }
  116. //-----------------------------------------------------------------------------
  117. template <typename T>
  118. inline Vector<T>::~Vector()
  119. {
  120. if (m_array)
  121. {
  122. for (uint32_t i = 0; i < m_size; i++)
  123. {
  124. m_array[i].~T();
  125. }
  126. m_allocator->deallocate(m_array);
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. template <typename T>
  131. inline T& Vector<T>::operator[](uint32_t index)
  132. {
  133. CE_ASSERT(index < m_size, "Index out of bounds");
  134. return m_array[index];
  135. }
  136. //-----------------------------------------------------------------------------
  137. template <typename T>
  138. inline const T& Vector<T>::operator[](uint32_t index) const
  139. {
  140. CE_ASSERT(index < m_size, "Index out of bounds");
  141. return m_array[index];
  142. }
  143. //-----------------------------------------------------------------------------
  144. template <typename T>
  145. inline bool Vector<T>::empty() const
  146. {
  147. return m_size == 0;
  148. }
  149. //-----------------------------------------------------------------------------
  150. template <typename T>
  151. inline uint32_t Vector<T>::size() const
  152. {
  153. return m_size;
  154. }
  155. //-----------------------------------------------------------------------------
  156. template <typename T>
  157. inline uint32_t Vector<T>::capacity() const
  158. {
  159. return m_capacity;
  160. }
  161. //-----------------------------------------------------------------------------
  162. template <typename T>
  163. inline void Vector<T>::resize(uint32_t size)
  164. {
  165. if (size > m_capacity)
  166. {
  167. set_capacity(size);
  168. }
  169. m_size = size;
  170. }
  171. //-----------------------------------------------------------------------------
  172. template <typename T>
  173. inline void Vector<T>::reserve(uint32_t capacity)
  174. {
  175. if (capacity > m_capacity)
  176. {
  177. grow(capacity);
  178. }
  179. }
  180. //-----------------------------------------------------------------------------
  181. template <typename T>
  182. inline void Vector<T>::set_capacity(uint32_t capacity)
  183. {
  184. if (capacity == m_capacity)
  185. {
  186. return;
  187. }
  188. if (capacity < m_size)
  189. {
  190. resize(capacity);
  191. }
  192. if (capacity > 0)
  193. {
  194. T* tmp = m_array;
  195. m_capacity = capacity;
  196. m_array = (T*)m_allocator->allocate(capacity * sizeof(T));
  197. for (uint32_t i = 0; i < m_size; i++)
  198. {
  199. new (m_array + i) T(tmp[i]);
  200. }
  201. if (tmp)
  202. {
  203. for (uint32_t i = 0; i < m_size; i++)
  204. {
  205. tmp[i].~T();
  206. }
  207. m_allocator->deallocate(tmp);
  208. }
  209. }
  210. }
  211. //-----------------------------------------------------------------------------
  212. template <typename T>
  213. inline void Vector<T>::grow(uint32_t min_capacity)
  214. {
  215. uint32_t new_capacity = m_capacity * 2 + 1;
  216. if (new_capacity < min_capacity)
  217. {
  218. new_capacity = min_capacity;
  219. }
  220. set_capacity(new_capacity);
  221. }
  222. //-----------------------------------------------------------------------------
  223. template <typename T>
  224. inline void Vector<T>::condense()
  225. {
  226. resize(m_size);
  227. }
  228. //-----------------------------------------------------------------------------
  229. template <typename T>
  230. inline uint32_t Vector<T>::push_back(const T& item)
  231. {
  232. if (m_capacity == m_size)
  233. {
  234. grow(0);
  235. }
  236. new (m_array + m_size) T(item);
  237. return m_size++;
  238. }
  239. //-----------------------------------------------------------------------------
  240. template <typename T>
  241. inline void Vector<T>::pop_back()
  242. {
  243. CE_ASSERT(m_size > 0, "The vector is empty");
  244. m_size--;
  245. }
  246. //-----------------------------------------------------------------------------
  247. template <typename T>
  248. inline uint32_t Vector<T>::push(const T* items, uint32_t count)
  249. {
  250. if (m_capacity <= m_size + count)
  251. {
  252. grow(m_size + count);
  253. }
  254. T* arr = &m_array[m_size];
  255. for (uint32_t i = 0; i < count; i++)
  256. {
  257. arr[i] = items[i];
  258. }
  259. m_size += count;
  260. return m_size;
  261. }
  262. //-----------------------------------------------------------------------------
  263. template <typename T>
  264. inline void Vector<T>::clear()
  265. {
  266. for (uint32_t i = 0; i < m_size; i++)
  267. {
  268. m_array[i].~T();
  269. }
  270. m_size = 0;
  271. }
  272. //-----------------------------------------------------------------------------
  273. template <typename T>
  274. inline const Vector<T>& Vector<T>::operator=(const Vector<T>& other)
  275. {
  276. const uint32_t size = other.m_size;
  277. resize(size);
  278. for (uint32_t i = 0; i < size; i++)
  279. {
  280. m_array[i] = other.m_array[i];
  281. }
  282. return *this;
  283. }
  284. //-----------------------------------------------------------------------------
  285. template <typename T>
  286. inline const T* Vector<T>::begin() const
  287. {
  288. return m_array;
  289. }
  290. //-----------------------------------------------------------------------------
  291. template <typename T>
  292. inline T* Vector<T>::begin()
  293. {
  294. return m_array;
  295. }
  296. //-----------------------------------------------------------------------------
  297. template <typename T>
  298. inline const T* Vector<T>::end() const
  299. {
  300. return m_array + m_size;
  301. }
  302. //-----------------------------------------------------------------------------
  303. template <typename T>
  304. inline T* Vector<T>::end()
  305. {
  306. return m_array + m_size;
  307. }
  308. //-----------------------------------------------------------------------------
  309. template <typename T>
  310. inline T& Vector<T>::front()
  311. {
  312. CE_ASSERT(m_size > 0, "The vector is empty");
  313. return m_array[0];
  314. }
  315. //-----------------------------------------------------------------------------
  316. template <typename T>
  317. inline const T& Vector<T>::front() const
  318. {
  319. CE_ASSERT(m_size > 0, "The vector is empty");
  320. return m_array[0];
  321. }
  322. //-----------------------------------------------------------------------------
  323. template <typename T>
  324. inline T& Vector<T>::back()
  325. {
  326. CE_ASSERT(m_size > 0, "The vector is empty");
  327. return m_array[m_size - 1];
  328. }
  329. //-----------------------------------------------------------------------------
  330. template <typename T>
  331. inline const T& Vector<T>::back() const
  332. {
  333. CE_ASSERT(m_size > 0, "The vector is empty");
  334. return m_array[m_size - 1];
  335. }
  336. } // namespace crown