2
0

array.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*************************************************************************/
  2. /* array.cpp */
  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. #include "array.h"
  31. #include "hashfuncs.h"
  32. #include "object.h"
  33. #include "variant.h"
  34. #include "vector.h"
  35. struct ArrayPrivate {
  36. SafeRefCount refcount;
  37. Vector<Variant> array;
  38. bool shared;
  39. };
  40. void Array::_ref(const Array &p_from) const {
  41. ArrayPrivate *_fp = p_from._p;
  42. ERR_FAIL_COND(!_fp); // should NOT happen.
  43. if (_fp == _p)
  44. return; //wathever it is, nothing to do here move along
  45. bool success = _fp->refcount.ref();
  46. ERR_FAIL_COND(!success); //should really not happen either
  47. _unref();
  48. if (_fp->shared) {
  49. _p = p_from._p;
  50. } else {
  51. _p = memnew(ArrayPrivate);
  52. _p->shared = false;
  53. _p->refcount.init();
  54. _p->array = _fp->array;
  55. if (_fp->refcount.unref())
  56. memdelete(_fp);
  57. }
  58. }
  59. void Array::_unref() const {
  60. if (!_p)
  61. return;
  62. if (_p->refcount.unref()) {
  63. memdelete(_p);
  64. }
  65. _p = NULL;
  66. }
  67. Variant &Array::operator[](int p_idx) {
  68. return _p->array[p_idx];
  69. }
  70. const Variant &Array::operator[](int p_idx) const {
  71. return _p->array[p_idx];
  72. }
  73. int Array::size() const {
  74. return _p->array.size();
  75. }
  76. bool Array::empty() const {
  77. return _p->array.empty();
  78. }
  79. void Array::clear() {
  80. _p->array.clear();
  81. }
  82. bool Array::is_shared() const {
  83. return _p->shared;
  84. }
  85. bool Array::operator==(const Array &p_array) const {
  86. return _p == p_array._p;
  87. }
  88. uint32_t Array::hash() const {
  89. uint32_t h = hash_djb2_one_32(0);
  90. for (int i = 0; i < _p->array.size(); i++) {
  91. h = hash_djb2_one_32(_p->array[i].hash(), h);
  92. }
  93. return h;
  94. }
  95. void Array::operator=(const Array &p_array) {
  96. _ref(p_array);
  97. }
  98. void Array::push_back(const Variant &p_value) {
  99. _p->array.push_back(p_value);
  100. }
  101. Error Array::resize(int p_new_size) {
  102. return _p->array.resize(p_new_size);
  103. }
  104. void Array::insert(int p_pos, const Variant &p_value) {
  105. _p->array.insert(p_pos, p_value);
  106. }
  107. void Array::erase(const Variant &p_value) {
  108. _p->array.erase(p_value);
  109. }
  110. Variant Array::front() const {
  111. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  112. return operator[](0);
  113. }
  114. Variant Array::back() const {
  115. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  116. return operator[](_p->array.size() - 1);
  117. }
  118. int Array::find(const Variant &p_value, int p_from) const {
  119. return _p->array.find(p_value, p_from);
  120. }
  121. int Array::rfind(const Variant &p_value, int p_from) const {
  122. if (_p->array.size() == 0)
  123. return -1;
  124. if (p_from < 0) {
  125. // Relative offset from the end
  126. p_from = _p->array.size() + p_from;
  127. }
  128. if (p_from < 0 || p_from >= _p->array.size()) {
  129. // Limit to array boundaries
  130. p_from = _p->array.size() - 1;
  131. }
  132. for (int i = p_from; i >= 0; i--) {
  133. if (_p->array[i] == p_value) {
  134. return i;
  135. };
  136. };
  137. return -1;
  138. }
  139. int Array::find_last(const Variant &p_value) const {
  140. return rfind(p_value);
  141. }
  142. int Array::count(const Variant &p_value) const {
  143. if (_p->array.size() == 0)
  144. return 0;
  145. int amount = 0;
  146. for (int i = 0; i < _p->array.size(); i++) {
  147. if (_p->array[i] == p_value) {
  148. amount++;
  149. };
  150. };
  151. return amount;
  152. }
  153. bool Array::has(const Variant &p_value) const {
  154. return _p->array.find(p_value, 0) != -1;
  155. }
  156. void Array::remove(int p_pos) {
  157. _p->array.remove(p_pos);
  158. }
  159. void Array::set(int p_idx, const Variant &p_value) {
  160. operator[](p_idx) = p_value;
  161. }
  162. const Variant &Array::get(int p_idx) const {
  163. return operator[](p_idx);
  164. }
  165. struct _ArrayVariantSort {
  166. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  167. bool valid = false;
  168. Variant res;
  169. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  170. if (!valid)
  171. res = false;
  172. return res;
  173. }
  174. };
  175. void Array::sort() {
  176. _p->array.sort_custom<_ArrayVariantSort>();
  177. }
  178. struct _ArrayVariantSortCustom {
  179. Object *obj;
  180. StringName func;
  181. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  182. const Variant *args[2] = { &p_l, &p_r };
  183. Variant::CallError err;
  184. bool res = obj->call(func, args, 2, err);
  185. if (err.error != Variant::CallError::CALL_OK)
  186. res = false;
  187. return res;
  188. }
  189. };
  190. void Array::sort_custom(Object *p_obj, const StringName &p_function) {
  191. ERR_FAIL_NULL(p_obj);
  192. SortArray<Variant, _ArrayVariantSortCustom> avs;
  193. avs.compare.obj = p_obj;
  194. avs.compare.func = p_function;
  195. avs.sort(_p->array.ptr(), _p->array.size());
  196. }
  197. void Array::invert() {
  198. _p->array.invert();
  199. }
  200. void Array::push_front(const Variant &p_value) {
  201. _p->array.insert(0, p_value);
  202. }
  203. void Array::pop_back() {
  204. if (!_p->array.empty())
  205. _p->array.resize(_p->array.size() - 1);
  206. }
  207. void Array::pop_front() {
  208. if (!_p->array.empty())
  209. _p->array.remove(0);
  210. }
  211. Array::Array(const Array &p_from) {
  212. _p = NULL;
  213. _ref(p_from);
  214. }
  215. Array::Array(bool p_shared) {
  216. _p = memnew(ArrayPrivate);
  217. _p->refcount.init();
  218. _p->shared = p_shared;
  219. }
  220. Array::~Array() {
  221. _unref();
  222. }