local_vector.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/sort_array.h"
  35. #include "core/vector.h"
  36. #include <type_traits>
  37. template <class T, class U = uint32_t, bool force_trivial = false>
  38. class LocalVector {
  39. private:
  40. U count = 0;
  41. U capacity = 0;
  42. T *data = nullptr;
  43. public:
  44. T *ptr() {
  45. return data;
  46. }
  47. const T *ptr() const {
  48. return data;
  49. }
  50. _FORCE_INLINE_ void push_back(T p_elem) {
  51. if (unlikely(count == capacity)) {
  52. if (capacity == 0) {
  53. capacity = 1;
  54. } else {
  55. capacity <<= 1;
  56. }
  57. data = (T *)memrealloc(data, capacity * sizeof(T));
  58. CRASH_COND_MSG(!data, "Out of memory");
  59. }
  60. if (!std::is_trivially_constructible<T>::value && !force_trivial) {
  61. memnew_placement(&data[count++], T(p_elem));
  62. } else {
  63. data[count++] = p_elem;
  64. }
  65. }
  66. void remove(U p_index) {
  67. ERR_FAIL_UNSIGNED_INDEX(p_index, count);
  68. count--;
  69. for (U i = p_index; i < count; i++) {
  70. data[i] = data[i + 1];
  71. }
  72. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  73. data[count].~T();
  74. }
  75. }
  76. // Removes the item copying the last value into the position of the one to
  77. // remove. It's generally faster than `remove`.
  78. void remove_unordered(U p_index) {
  79. ERR_FAIL_INDEX(p_index, count);
  80. count--;
  81. if (count > p_index) {
  82. data[p_index] = data[count];
  83. }
  84. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  85. data[count].~T();
  86. }
  87. }
  88. void erase(const T &p_val) {
  89. int64_t idx = find(p_val);
  90. if (idx >= 0) {
  91. remove(idx);
  92. }
  93. }
  94. void invert() {
  95. for (U i = 0; i < count / 2; i++) {
  96. SWAP(data[i], data[count - i - 1]);
  97. }
  98. }
  99. _FORCE_INLINE_ void clear() { resize(0); }
  100. _FORCE_INLINE_ void reset() {
  101. clear();
  102. if (data) {
  103. memfree(data);
  104. data = nullptr;
  105. capacity = 0;
  106. }
  107. }
  108. _FORCE_INLINE_ bool empty() const { return count == 0; }
  109. _FORCE_INLINE_ void reserve(U p_size) {
  110. p_size = nearest_power_of_2_templated(p_size);
  111. if (p_size > capacity) {
  112. capacity = p_size;
  113. data = (T *)memrealloc(data, capacity * sizeof(T));
  114. CRASH_COND_MSG(!data, "Out of memory");
  115. }
  116. }
  117. _FORCE_INLINE_ U size() const { return count; }
  118. void resize(U p_size) {
  119. if (p_size < count) {
  120. if (!std::is_trivially_destructible<T>::value && !force_trivial) {
  121. for (U i = p_size; i < count; i++) {
  122. data[i].~T();
  123. }
  124. }
  125. count = p_size;
  126. } else if (p_size > count) {
  127. if (unlikely(p_size > capacity)) {
  128. if (capacity == 0) {
  129. capacity = 1;
  130. }
  131. while (capacity < p_size) {
  132. capacity <<= 1;
  133. }
  134. data = (T *)memrealloc(data, capacity * sizeof(T));
  135. CRASH_COND_MSG(!data, "Out of memory");
  136. }
  137. if (!std::is_trivially_constructible<T>::value && !force_trivial) {
  138. for (U i = count; i < p_size; i++) {
  139. memnew_placement(&data[i], T);
  140. }
  141. }
  142. count = p_size;
  143. }
  144. }
  145. _FORCE_INLINE_ const T &operator[](U p_index) const {
  146. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  147. return data[p_index];
  148. }
  149. _FORCE_INLINE_ T &operator[](U p_index) {
  150. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  151. return data[p_index];
  152. }
  153. void insert(U p_pos, T p_val) {
  154. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  155. if (p_pos == count) {
  156. push_back(p_val);
  157. } else {
  158. resize(count + 1);
  159. for (U i = count - 1; i > p_pos; i--) {
  160. data[i] = data[i - 1];
  161. }
  162. data[p_pos] = p_val;
  163. }
  164. }
  165. int64_t find(const T &p_val, U p_from = 0) const {
  166. for (U i = p_from; i < count; i++) {
  167. if (data[i] == p_val) {
  168. return int64_t(i);
  169. }
  170. }
  171. return -1;
  172. }
  173. template <class C>
  174. void sort_custom() {
  175. U len = count;
  176. if (len == 0) {
  177. return;
  178. }
  179. SortArray<T, C> sorter;
  180. sorter.sort(data, len);
  181. }
  182. void sort() {
  183. sort_custom<_DefaultComparator<T>>();
  184. }
  185. void ordered_insert(T p_val) {
  186. U i;
  187. for (i = 0; i < count; i++) {
  188. if (p_val < data[i]) {
  189. break;
  190. }
  191. }
  192. insert(i, p_val);
  193. }
  194. operator Vector<T>() const {
  195. Vector<T> ret;
  196. ret.resize(size());
  197. T *w = ret.ptrw();
  198. memcpy(w, data, sizeof(T) * count);
  199. return ret;
  200. }
  201. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  202. Vector<uint8_t> ret;
  203. ret.resize(count * sizeof(T));
  204. uint8_t *w = ret.ptrw();
  205. memcpy(w, data, sizeof(T) * count);
  206. return ret;
  207. }
  208. _FORCE_INLINE_ LocalVector() {}
  209. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  210. resize(p_from.size());
  211. for (U i = 0; i < p_from.count; i++) {
  212. data[i] = p_from.data[i];
  213. }
  214. }
  215. inline LocalVector &operator=(const LocalVector &p_from) {
  216. resize(p_from.size());
  217. for (U i = 0; i < p_from.count; i++) {
  218. data[i] = p_from.data[i];
  219. }
  220. return *this;
  221. }
  222. inline LocalVector &operator=(const Vector<T> &p_from) {
  223. resize(p_from.size());
  224. for (U i = 0; i < count; i++) {
  225. data[i] = p_from[i];
  226. }
  227. return *this;
  228. }
  229. _FORCE_INLINE_ ~LocalVector() {
  230. if (data) {
  231. reset();
  232. }
  233. }
  234. };
  235. #endif // LOCAL_VECTOR_H