vector.h 9.2 KB

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