local_vector.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**************************************************************************/
  2. /* local_vector.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 LOCAL_VECTOR_H
  31. #define LOCAL_VECTOR_H
  32. #include "core/error/error_macros.h"
  33. #include "core/os/memory.h"
  34. #include "core/templates/sort_array.h"
  35. #include "core/templates/vector.h"
  36. #include <initializer_list>
  37. #include <type_traits>
  38. // If tight, it grows strictly as much as needed.
  39. // Otherwise, it grows exponentially (the default and what you want in most cases).
  40. template <class T, class U = uint32_t, bool force_trivial = false, bool tight = false>
  41. class LocalVector {
  42. private:
  43. U count = 0;
  44. U capacity = 0;
  45. T *data = nullptr;
  46. public:
  47. T *ptr() {
  48. return data;
  49. }
  50. const T *ptr() const {
  51. return data;
  52. }
  53. _FORCE_INLINE_ void push_back(T p_elem) {
  54. if (unlikely(count == capacity)) {
  55. capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);
  56. data = (T *)memrealloc(data, capacity * sizeof(T));
  57. CRASH_COND_MSG(!data, "Out of memory");
  58. }
  59. if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
  60. memnew_placement(&data[count++], T(p_elem));
  61. } else {
  62. data[count++] = p_elem;
  63. }
  64. }
  65. void remove_at(U p_index) {
  66. ERR_FAIL_UNSIGNED_INDEX(p_index, count);
  67. count--;
  68. for (U i = p_index; i < count; i++) {
  69. data[i] = data[i + 1];
  70. }
  71. if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
  72. data[count].~T();
  73. }
  74. }
  75. /// Removes the item copying the last value into the position of the one to
  76. /// remove. It's generally faster than `remove_at`.
  77. void remove_at_unordered(U p_index) {
  78. ERR_FAIL_INDEX(p_index, count);
  79. count--;
  80. if (count > p_index) {
  81. data[p_index] = data[count];
  82. }
  83. if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
  84. data[count].~T();
  85. }
  86. }
  87. _FORCE_INLINE_ bool erase(const T &p_val) {
  88. int64_t idx = find(p_val);
  89. if (idx >= 0) {
  90. remove_at(idx);
  91. return true;
  92. }
  93. return false;
  94. }
  95. void invert() {
  96. for (U i = 0; i < count / 2; i++) {
  97. SWAP(data[i], data[count - i - 1]);
  98. }
  99. }
  100. _FORCE_INLINE_ void clear() { resize(0); }
  101. _FORCE_INLINE_ void reset() {
  102. clear();
  103. if (data) {
  104. memfree(data);
  105. data = nullptr;
  106. capacity = 0;
  107. }
  108. }
  109. _FORCE_INLINE_ bool is_empty() const { return count == 0; }
  110. _FORCE_INLINE_ U get_capacity() const { return capacity; }
  111. _FORCE_INLINE_ void reserve(U p_size) {
  112. p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
  113. if (p_size > capacity) {
  114. capacity = p_size;
  115. data = (T *)memrealloc(data, capacity * sizeof(T));
  116. CRASH_COND_MSG(!data, "Out of memory");
  117. }
  118. }
  119. _FORCE_INLINE_ U size() const { return count; }
  120. void resize(U p_size) {
  121. if (p_size < count) {
  122. if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
  123. for (U i = p_size; i < count; i++) {
  124. data[i].~T();
  125. }
  126. }
  127. count = p_size;
  128. } else if (p_size > count) {
  129. if (unlikely(p_size > capacity)) {
  130. capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
  131. data = (T *)memrealloc(data, capacity * sizeof(T));
  132. CRASH_COND_MSG(!data, "Out of memory");
  133. }
  134. if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
  135. for (U i = count; i < p_size; i++) {
  136. memnew_placement(&data[i], T);
  137. }
  138. }
  139. count = p_size;
  140. }
  141. }
  142. _FORCE_INLINE_ const T &operator[](U p_index) const {
  143. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  144. return data[p_index];
  145. }
  146. _FORCE_INLINE_ T &operator[](U p_index) {
  147. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  148. return data[p_index];
  149. }
  150. struct Iterator {
  151. _FORCE_INLINE_ T &operator*() const {
  152. return *elem_ptr;
  153. }
  154. _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
  155. _FORCE_INLINE_ Iterator &operator++() {
  156. elem_ptr++;
  157. return *this;
  158. }
  159. _FORCE_INLINE_ Iterator &operator--() {
  160. elem_ptr--;
  161. return *this;
  162. }
  163. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
  164. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
  165. Iterator(T *p_ptr) { elem_ptr = p_ptr; }
  166. Iterator() {}
  167. Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
  168. private:
  169. T *elem_ptr = nullptr;
  170. };
  171. struct ConstIterator {
  172. _FORCE_INLINE_ const T &operator*() const {
  173. return *elem_ptr;
  174. }
  175. _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
  176. _FORCE_INLINE_ ConstIterator &operator++() {
  177. elem_ptr++;
  178. return *this;
  179. }
  180. _FORCE_INLINE_ ConstIterator &operator--() {
  181. elem_ptr--;
  182. return *this;
  183. }
  184. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
  185. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
  186. ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
  187. ConstIterator() {}
  188. ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
  189. private:
  190. const T *elem_ptr = nullptr;
  191. };
  192. _FORCE_INLINE_ Iterator begin() {
  193. return Iterator(data);
  194. }
  195. _FORCE_INLINE_ Iterator end() {
  196. return Iterator(data + size());
  197. }
  198. _FORCE_INLINE_ ConstIterator begin() const {
  199. return ConstIterator(ptr());
  200. }
  201. _FORCE_INLINE_ ConstIterator end() const {
  202. return ConstIterator(ptr() + size());
  203. }
  204. void insert(U p_pos, T p_val) {
  205. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  206. if (p_pos == count) {
  207. push_back(p_val);
  208. } else {
  209. resize(count + 1);
  210. for (U i = count - 1; i > p_pos; i--) {
  211. data[i] = data[i - 1];
  212. }
  213. data[p_pos] = p_val;
  214. }
  215. }
  216. int64_t find(const T &p_val, U p_from = 0) const {
  217. for (U i = p_from; i < count; i++) {
  218. if (data[i] == p_val) {
  219. return int64_t(i);
  220. }
  221. }
  222. return -1;
  223. }
  224. template <class C>
  225. void sort_custom() {
  226. U len = count;
  227. if (len == 0) {
  228. return;
  229. }
  230. SortArray<T, C> sorter;
  231. sorter.sort(data, len);
  232. }
  233. void sort() {
  234. sort_custom<_DefaultComparator<T>>();
  235. }
  236. void ordered_insert(T p_val) {
  237. U i;
  238. for (i = 0; i < count; i++) {
  239. if (p_val < data[i]) {
  240. break;
  241. }
  242. }
  243. insert(i, p_val);
  244. }
  245. operator Vector<T>() const {
  246. Vector<T> ret;
  247. ret.resize(size());
  248. T *w = ret.ptrw();
  249. memcpy(w, data, sizeof(T) * count);
  250. return ret;
  251. }
  252. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  253. Vector<uint8_t> ret;
  254. ret.resize(count * sizeof(T));
  255. uint8_t *w = ret.ptrw();
  256. memcpy(w, data, sizeof(T) * count);
  257. return ret;
  258. }
  259. _FORCE_INLINE_ LocalVector() {}
  260. _FORCE_INLINE_ LocalVector(std::initializer_list<T> p_init) {
  261. reserve(p_init.size());
  262. for (const T &element : p_init) {
  263. push_back(element);
  264. }
  265. }
  266. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  267. resize(p_from.size());
  268. for (U i = 0; i < p_from.count; i++) {
  269. data[i] = p_from.data[i];
  270. }
  271. }
  272. inline void operator=(const LocalVector &p_from) {
  273. resize(p_from.size());
  274. for (U i = 0; i < p_from.count; i++) {
  275. data[i] = p_from.data[i];
  276. }
  277. }
  278. inline void operator=(const Vector<T> &p_from) {
  279. resize(p_from.size());
  280. for (U i = 0; i < count; i++) {
  281. data[i] = p_from[i];
  282. }
  283. }
  284. _FORCE_INLINE_ ~LocalVector() {
  285. if (data) {
  286. reset();
  287. }
  288. }
  289. };
  290. template <class T, class U = uint32_t, bool force_trivial = false>
  291. using TightLocalVector = LocalVector<T, U, force_trivial, true>;
  292. #endif // LOCAL_VECTOR_H