array.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
  100. return operator[](0);
  101. }
  102. Variant Array::back() const {
  103. ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
  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. int Array::_fix_slice_index(int p_index, int p_arr_len, int p_top_mod) {
  163. p_index = CLAMP(p_index, -p_arr_len, p_arr_len + p_top_mod);
  164. if (p_index < 0) {
  165. p_index = (p_index % p_arr_len + p_arr_len) % p_arr_len; // positive modulo
  166. }
  167. return p_index;
  168. }
  169. int Array::_clamp_index(int p_index) const {
  170. return CLAMP(p_index, -size() + 1, size() - 1);
  171. }
  172. #define ARRAY_GET_DEEP(idx, is_deep) is_deep ? get(idx).duplicate(is_deep) : get(idx)
  173. Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound
  174. Array new_arr;
  175. if (empty()) // Don't try to slice empty arrays.
  176. return new_arr;
  177. p_begin = Array::_fix_slice_index(p_begin, size(), -1); // can't start out of range
  178. p_end = Array::_fix_slice_index(p_end, size(), 0);
  179. int x = p_begin;
  180. int new_arr_i = 0;
  181. ERR_FAIL_COND_V(p_step == 0, new_arr);
  182. if (Array::_clamp_index(p_begin) == Array::_clamp_index(p_end)) { // don't include element twice
  183. new_arr.resize(1);
  184. // new_arr[0] = 1;
  185. new_arr[0] = ARRAY_GET_DEEP(Array::_clamp_index(p_begin), p_deep);
  186. return new_arr;
  187. } else {
  188. int element_count = ceil((int)MAX(0, (p_end - p_begin) / p_step)) + 1;
  189. if (element_count == 1) { // delta going in wrong direction to reach end
  190. new_arr.resize(0);
  191. return new_arr;
  192. }
  193. new_arr.resize(element_count);
  194. }
  195. // if going backwards, have to have a different terminating condition
  196. if (p_step < 0) {
  197. while (x >= p_end) {
  198. new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep);
  199. x += p_step;
  200. new_arr_i += 1;
  201. }
  202. } else if (p_step > 0) {
  203. while (x <= p_end) {
  204. new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep);
  205. x += p_step;
  206. new_arr_i += 1;
  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. return res;
  219. }
  220. };
  221. Array &Array::sort() {
  222. _p->array.sort_custom<_ArrayVariantSort>();
  223. return *this;
  224. }
  225. struct _ArrayVariantSortCustom {
  226. Object *obj;
  227. StringName func;
  228. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  229. const Variant *args[2] = { &p_l, &p_r };
  230. Variant::CallError err;
  231. bool res = obj->call(func, args, 2, err);
  232. if (err.error != Variant::CallError::CALL_OK)
  233. res = false;
  234. return res;
  235. }
  236. };
  237. Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
  238. ERR_FAIL_NULL_V(p_obj, *this);
  239. SortArray<Variant, _ArrayVariantSortCustom, true> avs;
  240. avs.compare.obj = p_obj;
  241. avs.compare.func = p_function;
  242. avs.sort(_p->array.ptrw(), _p->array.size());
  243. return *this;
  244. }
  245. void Array::shuffle() {
  246. const int n = _p->array.size();
  247. if (n < 2)
  248. return;
  249. Variant *data = _p->array.ptrw();
  250. for (int i = n - 1; i >= 1; i--) {
  251. const int j = Math::rand() % (i + 1);
  252. const Variant tmp = data[j];
  253. data[j] = data[i];
  254. data[i] = tmp;
  255. }
  256. }
  257. template <typename Less>
  258. _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
  259. int lo = 0;
  260. int hi = p_array.size();
  261. if (p_before) {
  262. while (lo < hi) {
  263. const int mid = (lo + hi) / 2;
  264. if (p_less(p_array.get(mid), p_value)) {
  265. lo = mid + 1;
  266. } else {
  267. hi = mid;
  268. }
  269. }
  270. } else {
  271. while (lo < hi) {
  272. const int mid = (lo + hi) / 2;
  273. if (p_less(p_value, p_array.get(mid))) {
  274. hi = mid;
  275. } else {
  276. lo = mid + 1;
  277. }
  278. }
  279. }
  280. return lo;
  281. }
  282. int Array::bsearch(const Variant &p_value, bool p_before) {
  283. return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
  284. }
  285. int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
  286. ERR_FAIL_NULL_V(p_obj, 0);
  287. _ArrayVariantSortCustom less;
  288. less.obj = p_obj;
  289. less.func = p_function;
  290. return bisect(_p->array, p_value, p_before, less);
  291. }
  292. Array &Array::invert() {
  293. _p->array.invert();
  294. return *this;
  295. }
  296. void Array::push_front(const Variant &p_value) {
  297. _p->array.insert(0, p_value);
  298. }
  299. Variant Array::pop_back() {
  300. if (!_p->array.empty()) {
  301. int n = _p->array.size() - 1;
  302. Variant ret = _p->array.get(n);
  303. _p->array.resize(n);
  304. return ret;
  305. }
  306. return Variant();
  307. }
  308. Variant Array::pop_front() {
  309. if (!_p->array.empty()) {
  310. Variant ret = _p->array.get(0);
  311. _p->array.remove(0);
  312. return ret;
  313. }
  314. return Variant();
  315. }
  316. Variant Array::min() const {
  317. Variant minval;
  318. for (int i = 0; i < size(); i++) {
  319. if (i == 0) {
  320. minval = get(i);
  321. } else {
  322. bool valid;
  323. Variant ret;
  324. Variant test = get(i);
  325. Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
  326. if (!valid) {
  327. return Variant(); //not a valid comparison
  328. }
  329. if (bool(ret)) {
  330. //is less
  331. minval = test;
  332. }
  333. }
  334. }
  335. return minval;
  336. }
  337. Variant Array::max() const {
  338. Variant maxval;
  339. for (int i = 0; i < size(); i++) {
  340. if (i == 0) {
  341. maxval = get(i);
  342. } else {
  343. bool valid;
  344. Variant ret;
  345. Variant test = get(i);
  346. Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
  347. if (!valid) {
  348. return Variant(); //not a valid comparison
  349. }
  350. if (bool(ret)) {
  351. //is less
  352. maxval = test;
  353. }
  354. }
  355. }
  356. return maxval;
  357. }
  358. const void *Array::id() const {
  359. return _p->array.ptr();
  360. }
  361. Array::Array(const Array &p_from) {
  362. _p = NULL;
  363. _ref(p_from);
  364. }
  365. Array::Array() {
  366. _p = memnew(ArrayPrivate);
  367. _p->refcount.init();
  368. }
  369. Array::~Array() {
  370. _unref();
  371. }