diff options
Diffstat (limited to 'tests/auto/corelib/tools/qarraydata/simplevector.h')
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/simplevector.h | 147 |
1 files changed, 34 insertions, 113 deletions
diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index 6be785a628..b92cd4a887 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef QARRAY_TEST_SIMPLE_VECTOR_H @@ -32,6 +7,7 @@ #include <QtCore/qarraydata.h> #include <QtCore/qarraydatapointer.h> +#include <QtCore/qvarlengtharray.h> #include <algorithm> @@ -40,35 +16,42 @@ struct SimpleVector { private: typedef QTypedArrayData<T> Data; + typedef QArrayDataPointer<T> DataPointer; public: typedef T value_type; - typedef typename Data::iterator iterator; - typedef typename Data::const_iterator const_iterator; + typedef T *iterator; + typedef const T *const_iterator; SimpleVector() { } - explicit SimpleVector(size_t n) - : d(Data::allocate(n)) + explicit SimpleVector(size_t n, bool capacityReserved = false) + : d(n) { if (n) d->appendInitialize(n); + if (capacityReserved) + d.setFlag(QArrayData::CapacityReserved); } - SimpleVector(size_t n, const T &t) - : d(Data::allocate(n)) + SimpleVector(size_t n, const T &t, bool capacityReserved = false) + : d(n) { if (n) d->copyAppend(n, t); + if (capacityReserved) + d.setFlag(QArrayData::CapacityReserved); } - SimpleVector(const T *begin, const T *end) - : d(Data::allocate(end - begin)) + SimpleVector(const T *begin, const T *end, bool capacityReserved = false) + : d(end - begin) { if (end - begin) d->copyAppend(begin, end); + if (capacityReserved) + d.setFlag(QArrayData::CapacityReserved); } SimpleVector(Data *header, T *data, size_t len = 0) @@ -76,8 +59,8 @@ public: { } - explicit SimpleVector(QPair<Data*, T*> ptr, size_t len = 0) - : d(ptr, len) + SimpleVector(const QArrayDataPointer<T> &other) + : d(other) { } @@ -147,10 +130,11 @@ public: } } - SimpleVector detached(Data::allocate(qMax(n, size()), - d->detachFlags() | Data::CapacityReserved)); - if (size()) + SimpleVector detached(DataPointer(qMax(n, size()))); + if (size()) { detached.d->copyAppend(constBegin(), constEnd()); + detached.d->setFlag(QArrayData::CapacityReserved); + } detached.swap(*this); } @@ -160,8 +144,7 @@ public: return; if (d->needsDetach() || newSize > capacity()) { - SimpleVector detached(Data::allocate( - d->detachCapacity(newSize), d->detachFlags())); + SimpleVector detached(DataPointer(d->detachCapacity(newSize))); if (newSize) { if (newSize < size()) { const T *const begin = constBegin(); @@ -194,46 +177,10 @@ public: if (first == last) return; - T *const begin = d->begin(); - if (d->needsDetach() - || capacity() - size() < size_t(last - first)) { - SimpleVector detached(Data::allocate( - d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::GrowsForward)); - - detached.d->copyAppend(first, last); - detached.d->copyAppend(begin, begin + d->size); - detached.swap(*this); - - return; - } - - d->insert(begin, first, last); + d->insert(0, first, last - first); } - void append(const_iterator first, const_iterator last) - { - if (first == last) - return; - - if (d->needsDetach() - || capacity() - size() < size_t(last - first)) { - SimpleVector detached(Data::allocate( - d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::GrowsForward)); - - if (d->size) { - const T *const begin = constBegin(); - detached.d->copyAppend(begin, begin + d->size); - } - detached.d->copyAppend(first, last); - detached.swap(*this); - - return; - } - - d->copyAppend(first, last); - } + void append(const_iterator first, const_iterator last) { d->growAppend(first, last); } void insert(int position, const_iterator first, const_iterator last) { @@ -253,37 +200,13 @@ public: if (first == last) return; - const iterator begin = d->begin(); - const iterator where = begin + position; - const iterator end = begin + d->size; - if (d->needsDetach() - || capacity() - size() < size_t(last - first)) { - SimpleVector detached(Data::allocate( - d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::GrowsForward)); - - if (position) - detached.d->copyAppend(begin, where); - detached.d->copyAppend(first, last); - detached.d->copyAppend(where, end); - detached.swap(*this); - - return; - } - - if ((first >= where && first < end) - || (last > where && last <= end)) { - // Copy overlapping data first and only then shuffle it into place - iterator start = d->begin() + position; - iterator middle = d->end(); - - d->copyAppend(first, last); - std::rotate(start, middle, d->end()); - + if (first >= d.begin() && first <= d.end()) { + QVarLengthArray<T> copy(first, last); + insert(position, copy.begin(), copy.end()); return; } - d->insert(where, first, last); + d->insert(position, first, last - first); } void erase(iterator first, iterator last) @@ -295,9 +218,7 @@ public: const T *const end = begin + d->size; if (d->needsDetach()) { - SimpleVector detached(Data::allocate( - d->detachCapacity(size() - (last - first)), - d->detachFlags())); + SimpleVector detached(DataPointer(d->detachCapacity(size() - (last - first)))); if (first != begin) detached.d->copyAppend(begin, first); detached.d->copyAppend(last, end); @@ -309,7 +230,7 @@ public: if (last == end) d->truncate(end - first); else - d->erase(first, last); + d->erase(first, last - first); } void swap(SimpleVector &other) @@ -329,7 +250,7 @@ public: static SimpleVector fromRawData(const T *data, size_t size) { - return SimpleVector({ nullptr, const_cast<T *>(data), size }); + return SimpleVector(QArrayDataPointer<T>::fromRawData(data, size)); } private: |