From f8db8a3faa30b71dca33ced38be16d3f93f43e8a Mon Sep 17 00:00:00 2001 From: Rémi Verschelde Date: Sun, 19 Mar 2017 00:36:26 +0100 Subject: Bring that Whole New World to the Old Continent too Applies the clang-format style to the 2.1 branch as done for master in 5dbf1809c6e3e905b94b8764e99491e608122261. --- core/dvector.h | 239 ++++++++++++++++++++++++++------------------------------- 1 file changed, 107 insertions(+), 132 deletions(-) (limited to 'core/dvector.h') diff --git a/core/dvector.h b/core/dvector.h index 47883b23b..2c58b4a31 100644 --- a/core/dvector.h +++ b/core/dvector.h @@ -31,20 +31,17 @@ #include "os/memory.h" - /** @author Juan Linietsky */ +extern Mutex *dvector_lock; -extern Mutex* dvector_lock; - -template +template class DVector { mutable MID mem; - void copy_on_write() { if (!mem.is_valid()) @@ -53,56 +50,54 @@ class DVector { if (dvector_lock) dvector_lock->lock(); - MID_Lock lock( mem ); - + MID_Lock lock(mem); - if ( *(int*)lock.data() == 1 ) { + if (*(int *)lock.data() == 1) { // one reference, means no refcount changes if (dvector_lock) dvector_lock->unlock(); return; } - MID new_mem= dynalloc( mem.get_size() ); + MID new_mem = dynalloc(mem.get_size()); if (!new_mem.is_valid()) { if (dvector_lock) dvector_lock->unlock(); - ERR_FAIL_COND( new_mem.is_valid() ); // out of memory + ERR_FAIL_COND(new_mem.is_valid()); // out of memory } - MID_Lock dst_lock( new_mem ); + MID_Lock dst_lock(new_mem); - int *rc = (int*)dst_lock.data(); + int *rc = (int *)dst_lock.data(); - *rc=1; + *rc = 1; - T * dst = (T*)(rc + 1 ); + T *dst = (T *)(rc + 1); - T * src =(T*) ((int*)lock.data() + 1 ); + T *src = (T *)((int *)lock.data() + 1); int count = (mem.get_size() - sizeof(int)) / sizeof(T); - for (int i=0;iunlock(); - } - void reference( const DVector& p_dvector ) { + void reference(const DVector &p_dvector) { unreference(); @@ -118,18 +113,16 @@ class DVector { MID_Lock lock(p_dvector.mem); - int * rc = (int*)lock.data(); + int *rc = (int *)lock.data(); (*rc)++; lock = MID_Lock(); - mem=p_dvector.mem; + mem = p_dvector.mem; if (dvector_lock) dvector_lock->unlock(); - } - void unreference() { if (dvector_lock) @@ -144,65 +137,60 @@ class DVector { MID_Lock lock(mem); - int * rc = (int*)lock.data(); + int *rc = (int *)lock.data(); (*rc)--; - if (*rc==0) { + if (*rc == 0) { // no one else using it, destruct - T * t= (T*)(rc+1); + T *t = (T *)(rc + 1); int count = (mem.get_size() - sizeof(int)) / sizeof(T); - for (int i=0;iunlock(); - } public: - class Read { - friend class DVector; + friend class DVector; MID_Lock lock; - const T * mem; - public: + const T *mem; - _FORCE_INLINE_ const T& operator[](int p_index) const { return mem[p_index]; } + public: + _FORCE_INLINE_ const T &operator[](int p_index) const { return mem[p_index]; } _FORCE_INLINE_ const T *ptr() const { return mem; } - Read() { mem=NULL; } + Read() { mem = NULL; } }; class Write { - friend class DVector; + friend class DVector; MID_Lock lock; - T * mem; - public: + T *mem; - _FORCE_INLINE_ T& operator[](int p_index) { return mem[p_index]; } + public: + _FORCE_INLINE_ T &operator[](int p_index) { return mem[p_index]; } _FORCE_INLINE_ T *ptr() { return mem; } - Write() { mem=NULL; } + Write() { mem = NULL; } }; - Read read() const { Read r; if (mem.is_valid()) { - r.lock = MID_Lock( mem ); - r.mem = (const T*)((int*)r.lock.data()+1); + r.lock = MID_Lock(mem); + r.mem = (const T *)((int *)r.lock.data() + 1); } return r; } @@ -211,74 +199,70 @@ public: Write w; if (mem.is_valid()) { copy_on_write(); - w.lock = MID_Lock( mem ); - w.mem = (T*)((int*)w.lock.data()+1); + w.lock = MID_Lock(mem); + w.mem = (T *)((int *)w.lock.data() + 1); } return w; } - template - void fill_with(const MC& p_mc) { - + template + void fill_with(const MC &p_mc) { - int c=p_mc.size(); + int c = p_mc.size(); resize(c); - Write w=write(); - int idx=0; - for(const typename MC::Element *E=p_mc.front();E;E=E->next()) { + Write w = write(); + int idx = 0; + for (const typename MC::Element *E = p_mc.front(); E; E = E->next()) { - w[idx++]=E->get(); + w[idx++] = E->get(); } } - void remove(int p_index) { int s = size(); ERR_FAIL_INDEX(p_index, s); Write w = write(); - for (int i=p_index; i& p_arr) { + void set(int p_index, const T &p_val); + void push_back(const T &p_val); + void append(const T &p_val) { push_back(p_val); } + void append_array(const DVector &p_arr) { int ds = p_arr.size(); - if (ds==0) + if (ds == 0) return; int bs = size(); - resize( bs + ds); + resize(bs + ds); Write w = write(); Read r = p_arr.read(); - for(int i=0;ip_pos;i--) - w[i]=w[i-1]; - w[p_pos]=p_val; + for (int i = s; i > p_pos; i--) + w[i] = w[i - 1]; + w[p_pos] = p_val; } return OK; } - bool is_locked() const { return mem.is_locked(); } inline const T operator[](int p_index) const; @@ -287,49 +271,48 @@ public: void invert(); - void operator=(const DVector& p_dvector) { reference(p_dvector); } + void operator=(const DVector &p_dvector) { reference(p_dvector); } DVector() {} - DVector(const DVector& p_dvector) { reference(p_dvector); } + DVector(const DVector &p_dvector) { reference(p_dvector); } ~DVector() { unreference(); } - }; -template +template int DVector::size() const { - return mem.is_valid() ? ((mem.get_size() - sizeof(int)) / sizeof(T) ) : 0; + return mem.is_valid() ? ((mem.get_size() - sizeof(int)) / sizeof(T)) : 0; } -template +template T DVector::get(int p_index) const { return operator[](p_index); } -template -void DVector::set(int p_index, const T& p_val) { +template +void DVector::set(int p_index, const T &p_val) { - if (p_index<0 || p_index>=size()) { - ERR_FAIL_COND(p_index<0 || p_index>=size()); + if (p_index < 0 || p_index >= size()) { + ERR_FAIL_COND(p_index < 0 || p_index >= size()); } Write w = write(); - w[p_index]=p_val; + w[p_index] = p_val; } -template -void DVector::push_back(const T& p_val) { +template +void DVector::push_back(const T &p_val) { - resize( size() + 1 ); - set( size() -1, p_val ); + resize(size() + 1); + set(size() - 1, p_val); } -template +template const T DVector::operator[](int p_index) const { - 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); } Read r = read(); @@ -337,14 +320,13 @@ const T DVector::operator[](int p_index) const { return r[p_index]; } - -template +template Error DVector::resize(int p_size) { if (dvector_lock) dvector_lock->lock(); - bool same = p_size==size(); + bool same = p_size == size(); if (dvector_lock) dvector_lock->unlock(); @@ -353,89 +335,82 @@ Error DVector::resize(int p_size) { if (same) return OK; - if (p_size == 0 ) { + if (p_size == 0) { unreference(); return OK; } - copy_on_write(); // make it unique - ERR_FAIL_COND_V( mem.is_locked(), ERR_LOCKED ); // if after copy on write, memory is locked, fail. + ERR_FAIL_COND_V(mem.is_locked(), ERR_LOCKED); // if after copy on write, memory is locked, fail. - if (p_size > size() ) { + if (p_size > size()) { - int oldsize=size(); + int oldsize = size(); MID_Lock lock; - if (oldsize==0) { + if (oldsize == 0) { - mem = dynalloc( p_size * sizeof(T) + sizeof(int) ); - lock=MID_Lock(mem); - int *rc = ((int*)lock.data()); - *rc=1; + mem = dynalloc(p_size * sizeof(T) + sizeof(int)); + lock = MID_Lock(mem); + int *rc = ((int *)lock.data()); + *rc = 1; } else { - if (dynrealloc( mem, p_size * sizeof(T) + sizeof(int) )!=OK ) { + if (dynrealloc(mem, p_size * sizeof(T) + sizeof(int)) != OK) { ERR_FAIL_V(ERR_OUT_OF_MEMORY); // out of memory } - lock=MID_Lock(mem); + lock = MID_Lock(mem); } + T *t = (T *)((int *)lock.data() + 1); + for (int i = oldsize; i < p_size; i++) { - - T *t = (T*)((int*)lock.data() + 1); - - for (int i=oldsize;i +template void DVector::invert() { T temp; Write w = write(); int s = size(); - int half_s = s/2; + int half_s = s / 2; - for(int i=0;i