vector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_H
  31. #define VECTOR_H
  32. /**
  33. * @class Vector
  34. * @author Juan Linietsky
  35. * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use Vector for large arrays.
  36. */
  37. #include "core/error/error_macros.h"
  38. #include "core/os/copymem.h"
  39. #include "core/os/memory.h"
  40. #include "core/templates/cowdata.h"
  41. #include "core/templates/sort_array.h"
  42. template <class T>
  43. class VectorWriteProxy {
  44. public:
  45. _FORCE_INLINE_ T &operator[](int p_index) {
  46. CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
  47. return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
  48. }
  49. };
  50. template <class T>
  51. class Vector {
  52. friend class VectorWriteProxy<T>;
  53. public:
  54. VectorWriteProxy<T> write;
  55. private:
  56. CowData<T> _cowdata;
  57. public:
  58. bool push_back(T p_elem);
  59. _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
  60. void remove(int p_index) { _cowdata.remove(p_index); }
  61. void erase(const T &p_val) {
  62. int idx = find(p_val);
  63. if (idx >= 0) {
  64. remove(idx);
  65. }
  66. }
  67. void invert();
  68. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  69. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  70. _FORCE_INLINE_ void clear() { resize(0); }
  71. _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
  72. _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
  73. _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); }
  74. _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  75. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  76. Error resize(int p_size) { return _cowdata.resize(p_size); }
  77. _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
  78. Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
  79. int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); }
  80. void append_array(Vector<T> p_other);
  81. bool has(const T &p_val) {
  82. return find(p_val, 0) != -1;
  83. }
  84. template <class C>
  85. void sort_custom() {
  86. int len = _cowdata.size();
  87. if (len == 0) {
  88. return;
  89. }
  90. T *data = ptrw();
  91. SortArray<T, C> sorter;
  92. sorter.sort(data, len);
  93. }
  94. void sort() {
  95. sort_custom<_DefaultComparator<T>>();
  96. }
  97. Vector<T> duplicate() {
  98. return *this;
  99. }
  100. void ordered_insert(const T &p_val) {
  101. int i;
  102. for (i = 0; i < _cowdata.size(); i++) {
  103. if (p_val < operator[](i)) {
  104. break;
  105. }
  106. }
  107. insert(i, p_val);
  108. }
  109. inline Vector &operator=(const Vector &p_from) {
  110. _cowdata._ref(p_from._cowdata);
  111. return *this;
  112. }
  113. Vector<uint8_t> to_byte_array() const {
  114. Vector<uint8_t> ret;
  115. ret.resize(size() * sizeof(T));
  116. copymem(ret.ptrw(), ptr(), sizeof(T) * size());
  117. return ret;
  118. }
  119. Vector<T> subarray(int p_from, int p_to) const {
  120. if (p_from < 0) {
  121. p_from = size() + p_from;
  122. }
  123. if (p_to < 0) {
  124. p_to = size() + p_to;
  125. }
  126. ERR_FAIL_INDEX_V(p_from, size(), Vector<T>());
  127. ERR_FAIL_INDEX_V(p_to, size(), Vector<T>());
  128. Vector<T> slice;
  129. int span = 1 + p_to - p_from;
  130. slice.resize(span);
  131. const T *r = ptr();
  132. T *w = slice.ptrw();
  133. for (int i = 0; i < span; ++i) {
  134. w[i] = r[p_from + i];
  135. }
  136. return slice;
  137. }
  138. bool operator==(const Vector<T> &p_arr) const {
  139. int s = size();
  140. if (s != p_arr.size()) {
  141. return false;
  142. }
  143. for (int i = 0; i < s; i++) {
  144. if (operator[](i) != p_arr[i]) {
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. bool operator!=(const Vector<T> &p_arr) const {
  151. int s = size();
  152. if (s != p_arr.size()) {
  153. return true;
  154. }
  155. for (int i = 0; i < s; i++) {
  156. if (operator[](i) != p_arr[i]) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. _FORCE_INLINE_ Vector() {}
  163. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  164. _FORCE_INLINE_ ~Vector() {}
  165. };
  166. template <class T>
  167. void Vector<T>::invert() {
  168. for (int i = 0; i < size() / 2; i++) {
  169. T *p = ptrw();
  170. SWAP(p[i], p[size() - i - 1]);
  171. }
  172. }
  173. template <class T>
  174. void Vector<T>::append_array(Vector<T> p_other) {
  175. const int ds = p_other.size();
  176. if (ds == 0) {
  177. return;
  178. }
  179. const int bs = size();
  180. resize(bs + ds);
  181. for (int i = 0; i < ds; ++i) {
  182. ptrw()[bs + i] = p_other[i];
  183. }
  184. }
  185. template <class T>
  186. bool Vector<T>::push_back(T p_elem) {
  187. Error err = resize(size() + 1);
  188. ERR_FAIL_COND_V(err, true);
  189. set(size() - 1, p_elem);
  190. return false;
  191. }
  192. #endif // VECTOR_H