diff options
Diffstat (limited to '')
| -rw-r--r-- | core/vector.h | 284 |
1 files changed, 139 insertions, 145 deletions
diff --git a/core/vector.h b/core/vector.h index 8113099a6..47f43372c 100644 --- a/core/vector.h +++ b/core/vector.h @@ -34,44 +34,42 @@ * @author Juan Linietsky * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use DVector for large arrays. */ -#include "os/memory.h" #include "error_macros.h" +#include "os/memory.h" #include "safe_refcount.h" #include "sort.h" -template<class T> +template <class T> class Vector { - mutable T* _ptr; + mutable T *_ptr; - // internal helpers + // internal helpers - _FORCE_INLINE_ SafeRefCount* _get_refcount() const { + _FORCE_INLINE_ SafeRefCount *_get_refcount() const { if (!_ptr) - return NULL; + return NULL; - return reinterpret_cast<SafeRefCount*>((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount)); - } + return reinterpret_cast<SafeRefCount *>((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount)); + } - _FORCE_INLINE_ int* _get_size() const { + _FORCE_INLINE_ int *_get_size() const { if (!_ptr) - return NULL; - return reinterpret_cast<int*>((uint8_t*)_ptr-sizeof(int)); - - } - _FORCE_INLINE_ T* _get_data() const { + return NULL; + return reinterpret_cast<int *>((uint8_t *)_ptr - sizeof(int)); + } + _FORCE_INLINE_ T *_get_data() const { if (!_ptr) - return NULL; - return reinterpret_cast<T*>(_ptr); - - } + return NULL; + return reinterpret_cast<T *>(_ptr); + } _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { //return nearest_power_of_2_templated(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int)); - return nearest_power_of_2(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int)); + return nearest_power_of_2(p_elements * sizeof(T) + sizeof(SafeRefCount) + sizeof(int)); } _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { @@ -79,7 +77,7 @@ class Vector { size_t o; size_t p; if (_mul_overflow(p_elements, sizeof(T), &o)) return false; - if (_add_overflow(o, sizeof(SafeRefCount)+sizeof(int), &p)) return false; + if (_add_overflow(o, sizeof(SafeRefCount) + sizeof(int), &p)) return false; *out = nearest_power_of_2(p); return true; #else @@ -92,19 +90,24 @@ class Vector { void _unref(void *p_data); - void _copy_from(const Vector& p_from); + void _copy_from(const Vector &p_from); void _copy_on_write(); -public: - - - _FORCE_INLINE_ T *ptr() { if (!_ptr) return NULL; _copy_on_write(); return (T*)_get_data(); } - _FORCE_INLINE_ const T *ptr() const { if (!_ptr) return NULL; return _get_data(); } +public: + _FORCE_INLINE_ T *ptr() { + if (!_ptr) return NULL; + _copy_on_write(); + return (T *)_get_data(); + } + _FORCE_INLINE_ const T *ptr() const { + if (!_ptr) return NULL; + return _get_data(); + } _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ int size() const { - int* size = _get_size(); + int *size = _get_size(); if (size) return *size; else @@ -115,21 +118,23 @@ public: bool push_back(T p_elem); void remove(int p_index); - void erase(const T& p_val) { int idx = find(p_val); if (idx>=0) remove(idx); }; + void erase(const T &p_val) { + int idx = find(p_val); + if (idx >= 0) remove(idx); + }; void invert(); - template <class T_val> - int find(const T_val& p_val, int p_from=0) const; + int find(const T_val &p_val, int p_from = 0) const; - void set(int p_index,T p_elem); + void set(int p_index, T p_elem); T get(int p_index) const; - inline T& operator[](int p_index) { + inline T &operator[](int p_index) { - if (p_index<0 || p_index>=size()) { - T& aux=*((T*)0); //nullreturn - ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux); + if (p_index < 0 || p_index >= size()) { + T &aux = *((T *)0); //nullreturn + ERR_FAIL_COND_V(p_index < 0 || p_index >= size(), aux); } _copy_on_write(); // wants to write, so copy on write. @@ -137,27 +142,27 @@ public: return _get_data()[p_index]; } - inline const T& operator[](int p_index) const { + inline const T &operator[](int p_index) const { - if (p_index<0 || p_index>=size()) { - const T& aux=*((T*)0); //nullreturn - ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux); + if (p_index < 0 || p_index >= size()) { + const T &aux = *((T *)0); //nullreturn + ERR_FAIL_COND_V(p_index < 0 || p_index >= size(), aux); } // no cow needed, since it's reading return _get_data()[p_index]; } - Error insert(int p_pos,const T& p_val); + Error insert(int p_pos, const T &p_val); - template<class C> + template <class C> void sort_custom() { int len = size(); - if (len==0) + if (len == 0) return; T *data = &operator[](0); - SortArray<T,C> sorter; - sorter.sort(data,len); + SortArray<T, C> sorter; + sorter.sort(data, len); } void sort() { @@ -165,86 +170,84 @@ public: sort_custom<_DefaultComparator<T> >(); } - void ordered_insert(const T& p_val) { - int i; - for (i=0; i<size(); i++) { + void ordered_insert(const T &p_val) { + int i; + for (i = 0; i < size(); i++) { - if (p_val < operator[](i)) { - break; - }; + if (p_val < operator[](i)) { + break; }; - insert(i, p_val); + }; + insert(i, p_val); } - void operator=(const Vector& p_from); - Vector(const Vector& p_from); + void operator=(const Vector &p_from); + Vector(const Vector &p_from); _FORCE_INLINE_ Vector(); _FORCE_INLINE_ ~Vector(); - }; -template<class T> +template <class T> void Vector<T>::_unref(void *p_data) { if (!p_data) return; - SafeRefCount *src = reinterpret_cast<SafeRefCount*>((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); + SafeRefCount *src = reinterpret_cast<SafeRefCount *>((uint8_t *)p_data - sizeof(int) - sizeof(SafeRefCount)); if (!src->unref()) return; // still in use // clean up - int *count = (int*)(src+1); - T *data = (T*)(count+1); + int *count = (int *)(src + 1); + T *data = (T *)(count + 1); - for (int i=0;i<*count;i++) { + for (int i = 0; i < *count; i++) { // call destructors data[i].~T(); } // free mem - memfree((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); - + memfree((uint8_t *)p_data - sizeof(int) - sizeof(SafeRefCount)); } -template<class T> +template <class T> void Vector<T>::_copy_on_write() { if (!_ptr) return; - if (_get_refcount()->get() > 1 ) { + if (_get_refcount()->get() > 1) { /* in use by more than me */ - void* mem_new = memalloc(_get_alloc_size(*_get_size())); - SafeRefCount *src_new=(SafeRefCount *)mem_new; + void *mem_new = memalloc(_get_alloc_size(*_get_size())); + SafeRefCount *src_new = (SafeRefCount *)mem_new; src_new->init(); - int * _size = (int*)(src_new+1); - *_size=*_get_size(); + int *_size = (int *)(src_new + 1); + *_size = *_get_size(); - T*_data=(T*)(_size+1); + T *_data = (T *)(_size + 1); // initialize new elements - for (int i=0;i<*_size;i++) { + for (int i = 0; i < *_size; i++) { - memnew_placement(&_data[i], T( _get_data()[i] ) ); + memnew_placement(&_data[i], T(_get_data()[i])); } _unref(_ptr); - _ptr=_data; + _ptr = _data; } - } -template<class T> template<class T_val> +template <class T> +template <class T_val> int Vector<T>::find(const T_val &p_val, int p_from) const { int ret = -1; if (p_from < 0 || size() == 0) return ret; - for (int i=p_from; i<size(); i++) { + for (int i = p_from; i < size(); i++) { if (operator[](i) == p_val) { ret = i; @@ -255,18 +258,18 @@ int Vector<T>::find(const T_val &p_val, int p_from) const { return ret; } -template<class T> +template <class T> Error Vector<T>::resize(int p_size) { - ERR_FAIL_COND_V(p_size<0,ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); - if (p_size==size()) + if (p_size == size()) return OK; - if (p_size==0) { + if (p_size == 0) { // wants to clean up _unref(_ptr); - _ptr=NULL; + _ptr = NULL; return OK; } @@ -276,157 +279,148 @@ Error Vector<T>::resize(int p_size) { size_t alloc_size; ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY); - if (p_size>size()) { + if (p_size > size()) { - if (size()==0) { + if (size() == 0) { // alloc from scratch - void* ptr=memalloc(alloc_size); - ERR_FAIL_COND_V( !ptr ,ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)ptr+sizeof(int)+sizeof(SafeRefCount)); + void *ptr = memalloc(alloc_size); + ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY); + _ptr = (T *)((uint8_t *)ptr + sizeof(int) + sizeof(SafeRefCount)); _get_refcount()->init(); // init refcount - *_get_size()=0; // init size (currently, none) + *_get_size() = 0; // init size (currently, none) } else { - void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount), alloc_size); - ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); + void *_ptrnew = (T *)memrealloc((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount), alloc_size); + ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); + _ptr = (T *)((uint8_t *)_ptrnew + sizeof(int) + sizeof(SafeRefCount)); } // construct the newly created elements - T*elems = _get_data(); + T *elems = _get_data(); - for (int i=*_get_size();i<p_size;i++) { + for (int i = *_get_size(); i < p_size; i++) { - memnew_placement(&elems[i], T) ; + memnew_placement(&elems[i], T); } - *_get_size()=p_size; + *_get_size() = p_size; - } else if (p_size<size()) { + } else if (p_size < size()) { // deinitialize no longer needed elements - for (int i=p_size;i<*_get_size();i++) { + for (int i = p_size; i < *_get_size(); i++) { - T* t = &_get_data()[i]; + T *t = &_get_data()[i]; t->~T(); } - void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount), alloc_size); - ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); + void *_ptrnew = (T *)memrealloc((uint8_t *)_ptr - sizeof(int) - sizeof(SafeRefCount), alloc_size); + ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); - _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); - - *_get_size()=p_size; + _ptr = (T *)((uint8_t *)_ptrnew + sizeof(int) + sizeof(SafeRefCount)); + *_get_size() = p_size; } return OK; } - -template<class T> +template <class T> void Vector<T>::invert() { - for(int i=0;i<size()/2;i++) { + for (int i = 0; i < size() / 2; i++) { - SWAP( operator[](i), operator[](size()-i-1) ); + SWAP(operator[](i), operator[](size() - i - 1)); } } -template<class T> -void Vector<T>::set(int p_index,T p_elem) { +template <class T> +void Vector<T>::set(int p_index, T p_elem) { - operator[](p_index)=p_elem; + operator[](p_index) = p_elem; } -template<class T> +template <class T> T Vector<T>::get(int p_index) const { return operator[](p_index); } -template<class T> +template <class T> bool Vector<T>::push_back(T p_elem) { - Error err = resize(size()+1); - ERR_FAIL_COND_V( err, true ) - set(size()-1,p_elem); + Error err = resize(size() + 1); + ERR_FAIL_COND_V(err, true) + set(size() - 1, p_elem); return false; } - -template<class T> +template <class T> void Vector<T>::remove(int p_index) { ERR_FAIL_INDEX(p_index, size()); - T*p=ptr(); - int len=size(); - for (int i=p_index; i<len-1; i++) { + T *p = ptr(); + int len = size(); + for (int i = p_index; i < len - 1; i++) { - p[i]=p[i+1]; + p[i] = p[i + 1]; }; - resize(len-1); + resize(len - 1); }; -template<class T> -void Vector<T>::_copy_from(const Vector& p_from) { +template <class T> +void Vector<T>::_copy_from(const Vector &p_from) { if (_ptr == p_from._ptr) return; // self assign, do nothing. _unref(_ptr); - _ptr=NULL; + _ptr = NULL; if (!p_from._ptr) return; //nothing to do if (p_from._get_refcount()->ref()) // could reference - _ptr=p_from._ptr; - + _ptr = p_from._ptr; } -template<class T> -void Vector<T>::operator=(const Vector& p_from) { +template <class T> +void Vector<T>::operator=(const Vector &p_from) { _copy_from(p_from); } +template <class T> +Error Vector<T>::insert(int p_pos, const T &p_val) { -template<class T> -Error Vector<T>::insert(int p_pos,const T& p_val) { - - ERR_FAIL_INDEX_V(p_pos,size()+1,ERR_INVALID_PARAMETER); - resize(size()+1); - for (int i=(size()-1);i>p_pos;i--) - set( i, get(i-1) ); - set( p_pos, p_val ); + ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); + resize(size() + 1); + for (int i = (size() - 1); i > p_pos; i--) + set(i, get(i - 1)); + set(p_pos, p_val); return OK; } -template<class T> -Vector<T>::Vector(const Vector& p_from) { - - _ptr=NULL; - _copy_from( p_from ); +template <class T> +Vector<T>::Vector(const Vector &p_from) { + _ptr = NULL; + _copy_from(p_from); } -template<class T> +template <class T> Vector<T>::Vector() { - _ptr=NULL; + _ptr = NULL; } - -template<class T> +template <class T> Vector<T>::~Vector() { _unref(_ptr); - } - #endif |
