array.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. };
  39. void Array::_ref(const Array &p_from) const {
  40. ArrayPrivate *_fp = p_from._p;
  41. ERR_FAIL_COND(!_fp); // should NOT happen.
  42. if (_fp == _p)
  43. return; // whatever it is, nothing to do here move along
  44. bool success = _fp->refcount.ref();
  45. ERR_FAIL_COND(!success); // should really not happen either
  46. _unref();
  47. _p = p_from._p;
  48. }
  49. void Array::_unref() const {
  50. if (!_p)
  51. return;
  52. if (_p->refcount.unref()) {
  53. memdelete(_p);
  54. }
  55. _p = NULL;
  56. }
  57. Variant &Array::operator[](int p_idx) {
  58. return _p->array[p_idx];
  59. }
  60. const Variant &Array::operator[](int p_idx) const {
  61. return _p->array[p_idx];
  62. }
  63. int Array::size() const {
  64. return _p->array.size();
  65. }
  66. bool Array::empty() const {
  67. return _p->array.empty();
  68. }
  69. void Array::clear() {
  70. _p->array.clear();
  71. }
  72. bool Array::operator==(const Array &p_array) const {
  73. return _p == p_array._p;
  74. }
  75. uint32_t Array::hash() const {
  76. uint32_t h = hash_djb2_one_32(0);
  77. for (int i = 0; i < _p->array.size(); i++) {
  78. h = hash_djb2_one_32(_p->array[i].hash(), h);
  79. }
  80. return h;
  81. }
  82. void Array::operator=(const Array &p_array) {
  83. _ref(p_array);
  84. }
  85. void Array::push_back(const Variant &p_value) {
  86. _p->array.push_back(p_value);
  87. }
  88. Error Array::resize(int p_new_size) {
  89. return _p->array.resize(p_new_size);
  90. }
  91. void Array::insert(int p_pos, const Variant &p_value) {
  92. _p->array.insert(p_pos, p_value);
  93. }
  94. void Array::erase(const Variant &p_value) {
  95. _p->array.erase(p_value);
  96. }
  97. Variant Array::front() const {
  98. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  99. return operator[](0);
  100. }
  101. Variant Array::back() const {
  102. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  103. return operator[](_p->array.size() - 1);
  104. }
  105. int Array::find(const Variant &p_value, int p_from) const {
  106. return _p->array.find(p_value, p_from);
  107. }
  108. int Array::rfind(const Variant &p_value, int p_from) const {
  109. if (_p->array.size() == 0)
  110. return -1;
  111. if (p_from < 0) {
  112. // Relative offset from the end
  113. p_from = _p->array.size() + p_from;
  114. }
  115. if (p_from < 0 || p_from >= _p->array.size()) {
  116. // Limit to array boundaries
  117. p_from = _p->array.size() - 1;
  118. }
  119. for (int i = p_from; i >= 0; i--) {
  120. if (_p->array[i] == p_value) {
  121. return i;
  122. };
  123. };
  124. return -1;
  125. }
  126. int Array::find_last(const Variant &p_value) const {
  127. return rfind(p_value);
  128. }
  129. int Array::count(const Variant &p_value) const {
  130. if (_p->array.size() == 0)
  131. return 0;
  132. int amount = 0;
  133. for (int i = 0; i < _p->array.size(); i++) {
  134. if (_p->array[i] == p_value) {
  135. amount++;
  136. };
  137. };
  138. return amount;
  139. }
  140. bool Array::has(const Variant &p_value) const {
  141. return _p->array.find(p_value, 0) != -1;
  142. }
  143. void Array::remove(int p_pos) {
  144. _p->array.remove(p_pos);
  145. }
  146. void Array::set(int p_idx, const Variant &p_value) {
  147. operator[](p_idx) = p_value;
  148. }
  149. const Variant &Array::get(int p_idx) const {
  150. return operator[](p_idx);
  151. }
  152. Array Array::duplicate() const {
  153. Array new_arr;
  154. int element_count = size();
  155. new_arr.resize(element_count);
  156. for (int i = 0; i < element_count; i++) {
  157. new_arr[i] = get(i);
  158. }
  159. return new_arr;
  160. }
  161. struct _ArrayVariantSort {
  162. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  163. bool valid = false;
  164. Variant res;
  165. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  166. if (!valid)
  167. res = false;
  168. return res;
  169. }
  170. };
  171. Array &Array::sort() {
  172. _p->array.sort_custom<_ArrayVariantSort>();
  173. return *this;
  174. }
  175. struct _ArrayVariantSortCustom {
  176. Object *obj;
  177. StringName func;
  178. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  179. const Variant *args[2] = { &p_l, &p_r };
  180. Variant::CallError err;
  181. bool res = obj->call(func, args, 2, err);
  182. if (err.error != Variant::CallError::CALL_OK)
  183. res = false;
  184. return res;
  185. }
  186. };
  187. Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
  188. ERR_FAIL_NULL_V(p_obj, *this);
  189. SortArray<Variant, _ArrayVariantSortCustom> avs;
  190. avs.compare.obj = p_obj;
  191. avs.compare.func = p_function;
  192. avs.sort(_p->array.ptr(), _p->array.size());
  193. return *this;
  194. }
  195. template <typename Less>
  196. _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
  197. int lo = 0;
  198. int hi = p_array.size();
  199. if (p_before) {
  200. while (lo < hi) {
  201. const int mid = (lo + hi) / 2;
  202. if (p_less(p_array.get(mid), p_value)) {
  203. lo = mid + 1;
  204. } else {
  205. hi = mid;
  206. }
  207. }
  208. } else {
  209. while (lo < hi) {
  210. const int mid = (lo + hi) / 2;
  211. if (p_less(p_value, p_array.get(mid))) {
  212. hi = mid;
  213. } else {
  214. lo = mid + 1;
  215. }
  216. }
  217. }
  218. return lo;
  219. }
  220. int Array::bsearch(const Variant &p_value, bool p_before) {
  221. return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
  222. }
  223. int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
  224. ERR_FAIL_NULL_V(p_obj, 0);
  225. _ArrayVariantSortCustom less;
  226. less.obj = p_obj;
  227. less.func = p_function;
  228. return bisect(_p->array, p_value, p_before, less);
  229. }
  230. Array &Array::invert() {
  231. _p->array.invert();
  232. return *this;
  233. }
  234. void Array::push_front(const Variant &p_value) {
  235. _p->array.insert(0, p_value);
  236. }
  237. Variant Array::pop_back() {
  238. if (!_p->array.empty()) {
  239. int n = _p->array.size() - 1;
  240. Variant ret = _p->array.get(n);
  241. _p->array.resize(n);
  242. return ret;
  243. }
  244. return Variant();
  245. }
  246. Variant Array::pop_front() {
  247. if (!_p->array.empty()) {
  248. Variant ret = _p->array.get(0);
  249. _p->array.remove(0);
  250. return ret;
  251. }
  252. return Variant();
  253. }
  254. Array::Array(const Array &p_from) {
  255. _p = NULL;
  256. _ref(p_from);
  257. }
  258. Array::Array() {
  259. _p = memnew(ArrayPrivate);
  260. _p->refcount.init();
  261. }
  262. Array::~Array() {
  263. _unref();
  264. }