vector.h 10 KB

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