array.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. if (_p->array.size() == 0) {
  100. ERR_EXPLAIN("Can't take value from empty array");
  101. ERR_FAIL_V(Variant());
  102. }
  103. return operator[](0);
  104. }
  105. Variant Array::back() const {
  106. if (_p->array.size() == 0) {
  107. ERR_EXPLAIN("Can't take value from empty array");
  108. ERR_FAIL_V(Variant());
  109. }
  110. return operator[](_p->array.size() - 1);
  111. }
  112. int Array::find(const Variant &p_value, int p_from) const {
  113. return _p->array.find(p_value, p_from);
  114. }
  115. int Array::rfind(const Variant &p_value, int p_from) const {
  116. if (_p->array.size() == 0)
  117. return -1;
  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. int amount = 0;
  140. for (int i = 0; i < _p->array.size(); i++) {
  141. if (_p->array[i] == p_value) {
  142. amount++;
  143. }
  144. }
  145. return amount;
  146. }
  147. bool Array::has(const Variant &p_value) const {
  148. return _p->array.find(p_value, 0) != -1;
  149. }
  150. void Array::remove(int p_pos) {
  151. _p->array.remove(p_pos);
  152. }
  153. void Array::set(int p_idx, const Variant &p_value) {
  154. operator[](p_idx) = p_value;
  155. }
  156. const Variant &Array::get(int p_idx) const {
  157. return operator[](p_idx);
  158. }
  159. Array Array::duplicate(bool p_deep) const {
  160. Array new_arr;
  161. int element_count = size();
  162. new_arr.resize(element_count);
  163. for (int i = 0; i < element_count; i++) {
  164. new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
  165. }
  166. return new_arr;
  167. }
  168. struct _ArrayVariantSort {
  169. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  170. bool valid = false;
  171. Variant res;
  172. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  173. if (!valid)
  174. res = false;
  175. return res;
  176. }
  177. };
  178. Array &Array::sort() {
  179. _p->array.sort_custom<_ArrayVariantSort>();
  180. return *this;
  181. }
  182. struct _ArrayVariantSortCustom {
  183. Object *obj;
  184. StringName func;
  185. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  186. const Variant *args[2] = { &p_l, &p_r };
  187. Variant::CallError err;
  188. bool res = obj->call(func, args, 2, err);
  189. if (err.error != Variant::CallError::CALL_OK)
  190. res = false;
  191. return res;
  192. }
  193. };
  194. Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
  195. ERR_FAIL_NULL_V(p_obj, *this);
  196. SortArray<Variant, _ArrayVariantSortCustom, true> avs;
  197. avs.compare.obj = p_obj;
  198. avs.compare.func = p_function;
  199. avs.sort(_p->array.ptrw(), _p->array.size());
  200. return *this;
  201. }
  202. void Array::shuffle() {
  203. const int n = _p->array.size();
  204. if (n < 2)
  205. return;
  206. Variant *data = _p->array.ptrw();
  207. for (int i = n - 1; i >= 1; i--) {
  208. const int j = Math::rand() % (i + 1);
  209. const Variant tmp = data[j];
  210. data[j] = data[i];
  211. data[i] = tmp;
  212. }
  213. }
  214. template <typename Less>
  215. _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
  216. int lo = 0;
  217. int hi = p_array.size();
  218. if (p_before) {
  219. while (lo < hi) {
  220. const int mid = (lo + hi) / 2;
  221. if (p_less(p_array.get(mid), p_value)) {
  222. lo = mid + 1;
  223. } else {
  224. hi = mid;
  225. }
  226. }
  227. } else {
  228. while (lo < hi) {
  229. const int mid = (lo + hi) / 2;
  230. if (p_less(p_value, p_array.get(mid))) {
  231. hi = mid;
  232. } else {
  233. lo = mid + 1;
  234. }
  235. }
  236. }
  237. return lo;
  238. }
  239. int Array::bsearch(const Variant &p_value, bool p_before) {
  240. return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
  241. }
  242. int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
  243. ERR_FAIL_NULL_V(p_obj, 0);
  244. _ArrayVariantSortCustom less;
  245. less.obj = p_obj;
  246. less.func = p_function;
  247. return bisect(_p->array, p_value, p_before, less);
  248. }
  249. Array &Array::invert() {
  250. _p->array.invert();
  251. return *this;
  252. }
  253. void Array::push_front(const Variant &p_value) {
  254. _p->array.insert(0, p_value);
  255. }
  256. Variant Array::pop_back() {
  257. if (!_p->array.empty()) {
  258. int n = _p->array.size() - 1;
  259. Variant ret = _p->array.get(n);
  260. _p->array.resize(n);
  261. return ret;
  262. }
  263. return Variant();
  264. }
  265. Variant Array::pop_front() {
  266. if (!_p->array.empty()) {
  267. Variant ret = _p->array.get(0);
  268. _p->array.remove(0);
  269. return ret;
  270. }
  271. return Variant();
  272. }
  273. Variant Array::min() const {
  274. Variant minval;
  275. for (int i = 0; i < size(); i++) {
  276. if (i == 0) {
  277. minval = get(i);
  278. } else {
  279. bool valid;
  280. Variant ret;
  281. Variant test = get(i);
  282. Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
  283. if (!valid) {
  284. return Variant(); //not a valid comparison
  285. }
  286. if (bool(ret)) {
  287. //is less
  288. minval = test;
  289. }
  290. }
  291. }
  292. return minval;
  293. }
  294. Variant Array::max() const {
  295. Variant maxval;
  296. for (int i = 0; i < size(); i++) {
  297. if (i == 0) {
  298. maxval = get(i);
  299. } else {
  300. bool valid;
  301. Variant ret;
  302. Variant test = get(i);
  303. Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
  304. if (!valid) {
  305. return Variant(); //not a valid comparison
  306. }
  307. if (bool(ret)) {
  308. //is less
  309. maxval = test;
  310. }
  311. }
  312. }
  313. return maxval;
  314. }
  315. const void *Array::id() const {
  316. return _p->array.ptr();
  317. }
  318. Array::Array(const Array &p_from) {
  319. _p = NULL;
  320. _ref(p_from);
  321. }
  322. Array::Array() {
  323. _p = memnew(ArrayPrivate);
  324. _p->refcount.init();
  325. }
  326. Array::~Array() {
  327. _unref();
  328. }