local_vector.hpp 9.2 KB

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