summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydataops.h
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-11-06 15:57:03 +0100
committerLars Knoll <lars.knoll@qt.io>2020-11-17 11:45:52 +0100
commitfc172c43132a15e17bd435db90e85722c7100361 (patch)
treeb3a48eea73904e9262ef9ba1cee0e0c1300a8a63 /src/corelib/tools/qarraydataops.h
parentee122077b09430da54ca09750589b37326a22d85 (diff)
Simplify the code for QList::emplace()
Unify it with the code in QArrayDataOps and only have one emplace method there that handles it all. Adjust autotests to API changes in QArrayDataOps and fix a wrong test case (that just happened to pass by chance before). Change-Id: Ia08cadebe2f74b82c31f856b1ff8a3d8dc400a3c Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qarraydataops.h')
-rw-r--r--src/corelib/tools/qarraydataops.h34
1 files changed, 20 insertions, 14 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index 3548424b53d..c2414f22f25 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -1345,22 +1345,28 @@ public:
}
template<typename... Args>
- void emplace(T *where, Args &&... args)
+ void emplace(qsizetype i, Args &&... args)
{
- Q_ASSERT(!this->isShared());
- Q_ASSERT(where >= this->begin() && where <= this->end());
- Q_ASSERT(this->allocatedCapacity() - this->size >= 1);
-
- const T *begin = this->begin();
- const T *end = this->end();
- // Qt5 QList in insert(1): try to move less data around
- // Now:
- const bool shouldInsertAtBegin =
- (where - begin) < (end - where) || this->freeSpaceAtEnd() <= 0;
- if (this->freeSpaceAtBegin() > 0 && shouldInsertAtBegin) {
- Base::emplace(GrowsBackwardsTag{}, where, std::forward<Args>(args)...);
+ typename QArrayData::GrowthPosition pos = QArrayData::GrowsAtEnd;
+ if (this->size != 0 && i <= (this->size >> 1))
+ pos = QArrayData::GrowsAtBeginning;
+ if (this->needsDetach() ||
+ (pos == QArrayData::GrowsAtBeginning && !this->freeSpaceAtBegin()) ||
+ (pos == QArrayData::GrowsAtEnd && !this->freeSpaceAtEnd())) {
+ T tmp(std::forward<Args>(args)...);
+ this->reallocateAndGrow(pos, 1);
+
+ T *where = this->begin() + i;
+ if (pos == QArrayData::GrowsAtBeginning)
+ Base::emplace(GrowsBackwardsTag{}, where, std::move(tmp));
+ else
+ Base::emplace(GrowsForwardTag{}, where, std::move(tmp));
} else {
- Base::emplace(GrowsForwardTag{}, where, std::forward<Args>(args)...);
+ T *where = this->begin() + i;
+ if (pos == QArrayData::GrowsAtBeginning)
+ Base::emplace(GrowsBackwardsTag{}, where, std::forward<Args>(args)...);
+ else
+ Base::emplace(GrowsForwardTag{}, where, std::forward<Args>(args)...);
}
}