summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qarraydata/simplevector.h
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qarraydata/simplevector.h')
-rw-r--r--tests/auto/corelib/tools/qarraydata/simplevector.h155
1 files changed, 30 insertions, 125 deletions
diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h
index a6c43a92ee..b92cd4a887 100644
--- a/tests/auto/corelib/tools/qarraydata/simplevector.h
+++ b/tests/auto/corelib/tools/qarraydata/simplevector.h
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 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>
@@ -44,32 +20,38 @@ private:
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, typename Data::ArrayOptions f = Data::DefaultAllocationFlags)
- : d(Data::allocate(n, f))
+ 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, typename Data::ArrayOptions f = Data::DefaultAllocationFlags)
- : d(Data::allocate(n, f))
+ 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, typename Data::ArrayOptions f = Data::DefaultAllocationFlags)
- : d(Data::allocate(end - begin, f))
+ 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)
@@ -77,11 +59,6 @@ public:
{
}
- explicit SimpleVector(QPair<Data*, T*> ptr, size_t len = 0)
- : d(ptr, len)
- {
- }
-
SimpleVector(const QArrayDataPointer<T> &other)
: d(other)
{
@@ -153,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);
}
@@ -166,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();
@@ -200,51 +177,10 @@ public:
if (first == last)
return;
- T *const begin = d->begin();
- const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin(), last - first);
- const auto newSize = size() + (last - first);
- if (d->needsDetach()
- || capacity() - size() < size_t(last - first)
- || shouldGrow) {
- const auto flags = d->detachFlags() | Data::GrowsBackwards;
- SimpleVector detached(DataPointer::allocateGrow(d, d->detachCapacity(newSize), newSize,
- flags));
-
- 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;
-
- const bool shouldGrow = d->shouldGrowBeforeInsert(d.end(), last - first);
- const auto newSize = size() + (last - first);
- if (d->needsDetach()
- || capacity() - size() < size_t(last - first)
- || shouldGrow) {
- SimpleVector detached(DataPointer::allocateGrow(d, d->detachCapacity(newSize), newSize,
- 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)
{
@@ -264,42 +200,13 @@ public:
if (first == last)
return;
- const iterator begin = d->begin();
- const iterator where = begin + position;
- const iterator end = begin + d->size;
- const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + position, last - first);
- const auto newSize = size() + (last - first);
- if (d->needsDetach()
- || capacity() - size() < size_t(last - first)
- || shouldGrow) {
- auto flags = d->detachFlags() | Data::GrowsForward;
- if (size_t(position) <= (size() + (last - first)) / 4)
- flags |= Data::GrowsBackwards;
- SimpleVector detached(DataPointer::allocateGrow(d, d->detachCapacity(newSize), newSize,
- flags));
-
- 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)
@@ -311,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);
@@ -325,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)