local_vector.hpp 9.2 KB

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