array.cpp 6.6 KB

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