array.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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) const {
  110. return _p->array.find(p_value);
  111. }
  112. void Array::remove(int p_pos) {
  113. _p->array.remove(p_pos);
  114. }
  115. void Array::set(int p_idx,const Variant& p_value) {
  116. operator[](p_idx)=p_value;
  117. }
  118. const Variant& Array::get(int p_idx) const {
  119. return operator[](p_idx);
  120. }
  121. struct _ArrayVariantSort {
  122. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  123. bool valid=false;
  124. Variant res;
  125. Variant::evaluate(Variant::OP_LESS,p_l,p_r,res,valid);
  126. if (!valid)
  127. res=false;
  128. return res;
  129. }
  130. };
  131. void Array::sort() {
  132. _p->array.sort_custom<_ArrayVariantSort>();
  133. }
  134. struct _ArrayVariantSortCustom {
  135. Object *obj;
  136. StringName func;
  137. _FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
  138. const Variant*args[2]={&p_l,&p_r};
  139. Variant::CallError err;
  140. bool res = obj->call(func,args,2,err);
  141. if (err.error!=Variant::CallError::CALL_OK)
  142. res=false;
  143. return res;
  144. }
  145. };
  146. void Array::sort_custom(Object *p_obj,const StringName& p_function){
  147. ERR_FAIL_NULL(p_obj);
  148. SortArray<Variant,_ArrayVariantSortCustom> avs;
  149. avs.compare.obj=p_obj;
  150. avs.compare.func=p_function;
  151. avs.sort(_p->array.ptr(),_p->array.size());
  152. }
  153. void Array::invert(){
  154. _p->array.invert();
  155. }
  156. Array::Array(const Array& p_from) {
  157. _p=NULL;
  158. _ref(p_from);
  159. }
  160. Array::Array(bool p_shared) {
  161. _p = memnew( ArrayPrivate );
  162. _p->refcount.init();
  163. _p->shared=p_shared;
  164. }
  165. Array::~Array() {
  166. _unref();
  167. }