array.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. }
  46. bool success = _fp->refcount.ref();
  47. ERR_FAIL_COND(!success); // should really not happen either
  48. _unref();
  49. _p = p_from._p;
  50. }
  51. void Array::_unref() const {
  52. if (!_p) {
  53. return;
  54. }
  55. if (_p->refcount.unref()) {
  56. memdelete(_p);
  57. }
  58. _p = nullptr;
  59. }
  60. Variant &Array::operator[](int p_idx) {
  61. return _p->array.write[p_idx];
  62. }
  63. const Variant &Array::operator[](int p_idx) const {
  64. return _p->array[p_idx];
  65. }
  66. int Array::size() const {
  67. return _p->array.size();
  68. }
  69. bool Array::empty() const {
  70. return _p->array.empty();
  71. }
  72. void Array::clear() {
  73. _p->array.clear();
  74. }
  75. bool Array::operator==(const Array &p_array) const {
  76. return _p == p_array._p;
  77. }
  78. uint32_t Array::hash() const {
  79. uint32_t h = hash_djb2_one_32(0);
  80. for (int i = 0; i < _p->array.size(); i++) {
  81. h = hash_djb2_one_32(_p->array[i].hash(), h);
  82. }
  83. return h;
  84. }
  85. void Array::operator=(const Array &p_array) {
  86. _ref(p_array);
  87. }
  88. void Array::push_back(const Variant &p_value) {
  89. _p->array.push_back(p_value);
  90. }
  91. void Array::append_array(const Array &p_array) {
  92. _p->array.append_array(p_array._p->array);
  93. }
  94. Error Array::resize(int p_new_size) {
  95. return _p->array.resize(p_new_size);
  96. }
  97. void Array::insert(int p_pos, const Variant &p_value) {
  98. _p->array.insert(p_pos, p_value);
  99. }
  100. void Array::erase(const Variant &p_value) {
  101. _p->array.erase(p_value);
  102. }
  103. Variant Array::front() const {
  104. ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
  105. return operator[](0);
  106. }
  107. Variant Array::back() const {
  108. ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
  109. return operator[](_p->array.size() - 1);
  110. }
  111. int Array::find(const Variant &p_value, int p_from) const {
  112. return _p->array.find(p_value, p_from);
  113. }
  114. int Array::rfind(const Variant &p_value, int p_from) const {
  115. if (_p->array.size() == 0) {
  116. return -1;
  117. }
  118. if (p_from < 0) {
  119. // Relative offset from the end
  120. p_from = _p->array.size() + p_from;
  121. }
  122. if (p_from < 0 || p_from >= _p->array.size()) {
  123. // Limit to array boundaries
  124. p_from = _p->array.size() - 1;
  125. }
  126. for (int i = p_from; i >= 0; i--) {
  127. if (_p->array[i] == p_value) {
  128. return i;
  129. }
  130. }
  131. return -1;
  132. }
  133. int Array::find_last(const Variant &p_value) const {
  134. return rfind(p_value);
  135. }
  136. int Array::count(const Variant &p_value) const {
  137. if (_p->array.size() == 0) {
  138. return 0;
  139. }
  140. int amount = 0;
  141. for (int i = 0; i < _p->array.size(); i++) {
  142. if (_p->array[i] == p_value) {
  143. amount++;
  144. }
  145. }
  146. return amount;
  147. }
  148. bool Array::has(const Variant &p_value) const {
  149. return _p->array.find(p_value, 0) != -1;
  150. }
  151. void Array::remove(int p_pos) {
  152. _p->array.remove(p_pos);
  153. }
  154. void Array::set(int p_idx, const Variant &p_value) {
  155. operator[](p_idx) = p_value;
  156. }
  157. const Variant &Array::get(int p_idx) const {
  158. return operator[](p_idx);
  159. }
  160. Array Array::duplicate(bool p_deep) const {
  161. Array new_arr;
  162. int element_count = size();
  163. new_arr.resize(element_count);
  164. for (int i = 0; i < element_count; i++) {
  165. new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
  166. }
  167. return new_arr;
  168. }
  169. int Array::_clamp_slice_index(int p_index) const {
  170. int arr_size = size();
  171. int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1);
  172. if (fixed_index < 0) {
  173. fixed_index = arr_size + fixed_index;
  174. }
  175. return fixed_index;
  176. }
  177. Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound
  178. Array new_arr;
  179. ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero.");
  180. if (empty()) { // Don't try to slice empty arrays.
  181. return new_arr;
  182. }
  183. if (p_step > 0) {
  184. if (p_begin >= size() || p_end < -size()) {
  185. return new_arr;
  186. }
  187. } else { // p_step < 0
  188. if (p_begin < -size() || p_end >= size()) {
  189. return new_arr;
  190. }
  191. }
  192. int begin = _clamp_slice_index(p_begin);
  193. int end = _clamp_slice_index(p_end);
  194. int new_arr_size = MAX(((end - begin + p_step) / p_step), 0);
  195. new_arr.resize(new_arr_size);
  196. if (p_step > 0) {
  197. int dest_idx = 0;
  198. for (int idx = begin; idx <= end; idx += p_step) {
  199. ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
  200. new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
  201. }
  202. } else { // p_step < 0
  203. int dest_idx = 0;
  204. for (int idx = begin; idx >= end; idx += p_step) {
  205. ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
  206. new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
  207. }
  208. }
  209. return new_arr;
  210. }
  211. struct _ArrayVariantSort {
  212. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  213. bool valid = false;
  214. Variant res;
  215. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  216. if (!valid) {
  217. res = false;
  218. }
  219. return res;
  220. }
  221. };
  222. Array &Array::sort() {
  223. _p->array.sort_custom<_ArrayVariantSort>();
  224. return *this;
  225. }
  226. struct _ArrayVariantSortCustom {
  227. Object *obj;
  228. StringName func;
  229. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  230. const Variant *args[2] = { &p_l, &p_r };
  231. Variant::CallError err;
  232. bool res = obj->call(func, args, 2, err);
  233. if (err.error != Variant::CallError::CALL_OK) {
  234. res = false;
  235. }
  236. return res;
  237. }
  238. };
  239. Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
  240. ERR_FAIL_NULL_V(p_obj, *this);
  241. SortArray<Variant, _ArrayVariantSortCustom, true> avs;
  242. avs.compare.obj = p_obj;
  243. avs.compare.func = p_function;
  244. avs.sort(_p->array.ptrw(), _p->array.size());
  245. return *this;
  246. }
  247. void Array::shuffle() {
  248. const int n = _p->array.size();
  249. if (n < 2) {
  250. return;
  251. }
  252. Variant *data = _p->array.ptrw();
  253. for (int i = n - 1; i >= 1; i--) {
  254. const int j = Math::rand() % (i + 1);
  255. const Variant tmp = data[j];
  256. data[j] = data[i];
  257. data[i] = tmp;
  258. }
  259. }
  260. template <typename Less>
  261. _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
  262. int lo = 0;
  263. int hi = p_array.size();
  264. if (p_before) {
  265. while (lo < hi) {
  266. const int mid = (lo + hi) / 2;
  267. if (p_less(p_array.get(mid), p_value)) {
  268. lo = mid + 1;
  269. } else {
  270. hi = mid;
  271. }
  272. }
  273. } else {
  274. while (lo < hi) {
  275. const int mid = (lo + hi) / 2;
  276. if (p_less(p_value, p_array.get(mid))) {
  277. hi = mid;
  278. } else {
  279. lo = mid + 1;
  280. }
  281. }
  282. }
  283. return lo;
  284. }
  285. int Array::bsearch(const Variant &p_value, bool p_before) {
  286. return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
  287. }
  288. int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
  289. ERR_FAIL_NULL_V(p_obj, 0);
  290. _ArrayVariantSortCustom less;
  291. less.obj = p_obj;
  292. less.func = p_function;
  293. return bisect(_p->array, p_value, p_before, less);
  294. }
  295. Array &Array::invert() {
  296. _p->array.invert();
  297. return *this;
  298. }
  299. void Array::push_front(const Variant &p_value) {
  300. _p->array.insert(0, p_value);
  301. }
  302. Variant Array::pop_back() {
  303. if (!_p->array.empty()) {
  304. int n = _p->array.size() - 1;
  305. Variant ret = _p->array.get(n);
  306. _p->array.resize(n);
  307. return ret;
  308. }
  309. return Variant();
  310. }
  311. Variant Array::pop_front() {
  312. if (!_p->array.empty()) {
  313. Variant ret = _p->array.get(0);
  314. _p->array.remove(0);
  315. return ret;
  316. }
  317. return Variant();
  318. }
  319. Variant Array::min() const {
  320. Variant minval;
  321. for (int i = 0; i < size(); i++) {
  322. if (i == 0) {
  323. minval = get(i);
  324. } else {
  325. bool valid;
  326. Variant ret;
  327. Variant test = get(i);
  328. Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
  329. if (!valid) {
  330. return Variant(); //not a valid comparison
  331. }
  332. if (bool(ret)) {
  333. //is less
  334. minval = test;
  335. }
  336. }
  337. }
  338. return minval;
  339. }
  340. Variant Array::max() const {
  341. Variant maxval;
  342. for (int i = 0; i < size(); i++) {
  343. if (i == 0) {
  344. maxval = get(i);
  345. } else {
  346. bool valid;
  347. Variant ret;
  348. Variant test = get(i);
  349. Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
  350. if (!valid) {
  351. return Variant(); //not a valid comparison
  352. }
  353. if (bool(ret)) {
  354. //is less
  355. maxval = test;
  356. }
  357. }
  358. }
  359. return maxval;
  360. }
  361. const void *Array::id() const {
  362. return _p->array.ptr();
  363. }
  364. Array::Array(const Array &p_from) {
  365. _p = nullptr;
  366. _ref(p_from);
  367. }
  368. Array::Array() {
  369. _p = memnew(ArrayPrivate);
  370. _p->refcount.init();
  371. }
  372. Array::~Array() {
  373. _unref();
  374. }