vector.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/cowdata.h"
  38. #include "core/error_macros.h"
  39. #include "core/os/memory.h"
  40. #include "core/sort_array.h"
  41. template <class T>
  42. class VectorWriteProxy {
  43. public:
  44. _FORCE_INLINE_ T &operator[](int p_index) {
  45. CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
  46. return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
  47. }
  48. };
  49. template <class T>
  50. class Vector {
  51. friend class VectorWriteProxy<T>;
  52. public:
  53. VectorWriteProxy<T> write;
  54. private:
  55. CowData<T> _cowdata;
  56. public:
  57. bool push_back(T p_elem);
  58. _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
  59. void remove(int p_index) { _cowdata.remove(p_index); }
  60. void erase(const T &p_val) {
  61. int idx = find(p_val);
  62. if (idx >= 0) remove(idx);
  63. }
  64. void invert();
  65. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  66. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  67. _FORCE_INLINE_ void clear() { resize(0); }
  68. _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); }
  69. _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
  70. _FORCE_INLINE_ const T get(int p_index) const { return _cowdata.get(p_index); }
  71. _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  72. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  73. Error resize(int p_size) { return _cowdata.resize(p_size); }
  74. _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
  75. Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
  76. int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); }
  77. void append_array(Vector<T> p_other);
  78. template <class C>
  79. void sort_custom() {
  80. int len = _cowdata.size();
  81. if (len == 0)
  82. return;
  83. T *data = ptrw();
  84. SortArray<T, C> sorter;
  85. sorter.sort(data, len);
  86. }
  87. void sort() {
  88. sort_custom<_DefaultComparator<T> >();
  89. }
  90. void ordered_insert(const T &p_val) {
  91. int i;
  92. for (i = 0; i < _cowdata.size(); i++) {
  93. if (p_val < operator[](i)) {
  94. break;
  95. };
  96. };
  97. insert(i, p_val);
  98. }
  99. _FORCE_INLINE_ Vector() {}
  100. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  101. inline Vector &operator=(const Vector &p_from) {
  102. _cowdata._ref(p_from._cowdata);
  103. return *this;
  104. }
  105. Vector<T> subarray(int p_from, int p_to) const {
  106. if (p_from < 0) {
  107. p_from = size() + p_from;
  108. }
  109. if (p_to < 0) {
  110. p_to = size() + p_to;
  111. }
  112. ERR_FAIL_INDEX_V(p_from, size(), Vector<T>());
  113. ERR_FAIL_INDEX_V(p_to, size(), Vector<T>());
  114. Vector<T> slice;
  115. int span = 1 + p_to - p_from;
  116. slice.resize(span);
  117. const T *r = ptr();
  118. T *w = slice.ptrw();
  119. for (int i = 0; i < span; ++i) {
  120. w[i] = r[p_from + i];
  121. }
  122. return slice;
  123. }
  124. _FORCE_INLINE_ ~Vector() {}
  125. };
  126. template <class T>
  127. void Vector<T>::invert() {
  128. for (int i = 0; i < size() / 2; i++) {
  129. T *p = ptrw();
  130. SWAP(p[i], p[size() - i - 1]);
  131. }
  132. }
  133. template <class T>
  134. void Vector<T>::append_array(Vector<T> p_other) {
  135. const int ds = p_other.size();
  136. if (ds == 0)
  137. return;
  138. const int bs = size();
  139. resize(bs + ds);
  140. for (int i = 0; i < ds; ++i)
  141. ptrw()[bs + i] = p_other[i];
  142. }
  143. template <class T>
  144. bool Vector<T>::push_back(T p_elem) {
  145. Error err = resize(size() + 1);
  146. ERR_FAIL_COND_V(err, true);
  147. set(size() - 1, p_elem);
  148. return false;
  149. }
  150. #endif