local_vector.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*************************************************************************/
  2. /* local_vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_macros.h"
  33. #include "core/os/memory.h"
  34. #include "core/pool_vector.h"
  35. #include "core/sort_array.h"
  36. #include "core/vector.h"
  37. #include <type_traits>
  38. template <class T, class U = uint32_t, bool force_trivial = false>
  39. class LocalVector {
  40. protected:
  41. U count = 0;
  42. U capacity = 0;
  43. T *data = nullptr;
  44. public:
  45. T *ptr() {
  46. return data;
  47. }
  48. const T *ptr() const {
  49. return data;
  50. }
  51. _FORCE_INLINE_ void push_back(T p_elem) {
  52. if (unlikely(count == capacity)) {
  53. if (capacity == 0) {
  54. capacity = 1;
  55. } else {
  56. capacity <<= 1;
  57. }
  58. data = (T *)memrealloc(data, capacity * sizeof(T));
  59. CRASH_COND_MSG(!data, "Out of memory");
  60. }
  61. if (!std::is_trivially_constructible<T>::value && !force_trivial) {
  62. memnew_placement(&data[count++], T(p_elem));
  63. } else {
  64. data[count++] = p_elem;
  65. }
  66. }
  67. void remove(U p_index) {
  68. ERR_FAIL_UNSIGNED_INDEX(p_index, count);
  69. count--;
  70. for (U i = p_index; i < count; i++) {
  71. data[i] = data[i + 1];
  72. }
  73. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  74. data[count].~T();
  75. }
  76. }
  77. // Removes the item copying the last value into the position of the one to
  78. // remove. It's generally faster than `remove`.
  79. void remove_unordered(U p_index) {
  80. ERR_FAIL_INDEX(p_index, count);
  81. count--;
  82. if (count > p_index) {
  83. data[p_index] = data[count];
  84. }
  85. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  86. data[count].~T();
  87. }
  88. }
  89. void erase(const T &p_val) {
  90. int64_t idx = find(p_val);
  91. if (idx >= 0) {
  92. remove(idx);
  93. }
  94. }
  95. U erase_multiple_unordered(const T &p_val) {
  96. U from = 0;
  97. U count = 0;
  98. while (true) {
  99. int64_t idx = find(p_val, from);
  100. if (idx == -1) {
  101. break;
  102. }
  103. remove_unordered(idx);
  104. from = idx;
  105. count++;
  106. }
  107. return count;
  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 empty() const { return count == 0; }
  124. _FORCE_INLINE_ void reserve(U p_size) {
  125. 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. _FORCE_INLINE_ U size() const { return count; }
  133. void resize(U p_size) {
  134. if (p_size < count) {
  135. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  136. for (U i = p_size; i < count; i++) {
  137. data[i].~T();
  138. }
  139. }
  140. count = p_size;
  141. } else if (p_size > count) {
  142. if (unlikely(p_size > capacity)) {
  143. if (capacity == 0) {
  144. capacity = 1;
  145. }
  146. while (capacity < p_size) {
  147. capacity <<= 1;
  148. }
  149. data = (T *)memrealloc(data, capacity * sizeof(T));
  150. CRASH_COND_MSG(!data, "Out of memory");
  151. }
  152. if (!std::is_trivially_constructible<T>::value && !force_trivial) {
  153. for (U i = count; i < p_size; i++) {
  154. memnew_placement(&data[i], T);
  155. }
  156. }
  157. count = p_size;
  158. }
  159. }
  160. _FORCE_INLINE_ const T &operator[](U p_index) const {
  161. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  162. return data[p_index];
  163. }
  164. _FORCE_INLINE_ T &operator[](U p_index) {
  165. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  166. return data[p_index];
  167. }
  168. void fill(T p_val) {
  169. for (U i = 0; i < count; i++) {
  170. data[i] = p_val;
  171. }
  172. }
  173. void insert(U p_pos, T p_val) {
  174. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  175. if (p_pos == count) {
  176. push_back(p_val);
  177. } else {
  178. resize(count + 1);
  179. for (U i = count - 1; i > p_pos; i--) {
  180. data[i] = data[i - 1];
  181. }
  182. data[p_pos] = p_val;
  183. }
  184. }
  185. int64_t find(const T &p_val, U p_from = 0) const {
  186. for (U i = p_from; i < count; i++) {
  187. if (data[i] == p_val) {
  188. return int64_t(i);
  189. }
  190. }
  191. return -1;
  192. }
  193. template <class C>
  194. void sort_custom() {
  195. U len = count;
  196. if (len == 0) {
  197. return;
  198. }
  199. SortArray<T, C> sorter;
  200. sorter.sort(data, len);
  201. }
  202. void sort() {
  203. sort_custom<_DefaultComparator<T>>();
  204. }
  205. void ordered_insert(T p_val) {
  206. U i;
  207. for (i = 0; i < count; i++) {
  208. if (p_val < data[i]) {
  209. break;
  210. }
  211. }
  212. insert(i, p_val);
  213. }
  214. operator Vector<T>() const {
  215. Vector<T> ret;
  216. ret.resize(size());
  217. T *w = ret.ptrw();
  218. memcpy(w, data, sizeof(T) * count);
  219. return ret;
  220. }
  221. operator PoolVector<T>() const {
  222. PoolVector<T> pl;
  223. if (size()) {
  224. pl.resize(size());
  225. typename PoolVector<T>::Write w = pl.write();
  226. T *dest = w.ptr();
  227. memcpy(dest, data, sizeof(T) * count);
  228. }
  229. return pl;
  230. }
  231. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  232. Vector<uint8_t> ret;
  233. ret.resize(count * sizeof(T));
  234. uint8_t *w = ret.ptrw();
  235. memcpy(w, data, sizeof(T) * count);
  236. return ret;
  237. }
  238. _FORCE_INLINE_ LocalVector() {}
  239. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  240. resize(p_from.size());
  241. for (U i = 0; i < p_from.count; i++) {
  242. data[i] = p_from.data[i];
  243. }
  244. }
  245. LocalVector(const Vector<T> &p_from) {
  246. resize(p_from.size());
  247. for (U i = 0; i < count; i++) {
  248. data[i] = p_from[i];
  249. }
  250. }
  251. LocalVector(const PoolVector<T> &p_from) {
  252. resize(p_from.size());
  253. typename PoolVector<T>::Read r = p_from.read();
  254. for (U i = 0; i < count; i++) {
  255. data[i] = r[i];
  256. }
  257. }
  258. inline LocalVector &operator=(const LocalVector &p_from) {
  259. resize(p_from.size());
  260. for (U i = 0; i < p_from.count; i++) {
  261. data[i] = p_from.data[i];
  262. }
  263. return *this;
  264. }
  265. inline LocalVector &operator=(const Vector<T> &p_from) {
  266. resize(p_from.size());
  267. for (U i = 0; i < count; i++) {
  268. data[i] = p_from[i];
  269. }
  270. return *this;
  271. }
  272. inline LocalVector &operator=(const PoolVector<T> &p_from) {
  273. resize(p_from.size());
  274. typename PoolVector<T>::Read r = p_from.read();
  275. for (U i = 0; i < count; i++) {
  276. data[i] = r[i];
  277. }
  278. return *this;
  279. }
  280. _FORCE_INLINE_ ~LocalVector() {
  281. if (data) {
  282. reset();
  283. }
  284. }
  285. };
  286. // Integer default version
  287. template <class T, class I = int32_t, bool force_trivial = false>
  288. class LocalVectori : public LocalVector<T, I, force_trivial> {
  289. };
  290. #endif // LOCAL_VECTOR_H