vector.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*************************************************************************/
  2. /* vector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 operator=(const Vector& p_from);
  115. Vector(const Vector& p_from);
  116. _FORCE_INLINE_ Vector();
  117. _FORCE_INLINE_ ~Vector();
  118. };
  119. template<class T>
  120. void Vector<T>::_unref(void *p_data) {
  121. if (!p_data)
  122. return;
  123. SafeRefCount *src = reinterpret_cast<SafeRefCount*>(p_data);
  124. if (!src->unref())
  125. return; // still in use
  126. // clean up
  127. int *count = (int*)(src+1);
  128. T *data = (T*)(count+1);
  129. for (int i=0;i<*count;i++) {
  130. // call destructors
  131. data[i].~T();
  132. }
  133. // free mem
  134. memfree(p_data);
  135. }
  136. template<class T>
  137. void Vector<T>::_copy_on_write() {
  138. if (!_ptr)
  139. return;
  140. if (_get_refcount()->get() > 1 ) {
  141. /* in use by more than me */
  142. SafeRefCount *src_new=(SafeRefCount *)memalloc(_get_alloc_size(*_get_size()));
  143. src_new->init();
  144. int * _size = (int*)(src_new+1);
  145. *_size=*_get_size();
  146. T*_data=(T*)(_size+1);
  147. // initialize new elements
  148. for (int i=0;i<*_size;i++) {
  149. memnew_placement(&_data[i], T( _get_data()[i] ) );
  150. }
  151. _unref(_ptr);
  152. _ptr=src_new;
  153. }
  154. }
  155. template<class T> template<class T_val>
  156. int Vector<T>::find(T_val& p_val) const {
  157. int ret = -1;
  158. if (size() == 0)
  159. return ret;
  160. for (int i=0; i<size(); i++) {
  161. if (operator[](i) == p_val) {
  162. ret = i;
  163. break;
  164. };
  165. };
  166. return ret;
  167. };
  168. template<class T>
  169. Error Vector<T>::resize(int p_size) {
  170. ERR_FAIL_COND_V(p_size<0,ERR_INVALID_PARAMETER);
  171. if (p_size==size())
  172. return OK;
  173. if (p_size==0) {
  174. // wants to clean up
  175. _unref(_ptr);
  176. _ptr=NULL;
  177. return OK;
  178. }
  179. // possibly changing size, copy on write
  180. _copy_on_write();
  181. if (p_size>size()) {
  182. if (size()==0) {
  183. // alloc from scratch
  184. _ptr = (T*)memalloc(_get_alloc_size(p_size));
  185. ERR_FAIL_COND_V( !_ptr ,ERR_OUT_OF_MEMORY);
  186. _get_refcount()->init(); // init refcount
  187. *_get_size()=0; // init size (currently, none)
  188. } else {
  189. void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size));
  190. ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
  191. _ptr=_ptrnew;
  192. }
  193. // construct the newly created elements
  194. T*elems = _get_data();
  195. for (int i=*_get_size();i<p_size;i++) {
  196. memnew_placement(&elems[i], T) ;
  197. }
  198. *_get_size()=p_size;
  199. } else if (p_size<size()) {
  200. // deinitialize no longer needed elements
  201. for (int i=p_size;i<*_get_size();i++) {
  202. T* t = &_get_data()[i];
  203. t->~T();
  204. }
  205. void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size));
  206. ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
  207. _ptr=_ptrnew;
  208. *_get_size()=p_size;
  209. }
  210. return OK;
  211. }
  212. template<class T>
  213. void Vector<T>::invert() {
  214. for(int i=0;i<size()/2;i++) {
  215. SWAP( operator[](i), operator[](size()-i-1) );
  216. }
  217. }
  218. template<class T>
  219. void Vector<T>::set(int p_index,T p_elem) {
  220. operator[](p_index)=p_elem;
  221. }
  222. template<class T>
  223. T Vector<T>::get(int p_index) const {
  224. return operator[](p_index);
  225. }
  226. template<class T>
  227. bool Vector<T>::push_back(T p_elem) {
  228. Error err = resize(size()+1);
  229. ERR_FAIL_COND_V( err, true )
  230. set(size()-1,p_elem);
  231. return false;
  232. }
  233. template<class T>
  234. void Vector<T>::remove(int p_index) {
  235. ERR_FAIL_INDEX(p_index, size());
  236. for (int i=p_index; i<size()-1; i++) {
  237. set(i, get(i+1));
  238. };
  239. resize(size()-1);
  240. };
  241. template<class T>
  242. void Vector<T>::_copy_from(const Vector& p_from) {
  243. if (_ptr == p_from._ptr)
  244. return; // self assign, do nothing.
  245. _unref(_ptr);
  246. _ptr=NULL;
  247. if (!p_from._ptr)
  248. return; //nothing to do
  249. if (p_from._get_refcount()->ref()) // could reference
  250. _ptr=p_from._ptr;
  251. }
  252. template<class T>
  253. void Vector<T>::operator=(const Vector& p_from) {
  254. _copy_from(p_from);
  255. }
  256. template<class T>
  257. Error Vector<T>::insert(int p_pos,const T& p_val) {
  258. ERR_FAIL_INDEX_V(p_pos,size()+1,ERR_INVALID_PARAMETER);
  259. resize(size()+1);
  260. for (int i=(size()-1);i>p_pos;i--)
  261. set( i, get(i-1) );
  262. set( p_pos, p_val );
  263. return OK;
  264. }
  265. template<class T>
  266. Vector<T>::Vector(const Vector& p_from) {
  267. _ptr=NULL;
  268. _copy_from( p_from );
  269. }
  270. template<class T>
  271. Vector<T>::Vector() {
  272. _ptr=NULL;
  273. }
  274. template<class T>
  275. Vector<T>::~Vector() {
  276. _unref(_ptr);
  277. }
  278. #endif