vector.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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_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. 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, T p_val) { return _cowdata.insert(p_pos, p_val); }
  75. int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); }
  76. void append_array(Vector<T> p_other);
  77. template <class C>
  78. void sort_custom() {
  79. int len = _cowdata.size();
  80. if (len == 0)
  81. return;
  82. T *data = ptrw();
  83. SortArray<T, C> sorter;
  84. sorter.sort(data, len);
  85. }
  86. void sort() {
  87. sort_custom<_DefaultComparator<T> >();
  88. }
  89. void ordered_insert(const T &p_val) {
  90. int i;
  91. for (i = 0; i < _cowdata.size(); i++) {
  92. if (p_val < operator[](i)) {
  93. break;
  94. };
  95. };
  96. insert(i, p_val);
  97. }
  98. _FORCE_INLINE_ Vector() {}
  99. _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
  100. inline Vector &operator=(const Vector &p_from) {
  101. _cowdata._ref(p_from._cowdata);
  102. return *this;
  103. }
  104. _FORCE_INLINE_ ~Vector() {}
  105. };
  106. template <class T>
  107. void Vector<T>::invert() {
  108. for (int i = 0; i < size() / 2; i++) {
  109. T *p = ptrw();
  110. SWAP(p[i], p[size() - i - 1]);
  111. }
  112. }
  113. template <class T>
  114. void Vector<T>::append_array(Vector<T> p_other) {
  115. const int ds = p_other.size();
  116. if (ds == 0)
  117. return;
  118. const int bs = size();
  119. resize(bs + ds);
  120. for (int i = 0; i < ds; ++i)
  121. ptrw()[bs + i] = p_other[i];
  122. }
  123. template <class T>
  124. bool Vector<T>::push_back(T p_elem) {
  125. Error err = resize(size() + 1);
  126. ERR_FAIL_COND_V(err, true);
  127. set(size() - 1, p_elem);
  128. return false;
  129. }
  130. #endif