array.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*************************************************************************/
  2. /* array.cpp */
  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. #include "array.h"
  31. #include "core/hashfuncs.h"
  32. #include "core/object.h"
  33. #include "core/variant.h"
  34. #include "core/vector.h"
  35. class ArrayPrivate {
  36. public:
  37. SafeRefCount refcount;
  38. Vector<Variant> array;
  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; // whatever 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. _p = p_from._p;
  49. }
  50. void Array::_unref() const {
  51. if (!_p)
  52. return;
  53. if (_p->refcount.unref()) {
  54. memdelete(_p);
  55. }
  56. _p = NULL;
  57. }
  58. Variant &Array::operator[](int p_idx) {
  59. return _p->array.write[p_idx];
  60. }
  61. const Variant &Array::operator[](int p_idx) const {
  62. return _p->array[p_idx];
  63. }
  64. int Array::size() const {
  65. return _p->array.size();
  66. }
  67. bool Array::empty() const {
  68. return _p->array.empty();
  69. }
  70. void Array::clear() {
  71. _p->array.clear();
  72. }
  73. bool Array::operator==(const Array &p_array) const {
  74. return _p == p_array._p;
  75. }
  76. uint32_t Array::hash() const {
  77. uint32_t h = hash_djb2_one_32(0);
  78. for (int i = 0; i < _p->array.size(); i++) {
  79. h = hash_djb2_one_32(_p->array[i].hash(), h);
  80. }
  81. return h;
  82. }
  83. void Array::operator=(const Array &p_array) {
  84. _ref(p_array);
  85. }
  86. void Array::push_back(const Variant &p_value) {
  87. _p->array.push_back(p_value);
  88. }
  89. Error Array::resize(int p_new_size) {
  90. return _p->array.resize(p_new_size);
  91. }
  92. void Array::insert(int p_pos, const Variant &p_value) {
  93. _p->array.insert(p_pos, p_value);
  94. }
  95. void Array::erase(const Variant &p_value) {
  96. _p->array.erase(p_value);
  97. }
  98. Variant Array::front() const {
  99. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  100. return operator[](0);
  101. }
  102. Variant Array::back() const {
  103. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  104. return operator[](_p->array.size() - 1);
  105. }
  106. int Array::find(const Variant &p_value, int p_from) const {
  107. return _p->array.find(p_value, p_from);
  108. }
  109. int Array::rfind(const Variant &p_value, int p_from) const {
  110. if (_p->array.size() == 0)
  111. return -1;
  112. if (p_from < 0) {
  113. // Relative offset from the end
  114. p_from = _p->array.size() + p_from;
  115. }
  116. if (p_from < 0 || p_from >= _p->array.size()) {
  117. // Limit to array boundaries
  118. p_from = _p->array.size() - 1;
  119. }
  120. for (int i = p_from; i >= 0; i--) {
  121. if (_p->array[i] == p_value) {
  122. return i;
  123. };
  124. };
  125. return -1;
  126. }
  127. int Array::find_last(const Variant &p_value) const {
  128. return rfind(p_value);
  129. }
  130. int Array::count(const Variant &p_value) const {
  131. if (_p->array.size() == 0)
  132. return 0;
  133. int amount = 0;
  134. for (int i = 0; i < _p->array.size(); i++) {
  135. if (_p->array[i] == p_value) {
  136. amount++;
  137. };
  138. };
  139. return amount;
  140. }
  141. bool Array::has(const Variant &p_value) const {
  142. return _p->array.find(p_value, 0) != -1;
  143. }
  144. void Array::remove(int p_pos) {
  145. _p->array.remove(p_pos);
  146. }
  147. void Array::set(int p_idx, const Variant &p_value) {
  148. operator[](p_idx) = p_value;
  149. }
  150. const Variant &Array::get(int p_idx) const {
  151. return operator[](p_idx);
  152. }
  153. Array Array::duplicate(bool p_deep) const {
  154. Array new_arr;
  155. int element_count = size();
  156. new_arr.resize(element_count);
  157. for (int i = 0; i < element_count; i++) {
  158. new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
  159. }
  160. return new_arr;
  161. }
  162. struct _ArrayVariantSort {
  163. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  164. bool valid = false;
  165. Variant res;
  166. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  167. if (!valid)
  168. res = false;
  169. return res;
  170. }
  171. };
  172. Array &Array::sort() {
  173. _p->array.sort_custom<_ArrayVariantSort>();
  174. return *this;
  175. }
  176. struct _ArrayVariantSortCustom {
  177. Object *obj;
  178. StringName func;
  179. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  180. const Variant *args[2] = { &p_l, &p_r };
  181. Variant::CallError err;
  182. bool res = obj->call(func, args, 2, err);
  183. if (err.error != Variant::CallError::CALL_OK)
  184. res = false;
  185. return res;
  186. }
  187. };
  188. Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
  189. ERR_FAIL_NULL_V(p_obj, *this);
  190. SortArray<Variant, _ArrayVariantSortCustom, true> avs;
  191. avs.compare.obj = p_obj;
  192. avs.compare.func = p_function;
  193. avs.sort(_p->array.ptrw(), _p->array.size());
  194. return *this;
  195. }
  196. void Array::shuffle() {
  197. const int n = _p->array.size();
  198. if (n < 2)
  199. return;
  200. Variant *data = _p->array.ptrw();
  201. for (int i = n - 1; i >= 1; i--) {
  202. const int j = Math::rand() % (i + 1);
  203. const Variant tmp = data[j];
  204. data[j] = data[i];
  205. data[i] = tmp;
  206. }
  207. }
  208. template <typename Less>
  209. _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
  210. int lo = 0;
  211. int hi = p_array.size();
  212. if (p_before) {
  213. while (lo < hi) {
  214. const int mid = (lo + hi) / 2;
  215. if (p_less(p_array.get(mid), p_value)) {
  216. lo = mid + 1;
  217. } else {
  218. hi = mid;
  219. }
  220. }
  221. } else {
  222. while (lo < hi) {
  223. const int mid = (lo + hi) / 2;
  224. if (p_less(p_value, p_array.get(mid))) {
  225. hi = mid;
  226. } else {
  227. lo = mid + 1;
  228. }
  229. }
  230. }
  231. return lo;
  232. }
  233. int Array::bsearch(const Variant &p_value, bool p_before) {
  234. return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
  235. }
  236. int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
  237. ERR_FAIL_NULL_V(p_obj, 0);
  238. _ArrayVariantSortCustom less;
  239. less.obj = p_obj;
  240. less.func = p_function;
  241. return bisect(_p->array, p_value, p_before, less);
  242. }
  243. Array &Array::invert() {
  244. _p->array.invert();
  245. return *this;
  246. }
  247. void Array::push_front(const Variant &p_value) {
  248. _p->array.insert(0, p_value);
  249. }
  250. Variant Array::pop_back() {
  251. if (!_p->array.empty()) {
  252. int n = _p->array.size() - 1;
  253. Variant ret = _p->array.get(n);
  254. _p->array.resize(n);
  255. return ret;
  256. }
  257. return Variant();
  258. }
  259. Variant Array::pop_front() {
  260. if (!_p->array.empty()) {
  261. Variant ret = _p->array.get(0);
  262. _p->array.remove(0);
  263. return ret;
  264. }
  265. return Variant();
  266. }
  267. Variant Array::min() const {
  268. Variant minval;
  269. for (int i = 0; i < size(); i++) {
  270. if (i == 0) {
  271. minval = get(i);
  272. } else {
  273. bool valid;
  274. Variant ret;
  275. Variant test = get(i);
  276. Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
  277. if (!valid) {
  278. return Variant(); //not a valid comparison
  279. }
  280. if (bool(ret)) {
  281. //is less
  282. minval = test;
  283. }
  284. }
  285. }
  286. return minval;
  287. }
  288. Variant Array::max() const {
  289. Variant maxval;
  290. for (int i = 0; i < size(); i++) {
  291. if (i == 0) {
  292. maxval = get(i);
  293. } else {
  294. bool valid;
  295. Variant ret;
  296. Variant test = get(i);
  297. Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
  298. if (!valid) {
  299. return Variant(); //not a valid comparison
  300. }
  301. if (bool(ret)) {
  302. //is less
  303. maxval = test;
  304. }
  305. }
  306. }
  307. return maxval;
  308. }
  309. Array::Array(const Array &p_from) {
  310. _p = NULL;
  311. _ref(p_from);
  312. }
  313. Array::Array() {
  314. _p = memnew(ArrayPrivate);
  315. _p->refcount.init();
  316. }
  317. Array::~Array() {
  318. _unref();
  319. }