vector.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*************************************************************************/
  2. /* vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VECTOR_H
  31. #define VECTOR_H
  32. /**
  33. * @class Vector
  34. * @author Juan Linietsky
  35. * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use DVector for large arrays.
  36. */
  37. #include "error_macros.h"
  38. #include "os/memory.h"
  39. #include "safe_refcount.h"
  40. #include "sort.h"
  41. template <class T>
  42. class Vector {
  43. mutable T *_ptr;
  44. // internal helpers
  45. _FORCE_INLINE_ SafeRefCount *_get_refcount() const {
  46. if (!_ptr)
  47. return NULL;
  48. return reinterpret_cast<SafeRefCount *>((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount));
  49. }
  50. _FORCE_INLINE_ int *_get_size() const {
  51. if (!_ptr)
  52. return NULL;
  53. return reinterpret_cast<int *>((uint8_t *)_ptr - sizeof(int));
  54. }
  55. _FORCE_INLINE_ T *_get_data() const {
  56. if (!_ptr)
  57. return NULL;
  58. return reinterpret_cast<T *>(_ptr);
  59. }
  60. _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const {
  61. //return nearest_power_of_2_templated(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int));
  62. return next_power_of_2(p_elements * sizeof(T) + sizeof(SafeRefCount) + sizeof(int));
  63. }
  64. _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
  65. #if defined(_add_overflow) && defined(_mul_overflow)
  66. size_t o;
  67. size_t p;
  68. if (_mul_overflow(p_elements, sizeof(T), &o)) return false;
  69. if (_add_overflow(o, sizeof(SafeRefCount) + sizeof(int), &p)) return false;
  70. *out = next_power_of_2(p);
  71. return true;
  72. #else
  73. // Speed is more important than correctness here, do the operations unchecked
  74. // and hope the best
  75. *out = _get_alloc_size(p_elements);
  76. return true;
  77. #endif
  78. }
  79. void _unref(void *p_data);
  80. void _copy_from(const Vector &p_from);
  81. void _copy_on_write();
  82. public:
  83. _FORCE_INLINE_ T *ptr() {
  84. if (!_ptr) return NULL;
  85. _copy_on_write();
  86. return (T *)_get_data();
  87. }
  88. _FORCE_INLINE_ const T *ptr() const {
  89. if (!_ptr) return NULL;
  90. return _get_data();
  91. }
  92. _FORCE_INLINE_ void clear() { resize(0); }
  93. _FORCE_INLINE_ int size() const {
  94. int *size = _get_size();
  95. if (size)
  96. return *size;
  97. else
  98. return 0;
  99. }
  100. _FORCE_INLINE_ bool empty() const { return _ptr == 0; }
  101. Error resize(int p_size);
  102. bool push_back(T p_elem);
  103. void remove(int p_index);
  104. void erase(const T &p_val) {
  105. int idx = find(p_val);
  106. if (idx >= 0) remove(idx);
  107. };
  108. void invert();
  109. template <class T_val>
  110. int find(const T_val &p_val, int p_from = 0) const;
  111. void set(int p_index, T p_elem);
  112. T get(int p_index) const;
  113. inline T &operator[](int p_index) {
  114. PRAY_BAD_INDEX(p_index, size(), T);
  115. _copy_on_write(); // wants to write, so copy on write.
  116. return _get_data()[p_index];
  117. }
  118. inline const T &operator[](int p_index) const {
  119. PRAY_BAD_INDEX(p_index, size(), T);
  120. // no cow needed, since it's reading
  121. return _get_data()[p_index];
  122. }
  123. Error insert(int p_pos, const T &p_val);
  124. template <class C>
  125. void sort_custom() {
  126. int len = size();
  127. if (len == 0)
  128. return;
  129. T *data = &operator[](0);
  130. SortArray<T, C> sorter;
  131. sorter.sort(data, len);
  132. }
  133. void sort() {
  134. sort_custom<_DefaultComparator<T> >();
  135. }
  136. void ordered_insert(const T &p_val) {
  137. int i;
  138. for (i = 0; i < size(); i++) {
  139. if (p_val < operator[](i)) {
  140. break;
  141. };
  142. };
  143. insert(i, p_val);
  144. }
  145. void operator=(const Vector &p_from);
  146. Vector(const Vector &p_from);
  147. _FORCE_INLINE_ Vector();
  148. _FORCE_INLINE_ ~Vector();
  149. };
  150. template <class T>
  151. void Vector<T>::_unref(void *p_data) {
  152. if (!p_data)
  153. return;
  154. SafeRefCount *src = reinterpret_cast<SafeRefCount *>((uint8_t *)p_data - sizeof(int) - sizeof(SafeRefCount));
  155. if (!src->unref())
  156. return; // still in use
  157. // clean up
  158. int *count = (int *)(src + 1);
  159. T *data = (T *)(count + 1);
  160. for (int i = 0; i < *count; i++) {
  161. // call destructors
  162. data[i].~T();
  163. }
  164. // free mem
  165. memfree((uint8_t *)p_data - sizeof(int) - sizeof(SafeRefCount));
  166. }
  167. template <class T>
  168. void Vector<T>::_copy_on_write() {
  169. if (!_ptr)
  170. return;
  171. if (_get_refcount()->get() > 1) {
  172. /* in use by more than me */
  173. void *mem_new = memalloc(_get_alloc_size(*_get_size()));
  174. SafeRefCount *src_new = (SafeRefCount *)mem_new;
  175. src_new->init();
  176. int *_size = (int *)(src_new + 1);
  177. *_size = *_get_size();
  178. T *_data = (T *)(_size + 1);
  179. // initialize new elements
  180. for (int i = 0; i < *_size; i++) {
  181. memnew_placement(&_data[i], T(_get_data()[i]));
  182. }
  183. _unref(_ptr);
  184. _ptr = _data;
  185. }
  186. }
  187. template <class T>
  188. template <class T_val>
  189. int Vector<T>::find(const T_val &p_val, int p_from) const {
  190. int ret = -1;
  191. if (p_from < 0 || size() == 0)
  192. return ret;
  193. for (int i = p_from; i < size(); i++) {
  194. if (operator[](i) == p_val) {
  195. ret = i;
  196. break;
  197. };
  198. };
  199. return ret;
  200. }
  201. template <class T>
  202. Error Vector<T>::resize(int p_size) {
  203. ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
  204. if (p_size == size())
  205. return OK;
  206. if (p_size == 0) {
  207. // wants to clean up
  208. _unref(_ptr);
  209. _ptr = NULL;
  210. return OK;
  211. }
  212. // possibly changing size, copy on write
  213. _copy_on_write();
  214. size_t alloc_size;
  215. ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
  216. if (p_size > size()) {
  217. if (size() == 0) {
  218. // alloc from scratch
  219. void *ptr = memalloc(alloc_size);
  220. ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
  221. _ptr = (T *)((uint8_t *)ptr + sizeof(int) + sizeof(SafeRefCount));
  222. _get_refcount()->init(); // init refcount
  223. *_get_size() = 0; // init size (currently, none)
  224. } else {
  225. void *_ptrnew = (T *)memrealloc((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount), alloc_size);
  226. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  227. _ptr = (T *)((uint8_t *)_ptrnew + sizeof(int) + sizeof(SafeRefCount));
  228. }
  229. // construct the newly created elements
  230. T *elems = _get_data();
  231. for (int i = *_get_size(); i < p_size; i++) {
  232. memnew_placement(&elems[i], T);
  233. }
  234. *_get_size() = p_size;
  235. } else if (p_size < size()) {
  236. // deinitialize no longer needed elements
  237. for (int i = p_size; i < *_get_size(); i++) {
  238. T *t = &_get_data()[i];
  239. t->~T();
  240. }
  241. void *_ptrnew = (T *)memrealloc((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount), alloc_size);
  242. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  243. _ptr = (T *)((uint8_t *)_ptrnew + sizeof(int) + sizeof(SafeRefCount));
  244. *_get_size() = p_size;
  245. }
  246. return OK;
  247. }
  248. template <class T>
  249. void Vector<T>::invert() {
  250. for (int i = 0; i < size() / 2; i++) {
  251. SWAP(operator[](i), operator[](size() - i - 1));
  252. }
  253. }
  254. template <class T>
  255. void Vector<T>::set(int p_index, T p_elem) {
  256. operator[](p_index) = p_elem;
  257. }
  258. template <class T>
  259. T Vector<T>::get(int p_index) const {
  260. return operator[](p_index);
  261. }
  262. template <class T>
  263. bool Vector<T>::push_back(T p_elem) {
  264. Error err = resize(size() + 1);
  265. ERR_FAIL_COND_V(err, true)
  266. set(size() - 1, p_elem);
  267. return false;
  268. }
  269. template <class T>
  270. void Vector<T>::remove(int p_index) {
  271. ERR_FAIL_INDEX(p_index, size());
  272. T *p = ptr();
  273. int len = size();
  274. for (int i = p_index; i < len - 1; i++) {
  275. p[i] = p[i + 1];
  276. };
  277. resize(len - 1);
  278. };
  279. template <class T>
  280. void Vector<T>::_copy_from(const Vector &p_from) {
  281. if (_ptr == p_from._ptr)
  282. return; // self assign, do nothing.
  283. _unref(_ptr);
  284. _ptr = NULL;
  285. if (!p_from._ptr)
  286. return; //nothing to do
  287. if (p_from._get_refcount()->ref()) // could reference
  288. _ptr = p_from._ptr;
  289. }
  290. template <class T>
  291. void Vector<T>::operator=(const Vector &p_from) {
  292. _copy_from(p_from);
  293. }
  294. template <class T>
  295. Error Vector<T>::insert(int p_pos, const T &p_val) {
  296. ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
  297. resize(size() + 1);
  298. for (int i = (size() - 1); i > p_pos; i--)
  299. set(i, get(i - 1));
  300. set(p_pos, p_val);
  301. return OK;
  302. }
  303. template <class T>
  304. Vector<T>::Vector(const Vector &p_from) {
  305. _ptr = NULL;
  306. _copy_from(p_from);
  307. }
  308. template <class T>
  309. Vector<T>::Vector() {
  310. _ptr = NULL;
  311. }
  312. template <class T>
  313. Vector<T>::~Vector() {
  314. _unref(_ptr);
  315. }
  316. #endif