vector.h 9.6 KB

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