From 5dbf1809c6e3e905b94b8764e99491e608122261 Mon Sep 17 00:00:00 2001 From: Rémi Verschelde Date: Sun, 5 Mar 2017 16:44:50 +0100 Subject: A Whole New World (clang-format edition) I can show you the code Pretty, with proper whitespace Tell me, coder, now when did You last write readable code? I can open your eyes Make you see your bad indent Force you to respect the style The core devs agreed upon A whole new world A new fantastic code format A de facto standard With some sugar Enforced with clang-format A whole new world A dazzling style we all dreamed of And when we read it through It's crystal clear That now we're in a whole new world of code --- core/vector.h | 279 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 136 insertions(+), 143 deletions(-) (limited to 'core/vector.h') diff --git a/core/vector.h b/core/vector.h index 1cb3fee25..b7b6b92ed 100644 --- a/core/vector.h +++ b/core/vector.h @@ -34,45 +34,43 @@ * @author Juan Linietsky * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays. */ -#include "os/memory.h" #include "error_macros.h" +#include "os/memory.h" #include "safe_refcount.h" #include "sort.h" -template +template class Vector { - mutable T* _ptr; + mutable T *_ptr; - // internal helpers + // internal helpers - _FORCE_INLINE_ uint32_t* _get_refcount() const { + _FORCE_INLINE_ uint32_t *_get_refcount() const { if (!_ptr) - return NULL; + return NULL; - return reinterpret_cast(_ptr)-2; - } + return reinterpret_cast(_ptr) - 2; + } - _FORCE_INLINE_ uint32_t* _get_size() const { + _FORCE_INLINE_ uint32_t *_get_size() const { if (!_ptr) return NULL; - return reinterpret_cast(_ptr)-1; - - } - _FORCE_INLINE_ T* _get_data() const { + return reinterpret_cast(_ptr) - 1; + } + _FORCE_INLINE_ T *_get_data() const { if (!_ptr) - return NULL; - return reinterpret_cast(_ptr); - - } + return NULL; + return reinterpret_cast(_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)); + return nearest_power_of_2(p_elements * sizeof(T)); } _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { @@ -93,19 +91,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 { - uint32_t* size = (uint32_t*)_get_size(); + uint32_t *size = (uint32_t *)_get_size(); if (size) return *size; else @@ -116,21 +119,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 - 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. @@ -138,27 +143,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 + template void sort_custom() { int len = size(); - if (len==0) + if (len == 0) return; T *data = &operator[](0); - SortArray sorter; - sorter.sort(data,len); + SortArray sorter; + sorter.sort(data, len); } void sort() { @@ -166,26 +171,25 @@ public: sort_custom<_DefaultComparator >(); } - void ordered_insert(const T& p_val) { - int i; - for (i=0; i +template void Vector::_unref(void *p_data) { if (!p_data) @@ -193,24 +197,23 @@ void Vector::_unref(void *p_data) { uint32_t *refc = _get_refcount(); - if (atomic_decrement(refc)>0) + if (atomic_decrement(refc) > 0) return; // still in use // clean up uint32_t *count = _get_size(); - T *data = (T*)(count+1); + T *data = (T *)(count + 1); - for (uint32_t i=0;i<*count;i++) { + for (uint32_t i = 0; i < *count; i++) { // call destructors data[i].~T(); } // free mem - Memory::free_static((uint8_t*)p_data,true); - + Memory::free_static((uint8_t *)p_data, true); } -template +template void Vector::_copy_on_write() { if (!_ptr) @@ -222,34 +225,33 @@ void Vector::_copy_on_write() { /* in use by more than me */ uint32_t current_size = *_get_size(); - uint32_t* mem_new = (uint32_t*)Memory::alloc_static(_get_alloc_size(current_size),true); + uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); + *(mem_new - 2) = 1; //refcount + *(mem_new - 1) = current_size; //size - *(mem_new-2)=1; //refcount - *(mem_new-1)=current_size; //size - - T*_data=(T*)(mem_new); + T *_data = (T *)(mem_new); // initialize new elements - for (uint32_t i=0;i template +template +template int Vector::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::find(const T_val &p_val, int p_from) const { return ret; } -template +template Error Vector::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; } @@ -281,159 +283,150 @@ Error Vector::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 - uint32_t *ptr=(uint32_t*)Memory::alloc_static(alloc_size,true); - ERR_FAIL_COND_V( !ptr ,ERR_OUT_OF_MEMORY); - *(ptr-1)=0; //size, currently none - *(ptr-2)=1; //refcount + uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); + ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY); + *(ptr - 1) = 0; //size, currently none + *(ptr - 2) = 1; //refcount - _ptr=(T*)ptr; + _ptr = (T *)ptr; } else { - void *_ptrnew = (T*)Memory::realloc_static(_ptr, alloc_size,true); - ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=(T*)(_ptrnew); + void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true); + ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); + _ptr = (T *)(_ptrnew); } // construct the newly created elements - T*elems = _get_data(); + T *elems = _get_data(); - for (int i=*_get_size();i~T(); } - void *_ptrnew = (T*)Memory::realloc_static(_ptr, alloc_size,true); - ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); + void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true); + ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); - _ptr=(T*)(_ptrnew); - - *_get_size()=p_size; + _ptr = (T *)(_ptrnew); + *_get_size() = p_size; } return OK; } - -template +template void Vector::invert() { - for(int i=0;i -void Vector::set(int p_index,T p_elem) { +template +void Vector::set(int p_index, T p_elem) { - operator[](p_index)=p_elem; + operator[](p_index) = p_elem; } -template +template T Vector::get(int p_index) const { return operator[](p_index); } -template +template bool Vector::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 +template void Vector::remove(int p_index) { ERR_FAIL_INDEX(p_index, size()); - T*p=ptr(); - int len=size(); - for (int i=p_index; i -void Vector::_copy_from(const Vector& p_from) { +template +void Vector::_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 (atomic_conditional_increment(p_from._get_refcount())>0) { // could reference - _ptr=p_from._ptr; + if (atomic_conditional_increment(p_from._get_refcount()) > 0) { // could reference + _ptr = p_from._ptr; } - } -template -void Vector::operator=(const Vector& p_from) { +template +void Vector::operator=(const Vector &p_from) { _copy_from(p_from); } +template +Error Vector::insert(int p_pos, const T &p_val) { -template -Error Vector::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 -Vector::Vector(const Vector& p_from) { - - _ptr=NULL; - _copy_from( p_from ); +template +Vector::Vector(const Vector &p_from) { + _ptr = NULL; + _copy_from(p_from); } -template +template Vector::Vector() { - _ptr=NULL; + _ptr = NULL; } - -template +template Vector::~Vector() { _unref(_ptr); - } - #endif -- cgit v1.2.3-70-g09d2