vector.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "assert.h"
  8. #include "array.h"
  9. namespace crown
  10. {
  11. /// Functions to manipulate Vector.
  12. ///
  13. /// @ingroup Containers
  14. namespace vector
  15. {
  16. /// Returns whether the vector @a v is empty.
  17. template <typename T> bool empty(const Vector<T>& v);
  18. /// Returns the number of items in the vector @a v.
  19. template <typename T> uint32_t size(const Vector<T>& v);
  20. /// Returns the maximum number of items the vector @a v can hold.
  21. template <typename T> uint32_t capacity(const Vector<T>& v);
  22. /// Resizes the vector @a v to the given @a size.
  23. /// @note
  24. /// Old items will be copied to the newly created vector.
  25. /// If the new capacity is smaller than the previous one, the
  26. /// vector will be truncated.
  27. template <typename T> void resize(Vector<T>& v, uint32_t size);
  28. /// Reserves space in the vector @a v for at least @a capacity items.
  29. template <typename T> void reserve(Vector<T>& v, uint32_t capacity);
  30. /// Sets the capacity of vector @a v.
  31. template <typename T> void set_capacity(Vector<T>& v, uint32_t capacity);
  32. /// Grows the vector @a v to contain at least @a min_capacity items.
  33. template <typename T> void grow(Vector<T>& v, uint32_t min_capacity);
  34. /// Condenses the vector @a v so that its capacity matches the actual number
  35. /// of items in the vector.
  36. template <typename T> void condense(Vector<T>& v);
  37. /// Appends an item to the vector @a v and returns its index.
  38. template <typename T> uint32_t push_back(Vector<T>& v, const T& item);
  39. /// Removes the last item from the vector @a v.
  40. template <typename T> void pop_back(Vector<T>& v);
  41. /// Appends @a count @a items to the vector @a v and returns the number
  42. /// of items in the vector after the append operation.
  43. template <typename T> uint32_t push(Vector<T>& v, const T* items, uint32_t count);
  44. /// Clears the content of the vector @a v.
  45. /// @note
  46. /// Calls destructor on the items.
  47. template <typename T> void clear(Vector<T>& v);
  48. template <typename T> T* begin(Vector<T>& v);
  49. template <typename T> const T* begin(const Vector<T>& v);
  50. template <typename T> T* end(Vector<T>& v);
  51. template <typename T> const T* end(const Vector<T>& v);
  52. template <typename T> T& front(Vector<T>& v);
  53. template <typename T> const T& front(const Vector<T>& v);
  54. template <typename T> T& back(Vector<T>& v);
  55. template <typename T> const T& back(const Vector<T>& v);
  56. } // namespace vector
  57. namespace vector
  58. {
  59. template <typename T>
  60. bool empty(const Vector<T>& v)
  61. {
  62. return array::empty(v._array);
  63. }
  64. template <typename T>
  65. uint32_t size(const Vector<T>& v)
  66. {
  67. return array::size(v._array);
  68. }
  69. template <typename T>
  70. uint32_t capacity(const Vector<T>& v)
  71. {
  72. return array::capacity(v._array);
  73. }
  74. template <typename T>
  75. void resize(Vector<T>& v, uint32_t size)
  76. {
  77. array::resize(v._array, size);
  78. }
  79. template <typename T>
  80. void reserve(Vector<T>& v, uint32_t capacity)
  81. {
  82. array::reserve(v._array, capacity);
  83. }
  84. template <typename T>
  85. void set_capacity(Vector<T>& v, uint32_t capacity)
  86. {
  87. if (capacity == v._array._capacity)
  88. return;
  89. if (capacity < v._array._size)
  90. resize(v, capacity);
  91. if (capacity > 0)
  92. {
  93. Array<T> arr = v._array;
  94. T* tmp = arr._array;
  95. arr._capacity = capacity;
  96. arr._array = (T*)arr._allocator->allocate(capacity * sizeof(T));
  97. for (uint32_t i = 0; i < arr._size; i++)
  98. {
  99. new (arr._array + i) T(tmp[i]);
  100. }
  101. if (tmp)
  102. {
  103. for (uint32_t i = 0; i < arr._size; i++)
  104. {
  105. tmp[i].~T();
  106. }
  107. arr._allocator->deallocate(tmp);
  108. }
  109. }
  110. }
  111. template <typename T>
  112. void grow(Vector<T>& v, uint32_t min_capacity)
  113. {
  114. return array::grow(v._array, min_capacity);
  115. }
  116. template <typename T>
  117. void condense(Vector<T>& v)
  118. {
  119. return array::condense(v._array);
  120. }
  121. template <typename T>
  122. uint32_t push_back(Vector<T>& v, const T& item)
  123. {
  124. if (v._array._capacity == v._array._size)
  125. grow(v, 0);
  126. new (v._array._array + v._array._size) T(item);
  127. return v._array._size++;
  128. }
  129. template <typename T>
  130. void pop_back(Vector<T>& v)
  131. {
  132. CE_ASSERT(vector::size(v) > 0, "The vector is empty");
  133. v._array._array[v._array._size - 1].~T();
  134. v._array._size--;
  135. }
  136. template <typename T>
  137. uint32_t push(Vector<T>& v, const T* items, uint32_t count)
  138. {
  139. if (v._array._capacity <= v._array._size + count)
  140. grow(v, v._array._size + count);
  141. T* arr = &v._array._array[v._array._size];
  142. for (uint32_t i = 0; i < count; i++)
  143. {
  144. arr[i] = items[i];
  145. }
  146. v._array._size += count;
  147. return v._array._size;
  148. }
  149. template <typename T>
  150. void clear(Vector<T>& v)
  151. {
  152. for (uint32_t i = 0; i < v._array._size; i++)
  153. {
  154. v._array._array[i].~T();
  155. }
  156. v._array._size = 0;
  157. }
  158. template <typename T>
  159. T* begin(Vector<T>& v)
  160. {
  161. return array::begin(v._array);
  162. }
  163. template <typename T>
  164. const T* begin(const Vector<T>& v)
  165. {
  166. return array::begin(v._array);
  167. }
  168. template <typename T>
  169. T* end(Vector<T>& v)
  170. {
  171. return array::end(v._array);
  172. }
  173. template <typename T>
  174. const T* end(const Vector<T>& v)
  175. {
  176. return array::end(v._array);
  177. }
  178. template <typename T>
  179. T& front(Vector<T>& v)
  180. {
  181. return array::front(v._array);
  182. }
  183. template <typename T>
  184. const T& front(const Vector<T>& v)
  185. {
  186. return array::front(v._array);
  187. }
  188. template <typename T>
  189. T& back(Vector<T>& v)
  190. {
  191. return array::back(v._array);
  192. }
  193. template <typename T>
  194. const T& back(const Vector<T>& v)
  195. {
  196. return array::back(v._array);
  197. }
  198. } // namespace vector
  199. template <typename T>
  200. inline Vector<T>::Vector(Allocator& allocator)
  201. : _array(allocator)
  202. {
  203. }
  204. template <typename T>
  205. inline Vector<T>::Vector(Allocator& allocator, uint32_t capacity)
  206. : _array(allocator)
  207. {
  208. }
  209. template <typename T>
  210. inline Vector<T>::Vector(const Vector<T>& other)
  211. : _array(other._array)
  212. {
  213. *this = other;
  214. }
  215. template <typename T>
  216. inline Vector<T>::~Vector()
  217. {
  218. for (uint32_t i = 0; i < array::size(_array); i++)
  219. {
  220. _array[i].~T();
  221. }
  222. }
  223. template <typename T>
  224. inline T& Vector<T>::operator[](uint32_t index)
  225. {
  226. return _array[index];
  227. }
  228. template <typename T>
  229. inline const T& Vector<T>::operator[](uint32_t index) const
  230. {
  231. return _array[index];
  232. }
  233. template <typename T>
  234. inline const Vector<T>& Vector<T>::operator=(const Vector<T>& other)
  235. {
  236. const uint32_t size = vector::size(other);
  237. vector::resize(*this, size);
  238. for (uint32_t i = 0; i < size; i++)
  239. {
  240. _array[i] = other._array[i];
  241. }
  242. return *this;
  243. }
  244. } // namespace crown