From b42a2b3c3338a320a438bc081cb885fd4547f01f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 13 Jun 2012 18:22:27 +0200 Subject: Inline the size and begin pointer in QVector Add QGenericArray to simplify operations. This class can be shared by other tool classes. If there is nothing else to share it, we can move the code onto qvector.h. The one candidate is QList. All tests pass and valgrind is good. Change-Id: Ieaa80709caf5f50520aa97312ab726396f5475eb Reviewed-by: Simon Hausmann --- src/corelib/tools/qarraydataops.h | 53 +++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'src/corelib/tools/qarraydataops.h') diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index dbd535fa3e..f3de6b65a9 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -150,7 +150,7 @@ struct QPodArrayOps this->size += (e - b); } - void insert(T *where, size_t n, T t) + void insert(T *where, size_t n, parameter_type t) { Q_ASSERT(!this->isShared()); Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end @@ -163,6 +163,19 @@ struct QPodArrayOps *where++ = t; } + void insert(T *where, T &&t) + { + Q_ASSERT(!this->isShared()); + Q_ASSERT(where >= this->begin() && where <= this->end()); + Q_ASSERT(this->allocatedCapacity() - this->size >= 1); + + ::memmove(static_cast(where + 1), static_cast(where), + (static_cast(this->end()) - where) * sizeof(T)); + this->size += 1; + new (where) T(std::move(t)); + } + + void erase(T *b, T *e) { Q_ASSERT(this->isMutable()); @@ -369,7 +382,7 @@ struct QGenericArrayOps } } - void insert(T *where, size_t n, T t) + void insert(T *where, size_t n, parameter_type t) { Q_ASSERT(!this->isShared()); Q_ASSERT(where >= this->begin() && where <= this->end()); @@ -431,6 +444,33 @@ struct QGenericArrayOps } } + void insert(T *where, T &&t) + { + Q_ASSERT(!this->isShared()); + Q_ASSERT(where >= this->begin() && where <= this->end()); + Q_ASSERT(this->allocatedCapacity() - this->size >= 1); + + // Array may be truncated at where in case of exceptions + T *const end = this->end(); + + if (where != end) { + // Move elements in array + T *readIter = end - 1; + T *writeIter = end; + new (writeIter) T(std::move(*readIter)); + while (readIter > where) { + --readIter; + --writeIter; + *writeIter = std::move(*readIter); + } + *where = std::move(t); + } else { + new (where) T(std::move(t)); + } + + ++this->size; + } + void erase(T *b, T *e) { Q_ASSERT(this->isMutable()); @@ -554,7 +594,7 @@ struct QMovableArrayOps this->size += (e - b); } - void insert(T *where, size_t n, T t) + void insert(T *where, size_t n, parameter_type t) { Q_ASSERT(!this->isShared()); Q_ASSERT(where >= this->begin() && where <= this->end()); @@ -618,6 +658,9 @@ struct QMovableArrayOps this->size += n; } + // use moving insert + using QGenericArrayOps::insert; + void erase(T *b, T *e) { Q_ASSERT(this->isMutable()); @@ -627,7 +670,7 @@ struct QMovableArrayOps struct Mover { - Mover(T *&start, const T *finish, uint &sz) + Mover(T *&start, const T *finish, int &sz) : destination(start) , source(start) , n(finish - start) @@ -644,7 +687,7 @@ struct QMovableArrayOps T *&destination; const T *const source; size_t n; - uint &size; + int &size; } mover(e, this->end(), this->size); // destroy the elements we're erasing -- cgit v1.2.3