vector.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 PoolVector 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.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(const T &p_elem);
  58. void remove(int p_index) { _cowdata.remove(p_index); }
  59. void erase(const T &p_val) {
  60. int idx = find(p_val);
  61. if (idx >= 0) remove(idx);
  62. };
  63. void invert();
  64. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  65. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  66. _FORCE_INLINE_ void clear() { resize(0); }
  67. _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); }
  68. _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
  69. _FORCE_INLINE_ const T get(int p_index) const { return _cowdata.get(p_index); }
  70. _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  71. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  72. Error resize(int p_size) { return _cowdata.resize(p_size); }
  73. _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
  74. Error insert(int p_pos, const T &p_val) { return _cowdata.insert(p_pos, p_val); }
  75. void append_array(const Vector<T> &p_other);
  76. template <class C>
  77. void sort_custom() {
  78. int len = _cowdata.size();
  79. if (len == 0)
  80. return;
  81. T *data = ptrw();
  82. SortArray<T, C> sorter;
  83. sorter.sort(data, len);
  84. }
  85. void sort() {
  86. sort_custom<_DefaultComparator<T> >();
  87. }
  88. void ordered_insert(const T &p_val) {
  89. int i;
  90. for (i = 0; i < _cowdata.size(); i++) {
  91. if (p_val < operator[](i)) {
  92. break;
  93. };
  94. };
  95. insert(i, p_val);
  96. }
  97. int find(const T &p_val, int p_from = 0) const {
  98. int ret = -1;
  99. if (p_from < 0 || size() == 0)
  100. return ret;
  101. for (int i = p_from; i < size(); i++) {
  102. if (ptr()[i] == p_val) {
  103. ret = i;
  104. break;
  105. };
  106. };
  107. return ret;
  108. }
  109. _FORCE_INLINE_ Vector() {}
  110. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  111. inline Vector &operator=(const Vector &p_from) {
  112. _cowdata._ref(p_from._cowdata);
  113. return *this;
  114. }
  115. _FORCE_INLINE_ ~Vector() {}
  116. };
  117. template <class T>
  118. void Vector<T>::invert() {
  119. for (int i = 0; i < size() / 2; i++) {
  120. T *p = ptrw();
  121. SWAP(p[i], p[size() - i - 1]);
  122. }
  123. }
  124. template <class T>
  125. void Vector<T>::append_array(const Vector<T> &p_other) {
  126. const int ds = p_other.size();
  127. if (ds == 0)
  128. return;
  129. const int bs = size();
  130. resize(bs + ds);
  131. for (int i = 0; i < ds; ++i)
  132. ptrw()[bs + i] = p_other[i];
  133. }
  134. template <class T>
  135. bool Vector<T>::push_back(const T &p_elem) {
  136. Error err = resize(size() + 1);
  137. ERR_FAIL_COND_V(err, true)
  138. set(size() - 1, p_elem);
  139. return false;
  140. }
  141. #endif