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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/error_macros.h"
  33. #include "core/os/copymem.h"
  34. #include "core/os/memory.h"
  35. #include "core/templates/sort_array.h"
  36. #include "core/templates/vector.h"
  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 (!__has_trivial_constructor(T) && !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 (!__has_trivial_destructor(T) && !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 (!__has_trivial_destructor(T) && !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 is_empty() const { return count == 0; }
  109. _FORCE_INLINE_ U get_capacity() const { return capacity; }
  110. _FORCE_INLINE_ void reserve(U p_size) {
  111. p_size = nearest_power_of_2_templated(p_size);
  112. if (p_size > capacity) {
  113. capacity = p_size;
  114. data = (T *)memrealloc(data, capacity * sizeof(T));
  115. CRASH_COND_MSG(!data, "Out of memory");
  116. }
  117. }
  118. _FORCE_INLINE_ U size() const { return count; }
  119. void resize(U p_size) {
  120. if (p_size < count) {
  121. if (!__has_trivial_destructor(T) && !force_trivial) {
  122. for (U i = p_size; i < count; i++) {
  123. data[i].~T();
  124. }
  125. }
  126. count = p_size;
  127. } else if (p_size > count) {
  128. if (unlikely(p_size > capacity)) {
  129. if (capacity == 0) {
  130. capacity = 1;
  131. }
  132. while (capacity < p_size) {
  133. capacity <<= 1;
  134. }
  135. data = (T *)memrealloc(data, capacity * sizeof(T));
  136. CRASH_COND_MSG(!data, "Out of memory");
  137. }
  138. if (!__has_trivial_constructor(T) && !force_trivial) {
  139. for (U i = count; i < p_size; i++) {
  140. memnew_placement(&data[i], T);
  141. }
  142. }
  143. count = p_size;
  144. }
  145. }
  146. _FORCE_INLINE_ const T &operator[](U p_index) const {
  147. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  148. return data[p_index];
  149. }
  150. _FORCE_INLINE_ T &operator[](U p_index) {
  151. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  152. return data[p_index];
  153. }
  154. void insert(U p_pos, T p_val) {
  155. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  156. if (p_pos == count) {
  157. push_back(p_val);
  158. } else {
  159. resize(count + 1);
  160. for (U i = count; i > p_pos; i--) {
  161. data[i] = data[i - 1];
  162. }
  163. data[p_pos] = p_val;
  164. }
  165. }
  166. int64_t find(const T &p_val, U p_from = 0) const {
  167. for (U i = 0; i < count; i++) {
  168. if (data[i] == p_val) {
  169. return int64_t(i);
  170. }
  171. }
  172. return -1;
  173. }
  174. template <class C>
  175. void sort_custom() {
  176. U len = count;
  177. if (len == 0) {
  178. return;
  179. }
  180. SortArray<T, C> sorter;
  181. sorter.sort(data, len);
  182. }
  183. void sort() {
  184. sort_custom<_DefaultComparator<T>>();
  185. }
  186. void ordered_insert(T p_val) {
  187. U i;
  188. for (i = 0; i < count; i++) {
  189. if (p_val < data[i]) {
  190. break;
  191. }
  192. }
  193. insert(i, p_val);
  194. }
  195. operator Vector<T>() const {
  196. Vector<T> ret;
  197. ret.resize(size());
  198. T *w = ret.ptrw();
  199. copymem(w, data, sizeof(T) * count);
  200. return ret;
  201. }
  202. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  203. Vector<uint8_t> ret;
  204. ret.resize(count * sizeof(T));
  205. uint8_t *w = ret.ptrw();
  206. copymem(w, data, sizeof(T) * count);
  207. return ret;
  208. }
  209. _FORCE_INLINE_ LocalVector() {}
  210. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  211. resize(p_from.size());
  212. for (U i = 0; i < p_from.count; i++) {
  213. data[i] = p_from.data[i];
  214. }
  215. }
  216. inline LocalVector &operator=(const LocalVector &p_from) {
  217. resize(p_from.size());
  218. for (U i = 0; i < p_from.count; i++) {
  219. data[i] = p_from.data[i];
  220. }
  221. return *this;
  222. }
  223. inline LocalVector &operator=(const Vector<T> &p_from) {
  224. resize(p_from.size());
  225. for (U i = 0; i < count; i++) {
  226. data[i] = p_from[i];
  227. }
  228. return *this;
  229. }
  230. _FORCE_INLINE_ ~LocalVector() {
  231. if (data) {
  232. reset();
  233. }
  234. }
  235. };
  236. #endif // LOCAL_VECTOR_H