local_vector.h 6.8 KB

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