array.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "array.h"
  30. #include "vector.h"
  31. #include "hashfuncs.h"
  32. #include "variant.h"
  33. #include "object.h"
  34. struct ArrayPrivate {
  35. SafeRefCount refcount;
  36. Vector<Variant> array;
  37. };
  38. void Array::_ref(const Array& p_from) const {
  39. ArrayPrivate *_fp = p_from._p;
  40. ERR_FAIL_COND(!_fp); // should NOT happen.
  41. if (_fp == _p)
  42. return; //wathever it is, nothing to do here move along
  43. bool success = _fp->refcount.ref();
  44. ERR_FAIL_COND(!success); //should really not happen either
  45. _unref();
  46. _p = p_from._p;
  47. }
  48. void Array::_unref() const {
  49. if (!_p)
  50. return;
  51. if (_p->refcount.unref()) {
  52. memdelete(_p);
  53. }
  54. _p=NULL;
  55. }
  56. Variant& Array::operator[](int p_idx) {
  57. return _p->array[p_idx];
  58. }
  59. const Variant& Array::operator[](int p_idx) const {
  60. return _p->array[p_idx];
  61. }
  62. int Array::size() const {
  63. return _p->array.size();
  64. }
  65. bool Array::empty() const {
  66. return _p->array.empty();
  67. }
  68. void Array::clear() {
  69. _p->array.clear();
  70. }
  71. bool Array::operator==(const Array& p_array) const {
  72. return _p==p_array._p;
  73. }
  74. uint32_t Array::hash() const {
  75. uint32_t h=hash_djb2_one_32(0);
  76. for (int i=0;i<_p->array.size();i++) {
  77. h = hash_djb2_one_32( _p->array[i].hash(), h);
  78. }
  79. return h;
  80. }
  81. void Array::operator=(const Array& p_array) {
  82. _ref(p_array);
  83. }
  84. void Array::push_back(const Variant& p_value) {
  85. _p->array.push_back(p_value);
  86. }
  87. Error Array::resize(int p_new_size) {
  88. return _p->array.resize(p_new_size);
  89. }
  90. void Array::insert(int p_pos, const Variant& p_value) {
  91. _p->array.insert(p_pos,p_value);
  92. }
  93. void Array::erase(const Variant& p_value) {
  94. _p->array.erase(p_value);
  95. }
  96. Variant Array::front() const {
  97. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  98. return operator[](0);
  99. }
  100. Variant Array::back() const {
  101. ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
  102. return operator[](_p->array.size() - 1);
  103. }
  104. int Array::find(const Variant& p_value, int p_from) const {
  105. return _p->array.find(p_value, p_from);
  106. }
  107. int Array::rfind(const Variant& p_value, int p_from) const {
  108. if (_p->array.size() == 0)
  109. return -1;
  110. if (p_from < 0) {
  111. // Relative offset from the end
  112. p_from = _p->array.size() + p_from;
  113. }
  114. if (p_from < 0 || p_from >= _p->array.size()) {
  115. // Limit to array boundaries
  116. p_from = _p->array.size() - 1;
  117. }
  118. for (int i=p_from; i>=0; i--) {
  119. if(_p->array[i] == p_value){
  120. return i;
  121. };
  122. };
  123. return -1;
  124. }
  125. int Array::find_last(const Variant& p_value) const {
  126. return rfind(p_value);
  127. }
  128. int Array::count(const Variant& p_value) const {
  129. if(_p->array.size() == 0)
  130. return 0;
  131. int amount=0;
  132. for (int i=0; i<_p->array.size(); i++) {
  133. if(_p->array[i] == p_value){
  134. amount++;
  135. };
  136. };
  137. return amount;
  138. }
  139. bool Array::has(const Variant& p_value) const {
  140. return _p->array.find(p_value, 0) != -1;
  141. }
  142. void Array::remove(int p_pos) {
  143. _p->array.remove(p_pos);
  144. }
  145. void Array::set(int p_idx,const Variant& p_value) {
  146. operator[](p_idx)=p_value;
  147. }
  148. const Variant& Array::get(int p_idx) const {
  149. return operator[](p_idx);
  150. }
  151. struct _ArrayVariantSort {
  152. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  153. bool valid=false;
  154. Variant res;
  155. Variant::evaluate(Variant::OP_LESS,p_l,p_r,res,valid);
  156. if (!valid)
  157. res=false;
  158. return res;
  159. }
  160. };
  161. void Array::sort() {
  162. _p->array.sort_custom<_ArrayVariantSort>();
  163. }
  164. struct _ArrayVariantSortCustom {
  165. Object *obj;
  166. StringName func;
  167. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  168. const Variant*args[2]={&p_l,&p_r};
  169. Variant::CallError err;
  170. bool res = obj->call(func,args,2,err);
  171. if (err.error!=Variant::CallError::CALL_OK)
  172. res=false;
  173. return res;
  174. }
  175. };
  176. void Array::sort_custom(Object *p_obj,const StringName& p_function){
  177. ERR_FAIL_NULL(p_obj);
  178. SortArray<Variant,_ArrayVariantSortCustom> avs;
  179. avs.compare.obj=p_obj;
  180. avs.compare.func=p_function;
  181. avs.sort(_p->array.ptr(),_p->array.size());
  182. }
  183. void Array::invert(){
  184. _p->array.invert();
  185. }
  186. void Array::push_front(const Variant& p_value) {
  187. _p->array.insert(0,p_value);
  188. }
  189. Variant Array::pop_back(){
  190. if (!_p->array.empty()) {
  191. int n = _p->array.size() - 1;
  192. Variant ret = _p->array.get(n);
  193. _p->array.resize(n);
  194. return ret;
  195. }
  196. return Variant();
  197. }
  198. Variant Array::pop_front(){
  199. if (!_p->array.empty()) {
  200. Variant ret = _p->array.get(0);
  201. _p->array.remove(0);
  202. return ret;
  203. }
  204. return Variant();
  205. }
  206. Array::Array(const Array& p_from) {
  207. _p=NULL;
  208. _ref(p_from);
  209. }
  210. Array::Array() {
  211. _p = memnew( ArrayPrivate );
  212. _p->refcount.init();
  213. }
  214. Array::~Array() {
  215. _unref();
  216. }