vector.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**************************************************************************/
  2. /* 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. #pragma once
  31. /**
  32. * @class Vector
  33. * Vector container. Simple copy-on-write container.
  34. *
  35. * LocalVector is an alternative available for internal use when COW is not
  36. * required.
  37. */
  38. #include "core/error/error_macros.h"
  39. #include "core/templates/cowdata.h"
  40. #include "core/templates/search_array.h"
  41. #include "core/templates/sort_array.h"
  42. #include <initializer_list>
  43. template <typename T>
  44. class VectorWriteProxy {
  45. public:
  46. _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) {
  47. CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
  48. return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
  49. }
  50. };
  51. template <typename T>
  52. class Vector {
  53. friend class VectorWriteProxy<T>;
  54. public:
  55. VectorWriteProxy<T> write;
  56. typedef typename CowData<T>::Size Size;
  57. private:
  58. CowData<T> _cowdata;
  59. public:
  60. // Must take a copy instead of a reference (see GH-31736).
  61. bool push_back(T p_elem);
  62. _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
  63. void fill(T p_elem);
  64. void remove_at(Size p_index) { _cowdata.remove_at(p_index); }
  65. _FORCE_INLINE_ bool erase(const T &p_val) {
  66. Size idx = find(p_val);
  67. if (idx >= 0) {
  68. remove_at(idx);
  69. return true;
  70. }
  71. return false;
  72. }
  73. void reverse();
  74. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  75. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  76. _FORCE_INLINE_ Size size() const { return _cowdata.size(); }
  77. _FORCE_INLINE_ operator Span<T>() const { return _cowdata.span(); }
  78. _FORCE_INLINE_ Span<T> span() const { return _cowdata.span(); }
  79. _FORCE_INLINE_ void clear() { resize(0); }
  80. _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
  81. _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
  82. _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
  83. _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  84. Error resize(Size p_size) { return _cowdata.resize(p_size); }
  85. Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
  86. _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
  87. // Must take a copy instead of a reference (see GH-31736).
  88. Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
  89. Size find(const T &p_val, Size p_from = 0) const {
  90. if (p_from < 0) {
  91. p_from = size() + p_from;
  92. }
  93. if (p_from < 0 || p_from >= size()) {
  94. return -1;
  95. }
  96. return span().find(p_val, p_from);
  97. }
  98. Size rfind(const T &p_val, Size p_from = -1) const {
  99. if (p_from < 0) {
  100. p_from = size() + p_from;
  101. }
  102. if (p_from < 0 || p_from >= size()) {
  103. return -1;
  104. }
  105. return span().rfind(p_val, p_from);
  106. }
  107. Size count(const T &p_val) const { return span().count(p_val); }
  108. // Must take a copy instead of a reference (see GH-31736).
  109. void append_array(Vector<T> p_other);
  110. _FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
  111. void sort() {
  112. sort_custom<Comparator<T>>();
  113. }
  114. template <typename Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, typename... Args>
  115. void sort_custom(Args &&...args) {
  116. Size len = _cowdata.size();
  117. if (len == 0) {
  118. return;
  119. }
  120. T *data = ptrw();
  121. SortArray<T, Comparator, Validate> sorter{ args... };
  122. sorter.sort(data, len);
  123. }
  124. Size bsearch(const T &p_value, bool p_before) {
  125. return bsearch_custom<Comparator<T>>(p_value, p_before);
  126. }
  127. template <typename Comparator, typename Value, typename... Args>
  128. Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) {
  129. SearchArray<T, Comparator> search{ args... };
  130. return search.bisect(ptrw(), size(), p_value, p_before);
  131. }
  132. Vector<T> duplicate() {
  133. return *this;
  134. }
  135. void ordered_insert(const T &p_val) {
  136. Size i;
  137. for (i = 0; i < _cowdata.size(); i++) {
  138. if (p_val < operator[](i)) {
  139. break;
  140. }
  141. }
  142. insert(i, p_val);
  143. }
  144. void operator=(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  145. void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); }
  146. Vector<uint8_t> to_byte_array() const {
  147. Vector<uint8_t> ret;
  148. if (is_empty()) {
  149. return ret;
  150. }
  151. size_t alloc_size = size() * sizeof(T);
  152. ret.resize(alloc_size);
  153. if (alloc_size) {
  154. memcpy(ret.ptrw(), ptr(), alloc_size);
  155. }
  156. return ret;
  157. }
  158. Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const {
  159. Vector<T> result;
  160. const Size s = size();
  161. Size begin = CLAMP(p_begin, -s, s);
  162. if (begin < 0) {
  163. begin += s;
  164. }
  165. Size end = CLAMP(p_end, -s, s);
  166. if (end < 0) {
  167. end += s;
  168. }
  169. ERR_FAIL_COND_V(begin > end, result);
  170. Size result_size = end - begin;
  171. result.resize(result_size);
  172. const T *const r = ptr();
  173. T *const w = result.ptrw();
  174. for (Size i = 0; i < result_size; ++i) {
  175. w[i] = r[begin + i];
  176. }
  177. return result;
  178. }
  179. bool operator==(const Vector<T> &p_arr) const {
  180. Size s = size();
  181. if (s != p_arr.size()) {
  182. return false;
  183. }
  184. for (Size i = 0; i < s; i++) {
  185. if (operator[](i) != p_arr[i]) {
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. bool operator!=(const Vector<T> &p_arr) const {
  192. Size s = size();
  193. if (s != p_arr.size()) {
  194. return true;
  195. }
  196. for (Size i = 0; i < s; i++) {
  197. if (operator[](i) != p_arr[i]) {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. struct Iterator {
  204. _FORCE_INLINE_ T &operator*() const {
  205. return *elem_ptr;
  206. }
  207. _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
  208. _FORCE_INLINE_ Iterator &operator++() {
  209. elem_ptr++;
  210. return *this;
  211. }
  212. _FORCE_INLINE_ Iterator &operator--() {
  213. elem_ptr--;
  214. return *this;
  215. }
  216. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
  217. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
  218. Iterator(T *p_ptr) { elem_ptr = p_ptr; }
  219. Iterator() {}
  220. Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
  221. private:
  222. T *elem_ptr = nullptr;
  223. };
  224. struct ConstIterator {
  225. _FORCE_INLINE_ const T &operator*() const {
  226. return *elem_ptr;
  227. }
  228. _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
  229. _FORCE_INLINE_ ConstIterator &operator++() {
  230. elem_ptr++;
  231. return *this;
  232. }
  233. _FORCE_INLINE_ ConstIterator &operator--() {
  234. elem_ptr--;
  235. return *this;
  236. }
  237. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
  238. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
  239. ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
  240. ConstIterator() {}
  241. ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
  242. private:
  243. const T *elem_ptr = nullptr;
  244. };
  245. _FORCE_INLINE_ Iterator begin() {
  246. return Iterator(ptrw());
  247. }
  248. _FORCE_INLINE_ Iterator end() {
  249. return Iterator(ptrw() + size());
  250. }
  251. _FORCE_INLINE_ ConstIterator begin() const {
  252. return ConstIterator(ptr());
  253. }
  254. _FORCE_INLINE_ ConstIterator end() const {
  255. return ConstIterator(ptr() + size());
  256. }
  257. _FORCE_INLINE_ Vector() {}
  258. _FORCE_INLINE_ Vector(std::initializer_list<T> p_init) :
  259. _cowdata(p_init) {}
  260. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  261. _FORCE_INLINE_ Vector(Vector &&p_from) :
  262. _cowdata(std::move(p_from._cowdata)) {}
  263. _FORCE_INLINE_ ~Vector() {}
  264. };
  265. template <typename T>
  266. void Vector<T>::reverse() {
  267. for (Size i = 0; i < size() / 2; i++) {
  268. T *p = ptrw();
  269. SWAP(p[i], p[size() - i - 1]);
  270. }
  271. }
  272. template <typename T>
  273. void Vector<T>::append_array(Vector<T> p_other) {
  274. const Size ds = p_other.size();
  275. if (ds == 0) {
  276. return;
  277. }
  278. const Size bs = size();
  279. resize(bs + ds);
  280. for (Size i = 0; i < ds; ++i) {
  281. ptrw()[bs + i] = p_other[i];
  282. }
  283. }
  284. template <typename T>
  285. bool Vector<T>::push_back(T p_elem) {
  286. Error err = resize(size() + 1);
  287. ERR_FAIL_COND_V(err, true);
  288. set(size() - 1, p_elem);
  289. return false;
  290. }
  291. template <typename T>
  292. void Vector<T>::fill(T p_elem) {
  293. T *p = ptrw();
  294. for (Size i = 0; i < size(); i++) {
  295. p[i] = p_elem;
  296. }
  297. }
  298. // Zero-constructing Vector initializes CowData.ptr() to nullptr and thus empty.
  299. template <typename T>
  300. struct is_zero_constructible<Vector<T>> : std::true_type {};