local_vector.hpp 10 KB

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