array.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "hashfuncs.h"
  32. #include "object.h"
  33. #include "variant.h"
  34. #include "vector.h"
  35. struct ArrayPrivate {
  36. SafeRefCount refcount;
  37. Vector<Variant> array;
  38. };
  39. void Array::_ref(const Array &p_from) const {
  40. ArrayPrivate *_fp = p_from._p;
  41. ERR_FAIL_COND(!_fp); // should NOT happen.
  42. if (_fp == _p)
  43. return; // whatever it is, nothing to do here move along
  44. bool success = _fp->refcount.ref();
  45. ERR_FAIL_COND(!success); // should really not happen either
  46. _unref();
  47. _p = p_from._p;
  48. }
  49. void Array::_unref() const {
  50. if (!_p)
  51. return;
  52. if (_p->refcount.unref()) {
  53. memdelete(_p);
  54. }
  55. _p = NULL;
  56. }
  57. Variant &Array::operator[](int p_idx) {
  58. return _p->array[p_idx];
  59. }
  60. const Variant &Array::operator[](int p_idx) const {
  61. return _p->array[p_idx];
  62. }
  63. int Array::size() const {
  64. return _p->array.size();
  65. }
  66. bool Array::empty() const {
  67. return _p->array.empty();
  68. }
  69. void Array::clear() {
  70. _p->array.clear();
  71. }
  72. bool Array::operator==(const Array &p_array) const {
  73. return _p == p_array._p;
  74. }
  75. uint32_t Array::hash() const {
  76. uint32_t h = hash_djb2_one_32(0);
  77. for (int i = 0; i < _p->array.size(); i++) {
  78. h = hash_djb2_one_32(_p->array[i].hash(), h);
  79. }
  80. return h;
  81. }
  82. void Array::operator=(const Array &p_array) {
  83. _ref(p_array);
  84. }
  85. void Array::push_back(const Variant &p_value) {
  86. _p->array.push_back(p_value);
  87. }
  88. Error Array::resize(int p_new_size) {
  89. return _p->array.resize(p_new_size);
  90. }
  91. void Array::insert(int p_pos, const Variant &p_value) {
  92. _p->array.insert(p_pos, p_value);
  93. }
  94. void Array::erase(const Variant &p_value) {
  95. _p->array.erase(p_value);
  96. }
  97. Variant Array::front() const {
  98. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  99. return operator[](0);
  100. }
  101. Variant Array::back() const {
  102. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  103. return operator[](_p->array.size() - 1);
  104. }
  105. int Array::find(const Variant &p_value, int p_from) const {
  106. return _p->array.find(p_value, p_from);
  107. }
  108. int Array::rfind(const Variant &p_value, int p_from) const {
  109. if (_p->array.size() == 0)
  110. return -1;
  111. if (p_from < 0) {
  112. // Relative offset from the end
  113. p_from = _p->array.size() + p_from;
  114. }
  115. if (p_from < 0 || p_from >= _p->array.size()) {
  116. // Limit to array boundaries
  117. p_from = _p->array.size() - 1;
  118. }
  119. for (int i = p_from; i >= 0; i--) {
  120. if (_p->array[i] == p_value) {
  121. return i;
  122. };
  123. };
  124. return -1;
  125. }
  126. int Array::find_last(const Variant &p_value) const {
  127. return rfind(p_value);
  128. }
  129. int Array::count(const Variant &p_value) const {
  130. if (_p->array.size() == 0)
  131. return 0;
  132. int amount = 0;
  133. for (int i = 0; i < _p->array.size(); i++) {
  134. if (_p->array[i] == p_value) {
  135. amount++;
  136. };
  137. };
  138. return amount;
  139. }
  140. bool Array::has(const Variant &p_value) const {
  141. return _p->array.find(p_value, 0) != -1;
  142. }
  143. void Array::remove(int p_pos) {
  144. _p->array.remove(p_pos);
  145. }
  146. void Array::set(int p_idx, const Variant &p_value) {
  147. operator[](p_idx) = p_value;
  148. }
  149. const Variant &Array::get(int p_idx) const {
  150. return operator[](p_idx);
  151. }
  152. Array Array::duplicate() const {
  153. Array new_arr;
  154. int element_count = size();
  155. new_arr.resize(element_count);
  156. for (int i = 0; i < element_count; i++) {
  157. new_arr[i] = get(i);
  158. }
  159. return new_arr;
  160. }
  161. struct _ArrayVariantSort {
  162. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  163. bool valid = false;
  164. Variant res;
  165. Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
  166. if (!valid)
  167. res = false;
  168. return res;
  169. }
  170. };
  171. void Array::sort() {
  172. _p->array.sort_custom<_ArrayVariantSort>();
  173. }
  174. struct _ArrayVariantSortCustom {
  175. Object *obj;
  176. StringName func;
  177. _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
  178. const Variant *args[2] = { &p_l, &p_r };
  179. Variant::CallError err;
  180. bool res = obj->call(func, args, 2, err);
  181. if (err.error != Variant::CallError::CALL_OK)
  182. res = false;
  183. return res;
  184. }
  185. };
  186. void Array::sort_custom(Object *p_obj, const StringName &p_function) {
  187. ERR_FAIL_NULL(p_obj);
  188. SortArray<Variant, _ArrayVariantSortCustom> avs;
  189. avs.compare.obj = p_obj;
  190. avs.compare.func = p_function;
  191. avs.sort(_p->array.ptr(), _p->array.size());
  192. }
  193. void Array::invert() {
  194. _p->array.invert();
  195. }
  196. void Array::push_front(const Variant &p_value) {
  197. _p->array.insert(0, p_value);
  198. }
  199. Variant Array::pop_back() {
  200. if (!_p->array.empty()) {
  201. int n = _p->array.size() - 1;
  202. Variant ret = _p->array.get(n);
  203. _p->array.resize(n);
  204. return ret;
  205. }
  206. return Variant();
  207. }
  208. Variant Array::pop_front() {
  209. if (!_p->array.empty()) {
  210. Variant ret = _p->array.get(0);
  211. _p->array.remove(0);
  212. return ret;
  213. }
  214. return Variant();
  215. }
  216. Array::Array(const Array &p_from) {
  217. _p = NULL;
  218. _ref(p_from);
  219. }
  220. Array::Array() {
  221. _p = memnew(ArrayPrivate);
  222. _p->refcount.init();
  223. }
  224. Array::~Array() {
  225. _unref();
  226. }