local_vector.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_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_ U get_capacity() const { return capacity; }
  125. _FORCE_INLINE_ void reserve(U p_size, bool p_allow_shrink = false) {
  126. p_size = nearest_power_of_2_templated(p_size);
  127. if (!p_allow_shrink ? p_size > capacity : ((p_size >= count) && (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 (!std::is_trivially_destructible<T>::value && !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. if (capacity == 0) {
  145. capacity = 1;
  146. }
  147. while (capacity < p_size) {
  148. capacity <<= 1;
  149. }
  150. data = (T *)memrealloc(data, capacity * sizeof(T));
  151. CRASH_COND_MSG(!data, "Out of memory");
  152. }
  153. if (!std::is_trivially_constructible<T>::value && !force_trivial) {
  154. for (U i = count; i < p_size; i++) {
  155. memnew_placement(&data[i], T);
  156. }
  157. }
  158. count = p_size;
  159. }
  160. }
  161. _FORCE_INLINE_ const T &operator[](U p_index) const {
  162. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  163. return data[p_index];
  164. }
  165. _FORCE_INLINE_ T &operator[](U p_index) {
  166. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  167. return data[p_index];
  168. }
  169. void fill(T p_val) {
  170. for (U i = 0; i < count; i++) {
  171. data[i] = p_val;
  172. }
  173. }
  174. void insert(U p_pos, T p_val) {
  175. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  176. if (p_pos == count) {
  177. push_back(p_val);
  178. } else {
  179. resize(count + 1);
  180. for (U i = count - 1; i > p_pos; i--) {
  181. data[i] = data[i - 1];
  182. }
  183. data[p_pos] = p_val;
  184. }
  185. }
  186. int64_t find(const T &p_val, U p_from = 0) const {
  187. for (U i = p_from; i < count; i++) {
  188. if (data[i] == p_val) {
  189. return int64_t(i);
  190. }
  191. }
  192. return -1;
  193. }
  194. template <class C>
  195. void sort_custom() {
  196. U len = count;
  197. if (len == 0) {
  198. return;
  199. }
  200. SortArray<T, C> sorter;
  201. sorter.sort(data, len);
  202. }
  203. void sort() {
  204. sort_custom<_DefaultComparator<T>>();
  205. }
  206. void ordered_insert(T p_val) {
  207. U i;
  208. for (i = 0; i < count; i++) {
  209. if (p_val < data[i]) {
  210. break;
  211. }
  212. }
  213. insert(i, p_val);
  214. }
  215. operator Vector<T>() const {
  216. Vector<T> ret;
  217. ret.resize(size());
  218. T *w = ret.ptrw();
  219. memcpy(w, data, sizeof(T) * count);
  220. return ret;
  221. }
  222. operator PoolVector<T>() const {
  223. PoolVector<T> pl;
  224. if (size()) {
  225. pl.resize(size());
  226. typename PoolVector<T>::Write w = pl.write();
  227. T *dest = w.ptr();
  228. memcpy(dest, data, sizeof(T) * count);
  229. }
  230. return pl;
  231. }
  232. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  233. Vector<uint8_t> ret;
  234. ret.resize(count * sizeof(T));
  235. uint8_t *w = ret.ptrw();
  236. memcpy(w, data, sizeof(T) * count);
  237. return ret;
  238. }
  239. _FORCE_INLINE_ LocalVector() {}
  240. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  241. resize(p_from.size());
  242. for (U i = 0; i < p_from.count; i++) {
  243. data[i] = p_from.data[i];
  244. }
  245. }
  246. LocalVector(const Vector<T> &p_from) {
  247. resize(p_from.size());
  248. for (U i = 0; i < count; i++) {
  249. data[i] = p_from[i];
  250. }
  251. }
  252. LocalVector(const PoolVector<T> &p_from) {
  253. resize(p_from.size());
  254. typename PoolVector<T>::Read r = p_from.read();
  255. for (U i = 0; i < count; i++) {
  256. data[i] = r[i];
  257. }
  258. }
  259. inline LocalVector &operator=(const LocalVector &p_from) {
  260. resize(p_from.size());
  261. for (U i = 0; i < p_from.count; i++) {
  262. data[i] = p_from.data[i];
  263. }
  264. return *this;
  265. }
  266. inline LocalVector &operator=(const Vector<T> &p_from) {
  267. resize(p_from.size());
  268. for (U i = 0; i < count; i++) {
  269. data[i] = p_from[i];
  270. }
  271. return *this;
  272. }
  273. inline LocalVector &operator=(const PoolVector<T> &p_from) {
  274. resize(p_from.size());
  275. typename PoolVector<T>::Read r = p_from.read();
  276. for (U i = 0; i < count; i++) {
  277. data[i] = r[i];
  278. }
  279. return *this;
  280. }
  281. _FORCE_INLINE_ ~LocalVector() {
  282. if (data) {
  283. reset();
  284. }
  285. }
  286. };
  287. // Integer default version
  288. template <class T, class I = int32_t, bool force_trivial = false>
  289. class LocalVectori : public LocalVector<T, I, force_trivial> {
  290. };
  291. #endif // LOCAL_VECTOR_H