local_vector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*************************************************************************/
  2. /* local_vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/copymem.h"
  34. #include "core/os/memory.h"
  35. #include "core/sort_array.h"
  36. #include "core/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. _FORCE_INLINE_ void push_back(T p_elem) {
  45. if (unlikely(count == capacity)) {
  46. if (capacity == 0) {
  47. capacity = 1;
  48. } else {
  49. capacity <<= 1;
  50. }
  51. data = (T *)memrealloc(data, capacity * sizeof(T));
  52. CRASH_COND_MSG(!data, "Out of memory");
  53. }
  54. if (!__has_trivial_constructor(T) && !force_trivial) {
  55. memnew_placement(&data[count++], T(p_elem));
  56. } else {
  57. data[count++] = p_elem;
  58. }
  59. }
  60. void remove(U p_index) {
  61. ERR_FAIL_UNSIGNED_INDEX(p_index, count);
  62. for (U i = p_index; i < count; i++) {
  63. data[i] = data[i + 1];
  64. }
  65. count--;
  66. if (!__has_trivial_destructor(T) && !force_trivial) {
  67. data[count].~T();
  68. }
  69. }
  70. void erase(const T &p_val) {
  71. U idx = find(p_val);
  72. if (idx >= 0) {
  73. remove(idx);
  74. }
  75. }
  76. void invert() {
  77. for (U i = 0; i < count / 2; i++) {
  78. SWAP(data[i], data[count - i - 1]);
  79. }
  80. }
  81. _FORCE_INLINE_ void clear() { resize(0); }
  82. _FORCE_INLINE_ void reset() {
  83. clear();
  84. if (data) {
  85. memfree(data);
  86. data = nullptr;
  87. capacity = 0;
  88. }
  89. }
  90. _FORCE_INLINE_ bool empty() const { return count == 0; }
  91. _FORCE_INLINE_ void reserve(U p_size) {
  92. p_size = nearest_power_of_2_templated(p_size);
  93. if (p_size > capacity) {
  94. capacity = p_size;
  95. data = (T *)memrealloc(data, capacity * sizeof(T));
  96. CRASH_COND_MSG(!data, "Out of memory");
  97. }
  98. }
  99. _FORCE_INLINE_ U size() const { return count; }
  100. void resize(U p_size) {
  101. if (p_size < count) {
  102. if (!__has_trivial_destructor(T) && !force_trivial) {
  103. for (U i = p_size; i < count; i++) {
  104. data[i].~T();
  105. }
  106. }
  107. count = p_size;
  108. } else if (p_size > count) {
  109. if (unlikely(p_size > capacity)) {
  110. if (capacity == 0) {
  111. capacity = 1;
  112. }
  113. while (capacity < p_size) {
  114. capacity <<= 1;
  115. }
  116. data = (T *)memrealloc(data, capacity * sizeof(T));
  117. CRASH_COND_MSG(!data, "Out of memory");
  118. }
  119. if (!__has_trivial_constructor(T) && !force_trivial) {
  120. for (U i = count; i < p_size; i++) {
  121. memnew_placement(&data[i], T);
  122. }
  123. }
  124. count = p_size;
  125. }
  126. }
  127. _FORCE_INLINE_ const T &operator[](U p_index) const {
  128. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  129. return data[p_index];
  130. }
  131. _FORCE_INLINE_ T &operator[](U p_index) {
  132. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  133. return data[p_index];
  134. }
  135. void insert(U p_pos, T p_val) {
  136. ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
  137. if (p_pos == count) {
  138. push_back(p_val);
  139. } else {
  140. resize(count + 1);
  141. for (U i = count; i > p_pos; i--) {
  142. data[i] = data[i - 1];
  143. }
  144. data[p_pos] = p_val;
  145. }
  146. }
  147. int64_t find(const T &p_val, U p_from = 0) const {
  148. for (U i = 0; i < count; i++) {
  149. if (data[i] == p_val) {
  150. return int64_t(i);
  151. }
  152. }
  153. return -1;
  154. }
  155. template <class C>
  156. void sort_custom() {
  157. U len = count;
  158. if (len == 0) {
  159. return;
  160. }
  161. SortArray<T, C> sorter;
  162. sorter.sort(data, len);
  163. }
  164. void sort() {
  165. sort_custom<_DefaultComparator<T>>();
  166. }
  167. void ordered_insert(T p_val) {
  168. U i;
  169. for (i = 0; i < count; i++) {
  170. if (p_val < data[i]) {
  171. break;
  172. };
  173. };
  174. insert(i, p_val);
  175. }
  176. operator Vector<T>() const {
  177. Vector<T> ret;
  178. ret.resize(size());
  179. T *w = ret.ptrw();
  180. copymem(w, data, sizeof(T) * count);
  181. return ret;
  182. }
  183. Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
  184. Vector<uint8_t> ret;
  185. ret.resize(count * sizeof(T));
  186. uint8_t *w = ret.ptrw();
  187. copymem(w, data, sizeof(T) * count);
  188. return ret;
  189. }
  190. _FORCE_INLINE_ LocalVector() {}
  191. _FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
  192. resize(p_from.size());
  193. for (U i = 0; i < p_from.count; i++) {
  194. data[i] = p_from.data[i];
  195. }
  196. }
  197. inline LocalVector &operator=(const LocalVector &p_from) {
  198. resize(p_from.size());
  199. for (U i = 0; i < p_from.count; i++) {
  200. data[i] = p_from.data[i];
  201. }
  202. return *this;
  203. }
  204. inline LocalVector &operator=(const Vector<T> &p_from) {
  205. resize(p_from.size());
  206. for (U i = 0; i < count; i++) {
  207. data[i] = p_from[i];
  208. }
  209. return *this;
  210. }
  211. _FORCE_INLINE_ ~LocalVector() {
  212. if (data) {
  213. reset();
  214. }
  215. }
  216. };
  217. #endif // LOCAL_VECTOR_H