local_vector.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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(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. U erase_multiple_unordered(const T &p_val) {
  95. U from = 0;
  96. U occurrences = 0;
  97. while (true) {
  98. int64_t idx = find(p_val, from);
  99. if (idx == -1) {
  100. break;
  101. }
  102. remove_at_unordered(idx);
  103. from = idx;
  104. occurrences++;
  105. }
  106. return occurrences;
  107. }
  108. void invert() {
  109. for (U i = 0; i < count / 2; i++) {
  110. SWAP(data[i], data[count - i - 1]);
  111. }
  112. }
  113. _FORCE_INLINE_ void clear() { resize(0); }
  114. _FORCE_INLINE_ void reset() {
  115. clear();
  116. if (data) {
  117. memfree(data);
  118. data = nullptr;
  119. capacity = 0;
  120. }
  121. }
  122. _FORCE_INLINE_ bool is_empty() const { return count == 0; }
  123. _FORCE_INLINE_ U get_capacity() const { return capacity; }
  124. _FORCE_INLINE_ void reserve(U p_size) {
  125. p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
  126. if (p_size > capacity) {
  127. capacity = p_size;
  128. data = (T *)memrealloc(data, capacity * sizeof(T));
  129. CRASH_COND_MSG(!data, "Out of memory");
  130. }
  131. }
  132. void resize(U p_size) {
  133. if (p_size < count) {
  134. if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
  135. for (U i = p_size; i < count; i++) {
  136. data[i].~T();
  137. }
  138. }
  139. count = p_size;
  140. } else if (p_size > count) {
  141. if (unlikely(p_size > capacity)) {
  142. capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
  143. data = (T *)memrealloc(data, capacity * sizeof(T));
  144. CRASH_COND_MSG(!data, "Out of memory");
  145. }
  146. if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
  147. memnew_arr_placement(data + count, p_size - count);
  148. }
  149. count = p_size;
  150. }
  151. }
  152. _FORCE_INLINE_ const T &operator[](U p_index) const {
  153. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  154. return data[p_index];
  155. }
  156. _FORCE_INLINE_ T &operator[](U p_index) {
  157. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  158. return data[p_index];
  159. }
  160. struct Iterator {
  161. _FORCE_INLINE_ T &operator*() const {
  162. return *elem_ptr;
  163. }
  164. _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
  165. _FORCE_INLINE_ Iterator &operator++() {
  166. elem_ptr++;
  167. return *this;
  168. }
  169. _FORCE_INLINE_ Iterator &operator--() {
  170. elem_ptr--;
  171. return *this;
  172. }
  173. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
  174. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
  175. Iterator(T *p_ptr) { elem_ptr = p_ptr; }
  176. Iterator() {}
  177. Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
  178. private:
  179. T *elem_ptr = nullptr;
  180. };
  181. struct ConstIterator {
  182. _FORCE_INLINE_ const T &operator*() const {
  183. return *elem_ptr;
  184. }
  185. _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
  186. _FORCE_INLINE_ ConstIterator &operator++() {
  187. elem_ptr++;
  188. return *this;
  189. }
  190. _FORCE_INLINE_ ConstIterator &operator--() {
  191. elem_ptr--;
  192. return *this;
  193. }
  194. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
  195. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
  196. ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
  197. ConstIterator() {}
  198. ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
  199. private:
  200. const T *elem_ptr = nullptr;
  201. };
  202. _FORCE_INLINE_ Iterator begin() {
  203. return Iterator(data);
  204. }
  205. _FORCE_INLINE_ Iterator end() {
  206. return Iterator(data + size());
  207. }
  208. _FORCE_INLINE_ ConstIterator begin() const {
  209. return ConstIterator(ptr());
  210. }
  211. _FORCE_INLINE_ ConstIterator end() const {
  212. return ConstIterator(ptr() + size());
  213. }
  214. void insert(U p_pos, T p_val) {
  215. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  216. if (p_pos == count) {
  217. push_back(std::move(p_val));
  218. } else {
  219. resize(count + 1);
  220. for (U i = count - 1; i > p_pos; i--) {
  221. data[i] = std::move(data[i - 1]);
  222. }
  223. data[p_pos] = std::move(p_val);
  224. }
  225. }
  226. int64_t find(const T &p_val, int64_t p_from = 0) const {
  227. if (p_from < 0) {
  228. p_from = size() + p_from;
  229. }
  230. if (p_from < 0 || p_from >= size()) {
  231. return -1;
  232. }
  233. return span().find(p_val, p_from);
  234. }
  235. bool has(const T &p_val) const {
  236. return find(p_val) != -1;
  237. }
  238. template <typename C>
  239. void sort_custom() {
  240. U len = count;
  241. if (len == 0) {
  242. return;
  243. }
  244. SortArray<T, C> sorter;
  245. sorter.sort(data, len);
  246. }
  247. void sort() {
  248. sort_custom<Comparator<T>>();
  249. }
  250. void ordered_insert(T p_val) {
  251. U i;
  252. for (i = 0; i < count; i++) {
  253. if (p_val < data[i]) {
  254. break;
  255. }
  256. }
  257. insert(i, p_val);
  258. }
  259. operator Vector<T>() const {
  260. Vector<T> ret;
  261. ret.resize(count);
  262. T *w = ret.ptrw();
  263. if (w) {
  264. if constexpr (std::is_trivially_copyable_v<T>) {
  265. memcpy(w, data, sizeof(T) * count);
  266. } else {
  267. for (U i = 0; i < count; i++) {
  268. w[i] = data[i];
  269. }
  270. }
  271. }
  272. return ret;
  273. }
  274. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  275. Vector<uint8_t> ret;
  276. ret.resize(count * sizeof(T));
  277. uint8_t *w = ret.ptrw();
  278. if (w) {
  279. memcpy(w, data, sizeof(T) * count);
  280. }
  281. return ret;
  282. }
  283. _FORCE_INLINE_ LocalVector() {}
  284. _FORCE_INLINE_ LocalVector(std::initializer_list<T> p_init) {
  285. reserve(p_init.size());
  286. for (const T &element : p_init) {
  287. push_back(element);
  288. }
  289. }
  290. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  291. resize(p_from.size());
  292. for (U i = 0; i < p_from.count; i++) {
  293. data[i] = p_from.data[i];
  294. }
  295. }
  296. _FORCE_INLINE_ LocalVector(LocalVector &&p_from) {
  297. data = p_from.data;
  298. count = p_from.count;
  299. capacity = p_from.capacity;
  300. p_from.data = nullptr;
  301. p_from.count = 0;
  302. p_from.capacity = 0;
  303. }
  304. inline void operator=(const LocalVector &p_from) {
  305. resize(p_from.size());
  306. for (U i = 0; i < p_from.count; i++) {
  307. data[i] = p_from.data[i];
  308. }
  309. }
  310. inline void operator=(const Vector<T> &p_from) {
  311. resize(p_from.size());
  312. for (U i = 0; i < count; i++) {
  313. data[i] = p_from[i];
  314. }
  315. }
  316. inline void operator=(LocalVector &&p_from) {
  317. if (unlikely(this == &p_from)) {
  318. return;
  319. }
  320. reset();
  321. data = p_from.data;
  322. count = p_from.count;
  323. capacity = p_from.capacity;
  324. p_from.data = nullptr;
  325. p_from.count = 0;
  326. p_from.capacity = 0;
  327. }
  328. inline void operator=(Vector<T> &&p_from) {
  329. resize(p_from.size());
  330. for (U i = 0; i < count; i++) {
  331. data[i] = std::move(p_from[i]);
  332. }
  333. }
  334. _FORCE_INLINE_ ~LocalVector() {
  335. if (data) {
  336. reset();
  337. }
  338. }
  339. };
  340. template <typename T, typename U = uint32_t, bool force_trivial = false>
  341. using TightLocalVector = LocalVector<T, U, force_trivial, true>;
  342. // Zero-constructing LocalVector initializes count, capacity and data to 0 and thus empty.
  343. template <typename T, typename U, bool force_trivial, bool tight>
  344. struct is_zero_constructible<LocalVector<T, U, force_trivial, tight>> : std::true_type {};