diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-06-13 18:22:27 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-12-08 18:19:38 +0100 |
commit | b42a2b3c3338a320a438bc081cb885fd4547f01f (patch) | |
tree | 80c729495f45bb9db011ed07c3c542f8a2727989 /src/corelib/tools/qarraydataops.h | |
parent | 3d9bae304cb1fa8f5f6f8854141fc8ecca92a333 (diff) |
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 <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/tools/qarraydataops.h')
-rw-r--r-- | src/corelib/tools/qarraydataops.h | 53 |
1 files changed, 48 insertions, 5 deletions
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<void *>(where + 1), static_cast<void *>(where), + (static_cast<const T*>(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<T>::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 |