vector.h 10.0 KB

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