2
0

vector.hpp 9.5 KB

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