From d3d284bec423d936e936cdb756b6e1bd2ad537b8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 8 Dec 2021 13:31:42 +0100 Subject: [PATCH] QVLA: separate control from inline storage [10/N]: range-insert() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also added a QVLABase::resize_impl() because insert(it, n, v) was the only function moved down into QVLABase to call resize(). Task-number: QTBUG-84785 Change-Id: I5dd0092216d73b28b957a01845325d744a5c0ba9 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qvarlengtharray.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index e51361623b..e6ff615b03 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -206,6 +206,8 @@ protected: template iterator emplace_impl(qsizetype prealloc, void *array, const_iterator pos, Args&&...arg); + iterator insert_impl(qsizetype prealloc, void *array, const_iterator pos, qsizetype n, const T &t); + template bool equal(const QVLABase &other) const { @@ -219,6 +221,8 @@ protected: void append_impl(qsizetype prealloc, void *array, const T *buf, qsizetype n); void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc); + void resize_impl(qsizetype prealloc, void *array, qsizetype sz) + { reallocate_impl(prealloc, array, sz, qMax(sz, capacity())); } bool isValidIterator(const const_iterator &i) const { @@ -371,7 +375,7 @@ public: return back(); } bool isEmpty() const { return empty(); } - void resize(qsizetype sz) { reallocate(sz, qMax(sz, capacity())); } + void resize(qsizetype sz) { Base::resize_impl(Prealloc, this->array, sz); } inline void clear() { resize(0); } void squeeze() { reallocate(size(), size()); } @@ -498,7 +502,8 @@ public: using Base::rend; using Base::crend; - iterator insert(const_iterator before, qsizetype n, const T &x); + iterator insert(const_iterator before, qsizetype n, const T &x) + { return Base::insert_impl(Prealloc, this->array, before, n, x); } iterator insert(const_iterator before, T &&x) { return emplace(before, std::move(x)); } inline iterator insert(const_iterator before, const T &x) { return insert(before, 1, x); } #ifdef Q_QDOC @@ -845,15 +850,15 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::emplace_impl(qsizetype prealloc, void *ar return data() + offset; } -template -Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthArray::insert(const_iterator before, qsizetype n, const T &t) +template +Q_OUTOFLINE_TEMPLATE auto QVLABase::insert_impl(qsizetype prealloc, void *array, const_iterator before, qsizetype n, const T &t) -> iterator { Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid"); qsizetype offset = qsizetype(before - cbegin()); if (n != 0) { const T copy(t); // `t` could alias an element in [begin(), end()[ - resize(size() + n); + resize_impl(prealloc, array, size() + n); if constexpr (!QTypeInfo::isRelocatable) { T *b = begin() + offset; T *j = end();