vector.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*************************************************************************/
  2. /* vector.h */
  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. #ifndef VECTOR_H
  30. #define VECTOR_H
  31. /**
  32. * @class Vector
  33. * @author Juan Linietsky
  34. * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use DVector for large arrays.
  35. */
  36. #include "os/memory.h"
  37. #include "error_macros.h"
  38. #include "safe_refcount.h"
  39. #include "sort.h"
  40. template<class T>
  41. class Vector {
  42. mutable void* _ptr;
  43. // internal helpers
  44. _FORCE_INLINE_ SafeRefCount* _get_refcount() const {
  45. if (!_ptr)
  46. return NULL;
  47. return reinterpret_cast<SafeRefCount*>(_ptr);
  48. }
  49. _FORCE_INLINE_ int* _get_size() const {
  50. if (!_ptr)
  51. return NULL;
  52. return reinterpret_cast<int*>(((uint8_t*)(_ptr))+sizeof(SafeRefCount));
  53. }
  54. _FORCE_INLINE_ T* _get_data() const {
  55. if (!_ptr)
  56. return NULL;
  57. return reinterpret_cast<T*>(((uint8_t*)(_ptr))+sizeof(SafeRefCount)+sizeof(int));
  58. }
  59. _FORCE_INLINE_ int _get_alloc_size(int p_elements) const {
  60. return nearest_power_of_2(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int));
  61. }
  62. void _unref(void *p_data);
  63. void _copy_from(const Vector& p_from);
  64. void _copy_on_write();
  65. public:
  66. _FORCE_INLINE_ T *ptr() { if (!_ptr) return NULL; _copy_on_write(); return (T*)_get_data(); }
  67. _FORCE_INLINE_ const T *ptr() const { if (!_ptr) return NULL; return _get_data(); }
  68. _FORCE_INLINE_ void clear() { resize(0); }
  69. _FORCE_INLINE_ int size() const {
  70. if (!_ptr)
  71. return 0;
  72. else
  73. return *reinterpret_cast<int*>(((uint8_t*)(_ptr))+sizeof(SafeRefCount));
  74. }
  75. _FORCE_INLINE_ bool empty() const { return _ptr == 0; }
  76. Error resize(int p_size);
  77. bool push_back(T p_elem);
  78. void remove(int p_index);
  79. void erase(const T& p_val) { int idx = find(p_val); if (idx>=0) remove(idx); };
  80. void invert();
  81. template <class T_val>
  82. int find(T_val& p_val) const;
  83. void set(int p_index,T p_elem);
  84. T get(int p_index) const;
  85. inline T& operator[](int p_index) {
  86. if (p_index<0 || p_index>=size()) {
  87. T& aux=*((T*)0); //nullreturn
  88. ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
  89. }
  90. _copy_on_write(); // wants to write, so copy on write.
  91. return _get_data()[p_index];
  92. }
  93. inline const T& operator[](int p_index) const {
  94. if (p_index<0 || p_index>=size()) {
  95. const T& aux=*((T*)0); //nullreturn
  96. ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
  97. }
  98. // no cow needed, since it's reading
  99. return _get_data()[p_index];
  100. }
  101. Error insert(int p_pos,const T& p_val);
  102. template<class C>
  103. void sort_custom() {
  104. int len = size();
  105. if (len==0)
  106. return;
  107. T *data = &operator[](0);
  108. SortArray<T,C> sorter;
  109. sorter.sort(data,len);
  110. }
  111. void sort() {
  112. sort_custom<_DefaultComparator<T> >();
  113. }
  114. void ordered_insert(const T& p_val) {
  115. int i;
  116. for (i=0; i<size(); i++) {
  117. if (p_val < operator[](i)) {
  118. break;
  119. };
  120. };
  121. insert(i, p_val);
  122. }
  123. void operator=(const Vector& p_from);
  124. Vector(const Vector& p_from);
  125. _FORCE_INLINE_ Vector();
  126. _FORCE_INLINE_ ~Vector();
  127. };
  128. template<class T>
  129. void Vector<T>::_unref(void *p_data) {
  130. if (!p_data)
  131. return;
  132. SafeRefCount *src = reinterpret_cast<SafeRefCount*>(p_data);
  133. if (!src->unref())
  134. return; // still in use
  135. // clean up
  136. int *count = (int*)(src+1);
  137. T *data = (T*)(count+1);
  138. for (int i=0;i<*count;i++) {
  139. // call destructors
  140. data[i].~T();
  141. }
  142. // free mem
  143. memfree(p_data);
  144. }
  145. template<class T>
  146. void Vector<T>::_copy_on_write() {
  147. if (!_ptr)
  148. return;
  149. if (_get_refcount()->get() > 1 ) {
  150. /* in use by more than me */
  151. SafeRefCount *src_new=(SafeRefCount *)memalloc(_get_alloc_size(*_get_size()));
  152. src_new->init();
  153. int * _size = (int*)(src_new+1);
  154. *_size=*_get_size();
  155. T*_data=(T*)(_size+1);
  156. // initialize new elements
  157. for (int i=0;i<*_size;i++) {
  158. memnew_placement(&_data[i], T( _get_data()[i] ) );
  159. }
  160. _unref(_ptr);
  161. _ptr=src_new;
  162. }
  163. }
  164. template<class T> template<class T_val>
  165. int Vector<T>::find(T_val& p_val) const {
  166. int ret = -1;
  167. if (size() == 0)
  168. return ret;
  169. for (int i=0; i<size(); i++) {
  170. if (operator[](i) == p_val) {
  171. ret = i;
  172. break;
  173. };
  174. };
  175. return ret;
  176. };
  177. template<class T>
  178. Error Vector<T>::resize(int p_size) {
  179. ERR_FAIL_COND_V(p_size<0,ERR_INVALID_PARAMETER);
  180. if (p_size==size())
  181. return OK;
  182. if (p_size==0) {
  183. // wants to clean up
  184. _unref(_ptr);
  185. _ptr=NULL;
  186. return OK;
  187. }
  188. // possibly changing size, copy on write
  189. _copy_on_write();
  190. if (p_size>size()) {
  191. if (size()==0) {
  192. // alloc from scratch
  193. _ptr = (T*)memalloc(_get_alloc_size(p_size));
  194. ERR_FAIL_COND_V( !_ptr ,ERR_OUT_OF_MEMORY);
  195. _get_refcount()->init(); // init refcount
  196. *_get_size()=0; // init size (currently, none)
  197. } else {
  198. void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size));
  199. ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
  200. _ptr=_ptrnew;
  201. }
  202. // construct the newly created elements
  203. T*elems = _get_data();
  204. for (int i=*_get_size();i<p_size;i++) {
  205. memnew_placement(&elems[i], T) ;
  206. }
  207. *_get_size()=p_size;
  208. } else if (p_size<size()) {
  209. // deinitialize no longer needed elements
  210. for (int i=p_size;i<*_get_size();i++) {
  211. T* t = &_get_data()[i];
  212. t->~T();
  213. }
  214. void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size));
  215. ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
  216. _ptr=_ptrnew;
  217. *_get_size()=p_size;
  218. }
  219. return OK;
  220. }
  221. template<class T>
  222. void Vector<T>::invert() {
  223. for(int i=0;i<size()/2;i++) {
  224. SWAP( operator[](i), operator[](size()-i-1) );
  225. }
  226. }
  227. template<class T>
  228. void Vector<T>::set(int p_index,T p_elem) {
  229. operator[](p_index)=p_elem;
  230. }
  231. template<class T>
  232. T Vector<T>::get(int p_index) const {
  233. return operator[](p_index);
  234. }
  235. template<class T>
  236. bool Vector<T>::push_back(T p_elem) {
  237. Error err = resize(size()+1);
  238. ERR_FAIL_COND_V( err, true )
  239. set(size()-1,p_elem);
  240. return false;
  241. }
  242. template<class T>
  243. void Vector<T>::remove(int p_index) {
  244. ERR_FAIL_INDEX(p_index, size());
  245. for (int i=p_index; i<size()-1; i++) {
  246. set(i, get(i+1));
  247. };
  248. resize(size()-1);
  249. };
  250. template<class T>
  251. void Vector<T>::_copy_from(const Vector& p_from) {
  252. if (_ptr == p_from._ptr)
  253. return; // self assign, do nothing.
  254. _unref(_ptr);
  255. _ptr=NULL;
  256. if (!p_from._ptr)
  257. return; //nothing to do
  258. if (p_from._get_refcount()->ref()) // could reference
  259. _ptr=p_from._ptr;
  260. }
  261. template<class T>
  262. void Vector<T>::operator=(const Vector& p_from) {
  263. _copy_from(p_from);
  264. }
  265. template<class T>
  266. Error Vector<T>::insert(int p_pos,const T& p_val) {
  267. ERR_FAIL_INDEX_V(p_pos,size()+1,ERR_INVALID_PARAMETER);
  268. resize(size()+1);
  269. for (int i=(size()-1);i>p_pos;i--)
  270. set( i, get(i-1) );
  271. set( p_pos, p_val );
  272. return OK;
  273. }
  274. template<class T>
  275. Vector<T>::Vector(const Vector& p_from) {
  276. _ptr=NULL;
  277. _copy_from( p_from );
  278. }
  279. template<class T>
  280. Vector<T>::Vector() {
  281. _ptr=NULL;
  282. }
  283. template<class T>
  284. Vector<T>::~Vector() {
  285. _unref(_ptr);
  286. }
  287. #endif