local_vector.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/os/memory.h"
  33. #include "core/templates/sort_array.h"
  34. #include "core/templates/vector.h"
  35. #include <initializer_list>
  36. #include <type_traits>
  37. // If tight, it grows strictly as much as needed.
  38. // Otherwise, it grows exponentially (the default and what you want in most cases).
  39. template <typename T, typename U = uint32_t, bool force_trivial = false, bool tight = false>
  40. class LocalVector {
  41. private:
  42. U count = 0;
  43. U capacity = 0;
  44. T *data = nullptr;
  45. public:
  46. _FORCE_INLINE_ T *ptr() { return data; }
  47. _FORCE_INLINE_ const T *ptr() const { return data; }
  48. _FORCE_INLINE_ U size() const { return count; }
  49. _FORCE_INLINE_ Span<T> span() const { return Span(data, count); }
  50. _FORCE_INLINE_ operator Span<T>() const { return span(); }
  51. // Must take a copy instead of a reference (see GH-31736).
  52. _FORCE_INLINE_ void push_back(T p_elem) {
  53. if (unlikely(count == capacity)) {
  54. capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);
  55. data = (T *)memrealloc(data, capacity * sizeof(T));
  56. CRASH_COND_MSG(!data, "Out of memory");
  57. }
  58. if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
  59. memnew_placement(&data[count++], T(std::move(p_elem)));
  60. } else {
  61. data[count++] = std::move(p_elem);
  62. }
  63. }
  64. void remove_at(U p_index) {
  65. ERR_FAIL_UNSIGNED_INDEX(p_index, count);
  66. count--;
  67. for (U i = p_index; i < count; i++) {
  68. data[i] = std::move(data[i + 1]);
  69. }
  70. if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
  71. data[count].~T();
  72. }
  73. }
  74. /// Removes the item copying the last value into the position of the one to
  75. /// remove. It's generally faster than `remove_at`.
  76. void remove_at_unordered(U p_index) {
  77. ERR_FAIL_INDEX(p_index, count);
  78. count--;
  79. if (count > p_index) {
  80. data[p_index] = std::move(data[count]);
  81. }
  82. if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
  83. data[count].~T();
  84. }
  85. }
  86. _FORCE_INLINE_ bool erase(const T &p_val) {
  87. int64_t idx = find(p_val);
  88. if (idx >= 0) {
  89. remove_at(idx);
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool erase_unordered(const T &p_val) {
  95. int64_t idx = find(p_val);
  96. if (idx >= 0) {
  97. remove_at_unordered(idx);
  98. return true;
  99. }
  100. return false;
  101. }
  102. U erase_multiple_unordered(const T &p_val) {
  103. U from = 0;
  104. U occurrences = 0;
  105. while (true) {
  106. int64_t idx = find(p_val, from);
  107. if (idx == -1) {
  108. break;
  109. }
  110. remove_at_unordered(idx);
  111. from = idx;
  112. occurrences++;
  113. }
  114. return occurrences;
  115. }
  116. void invert() {
  117. for (U i = 0; i < count / 2; i++) {
  118. SWAP(data[i], data[count - i - 1]);
  119. }
  120. }
  121. _FORCE_INLINE_ void clear() { resize(0); }
  122. _FORCE_INLINE_ void reset() {
  123. clear();
  124. if (data) {
  125. memfree(data);
  126. data = nullptr;
  127. capacity = 0;
  128. }
  129. }
  130. _FORCE_INLINE_ bool is_empty() const { return count == 0; }
  131. _FORCE_INLINE_ U get_capacity() const { return capacity; }
  132. _FORCE_INLINE_ void reserve(U p_size) {
  133. p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
  134. if (p_size > capacity) {
  135. capacity = p_size;
  136. data = (T *)memrealloc(data, capacity * sizeof(T));
  137. CRASH_COND_MSG(!data, "Out of memory");
  138. }
  139. }
  140. void resize(U p_size) {
  141. if (p_size < count) {
  142. if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
  143. for (U i = p_size; i < count; i++) {
  144. data[i].~T();
  145. }
  146. }
  147. count = p_size;
  148. } else if (p_size > count) {
  149. if (unlikely(p_size > capacity)) {
  150. capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
  151. data = (T *)memrealloc(data, capacity * sizeof(T));
  152. CRASH_COND_MSG(!data, "Out of memory");
  153. }
  154. if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
  155. memnew_arr_placement(data + count, p_size - count);
  156. }
  157. count = p_size;
  158. }
  159. }
  160. _FORCE_INLINE_ const T &operator[](U p_index) const {
  161. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  162. return data[p_index];
  163. }
  164. _FORCE_INLINE_ T &operator[](U p_index) {
  165. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  166. return data[p_index];
  167. }
  168. struct Iterator {
  169. _FORCE_INLINE_ T &operator*() const {
  170. return *elem_ptr;
  171. }
  172. _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
  173. _FORCE_INLINE_ Iterator &operator++() {
  174. elem_ptr++;
  175. return *this;
  176. }
  177. _FORCE_INLINE_ Iterator &operator--() {
  178. elem_ptr--;
  179. return *this;
  180. }
  181. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
  182. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
  183. Iterator(T *p_ptr) { elem_ptr = p_ptr; }
  184. Iterator() {}
  185. Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
  186. private:
  187. T *elem_ptr = nullptr;
  188. };
  189. struct ConstIterator {
  190. _FORCE_INLINE_ const T &operator*() const {
  191. return *elem_ptr;
  192. }
  193. _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
  194. _FORCE_INLINE_ ConstIterator &operator++() {
  195. elem_ptr++;
  196. return *this;
  197. }
  198. _FORCE_INLINE_ ConstIterator &operator--() {
  199. elem_ptr--;
  200. return *this;
  201. }
  202. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
  203. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
  204. ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
  205. ConstIterator() {}
  206. ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
  207. private:
  208. const T *elem_ptr = nullptr;
  209. };
  210. _FORCE_INLINE_ Iterator begin() {
  211. return Iterator(data);
  212. }
  213. _FORCE_INLINE_ Iterator end() {
  214. return Iterator(data + size());
  215. }
  216. _FORCE_INLINE_ ConstIterator begin() const {
  217. return ConstIterator(ptr());
  218. }
  219. _FORCE_INLINE_ ConstIterator end() const {
  220. return ConstIterator(ptr() + size());
  221. }
  222. void insert(U p_pos, T p_val) {
  223. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  224. if (p_pos == count) {
  225. push_back(std::move(p_val));
  226. } else {
  227. resize(count + 1);
  228. for (U i = count - 1; i > p_pos; i--) {
  229. data[i] = std::move(data[i - 1]);
  230. }
  231. data[p_pos] = std::move(p_val);
  232. }
  233. }
  234. int64_t find(const T &p_val, int64_t p_from = 0) const {
  235. if (p_from < 0) {
  236. p_from = size() + p_from;
  237. }
  238. if (p_from < 0 || p_from >= size()) {
  239. return -1;
  240. }
  241. return span().find(p_val, p_from);
  242. }
  243. bool has(const T &p_val) const {
  244. return find(p_val) != -1;
  245. }
  246. template <typename C>
  247. void sort_custom() {
  248. U len = count;
  249. if (len == 0) {
  250. return;
  251. }
  252. SortArray<T, C> sorter;
  253. sorter.sort(data, len);
  254. }
  255. void sort() {
  256. sort_custom<Comparator<T>>();
  257. }
  258. void ordered_insert(T p_val) {
  259. U i;
  260. for (i = 0; i < count; i++) {
  261. if (p_val < data[i]) {
  262. break;
  263. }
  264. }
  265. insert(i, p_val);
  266. }
  267. operator Vector<T>() const {
  268. Vector<T> ret;
  269. ret.resize(count);
  270. T *w = ret.ptrw();
  271. if (w) {
  272. if constexpr (std::is_trivially_copyable_v<T>) {
  273. memcpy(w, data, sizeof(T) * count);
  274. } else {
  275. for (U i = 0; i < count; i++) {
  276. w[i] = data[i];
  277. }
  278. }
  279. }
  280. return ret;
  281. }
  282. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  283. Vector<uint8_t> ret;
  284. ret.resize(count * sizeof(T));
  285. uint8_t *w = ret.ptrw();
  286. if (w) {
  287. memcpy(w, data, sizeof(T) * count);
  288. }
  289. return ret;
  290. }
  291. _FORCE_INLINE_ LocalVector() {}
  292. _FORCE_INLINE_ LocalVector(std::initializer_list<T> p_init) {
  293. reserve(p_init.size());
  294. for (const T &element : p_init) {
  295. push_back(element);
  296. }
  297. }
  298. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  299. resize(p_from.size());
  300. for (U i = 0; i < p_from.count; i++) {
  301. data[i] = p_from.data[i];
  302. }
  303. }
  304. _FORCE_INLINE_ LocalVector(LocalVector &&p_from) {
  305. data = p_from.data;
  306. count = p_from.count;
  307. capacity = p_from.capacity;
  308. p_from.data = nullptr;
  309. p_from.count = 0;
  310. p_from.capacity = 0;
  311. }
  312. inline void operator=(const LocalVector &p_from) {
  313. resize(p_from.size());
  314. for (U i = 0; i < p_from.count; i++) {
  315. data[i] = p_from.data[i];
  316. }
  317. }
  318. inline void operator=(const Vector<T> &p_from) {
  319. resize(p_from.size());
  320. for (U i = 0; i < count; i++) {
  321. data[i] = p_from[i];
  322. }
  323. }
  324. inline void operator=(LocalVector &&p_from) {
  325. if (unlikely(this == &p_from)) {
  326. return;
  327. }
  328. reset();
  329. data = p_from.data;
  330. count = p_from.count;
  331. capacity = p_from.capacity;
  332. p_from.data = nullptr;
  333. p_from.count = 0;
  334. p_from.capacity = 0;
  335. }
  336. inline void operator=(Vector<T> &&p_from) {
  337. resize(p_from.size());
  338. for (U i = 0; i < count; i++) {
  339. data[i] = std::move(p_from[i]);
  340. }
  341. }
  342. _FORCE_INLINE_ ~LocalVector() {
  343. if (data) {
  344. reset();
  345. }
  346. }
  347. };
  348. template <typename T, typename U = uint32_t, bool force_trivial = false>
  349. using TightLocalVector = LocalVector<T, U, force_trivial, true>;
  350. // Zero-constructing LocalVector initializes count, capacity and data to 0 and thus empty.
  351. template <typename T, typename U, bool force_trivial, bool tight>
  352. struct is_zero_constructible<LocalVector<T, U, force_trivial, tight>> : std::true_type {};