vector.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*************************************************************************/
  2. /* vector.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_HPP
  31. #define VECTOR_HPP
  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 <godot_cpp/core/error_macros.hpp>
  37. #include <godot_cpp/core/memory.hpp>
  38. #include <godot_cpp/templates/cowdata.hpp>
  39. #include <godot_cpp/templates/search_array.hpp>
  40. #include <godot_cpp/templates/sort_array.hpp>
  41. #include <climits>
  42. #include <initializer_list>
  43. namespace godot {
  44. template <class T>
  45. class VectorWriteProxy {
  46. public:
  47. _FORCE_INLINE_ T &operator[](int 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 <class T>
  53. class Vector {
  54. friend class VectorWriteProxy<T>;
  55. public:
  56. VectorWriteProxy<T> write;
  57. private:
  58. CowData<T> _cowdata;
  59. public:
  60. bool push_back(T p_elem);
  61. _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } // alias
  62. void fill(T p_elem);
  63. void remove_at(int p_index) { _cowdata.remove_at(p_index); }
  64. void erase(const T &p_val) {
  65. int idx = find(p_val);
  66. if (idx >= 0) {
  67. remove_at(idx);
  68. }
  69. }
  70. void reverse();
  71. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  72. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  73. _FORCE_INLINE_ void clear() { resize(0); }
  74. _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
  75. _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
  76. _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); }
  77. _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  78. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  79. Error resize(int p_size) { return _cowdata.resize(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. void append_array(Vector<T> p_other);
  84. _FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
  85. template <class C>
  86. void sort_custom() {
  87. int len = _cowdata.size();
  88. if (len == 0) {
  89. return;
  90. }
  91. T *data = ptrw();
  92. SortArray<T, C> sorter;
  93. sorter.sort(data, len);
  94. }
  95. void sort() {
  96. sort_custom<_DefaultComparator<T>>();
  97. }
  98. int bsearch(const T &p_value, bool p_before) {
  99. SearchArray<T> search;
  100. return search.bisect(ptrw(), size(), p_value, p_before);
  101. }
  102. Vector<T> duplicate() {
  103. return *this;
  104. }
  105. void ordered_insert(const T &p_val) {
  106. int i;
  107. for (i = 0; i < _cowdata.size(); i++) {
  108. if (p_val < operator[](i)) {
  109. break;
  110. }
  111. }
  112. insert(i, p_val);
  113. }
  114. inline void operator=(const Vector &p_from) {
  115. _cowdata._ref(p_from._cowdata);
  116. }
  117. Vector<uint8_t> to_byte_array() const {
  118. Vector<uint8_t> ret;
  119. ret.resize(size() * sizeof(T));
  120. memcpy(ret.ptrw(), ptr(), sizeof(T) * size());
  121. return ret;
  122. }
  123. Vector<T> slice(int p_begin, int p_end = INT_MAX) const {
  124. Vector<T> result;
  125. const int s = size();
  126. int begin = Math::clamp(p_begin, -s, s);
  127. if (begin < 0) {
  128. begin += s;
  129. }
  130. int end = Math::clamp(p_end, -s, s);
  131. if (end < 0) {
  132. end += s;
  133. }
  134. ERR_FAIL_COND_V(begin > end, result);
  135. int result_size = end - begin;
  136. result.resize(result_size);
  137. const T *const r = ptr();
  138. T *const w = result.ptrw();
  139. for (int i = 0; i < result_size; ++i) {
  140. w[i] = r[begin + i];
  141. }
  142. return result;
  143. }
  144. bool operator==(const Vector<T> &p_arr) const {
  145. int s = size();
  146. if (s != p_arr.size()) {
  147. return false;
  148. }
  149. for (int i = 0; i < s; i++) {
  150. if (operator[](i) != p_arr[i]) {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. bool operator!=(const Vector<T> &p_arr) const {
  157. int s = size();
  158. if (s != p_arr.size()) {
  159. return true;
  160. }
  161. for (int i = 0; i < s; i++) {
  162. if (operator[](i) != p_arr[i]) {
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. struct Iterator {
  169. _FORCE_INLINE_ T &operator*() const {
  170. return *elem_ptr;
  171. }
  172. _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
  173. _FORCE_INLINE_ Iterator &operator++() {
  174. elem_ptr++;
  175. return *this;
  176. }
  177. _FORCE_INLINE_ Iterator &operator--() {
  178. elem_ptr--;
  179. return *this;
  180. }
  181. _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
  182. _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
  183. Iterator(T *p_ptr) { elem_ptr = p_ptr; }
  184. Iterator() {}
  185. Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
  186. private:
  187. T *elem_ptr = nullptr;
  188. };
  189. struct ConstIterator {
  190. _FORCE_INLINE_ const T &operator*() const {
  191. return *elem_ptr;
  192. }
  193. _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
  194. _FORCE_INLINE_ ConstIterator &operator++() {
  195. elem_ptr++;
  196. return *this;
  197. }
  198. _FORCE_INLINE_ ConstIterator &operator--() {
  199. elem_ptr--;
  200. return *this;
  201. }
  202. _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
  203. _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
  204. ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
  205. ConstIterator() {}
  206. ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
  207. private:
  208. const T *elem_ptr = nullptr;
  209. };
  210. _FORCE_INLINE_ Iterator begin() {
  211. return Iterator(ptrw());
  212. }
  213. _FORCE_INLINE_ Iterator end() {
  214. return Iterator(ptrw() + size());
  215. }
  216. _FORCE_INLINE_ ConstIterator begin() const {
  217. return ConstIterator(ptr());
  218. }
  219. _FORCE_INLINE_ ConstIterator end() const {
  220. return ConstIterator(ptr() + size());
  221. }
  222. _FORCE_INLINE_ Vector() {}
  223. _FORCE_INLINE_ Vector(std::initializer_list<T> p_init) {
  224. Error err = _cowdata.resize(p_init.size());
  225. ERR_FAIL_COND(err);
  226. int i = 0;
  227. for (const T &element : p_init) {
  228. _cowdata.set(i++, element);
  229. }
  230. }
  231. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  232. _FORCE_INLINE_ ~Vector() {}
  233. };
  234. template <class T>
  235. void Vector<T>::reverse() {
  236. for (int i = 0; i < size() / 2; i++) {
  237. T *p = ptrw();
  238. SWAP(p[i], p[size() - i - 1]);
  239. }
  240. }
  241. template <class T>
  242. void Vector<T>::append_array(Vector<T> p_other) {
  243. const int ds = p_other.size();
  244. if (ds == 0) {
  245. return;
  246. }
  247. const int bs = size();
  248. resize(bs + ds);
  249. for (int i = 0; i < ds; ++i) {
  250. ptrw()[bs + i] = p_other[i];
  251. }
  252. }
  253. template <class T>
  254. bool Vector<T>::push_back(T p_elem) {
  255. Error err = resize(size() + 1);
  256. ERR_FAIL_COND_V(err, true);
  257. set(size() - 1, p_elem);
  258. return false;
  259. }
  260. template <class T>
  261. void Vector<T>::fill(T p_elem) {
  262. T *p = ptrw();
  263. for (int i = 0; i < size(); i++) {
  264. p[i] = p_elem;
  265. }
  266. }
  267. } // namespace godot
  268. #endif // ! VECTOR_HPP