vector.h 11 KB

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