array.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. int Array::find(const Variant& p_value, int p_from) const {
  110. return _p->array.find(p_value, p_from);
  111. }
  112. int Array::rfind(const Variant& p_value, int p_from) const {
  113. if (_p->array.size() == 0)
  114. return -1;
  115. if (p_from < 0) {
  116. // Relative offset from the end
  117. p_from = _p->array.size() + p_from;
  118. }
  119. if (p_from < 0 || p_from >= _p->array.size()) {
  120. // Limit to array boundaries
  121. p_from = _p->array.size() - 1;
  122. }
  123. for (int i=p_from; i>=0; i--) {
  124. if(_p->array[i] == p_value){
  125. return i;
  126. };
  127. };
  128. return -1;
  129. }
  130. int Array::find_last(const Variant& p_value) const {
  131. return rfind(p_value);
  132. }
  133. int Array::count(const Variant& p_value) const {
  134. if(_p->array.size() == 0)
  135. return 0;
  136. int amount=0;
  137. for (int i=0; i<_p->array.size(); i++) {
  138. if(_p->array[i] == p_value){
  139. amount++;
  140. };
  141. };
  142. return amount;
  143. }
  144. bool Array::has(const Variant& p_value) const {
  145. return _p->array.find(p_value, 0) != -1;
  146. }
  147. void Array::remove(int p_pos) {
  148. _p->array.remove(p_pos);
  149. }
  150. void Array::set(int p_idx,const Variant& p_value) {
  151. operator[](p_idx)=p_value;
  152. }
  153. const Variant& Array::get(int p_idx) const {
  154. return operator[](p_idx);
  155. }
  156. struct _ArrayVariantSort {
  157. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  158. bool valid=false;
  159. Variant res;
  160. Variant::evaluate(Variant::OP_LESS,p_l,p_r,res,valid);
  161. if (!valid)
  162. res=false;
  163. return res;
  164. }
  165. };
  166. void Array::sort() {
  167. _p->array.sort_custom<_ArrayVariantSort>();
  168. }
  169. struct _ArrayVariantSortCustom {
  170. Object *obj;
  171. StringName func;
  172. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  173. const Variant*args[2]={&p_l,&p_r};
  174. Variant::CallError err;
  175. bool res = obj->call(func,args,2,err);
  176. if (err.error!=Variant::CallError::CALL_OK)
  177. res=false;
  178. return res;
  179. }
  180. };
  181. void Array::sort_custom(Object *p_obj,const StringName& p_function){
  182. ERR_FAIL_NULL(p_obj);
  183. SortArray<Variant,_ArrayVariantSortCustom> avs;
  184. avs.compare.obj=p_obj;
  185. avs.compare.func=p_function;
  186. avs.sort(_p->array.ptr(),_p->array.size());
  187. }
  188. void Array::invert(){
  189. _p->array.invert();
  190. }
  191. void Array::push_front(const Variant& p_value) {
  192. _p->array.insert(0,p_value);
  193. }
  194. void Array::pop_back(){
  195. if (!_p->array.empty())
  196. _p->array.resize( _p->array.size() -1 );
  197. }
  198. void Array::pop_front(){
  199. if (!_p->array.empty())
  200. _p->array.remove(0);
  201. }
  202. Array::Array(const Array& p_from) {
  203. _p=NULL;
  204. _ref(p_from);
  205. }
  206. Array::Array(bool p_shared) {
  207. _p = memnew( ArrayPrivate );
  208. _p->refcount.init();
  209. _p->shared=p_shared;
  210. }
  211. Array::~Array() {
  212. _unref();
  213. }