local_vector.h 9.4 KB

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