array.cpp 10 KB

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