From 03326a2fec416405b437089874f6439e937bbada Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 27 May 2020 09:58:12 +0200 Subject: Move implementation of QVector/List back to qlist.h And name the main class QList. That's also the one we document. This gives less porting pain for our users, and a lot less churn in our API, as we use QList in Qt 5 in 95% of our API. In addition, it gives more consistent naming with QStringList and QByteArrayList and disambiguates QList vs QVector(2|3|4)D. Fixes: QTBUG-84468 Change-Id: I3cba9d1d3179969d8bf9320b31be2230d021d1a9 Reviewed-by: Volker Hilsheimer --- .../doc/snippets/code/src_corelib_tools_qlist.cpp | 210 +++ .../snippets/code/src_corelib_tools_qvector.cpp | 229 --- src/corelib/global/qtypeinfo.h | 2 +- src/corelib/itemmodels/qitemselectionmodel.h | 2 +- src/corelib/kernel/qmetaobject.cpp | 2 +- src/corelib/kernel/qmetatype.h | 10 +- src/corelib/text/qbytearraylist.h | 16 +- src/corelib/text/qstring.h | 1 - src/corelib/text/qstringalgorithms.h | 3 +- src/corelib/text/qstringlist.h | 70 +- src/corelib/text/qstringtokenizer.h | 2 +- src/corelib/tools/qcontainerfwd.h | 4 +- src/corelib/tools/qlist.cpp | 8 +- src/corelib/tools/qlist.h | 756 +++++++++- src/corelib/tools/qlist.qdoc | 1472 +++++++++++++++++++ src/corelib/tools/qvector.h | 755 +--------- src/corelib/tools/qvector.qdoc | 1473 -------------------- 17 files changed, 2496 insertions(+), 2519 deletions(-) create mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp delete mode 100644 src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp create mode 100644 src/corelib/tools/qlist.qdoc delete mode 100644 src/corelib/tools/qvector.qdoc (limited to 'src/corelib') diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp new file mode 100644 index 0000000000..cdf1ae0eb1 --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +QList integerVector; +QList stringVector; +//! [0] + + +//! [1] +QList vector(200); +//! [1] + + +//! [2] +QList vector(200, "Pass"); +//! [2] + + +//! [3] +if (vector[0] == "Liz") + vector[0] = "Elizabeth"; +//! [3] + + +//! [4] +for (int i = 0; i < vector.size(); ++i) { + if (vector.at(i) == "Alfonso") + cout << "Found Alfonso at position " << i << Qt::endl; +} +//! [4] + + +//! [5] +int i = vector.indexOf("Harumi"); +if (i != -1) + cout << "First occurrence of Harumi is at position " << i << Qt::endl; +//! [5] + + +//! [6] +QList vector(10); +int *data = vector.data(); +for (int i = 0; i < 10; ++i) + data[i] = 2 * i; +//! [6] + + +//! [7] +QList vector; +vector.append("one"); +vector.append("two"); +QString three = "three"; +vector.append(three); +// vector: ["one", "two", "three"] +// three: "three" +//! [7] + + +//! [move-append] +QList vector; +vector.append("one"); +vector.append("two"); +QString three = "three"; +vector.append(std::move(three)); +// vector: ["one", "two", "three"] +// three: "" +//! [move-append] + + +//! [emplace] +QList vector{"a", "ccc"}; +vector.emplace(1, 2, 'b'); +// vector: ["a", "bb", "ccc"] +//! [emplace] + + +//! [emplace-back] +QList vector{"one", "two"}; +vector.emplaceBack(3, 'a'); +qDebug() << vector; +// vector: ["one", "two", "aaa"] +//! [emplace-back] + + +//! [emplace-back-ref] +QList vector; +auto &ref = vector.emplaceBack(); +ref = "one"; +// vector: ["one"] +//! [emplace-back-ref] + + +//! [8] +QList vector; +vector.prepend("one"); +vector.prepend("two"); +vector.prepend("three"); +// vector: ["three", "two", "one"] +//! [8] + + +//! [9] +QList vector; +vector << "alpha" << "beta" << "delta"; +vector.insert(2, "gamma"); +// vector: ["alpha", "beta", "gamma", "delta"] +//! [9] + + +//! [10] +QList vector; +vector << 2.718 << 1.442 << 0.4342; +vector.insert(1, 3, 9.9); +// vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] +//! [10] + + +//! [11] +QList vector(3); +vector.fill("Yes"); +// vector: ["Yes", "Yes", "Yes"] + +vector.fill("oh", 5); +// vector: ["oh", "oh", "oh", "oh", "oh"] +//! [11] + + +//! [12] +QList vector; +vector << "A" << "B" << "C" << "B" << "A"; +vector.indexOf("B"); // returns 1 +vector.indexOf("B", 1); // returns 1 +vector.indexOf("B", 2); // returns 3 +vector.indexOf("X"); // returns -1 +//! [12] + + +//! [13] +QList vector; +vector << "A" << "B" << "C" << "B" << "A"; +vector.lastIndexOf("B"); // returns 3 +vector.lastIndexOf("B", 3); // returns 3 +vector.lastIndexOf("B", 2); // returns 1 +vector.lastIndexOf("X"); // returns -1 +//! [13] + +//! [16] +std::vector stdvector; +vector.push_back(1.2); +vector.push_back(0.5); +vector.push_back(3.14); + +QList vector = QList::fromStdVector(stdvector); +//! [16] + + +//! [17] +QList vector; +vector << 1.2 << 0.5 << 3.14; + +std::vector stdvector = vector.toStdVector(); +//! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp deleted file mode 100644 index 76a8d68f64..0000000000 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] -QVector integerVector; -QVector stringVector; -//! [0] - - -//! [1] -QVector vector(200); -//! [1] - - -//! [2] -QVector vector(200, "Pass"); -//! [2] - - -//! [3] -if (vector[0] == "Liz") - vector[0] = "Elizabeth"; -//! [3] - - -//! [4] -for (int i = 0; i < vector.size(); ++i) { - if (vector.at(i) == "Alfonso") - cout << "Found Alfonso at position " << i << Qt::endl; -} -//! [4] - - -//! [5] -int i = vector.indexOf("Harumi"); -if (i != -1) - cout << "First occurrence of Harumi is at position " << i << Qt::endl; -//! [5] - - -//! [6] -QVector vector(10); -int *data = vector.data(); -for (int i = 0; i < 10; ++i) - data[i] = 2 * i; -//! [6] - - -//! [7] -QVector vector; -vector.append("one"); -vector.append("two"); -QString three = "three"; -vector.append(three); -// vector: ["one", "two", "three"] -// three: "three" -//! [7] - - -//! [move-append] -QVector vector; -vector.append("one"); -vector.append("two"); -QString three = "three"; -vector.append(std::move(three)); -// vector: ["one", "two", "three"] -// three: "" -//! [move-append] - - -//! [emplace] -QVector vector{"a", "ccc"}; -vector.emplace(1, 2, 'b'); -// vector: ["a", "bb", "ccc"] -//! [emplace] - - -//! [emplace-back] -QVector vector{"one", "two"}; -vector.emplaceBack(3, 'a'); -qDebug() << vector; -// vector: ["one", "two", "aaa"] -//! [emplace-back] - - -//! [emplace-back-ref] -QVector vector; -auto &ref = vector.emplaceBack(); -ref = "one"; -// vector: ["one"] -//! [emplace-back-ref] - - -//! [8] -QVector vector; -vector.prepend("one"); -vector.prepend("two"); -vector.prepend("three"); -// vector: ["three", "two", "one"] -//! [8] - - -//! [9] -QVector vector; -vector << "alpha" << "beta" << "delta"; -vector.insert(2, "gamma"); -// vector: ["alpha", "beta", "gamma", "delta"] -//! [9] - - -//! [10] -QVector vector; -vector << 2.718 << 1.442 << 0.4342; -vector.insert(1, 3, 9.9); -// vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] -//! [10] - - -//! [11] -QVector vector(3); -vector.fill("Yes"); -// vector: ["Yes", "Yes", "Yes"] - -vector.fill("oh", 5); -// vector: ["oh", "oh", "oh", "oh", "oh"] -//! [11] - - -//! [12] -QVector vector; -vector << "A" << "B" << "C" << "B" << "A"; -vector.indexOf("B"); // returns 1 -vector.indexOf("B", 1); // returns 1 -vector.indexOf("B", 2); // returns 3 -vector.indexOf("X"); // returns -1 -//! [12] - - -//! [13] -QList vector; -vector << "A" << "B" << "C" << "B" << "A"; -vector.lastIndexOf("B"); // returns 3 -vector.lastIndexOf("B", 3); // returns 3 -vector.lastIndexOf("B", 2); // returns 1 -vector.lastIndexOf("X"); // returns -1 -//! [13] - - -//! [14] -QVector vect; -vect << "red" << "green" << "blue" << "black"; - -QList list = vect.toList(); -// list: ["red", "green", "blue", "black"] -//! [14] - - -//! [15] -QStringList list; -list << "Sven" << "Kim" << "Ola"; - -QVector vect = QVector::fromList(list); -// vect: ["Sven", "Kim", "Ola"] -//! [15] - - -//! [16] -std::vector stdvector; -vector.push_back(1.2); -vector.push_back(0.5); -vector.push_back(3.14); - -QVector vector = QVector::fromStdVector(stdvector); -//! [16] - - -//! [17] -QVector vector; -vector << 1.2 << 0.5 << 3.14; - -std::vector stdvector = vector.toStdVector(); -//! [17] diff --git a/src/corelib/global/qtypeinfo.h b/src/corelib/global/qtypeinfo.h index 175d5205af..121c9fdcd8 100644 --- a/src/corelib/global/qtypeinfo.h +++ b/src/corelib/global/qtypeinfo.h @@ -182,7 +182,7 @@ public: \ }; \ } -Q_DECLARE_MOVABLE_CONTAINER(QVector); +Q_DECLARE_MOVABLE_CONTAINER(QList); Q_DECLARE_MOVABLE_CONTAINER(QQueue); Q_DECLARE_MOVABLE_CONTAINER(QStack); Q_DECLARE_MOVABLE_CONTAINER(QSet); diff --git a/src/corelib/itemmodels/qitemselectionmodel.h b/src/corelib/itemmodels/qitemselectionmodel.h index 529dc6f100..c1ce49521c 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.h +++ b/src/corelib/itemmodels/qitemselectionmodel.h @@ -223,7 +223,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags) # define Q_TEMPLATE_EXTERN extern # endif # endif -Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector; +Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList; #endif // Q_CC_MSVC class Q_CORE_EXPORT QItemSelection : public QList diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 6799057080..c20fb5b6e0 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -682,7 +682,7 @@ static void argumentTypesFromString(const char *str, const char *end, ++str; } QByteArray argType(begin, str - begin); - argType.replace("QList<", "QVector<"); + argType.replace("QVector<", "QList<"); types += QArgumentType(std::move(argType)); } } diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 196df677e9..c7e754cb5c 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -232,7 +232,7 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId(); TypeName = Id, #define QT_FOR_EACH_AUTOMATIC_TEMPLATE_1ARG(F) \ - F(QVector) \ + F(QList) \ F(QQueue) \ F(QStack) \ F(QSet) \ @@ -2014,7 +2014,7 @@ typedef QHash QVariantHash; #ifdef Q_CLANG_QDOC class QByteArrayList; #else -typedef QVector QByteArrayList; +using QByteArrayList = QList; #endif #define Q_DECLARE_METATYPE_TEMPLATE_1ARG(SINGLE_ARG_TEMPLATE) \ @@ -2515,9 +2515,9 @@ public: } #endif - if (skipToken(begin, end, "QList")) { - // Replace QList by QVector - appendStr("QVector"); + if (skipToken(begin, end, "QVector")) { + // Replace QVector by QList + appendStr("QList"); } if (skipToken(begin, end, "QPair")) { diff --git a/src/corelib/text/qbytearraylist.h b/src/corelib/text/qbytearraylist.h index d02fa6d20f..3f85a24329 100644 --- a/src/corelib/text/qbytearraylist.h +++ b/src/corelib/text/qbytearraylist.h @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include +#include #ifndef QBYTEARRAYLIST_H #define QBYTEARRAYLIST_H @@ -49,12 +49,12 @@ QT_BEGIN_NAMESPACE #if !defined(QT_NO_JAVA_STYLE_ITERATORS) -typedef QVectorIterator QByteArrayListIterator; -typedef QMutableVectorIterator QMutableByteArrayListIterator; +typedef QListIterator QByteArrayListIterator; +typedef QMutableListIterator QMutableByteArrayListIterator; #endif #ifndef Q_CLANG_QDOC -typedef QVector QByteArrayList; +typedef QList QByteArrayList; namespace QtPrivate { QByteArray Q_CORE_EXPORT QByteArrayList_join(const QByteArrayList *that, const char *separator, int separatorLength); @@ -63,14 +63,14 @@ namespace QtPrivate { #endif #ifdef Q_CLANG_QDOC -class QByteArrayList : public QVector +class QByteArrayList : public QList #else -template <> struct QVectorSpecialMethods +template <> struct QListSpecialMethods #endif { #ifndef Q_CLANG_QDOC protected: - ~QVectorSpecialMethods() = default; + ~QListSpecialMethods() = default; #endif public: inline QByteArray join() const @@ -81,7 +81,7 @@ public: { return QtPrivate::QByteArrayList_join(self(), &sep, 1); } private: - typedef QVector Self; + typedef QList Self; Self *self() { return static_cast(this); } const Self *self() const { return static_cast(this); } }; diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 77a95c6280..f14644d956 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -77,7 +77,6 @@ class QRegularExpressionMatch; class QString; class QStringList; class QStringRef; -template class QVector; namespace QtPrivate { template class BoolList; diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h index f1aa052eac..3b67739232 100644 --- a/src/corelib/text/qstringalgorithms.h +++ b/src/corelib/text/qstringalgorithms.h @@ -41,7 +41,7 @@ #define QSTRINGALGORITHMS_H #include - +#include #if 0 #pragma qt_class(QStringAlgorithms) #endif @@ -52,7 +52,6 @@ class QByteArray; class QLatin1String; class QStringView; class QChar; -template class QVector; namespace QtPrivate { diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index b94671746b..9c4daedd4b 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -38,7 +38,7 @@ ** ****************************************************************************/ -#include +#include #ifndef QSTRINGLIST_H #define QSTRINGLIST_H @@ -53,21 +53,21 @@ QT_BEGIN_NAMESPACE class QRegularExpression; #if !defined(QT_NO_JAVA_STYLE_ITERATORS) -typedef QVectorIterator QStringListIterator; -typedef QMutableVectorIterator QMutableStringListIterator; +using QStringListIterator = QListIterator; +using QMutableStringListIterator = QMutableListIterator; #endif class QStringList; #ifdef Q_QDOC -class QStringList : public QVector +class QStringList : public QList #else -template <> struct QVectorSpecialMethods +template <> struct QListSpecialMethods #endif { #ifndef Q_QDOC protected: - ~QVectorSpecialMethods() = default; + ~QListSpecialMethods() = default; #endif public: inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive); @@ -101,23 +101,23 @@ private: }; // ### Qt6: check if there's a better way -class QStringList : public QVector +class QStringList : public QList { #endif public: inline QStringList() noexcept { } inline explicit QStringList(const QString &i) { append(i); } - inline QStringList(const QVector &l) : QVector(l) { } - inline QStringList(QVector &&l) noexcept : QVector(std::move(l)) { } - inline QStringList(std::initializer_list args) : QVector(args) { } + inline QStringList(const QList &l) : QList(l) { } + inline QStringList(QList &&l) noexcept : QList(std::move(l)) { } + inline QStringList(std::initializer_list args) : QList(args) { } template = true> inline QStringList(InputIterator first, InputIterator last) - : QVector(first, last) { } + : QList(first, last) { } - QStringList &operator=(const QVector &other) - { QVector::operator=(other); return *this; } - QStringList &operator=(QVector &&other) noexcept - { QVector::operator=(std::move(other)); return *this; } + QStringList &operator=(const QList &other) + { QList::operator=(other); return *this; } + QStringList &operator=(QList &&other) noexcept + { QList::operator=(std::move(other)); return *this; } #if QT_STRINGVIEW_LEVEL < 2 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; @@ -131,7 +131,7 @@ public: { append(str); return *this; } inline QStringList &operator<<(const QStringList &l) { *this += l; return *this; } - inline QStringList &operator<<(const QVector &l) + inline QStringList &operator<<(const QList &l) { *this += l; return *this; } inline int indexOf(QStringView str, int from = 0) const; @@ -145,16 +145,16 @@ public: inline int lastIndexOf(const QRegularExpression &re, int from = -1) const; #endif // QT_CONFIG(regularexpression) - using QVector::indexOf; - using QVector::lastIndexOf; + using QList::indexOf; + using QList::lastIndexOf; }; Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE); #ifndef Q_QDOC -inline QStringList *QVectorSpecialMethods::self() +inline QStringList *QListSpecialMethods::self() { return static_cast(this); } -inline const QStringList *QVectorSpecialMethods::self() const +inline const QStringList *QListSpecialMethods::self() const { return static_cast(this); } namespace QtPrivate { @@ -190,45 +190,45 @@ namespace QtPrivate { #endif // QT_CONFIG(regularexpression) } -inline void QVectorSpecialMethods::sort(Qt::CaseSensitivity cs) +inline void QListSpecialMethods::sort(Qt::CaseSensitivity cs) { QtPrivate::QStringList_sort(self(), cs); } -inline int QVectorSpecialMethods::removeDuplicates() +inline int QListSpecialMethods::removeDuplicates() { return QtPrivate::QStringList_removeDuplicates(self()); } #if QT_STRINGVIEW_LEVEL < 2 -inline QString QVectorSpecialMethods::join(const QString &sep) const +inline QString QListSpecialMethods::join(const QString &sep) const { return QtPrivate::QStringList_join(self(), sep.constData(), sep.length()); } #endif -inline QString QVectorSpecialMethods::join(QStringView sep) const +inline QString QListSpecialMethods::join(QStringView sep) const { return QtPrivate::QStringList_join(self(), sep); } -QString QVectorSpecialMethods::join(QLatin1String sep) const +QString QListSpecialMethods::join(QLatin1String sep) const { return QtPrivate::QStringList_join(*self(), sep); } -inline QString QVectorSpecialMethods::join(QChar sep) const +inline QString QListSpecialMethods::join(QChar sep) const { return QtPrivate::QStringList_join(self(), &sep, 1); } -inline QStringList QVectorSpecialMethods::filter(QStringView str, Qt::CaseSensitivity cs) const +inline QStringList QListSpecialMethods::filter(QStringView str, Qt::CaseSensitivity cs) const { return QtPrivate::QStringList_filter(self(), str, cs); } #if QT_STRINGVIEW_LEVEL < 2 -inline QStringList QVectorSpecialMethods::filter(const QString &str, Qt::CaseSensitivity cs) const +inline QStringList QListSpecialMethods::filter(const QString &str, Qt::CaseSensitivity cs) const { return QtPrivate::QStringList_filter(self(), str, cs); } @@ -251,33 +251,33 @@ inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const return QtPrivate::QStringList_contains(this, str, cs); } -inline QStringList &QVectorSpecialMethods::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs) +inline QStringList &QListSpecialMethods::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); return *self(); } #if QT_STRINGVIEW_LEVEL < 2 -inline QStringList &QVectorSpecialMethods::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) +inline QStringList &QListSpecialMethods::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); return *self(); } -inline QStringList &QVectorSpecialMethods::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs) +inline QStringList &QListSpecialMethods::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, qToStringViewIgnoringNull(after), cs); return *self(); } -inline QStringList &QVectorSpecialMethods::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs) +inline QStringList &QListSpecialMethods::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), QStringView(before), after, cs); return *self(); } #endif -inline QStringList operator+(const QVector &one, const QStringList &other) +inline QStringList operator+(const QList &one, const QStringList &other) { QStringList n = one; n += other; @@ -305,13 +305,13 @@ inline int QStringList::lastIndexOf(QLatin1String string, int from) const } #if QT_CONFIG(regularexpression) -inline QStringList &QVectorSpecialMethods::replaceInStrings(const QRegularExpression &rx, const QString &after) +inline QStringList &QListSpecialMethods::replaceInStrings(const QRegularExpression &rx, const QString &after) { QtPrivate::QStringList_replaceInStrings(self(), rx, after); return *self(); } -inline QStringList QVectorSpecialMethods::filter(const QRegularExpression &rx) const +inline QStringList QListSpecialMethods::filter(const QRegularExpression &rx) const { return QtPrivate::QStringList_filter(self(), rx); } diff --git a/src/corelib/text/qstringtokenizer.h b/src/corelib/text/qstringtokenizer.h index 31bbbf01c8..d042fbbdab 100644 --- a/src/corelib/text/qstringtokenizer.h +++ b/src/corelib/text/qstringtokenizer.h @@ -40,11 +40,11 @@ #define QSTRINGTOKENIZER_H #include +#include QT_BEGIN_NAMESPACE template class QStringBuilder; -template class QVector; #if defined(Q_QDOC) || 1 || (defined(__cpp_range_based_for) && __cpp_range_based_for >= 201603) # define Q_STRINGTOKENIZER_USE_SENTINEL diff --git a/src/corelib/tools/qcontainerfwd.h b/src/corelib/tools/qcontainerfwd.h index fd4701a8b1..715583e03b 100644 --- a/src/corelib/tools/qcontainerfwd.h +++ b/src/corelib/tools/qcontainerfwd.h @@ -56,8 +56,8 @@ template class QQueue; template class QSet; template class QStack; template class QVarLengthArray; -template class QVector; -template using QList = QVector; +template class QList; +template using QVector = QList; QT_END_NAMESPACE diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 62201fd5d6..cdcfc30f28 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -50,8 +50,8 @@ QT_BEGIN_NAMESPACE /* ### Qt 5: ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because - ### Qt exports QPolygon and QPolygonF that inherit QVector and - ### QVector respectively. + ### Qt exports QPolygon and QPolygonF that inherit QList and + ### QList respectively. */ #if defined(Q_CC_MSVC) && defined(QT_BUILD_CORE_LIB) @@ -59,8 +59,8 @@ QT_BEGIN_INCLUDE_NAMESPACE #include QT_END_INCLUDE_NAMESPACE -template class Q_CORE_EXPORT QVector; -template class Q_CORE_EXPORT QVector; +template class Q_CORE_EXPORT QList; +template class Q_CORE_EXPORT QList; #endif QT_END_NAMESPACE diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 515fba6530..1c9a547395 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -1,6 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2019 Intel Corporation ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -40,18 +41,757 @@ #ifndef QLIST_H #define QLIST_H -#include -#include +#include +#include +#include +#include + +#include +#include +#include +#include -#if !defined(QT_NO_JAVA_STYLE_ITERATORS) QT_BEGIN_NAMESPACE + +namespace QtPrivate { + template int indexOf(const QList &list, const U &u, int from); + template int lastIndexOf(const QList &list, const U &u, int from); +} + +template struct QListSpecialMethods +{ +protected: + ~QListSpecialMethods() = default; +}; +template <> struct QListSpecialMethods; +template <> struct QListSpecialMethods; + +template +class QList +#ifndef Q_QDOC + : public QListSpecialMethods +#endif +{ + typedef QTypedArrayData Data; + typedef QArrayDataOps DataOps; + typedef QArrayDataPointer DataPointer; + class DisableRValueRefs {}; + + DataPointer d; + + template friend int QtPrivate::indexOf(const QList &list, const U &u, int from); + template friend int QtPrivate::lastIndexOf(const QList &list, const U &u, int from); + +public: + typedef T Type; + typedef T value_type; + typedef value_type *pointer; + typedef const value_type *const_pointer; + typedef value_type &reference; + typedef const value_type &const_reference; + typedef int size_type; + typedef qptrdiff difference_type; + typedef typename Data::iterator iterator; + typedef typename Data::const_iterator const_iterator; + typedef iterator Iterator; + typedef const_iterator ConstIterator; + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef typename DataPointer::parameter_type parameter_type; + using rvalue_ref = typename std::conditional::type; + +private: + void resize_internal(int i, Qt::Initialization); + bool isValidIterator(const_iterator i) const + { + const std::less less = {}; + return !less(d->end(), i) && !less(i, d->begin()); + } +public: + QList(DataPointer dd) noexcept + : d(dd) + { + } + +public: + constexpr inline QList() noexcept { } + explicit QList(int size) + : d(Data::allocate(size)) + { + if (size) + d->appendInitialize(size); + } + QList(int size, const T &t) + : d(Data::allocate(size)) + { + if (size) + d->copyAppend(size, t); + } + + inline QList(const QList &other) noexcept : d(other.d) {} + QList(QList &&other) noexcept : d(std::move(other.d)) {} + inline QList(std::initializer_list args) + : d(Data::allocate(args.size())) + { + if (args.size()) + d->copyAppend(args.begin(), args.end()); + } + + ~QList() /*noexcept(std::is_nothrow_destructible::value)*/ {} + QList &operator=(const QList &other) { d = other.d; return *this; } + QList &operator=(QList &&other) noexcept(std::is_nothrow_destructible::value) + { + d = std::move(other.d); + return *this; + } + QList &operator=(std::initializer_list args) + { + d = DataPointer(Data::allocate(args.size())); + if (args.size()) + d->copyAppend(args.begin(), args.end()); + return *this; + } + template = true> + QList(InputIterator i1, InputIterator i2) + : d(Data::allocate(std::distance(i1, i2))) + { + if (std::distance(i1, i2)) + d->copyAppend(i1, i2); + } + + template = true> + QList(InputIterator i1, InputIterator i2) + : QList() + { + QtPrivate::reserveIfForwardIterator(this, i1, i2); + std::copy(i1, i2, std::back_inserter(*this)); + } + + void swap(QList &other) noexcept { qSwap(d, other.d); } + + friend bool operator==(const QList &l, const QList &r) + { + if (l.size() != r.size()) + return false; + if (l.begin() == r.begin()) + return true; + + // do element-by-element comparison + return l.d->compare(l.begin(), r.begin(), l.size()); + } + friend bool operator!=(const QList &l, const QList &r) + { + return !(l == r); + } + + int size() const noexcept { return int(d->size); } + int count() const noexcept { return size(); } + int length() const noexcept { return size(); } + + inline bool isEmpty() const noexcept { return d->size == 0; } + + void resize(int size) + { + resize_internal(size, Qt::Uninitialized); + if (size > this->size()) + d->appendInitialize(size); + } + void resize(int size, parameter_type c) + { + resize_internal(size, Qt::Uninitialized); + if (size > this->size()) + d->copyAppend(size - this->size(), c); + } + + inline int capacity() const { return int(d->constAllocatedCapacity()); } + void reserve(int size); + inline void squeeze(); + + void detach() { d.detach(); } + bool isDetached() const noexcept { return !d->isShared(); } + + inline bool isSharedWith(const QList &other) const { return d == other.d; } + + pointer data() { detach(); return d->data(); } + const_pointer data() const noexcept { return d->data(); } + const_pointer constData() const noexcept { return d->data(); } + void clear() { + if (!size()) + return; + if (d->needsDetach()) { + // must allocate memory + DataPointer detached(Data::allocate(d.allocatedCapacity(), d->detachFlags())); + d.swap(detached); + } else { + d->truncate(0); + } + } + + const_reference at(int i) const noexcept + { + Q_ASSERT_X(size_t(i) < size_t(d->size), "QList::at", "index out of range"); + return data()[i]; + } + reference operator[](int i) + { + Q_ASSERT_X(size_t(i) < size_t(d->size), "QList::operator[]", "index out of range"); + detach(); + return data()[i]; + } + const_reference operator[](int i) const noexcept { return at(i); } + void append(const_reference t) + { append(const_iterator(std::addressof(t)), const_iterator(std::addressof(t)) + 1); } + void append(const_iterator i1, const_iterator i2); + void append(rvalue_ref t) { emplaceBack(std::move(t)); } + void append(const QList &l) { append(l.constBegin(), l.constEnd()); } + void prepend(rvalue_ref t); + void prepend(const T &t); + + template + reference emplaceBack(Args&&... args) { return *emplace(count(), std::forward(args)...); } + + iterator insert(int i, parameter_type t) + { return insert(i, 1, t); } + iterator insert(int i, int n, parameter_type t); + iterator insert(const_iterator before, parameter_type t) + { + Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); + return insert(before, 1, t); + } + iterator insert(const_iterator before, int n, parameter_type t) + { + Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); + return insert(std::distance(constBegin(), before), n, t); + } + iterator insert(const_iterator before, rvalue_ref t) + { + Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); + return insert(std::distance(constBegin(), before), std::move(t)); + } + iterator insert(int i, rvalue_ref t) { return emplace(i, std::move(t)); } + + template + iterator emplace(const_iterator before, Args&&... args) + { + Q_ASSERT_X(isValidIterator(before), "QList::emplace", "The specified iterator argument 'before' is invalid"); + return emplace(std::distance(constBegin(), before), std::forward(args)...); + } + + template + iterator emplace(int i, Args&&... args); +#if 0 + template< class InputIt > + iterator insert( const_iterator pos, InputIt first, InputIt last ); + iterator insert( const_iterator pos, std::initializer_list ilist ); +#endif + void replace(int i, const T &t) + { + Q_ASSERT_X(i >= 0 && i < d->size, "QList::replace", "index out of range"); + const T copy(t); + data()[i] = copy; + } + void replace(int i, rvalue_ref t) + { + Q_ASSERT_X(i >= 0 && i < d->size, "QList::replace", "index out of range"); + const T copy(std::move(t)); + data()[i] = std::move(copy); + } + + void remove(int i, int n = 1); + void removeFirst() { Q_ASSERT(!isEmpty()); remove(0); } + void removeLast() { Q_ASSERT(!isEmpty()); remove(size() - 1); } + value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); remove(0); return v; } + value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); remove(size() - 1); return v; } + + QList &fill(parameter_type t, int size = -1); + + int indexOf(const T &t, int from = 0) const noexcept; + int lastIndexOf(const T &t, int from = -1) const noexcept; + bool contains(const T &t) const noexcept + { + return indexOf(t) != -1; + } + int count(const T &t) const noexcept + { + return int(std::count(&*cbegin(), &*cend(), t)); + } + + // QList compatibility + void removeAt(int i) { remove(i); } + int removeAll(const T &t) + { + const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); + if (cit == ce) + return 0; + int index = cit - this->cbegin(); + // next operation detaches, so ce, cit, t may become invalidated: + const T tCopy = t; + const iterator e = end(), it = std::remove(begin() + index, e, tCopy); + const int result = std::distance(it, e); + erase(it, e); + return result; + } + bool removeOne(const T &t) + { + const int i = indexOf(t); + if (i < 0) + return false; + remove(i); + return true; + } + T takeAt(int i) { T t = std::move((*this)[i]); remove(i); return t; } + void move(int from, int to) + { + Q_ASSERT_X(from >= 0 && from < size(), "QList::move(int,int)", "'from' is out-of-range"); + Q_ASSERT_X(to >= 0 && to < size(), "QList::move(int,int)", "'to' is out-of-range"); + if (from == to) // don't detach when no-op + return; + detach(); + T * const b = d->begin(); + if (from < to) + std::rotate(b + from, b + from + 1, b + to + 1); + else + std::rotate(b + to, b + from, b + from + 1); + } + + // STL-style + iterator begin() { detach(); return d->begin(); } + iterator end() { detach(); return d->end(); } + + const_iterator begin() const noexcept { return d->constBegin(); } + const_iterator end() const noexcept { return d->constEnd(); } + const_iterator cbegin() const noexcept { return d->constBegin(); } + const_iterator cend() const noexcept { return d->constEnd(); } + const_iterator constBegin() const noexcept { return d->constBegin(); } + const_iterator constEnd() const noexcept { return d->constEnd(); } + reverse_iterator rbegin() { return reverse_iterator(end()); } + reverse_iterator rend() { return reverse_iterator(begin()); } + const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } + const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } + + iterator erase(const_iterator begin, const_iterator end); + inline iterator erase(const_iterator pos) { return erase(pos, pos+1); } + + // more Qt + inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); } + inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); } + inline const T &constFirst() const { Q_ASSERT(!isEmpty()); return *begin(); } + inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); } + inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); } + inline const T &constLast() const { Q_ASSERT(!isEmpty()); return *(end()-1); } + inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; } + inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; } + QList mid(int pos, int len = -1) const; + + T value(int i) const { return value(i, T()); } + T value(int i, const T &defaultValue) const; + + void swapItemsAt(int i, int j) { + Q_ASSERT_X(i >= 0 && i < size() && j >= 0 && j < size(), + "QList::swap", "index out of range"); + detach(); + qSwap(d->begin()[i], d->begin()[j]); + } + + // STL compatibility + inline void push_back(const T &t) { append(t); } + void push_back(rvalue_ref t) { append(std::move(t)); } + void push_front(rvalue_ref t) { prepend(std::move(t)); } + inline void push_front(const T &t) { prepend(t); } + void pop_back() { removeLast(); } + void pop_front() { removeFirst(); } + + template + reference emplace_back(Args&&... args) { return emplaceBack(std::forward(args)...); } + + inline bool empty() const + { return d->size == 0; } + inline reference front() { return first(); } + inline const_reference front() const { return first(); } + inline reference back() { return last(); } + inline const_reference back() const { return last(); } + void shrink_to_fit() { squeeze(); } + + // comfort + QList &operator+=(const QList &l) { append(l.cbegin(), l.cend()); return *this; } + inline QList operator+(const QList &l) const + { QList n = *this; n += l; return n; } + inline QList &operator+=(const T &t) + { append(t); return *this; } + inline QList &operator<< (const T &t) + { append(t); return *this; } + inline QList &operator<<(const QList &l) + { *this += l; return *this; } + inline QList &operator+=(rvalue_ref t) + { append(std::move(t)); return *this; } + inline QList &operator<<(rvalue_ref t) + { append(std::move(t)); return *this; } + + // Consider deprecating in 6.4 or later + static QList fromList(const QList &list) { return list; } + QList toList() const { return *this; } + + static inline QList fromVector(const QList &vector) { return vector; } + inline QList toVector() const { return *this; } +}; + +#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 +template ::value_type, + QtPrivate::IfIsInputIterator = true> +QList(InputIterator, InputIterator) -> QList; +#endif + +template +inline void QList::resize_internal(int newSize, Qt::Initialization) +{ + Q_ASSERT(newSize >= 0); + + if (d->needsDetach() || newSize > capacity()) { + // must allocate memory + DataPointer detached(Data::allocate(d->detachCapacity(newSize), + d->detachFlags())); + if (size() && newSize) { + detached->copyAppend(constBegin(), constBegin() + qMin(newSize, size())); + } + d.swap(detached); + } + + if (newSize < size()) + d->truncate(newSize); +} + +template +void QList::reserve(int asize) +{ + // capacity() == 0 for immutable data, so this will force a detaching below + if (asize <= capacity()) { + if (d->flags() & Data::CapacityReserved) + return; // already reserved, don't shrink + if (!d->isShared()) { + // accept current allocation, don't shrink + d->flags() |= Data::CapacityReserved; + return; + } + } + + DataPointer detached(Data::allocate(qMax(asize, size()), + d->detachFlags() | Data::CapacityReserved)); + detached->copyAppend(constBegin(), constEnd()); + d.swap(detached); +} + +template +inline void QList::squeeze() +{ + if (d->needsDetach() || size() != capacity()) { + // must allocate memory + DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved)); + if (size()) { + detached->copyAppend(constBegin(), constEnd()); + } + d.swap(detached); + } +} + +template +inline void QList::remove(int i, int n) +{ + Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range"); + Q_ASSERT_X(n >= 0, "QList::remove", "invalid count"); + + if (n == 0) + return; + + const size_t newSize = size() - n; + if (d->needsDetach() || + ((d->flags() & Data::CapacityReserved) == 0 + && newSize < d->allocatedCapacity()/2)) { + // allocate memory + DataPointer detached(Data::allocate(d->detachCapacity(newSize), + d->detachFlags() & ~(Data::GrowsBackwards | Data::GrowsForward))); + const_iterator where = constBegin() + i; + if (newSize) { + detached->copyAppend(constBegin(), where); + detached->copyAppend(where + n, constEnd()); + } + d.swap(detached); + } else { + // we're detached and we can just move data around + d->erase(d->begin() + i, d->begin() + i + n); + } +} + +template +inline void QList::prepend(const T &t) +{ insert(0, 1, t); } +template +void QList::prepend(rvalue_ref t) +{ insert(0, std::move(t)); } + template -using QMutableListIterator = QMutableVectorIterator; -template -using QListIterator = QVectorIterator; -QT_END_NAMESPACE +inline T QList::value(int i, const T &defaultValue) const +{ + return size_t(i) < size_t(d->size) ? at(i) : defaultValue; +} + +template +inline void QList::append(const_iterator i1, const_iterator i2) +{ + if (i1 == i2) + return; + const size_t newSize = size() + std::distance(i1, i2); + if (d->needsDetach() || newSize > d->allocatedCapacity()) { + DataPointer detached(Data::allocate(d->detachCapacity(newSize), + d->detachFlags() | Data::GrowsForward)); + detached->copyAppend(constBegin(), constEnd()); + detached->copyAppend(i1, i2); + d.swap(detached); + } else { + // we're detached and we can just move data around + d->copyAppend(i1, i2); + } +} + +template +inline typename QList::iterator +QList::insert(int i, int n, parameter_type t) +{ + Q_ASSERT_X(size_t(i) <= size_t(d->size), "QList::insert", "index out of range"); + + // we don't have a quick exit for n == 0 + // it's not worth wasting CPU cycles for that + + const size_t newSize = size() + n; + if (d->needsDetach() || newSize > d->allocatedCapacity()) { + typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; + if (size_t(i) <= newSize / 4) + flags |= Data::GrowsBackwards; + + DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags)); + const_iterator where = constBegin() + i; + detached->copyAppend(constBegin(), where); + detached->copyAppend(n, t); + detached->copyAppend(where, constEnd()); + d.swap(detached); + } else { + // we're detached and we can just move data around + if (i == size()) { + d->copyAppend(n, t); + } else { + T copy(t); + d->insert(d.begin() + i, n, copy); + } + } + return d.begin() + i; +} + +template +template +typename QList::iterator +QList::emplace(int i, Args&&... args) +{ + Q_ASSERT_X(i >= 0 && i <= d->size, "QList::insert", "index out of range"); + + const size_t newSize = size() + 1; + if (d->needsDetach() || newSize > d->allocatedCapacity()) { + typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; + if (size_t(i) <= newSize / 4) + flags |= Data::GrowsBackwards; + + DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags)); + const_iterator where = constBegin() + i; + + // First, create an element to handle cases, when a user moves + // the element from a container to the same container + detached->createInPlace(detached.begin() + i, std::forward(args)...); + + // Then, put the first part of the elements to the new location + detached->copyAppend(constBegin(), where); + + // After that, increase the actual size, because we created + // one extra element + ++detached.size; + + // Finally, put the rest of the elements to the new location + detached->copyAppend(where, constEnd()); + + d.swap(detached); + } else { + d->emplace(d.begin() + i, std::forward(args)...); + } + return d.begin() + i; +} + +template +typename QList::iterator QList::erase(const_iterator abegin, const_iterator aend) +{ + Q_ASSERT_X(isValidIterator(abegin), "QList::erase", "The specified iterator argument 'abegin' is invalid"); + Q_ASSERT_X(isValidIterator(aend), "QList::erase", "The specified iterator argument 'aend' is invalid"); + Q_ASSERT(aend >= abegin); + + int i = std::distance(d.constBegin(), abegin); + int n = std::distance(abegin, aend); + remove(i, n); + + return d.begin() + i; +} + +template +inline QList &QList::fill(parameter_type t, int newSize) +{ + if (newSize == -1) + newSize = size(); + if (d->needsDetach() || newSize > capacity()) { + // must allocate memory + DataPointer detached(Data::allocate(d->detachCapacity(newSize), + d->detachFlags())); + detached->copyAppend(newSize, t); + d.swap(detached); + } else { + // we're detached + const T copy(t); + d->assign(d.begin(), d.begin() + qMin(size(), newSize), t); + if (newSize > size()) + d->copyAppend(newSize - size(), copy); + } + return *this; +} + +namespace QtPrivate { +template +int indexOf(const QList &vector, const U &u, int from) +{ + if (from < 0) + from = qMax(from + vector.size(), 0); + if (from < vector.size()) { + auto n = vector.begin() + from - 1; + auto e = vector.end(); + while (++n != e) + if (*n == u) + return int(n - vector.begin()); + } + return -1; +} + +template +int lastIndexOf(const QList &vector, const U &u, int from) +{ + if (from < 0) + from += vector.d->size; + else if (from >= vector.size()) + from = vector.size() - 1; + if (from >= 0) { + auto b = vector.begin(); + auto n = vector.begin() + from + 1; + while (n != b) { + if (*--n == u) + return int(n - b); + } + } + return -1; +} +} + +template +int QList::indexOf(const T &t, int from) const noexcept +{ + return QtPrivate::indexOf(*this, t, from); +} + +template +int QList::lastIndexOf(const T &t, int from) const noexcept +{ + return QtPrivate::lastIndexOf(*this, t, from); +} + +template +inline QList QList::mid(int pos, int len) const +{ + qsizetype p = pos; + qsizetype l = len; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(d.size, &p, &l)) { + case QContainerImplHelper::Null: + case QContainerImplHelper::Empty: + return QList(); + case QContainerImplHelper::Full: + return *this; + case QContainerImplHelper::Subset: + break; + } + + // Allocate memory + DataPointer copied(Data::allocate(l)); + copied->copyAppend(constBegin() + p, constBegin() + p + l); + return copied; +} + +Q_DECLARE_SEQUENTIAL_ITERATOR(List) +Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) + +template +size_t qHash(const QList &key, size_t seed = 0) + noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) +{ + return qHashRange(key.cbegin(), key.cend(), seed); +} + +template +auto operator<(const QList &lhs, const QList &rhs) + noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), + rhs.begin(), rhs.end()))) + -> decltype(std::declval() < std::declval()) +{ + return std::lexicographical_compare(lhs.begin(), lhs.end(), + rhs.begin(), rhs.end()); +} + +template +auto operator>(const QList &lhs, const QList &rhs) + noexcept(noexcept(lhs < rhs)) + -> decltype(lhs < rhs) +{ + return rhs < lhs; +} + +template +auto operator<=(const QList &lhs, const QList &rhs) + noexcept(noexcept(lhs < rhs)) + -> decltype(lhs < rhs) +{ + return !(lhs > rhs); +} + +template +auto operator>=(const QList &lhs, const QList &rhs) + noexcept(noexcept(lhs < rhs)) + -> decltype(lhs < rhs) +{ + return !(lhs < rhs); +} + +/* + ### Qt 5: + ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because + ### Qt exports QPolygon and QPolygonF that inherit QList and + ### QList respectively. +*/ + +#if defined(Q_CC_MSVC) && !defined(QT_BUILD_CORE_LIB) +QT_BEGIN_INCLUDE_NAMESPACE +#include +QT_END_INCLUDE_NAMESPACE +extern template class Q_CORE_EXPORT QList; +extern template class Q_CORE_EXPORT QList; #endif +QList QStringView::toUcs4() const { return QtPrivate::convertToUcs4(*this); } + +QT_END_NAMESPACE + #include #include diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc new file mode 100644 index 0000000000..c0af194858 --- /dev/null +++ b/src/corelib/tools/qlist.qdoc @@ -0,0 +1,1472 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \class QVector + \inmodule QtCore + \brief QVector is an alias for QList. + + Please see the QList documentation for details. +*/ + +/*! + \class QList + \inmodule QtCore + \brief The QList class is a template class that provides a dynamic array. + + \ingroup tools + \ingroup shared + + \reentrant + + QList\ is one of Qt's generic \l{container classes}. It + stores its items in adjacent memory locations and provides fast + index-based access. QVector\ used to be a different class in + Qt 5, but is now a simple alias to QList. + + QList\ and QVarLengthArray\ + provide similar APIs and functionality. They are often interchangeable, + but there are performance consequences. Here is an overview of use cases: + + \list + \li QList should be your default first choice. + \li QVarLengthArray provides an array that reserves space on the stack, + but can dynamically grow onto the heap if required. It's good to + use for short lived containers that are usually small. + \li If you need a real linked list, which guarantees + \l{Algorithmic Complexity}{constant time} insertions mid-list and + uses iterators to items rather than indexes, use std::list. + \endlist + + \note QList and QVarLengthArray both guarantee C-compatible + array layout. + \note QList in Qt 5 did not always have a C-compatible array layout and + we often recommended to use QVector instead for more predictable + performance. This is not the case in Qt 6 anymore, where both classes + now share an implementation and can be used interchangeably. + + Here's an example of a QList that stores integers and a QList + that stores QString values: + + \snippet code/src_corelib_tools_qlist.cpp 0 + + QList stores its items in a vector (array). Typically, vectors + are created with an initial size. For example, the following code + constructs a QList with 200 elements: + + \snippet code/src_corelib_tools_qlist.cpp 1 + + The elements are automatically initialized with a + \l{default-constructed value}. If you want to initialize the + vector with a different value, pass that value as the second + argument to the constructor: + + \snippet code/src_corelib_tools_qlist.cpp 2 + + You can also call fill() at any time to fill the vector with a + value. + + QList uses 0-based indexes, just like C++ arrays. To access the + item at a particular index position, you can use operator[](). On + non-const vectors, operator[]() returns a reference to the item + that can be used on the left side of an assignment: + + \snippet code/src_corelib_tools_qlist.cpp 3 + + For read-only access, an alternative syntax is to use at(): + + \snippet code/src_corelib_tools_qlist.cpp 4 + + at() can be faster than operator[](), because it never causes a + \l{deep copy} to occur. + + Another way to access the data stored in a QList is to call + data(). The function returns a pointer to the first item in the + vector. You can use the pointer to directly access and modify the + elements stored in the vector. The pointer is also useful if you + need to pass a QList to a function that accepts a plain C++ + array. + + If you want to find all occurrences of a particular value in a + vector, use indexOf() or lastIndexOf(). The former searches + forward starting from a given index position, the latter searches + backward. Both return the index of the matching item if they found + one; otherwise, they return -1. For example: + + \snippet code/src_corelib_tools_qlist.cpp 5 + + If you simply want to check whether a vector contains a + particular value, use contains(). If you want to find out how + many times a particular value occurs in the vector, use count(). + + QList provides these basic functions to add, move, and remove + items: insert(), replace(), remove(), prepend(), append(). With + the exception of append() and replace(), these functions can be slow + (\l{linear time}) for large vectors, because they require moving many + items in the vector by one position in memory. If you want a container + class that provides fast insertion/removal in the middle, use + std::list instead. + + Unlike plain C++ arrays, QLists can be resized at any time by + calling resize(). If the new size is larger than the old size, + QList might need to reallocate the whole vector. QList tries + to reduce the number of reallocations by preallocating up to twice + as much memory as the actual data needs. + + If you know in advance approximately how many items the QList + will contain, you can call reserve(), asking QList to + preallocate a certain amount of memory. You can also call + capacity() to find out how much memory QList actually + allocated. + + Note that using non-const operators and functions can cause + QList to do a deep copy of the data. This is due to \l{implicit sharing}. + + QList's value type must be an \l{assignable data type}. This + covers most data types that are commonly used, but the compiler + won't let you, for example, store a QWidget as a value; instead, + store a QWidget *. A few functions have additional requirements; + for example, indexOf() and lastIndexOf() expect the value type to + support \c operator==(). These requirements are documented on a + per-function basis. + + Like the other container classes, QList provides \l{Java-style + iterators} (QListIterator and QMutableVectorIterator) and + \l{STL-style iterators} (QList::const_iterator and + QList::iterator). In practice, these are rarely used, because + you can use indexes into the QList. + + In addition to QList, Qt also provides QVarLengthArray, a very + low-level class with little functionality that is optimized for + speed. + + QList does \e not support inserting, prepending, appending or replacing + with references to its own values. Doing so will cause your application to + abort with an error message. + + \section2 More Information on Using Qt Containers + + For a detailed discussion comparing Qt containers with each other and + with STL containers, see \l {Understand the Qt Containers}. + + \section1 Maximum size and out-of-memory conditions + + The current version of QList is limited to just under 2 GB (2^31 bytes) + in size. The exact value is architecture-dependent, since it depends on the + overhead required for managing the data block, but is no more than 32 + bytes. The number of elements that can be stored in a QList is that size + divided by the size of each element. + + In case memory allocation fails, QList will use the \l Q_CHECK_PTR macro, + which will throw a \c std::bad_alloc exception if the application is being + compiled with exception support. If exceptions are disabled, then running + out of memory is undefined behavior. + + Note that the operating system may impose further limits on applications + holding a lot of allocated memory, especially large, contiguous blocks. + Such considerations, the configuration of such behavior or any mitigation + are outside the scope of the Qt API. +*/ + +/*! + \fn template QList QList::mid(int pos, int length = -1) const + + Returns a sub-vector which contains elements from this vector, + starting at position \a pos. If \a length is -1 (the default), all + elements after \a pos are included; otherwise \a length elements (or + all remaining elements if there are less than \a length elements) + are included. +*/ + + +/*! \fn template QList::QList() + + Constructs an empty vector. + + \sa resize() +*/ + +/*! + \fn template QList::QList(QList &&other) + + Move-constructs a QList instance, making it point at the same + object that \a other was pointing to. + + \since 5.2 +*/ + +/*! \fn template QList::QList(int size) + + Constructs a vector with an initial size of \a size elements. + + The elements are initialized with a \l{default-constructed + value}. + + \sa resize() +*/ + +/*! \fn template QList::QList(int size, const T &value) + + Constructs a vector with an initial size of \a size elements. + Each element is initialized with \a value. + + \sa resize(), fill() +*/ + +/*! \fn template QList::QList(const QList &other) + + Constructs a copy of \a other. + + This operation takes \l{Algorithmic Complexity}{constant time}, + because QList is \l{implicitly shared}. This makes returning + a QList from a function very fast. If a shared instance is + modified, it will be copied (copy-on-write), and that takes + \l{Algorithmic Complexity}{linear time}. + + \sa operator=() +*/ + +/*! \fn template QList::QList(std::initializer_list args) + \since 4.8 + + Constructs a vector from the std::initializer_list given by \a args. + + This constructor is only enabled if the compiler supports C++11 initializer + lists. +*/ + +/*! \fn template template QList::QList(InputIterator first, InputIterator last) + \since 5.14 + + Constructs a vector with the contents in the iterator range [\a first, \a last). + + The value type of \c InputIterator must be convertible to \c T. +*/ + +/*! + \fn template QList::QList(QArrayDataPointerRef ref) + \internal +*/ + +/*! \fn template QList::~QList() + + Destroys the list. +*/ + +/*! \fn template QList &QList::operator=(const QList &other) + + Assigns \a other to this vector and returns a reference to this + vector. +*/ + +/*! + \fn template QList &QList::operator=(QList &&other) + + Move-assigns \a other to this QList instance. + + \since 5.2 +*/ + +/*! + \fn template QList &QList::operator=(std::initializer_list args) + + Assigns the collection of values in \a args to this QList instance. + + This operator is only enabled if the compiler supports C++11 initializer + lists. + + \since 5.14 +*/ + +/*! \fn template void QList::swap(QList &other) + \since 4.8 + + Swaps vector \a other with this vector. This operation is very fast and + never fails. +*/ + +/*! \fn template void QList::swapItemsAt(int i, int j) + \since 5.14 + + Exchange the item at index position \a i with the item at index + position \a j. This function assumes that both \a i and \a j are + at least 0 but less than size(). To avoid failure, test that both + \a i and \a j are at least 0 and less than size(). +*/ + + +/*! \fn template bool QList::operator==(const QList &other) const + + Returns \c true if \a other is equal to this vector; otherwise + returns \c false. + + Two vectors are considered equal if they contain the same values + in the same order. + + This function requires the value type to have an implementation + of \c operator==(). + + \sa operator!=() +*/ + +/*! \fn template bool QList::operator!=(const QList &other) const + + Returns \c true if \a other is not equal to this vector; otherwise + returns \c false. + + Two vectors are considered equal if they contain the same values + in the same order. + + This function requires the value type to have an implementation + of \c operator==(). + + \sa operator==() +*/ + +/*! \fn template bool operator<(const QList &lhs, const QList &rhs) + \since 5.6 + \relates QList + + Returns \c true if vector \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically less than} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn template bool operator<=(const QList &lhs, const QList &rhs) + \since 5.6 + \relates QList + + Returns \c true if vector \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically less than or equal to} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn template bool operator>(const QList &lhs, const QList &rhs) + \since 5.6 + \relates QList + + Returns \c true if vector \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically greater than} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! \fn template bool operator>=(const QList &lhs, const QList &rhs) + \since 5.6 + \relates QList + + Returns \c true if vector \a lhs is + \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} + {lexicographically greater than or equal to} \a rhs; otherwise returns \c false. + + This function requires the value type to have an implementation + of \c operator<(). +*/ + +/*! + \fn template size_t qHash(const QList &key, size_t seed = 0) + \since 5.6 + \relates QList + + Returns the hash value for \a key, + using \a seed to seed the calculation. + + This function requires qHash() to be overloaded for the value type \c T. +*/ + +/*! \fn template int QList::size() const + + Returns the number of items in the vector. + + \sa isEmpty(), resize() +*/ + +/*! \fn template bool QList::isEmpty() const + + Returns \c true if the vector has size 0; otherwise returns \c false. + + \sa size(), resize() +*/ + +/*! \fn template void QList::resize(int size) + + Sets the size of the vector to \a size. If \a size is greater than the + current size, elements are added to the end; the new elements are + initialized with a \l{default-constructed value}. If \a size is less + than the current size, elements are removed from the end. + + Since Qt 5.6, resize() doesn't shrink the capacity anymore. + To shed excess capacity, use squeeze(). + + \sa size() +*/ + +/*! \fn template int QList::capacity() const + + Returns the maximum number of items that can be stored in the + vector without forcing a reallocation. + + The sole purpose of this function is to provide a means of fine + tuning QList's memory usage. In general, you will rarely ever + need to call this function. If you want to know how many items are + in the vector, call size(). + + \note a statically allocated vector will report a capacity of 0, + even if it's not empty. + + \sa reserve(), squeeze() +*/ + +/*! \fn template void QList::reserve(int size) + + Attempts to allocate memory for at least \a size elements. If you + know in advance how large the vector will be, you should call this + function to prevent reallocations and memory fragmentation. + + If \a size is an underestimate, the worst that will happen is that + the QList will be a bit slower. If \a size is an overestimate, you + may have used more memory than the normal QList growth strategy + would have allocated—or you may have used less. + + An alternative to reserve() is calling resize(). Whether or not that is + faster than reserve() depends on the element type, because resize() + default-constructs all elements, and requires assignment to existing + entries rather than calling append(), which copy- or move-constructs. + For simple types, like \c int or \c double, resize() is typically faster, + but for anything more complex, you should prefer reserve(). + + \warning If the size passed to resize() was underestimated, you run out + of allocated space and into undefined behavior. This problem does not + exist with reserve(), because it treats the size as just a hint. + + \sa squeeze(), capacity() +*/ + +/*! \fn template void QList::squeeze() + + Releases any memory not required to store the items. + + The sole purpose of this function is to provide a means of fine + tuning QList's memory usage. In general, you will rarely ever + need to call this function. + + \sa reserve(), capacity() +*/ + +/*! \fn template void QList::detach() + + \internal +*/ + +/*! \fn template bool QList::isDetached() const + + \internal +*/ + +/*! \fn template void QList::setSharable(bool sharable) + + \internal +*/ + +/*! \fn template bool QList::isSharedWith(const QList &other) const + + \internal +*/ + +/*! \fn template T *QList::data() + + Returns a pointer to the data stored in the vector. The pointer + can be used to access and modify the items in the vector. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 6 + + The pointer remains valid as long as the vector isn't + reallocated. + + This function is mostly useful to pass a vector to a function + that accepts a plain C++ array. + + \sa constData(), operator[]() +*/ + +/*! \fn template const T *QList::data() const + + \overload +*/ + +/*! \fn template const T *QList::constData() const + + Returns a const pointer to the data stored in the vector. The + pointer can be used to access the items in the vector. + The pointer remains valid as long as the vector isn't + reallocated. + + This function is mostly useful to pass a vector to a function + that accepts a plain C++ array. + + \sa data(), operator[]() +*/ + +/*! \fn template void QList::clear() + + Removes all the elements from the vector. + + \note Until Qt 5.6, this also released the memory used by + the vector. From Qt 5.7, the capacity is preserved. To shed + all capacity, swap with a default-constructed vector: + \code + QList v ...; + QList().swap(v); + Q_ASSERT(v.capacity() == 0); + \endcode + or call squeeze(). + + \sa squeeze() +*/ + +/*! \fn template const T &QList::at(int i) const + + Returns the item at index position \a i in the vector. + + \a i must be a valid index position in the vector (i.e., 0 <= \a + i < size()). + + \sa value(), operator[]() +*/ + +/*! \fn template T &QList::operator[](int i) + + Returns the item at index position \a i as a modifiable reference. + + \a i must be a valid index position in the vector (i.e., 0 <= \a i + < size()). + + Note that using non-const operators can cause QList to do a deep + copy. + + \sa at(), value() +*/ + +/*! \fn template const T &QList::operator[](int i) const + + \overload + + Same as at(\a i). +*/ + +/*! + \fn template void QList::append(const T &value) + + Inserts \a value at the end of the vector. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 7 + + This is the same as calling resize(size() + 1) and assigning \a + value to the new last element in the vector. + + This operation is relatively fast, because QList typically + allocates more memory than necessary, so it can grow without + reallocating the entire vector each time. + + \sa operator<<(), prepend(), insert() +*/ + +/*! + \fn template void QList::append(T &&value) + \since 5.6 + + \overload + + Example: + \snippet code/src_corelib_tools_qlist.cpp move-append +*/ + +/*! \fn template void QList::append(const QList &value) + + \overload + + \since 5.5 + + Appends the items of the \a value vector to this vector. + + \sa operator<<(), operator+=() +*/ + + +/*! + \fn template void QList::prepend(const T &value) + \fn template void QList::prepend(T &&value) + + Inserts \a value at the beginning of the vector. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 8 + + This is the same as vector.insert(0, \a value). + + For large vectors, this operation can be slow (\l{linear time}), + because it requires moving all the items in the vector by one + position further in memory. If you want a container class that + provides a fast prepend operation, use std::list + instead. + + \sa append(), insert() +*/ + +/*! + \fn template template T &QList::emplaceBack(Args&&... args) + \fn template template T &QList::emplace_back(Args&&... args) + + Adds a new element to the end for the container. This new element + is constructed in-place using \a args as the arguments for its + construction. + + Returns a reference to the new element. + + Example: + \snippet code/src_corelib_tools_qlist.cpp emplace-back + + It is also possible to access a newly created object by using + returned reference: + \snippet code/src_corelib_tools_qlist.cpp emplace-back-ref + + This is the same as vector.emplace(vector.size(), \a args). + + \sa emplace +*/ + +/*! \fn template void QList::insert(int i, const T &value) + \fn template void QList::insert(int i, T &&value) + + Inserts \a value at index position \a i in the vector. If \a i is + 0, the value is prepended to the vector. If \a i is size(), the + value is appended to the vector. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 9 + + For large vectors, this operation can be slow (\l{linear time}), + because it requires moving all the items at indexes \a i and + above by one position further in memory. If you want a container + class that provides a fast insert() function, use std::list + instead. + + \sa append(), prepend(), remove() +*/ + +/*! \fn template void QList::insert(int i, int count, const T &value) + + \overload + + Inserts \a count copies of \a value at index position \a i in the + vector. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 10 +*/ + +/*! + \fn template QList::iterator QList::insert(iterator before, const T &value) + \fn template QList::iterator QList::insert(iterator before, T &&value) + + \overload + + Inserts \a value in front of the item pointed to by the iterator + \a before. Returns an iterator pointing at the inserted item. +*/ + +/*! \fn template QList::iterator QList::insert(iterator before, int count, const T &value) + + Inserts \a count copies of \a value in front of the item pointed to + by the iterator \a before. Returns an iterator pointing at the + first of the inserted items. +*/ + +/*! + \fn template template QList::iterator QList::emplace(int i, Args&&... args) + + Extends the container by inserting a new element at position \a i. + This new element is constructed in-place using \a args as the + arguments for its construction. + + Returns an iterator to the new element. + + Example: + \snippet code/src_corelib_tools_qlist.cpp emplace + + \note It is garanteed that the element will be created in place + at the beginning, but after that it might be copied or + moved to the right position. + + \sa emplaceBack +*/ + + +/*! \fn template void QList::replace(int i, const T &value) + + Replaces the item at index position \a i with \a value. + + \a i must be a valid index position in the vector (i.e., 0 <= \a + i < size()). + + \sa operator[](), remove() +*/ + +/*! \fn template void QList::remove(int i) + + \overload + + Removes the element at index position \a i. + + \sa insert(), replace(), fill() +*/ + +/*! \fn template void QList::remove(int i, int count) + + \overload + + Removes \a count elements from the middle of the vector, starting at + index position \a i. + + \sa insert(), replace(), fill() +*/ + +/*! \fn template void QList::removeAt(int i) + \since 5.2 + + Removes the element at index position \a i. + Equivalent to + \code + remove(i); + \endcode + + Provided for compatibility with QList. + + \sa remove(), QList::removeAt() +*/ + +/*! \fn template int QList::removeAll(const T &t) + \since 5.4 + + Removes all elements that compare equal to \a t from the + vector. Returns the number of elements removed, if any. + + Provided for compatibility with QList. + + \sa removeOne(), QList::removeAll() +*/ + +/*! \fn template bool QList::removeOne(const T &t) + \since 5.4 + + Removes the first element that compares equal to \a t from the + vector. Returns whether an element was, in fact, removed. + + Provided for compatibility with QList. + + \sa removeAll(), QList::removeOne() +*/ + +/*! \fn template int QList::length() const + \since 5.2 + + Same as size() and count(). + + Provided for compatibility with QList. + + \sa size(), count(), QList::length() +*/ + +/*! \fn template T QList::takeAt(int i) + \since 5.2 + + Removes the element at index position \a i and returns it. + + Equivalent to + \code + T t = at(i); + remove(i); + return t; + \endcode + + Provided for compatibility with QList. + + \sa takeFirst(), takeLast(), QList::takeAt() +*/ + +/*! \fn template void QList::move(int from, int to) + \since 5.6 + + Moves the item at index position \a from to index position \a to. + + Provided for compatibility with QList. + + \sa QList::move() +*/ + +/*! \fn template void QList::removeFirst() + \since 5.1 + Removes the first item in the vector. Calling this function is + equivalent to calling remove(0). The vector must not be empty. If + the vector can be empty, call isEmpty() before calling this + function. + + \sa remove(), takeFirst(), isEmpty() +*/ + +/*! \fn template void QList::removeLast() + \since 5.1 + Removes the last item in the vector. Calling this function is + equivalent to calling remove(size() - 1). The vector must not be + empty. If the vector can be empty, call isEmpty() before calling + this function. + + \sa remove(), takeLast(), removeFirst(), isEmpty() +*/ + +/*! \fn template T QList::takeFirst() + \since 5.1 + + Removes the first item in the vector and returns it. This function + assumes the vector is not empty. To avoid failure, call isEmpty() + before calling this function. + + \sa takeLast(), removeFirst() +*/ + +/*! \fn template T QList::takeLast() + \since 5.1 + + Removes the last item in the list and returns it. This function + assumes the vector is not empty. To avoid failure, call isEmpty() + before calling this function. + + If you don't use the return value, removeLast() is more + efficient. + + \sa takeFirst(), removeLast() +*/ + +/*! + \fn template template QList::iterator QList::emplace(QList::iterator before, Args&&... args) + + \overload + + Creates a new element in front of the item pointed to by the + iterator \a before. This new element is constructed in-place + using \a args as the arguments for its construction. + + Returns an iterator to the new element. +*/ + +/*! \fn template QList &QList::fill(const T &value, int size = -1) + + Assigns \a value to all items in the vector. If \a size is + different from -1 (the default), the vector is resized to size \a + size beforehand. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 11 + + \sa resize() +*/ + +/*! \fn template int QList::indexOf(const T &value, int from = 0) const + + Returns the index position of the first occurrence of \a value in + the vector, searching forward from index position \a from. + Returns -1 if no item matched. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 12 + + This function requires the value type to have an implementation of + \c operator==(). + + \sa lastIndexOf(), contains() +*/ + +/*! \fn template int QList::lastIndexOf(const T &value, int from = -1) const + + Returns the index position of the last occurrence of the value \a + value in the vector, searching backward from index position \a + from. If \a from is -1 (the default), the search starts at the + last item. Returns -1 if no item matched. + + Example: + \snippet code/src_corelib_tools_qlist.cpp 13 + + This function requires the value type to have an implementation of + \c operator==(). + + \sa indexOf() +*/ + +/*! \fn template bool QList::contains(const T &value) const + + Returns \c true if the vector contains an occurrence of \a value; + otherwise returns \c false. + + This function requires the value type to have an implementation of + \c operator==(). + + \sa indexOf(), count() +*/ + +/*! \fn template bool QList::startsWith(const T &value) const + \since 4.5 + + Returns \c true if this vector is not empty and its first + item is equal to \a value; otherwise returns \c false. + + \sa isEmpty(), first() +*/ + +/*! \fn template bool QList::endsWith(const T &value) const + \since 4.5 + + Returns \c true if this vector is not empty and its last + item is equal to \a value; otherwise returns \c false. + + \sa isEmpty(), last() +*/ + + +/*! \fn template int QList::count(const T &value) const + + Returns the number of occurrences of \a value in the vector. + + This function requires the value type to have an implementation of + \c operator==(). + + \sa contains(), indexOf() +*/ + +/*! \fn template int QList::count() const + + \overload + + Same as size(). +*/ + +/*! \fn template QList::iterator QList::begin() + + Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in + the vector. + + \sa constBegin(), end() +*/ + +/*! \fn template QList::const_iterator QList::begin() const + + \overload +*/ + +/*! \fn template QList::const_iterator QList::cbegin() const + \since 5.0 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item + in the vector. + + \sa begin(), cend() +*/ + +/*! \fn template QList::const_iterator QList::constBegin() const + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item + in the vector. + + \sa begin(), constEnd() +*/ + +/*! \fn template QList::iterator QList::end() + + Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item + after the last item in the vector. + + \sa begin(), constEnd() +*/ + +/*! \fn template QList::const_iterator QList::end() const + + \overload +*/ + +/*! \fn template QList::const_iterator QList::cend() const + \since 5.0 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + item after the last item in the vector. + + \sa cbegin(), end() +*/ + +/*! \fn template QList::const_iterator QList::constEnd() const + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + item after the last item in the vector. + + \sa constBegin(), end() +*/ + +/*! \fn template QList::reverse_iterator QList::rbegin() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + item in the vector, in reverse order. + + \sa begin(), crbegin(), rend() +*/ + +/*! \fn template QList::const_reverse_iterator QList::rbegin() const + \since 5.6 + \overload +*/ + +/*! \fn template QList::const_reverse_iterator QList::crbegin() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + item in the vector, in reverse order. + + \sa begin(), rbegin(), rend() +*/ + +/*! \fn template QList::reverse_iterator QList::rend() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last item in the vector, in reverse order. + + \sa end(), crend(), rbegin() +*/ + +/*! \fn template QList::const_reverse_iterator QList::rend() const + \since 5.6 + \overload +*/ + +/*! \fn template QList::const_reverse_iterator QList::crend() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one + past the last item in the vector, in reverse order. + + \sa end(), rend(), rbegin() +*/ + +/*! \fn template QList::iterator QList::erase(iterator pos) + + Removes the item pointed to by the iterator \a pos from the + vector, and returns an iterator to the next item in the vector + (which may be end()). + + \sa insert(), remove() +*/ + +/*! \fn template QList::iterator QList::erase(iterator begin, iterator end) + + \overload + + Removes all the items from \a begin up to (but not including) \a + end. Returns an iterator to the same item that \a end referred to + before the call. +*/ + +/*! \fn template T& QList::first() + + Returns a reference to the first item in the vector. This + function assumes that the vector isn't empty. + + \sa last(), isEmpty(), constFirst() +*/ + +/*! \fn template const T& QList::first() const + + \overload +*/ + +/*! \fn template const T& QList::constFirst() const + \since 5.6 + + Returns a const reference to the first item in the vector. This + function assumes that the vector isn't empty. + + \sa constLast(), isEmpty(), first() +*/ + +/*! \fn template T& QList::last() + + Returns a reference to the last item in the vector. This function + assumes that the vector isn't empty. + + \sa first(), isEmpty(), constLast() +*/ + +/*! \fn template const T& QList::last() const + + \overload +*/ + +/*! \fn template const T& QList::constLast() const + \since 5.6 + + Returns a const reference to the last item in the vector. This function + assumes that the vector isn't empty. + + \sa constFirst(), isEmpty(), last() +*/ + +/*! \fn template T QList::value(int i) const + + Returns the value at index position \a i in the vector. + + If the index \a i is out of bounds, the function returns + a \l{default-constructed value}. If you are certain that + \a i is within bounds, you can use at() instead, which is slightly + faster. + + \sa at(), operator[]() +*/ + +/*! \fn template T QList::value(int i, const T &defaultValue) const + + \overload + + If the index \a i is out of bounds, the function returns + \a defaultValue. +*/ + +/*! \fn template void QList::push_back(const T &value) + + This function is provided for STL compatibility. It is equivalent + to append(\a value). +*/ + +/*! \fn template void QList::push_back(T &&value) + \since 5.6 + \overload +*/ + +/*! + \fn template void QList::push_front(const T &value) + \fn template void QList::push_front(T &&value) + + This function is provided for STL compatibility. It is equivalent + to prepend(\a value). +*/ + +/*! \fn template void QList::pop_front() + + This function is provided for STL compatibility. It is equivalent + to removeFirst(). +*/ + +/*! \fn template void QList::pop_back() + + This function is provided for STL compatibility. It is equivalent + to removeLast(). +*/ + +/*! \fn template T& QList::front() + + This function is provided for STL compatibility. It is equivalent + to first(). +*/ + +/*! \fn template QList::const_reference QList::front() const + + \overload +*/ + +/*! \fn template QList::reference QList::back() + + This function is provided for STL compatibility. It is equivalent + to last(). +*/ + +/*! \fn template QList::const_reference QList::back() const + + \overload +*/ + +/*! \fn template void QList::shrink_to_fit() + \since 5.10 + + This function is provided for STL compatibility. It is equivalent + to squeeze(). +*/ + +/*! \fn template bool QList::empty() const + + This function is provided for STL compatibility. It is equivalent + to isEmpty(), returning \c true if the vector is empty; otherwise + returns \c false. +*/ + +/*! \fn template QList &QList::operator+=(const QList &other) + + Appends the items of the \a other vector to this vector and + returns a reference to this vector. + + \sa operator+(), append() +*/ + +/*! \fn template void QList::operator+=(const T &value) + + \overload + + Appends \a value to the vector. + + \sa append(), operator<<() +*/ + +/*! \fn template void QList::operator+=(T &&value) + \since 5.11 + + \overload + + \sa append(), operator<<() +*/ + +/*! \fn template QList QList::operator+(const QList &other) const + + Returns a vector that contains all the items in this vector + followed by all the items in the \a other vector. + + \sa operator+=() +*/ + +/*! \fn template QList &QList::operator<<(const T &value) + + Appends \a value to the vector and returns a reference to this + vector. + + \sa append(), operator+=() +*/ + +/*! \fn template QList &QList::operator<<(T &&value) + \since 5.11 + + \overload + + \sa append(), operator+=() +*/ + + +/*! \fn template QList &QList::operator<<(const QList &other) + + Appends \a other to the vector and returns a reference to the + vector. +*/ + +/*! \typedef QList::iterator + + The QList::iterator typedef provides an STL-style non-const + iterator for QList and QStack. + + QList provides both \l{STL-style iterators} and \l{Java-style + iterators}. The STL-style non-const iterator is simply a typedef + for "T *" (pointer to T). + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::begin(), QList::end(), QList::const_iterator, QMutableVectorIterator +*/ + +/*! \typedef QList::const_iterator + + The QList::const_iterator typedef provides an STL-style const + iterator for QList and QStack. + + QList provides both \l{STL-style iterators} and \l{Java-style + iterators}. The STL-style const iterator is simply a typedef for + "const T *" (pointer to const T). + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::constBegin(), QList::constEnd(), QList::iterator, QListIterator +*/ + +/*! \typedef QList::reverse_iterator + \since 5.6 + + The QList::reverse_iterator typedef provides an STL-style non-const + reverse iterator for QList. + + It is simply a typedef for \c{std::reverse_iterator}. + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator +*/ + +/*! \typedef QList::const_reverse_iterator + \since 5.6 + + The QList::const_reverse_iterator typedef provides an STL-style const + reverse iterator for QList. + + It is simply a typedef for \c{std::reverse_iterator}. + + \warning Iterators on implicitly shared containers do not work + exactly like STL-iterators. You should avoid copying a container + while iterators are active on that container. For more information, + read \l{Implicit sharing iterator problem}. + + \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator +*/ + +/*! \typedef QList::Iterator + + Qt-style synonym for QList::iterator. +*/ + +/*! \typedef QList::ConstIterator + + Qt-style synonym for QList::const_iterator. +*/ + +/*! \typedef QList::const_pointer + + Typedef for const T *. Provided for STL compatibility. +*/ + +/*! \typedef QList::const_reference + + Typedef for T &. Provided for STL compatibility. +*/ + +/*! \typedef QList::difference_type + + Typedef for ptrdiff_t. Provided for STL compatibility. +*/ + +/*! \typedef QList::pointer + + Typedef for T *. Provided for STL compatibility. +*/ + +/*! \typedef QList::reference + + Typedef for T &. Provided for STL compatibility. +*/ + +/*! \typedef QList::size_type + + Typedef for int. Provided for STL compatibility. +*/ + +/*! \typedef QList::value_type + + Typedef for T. Provided for STL compatibility. +*/ + +/*! \fn template QList QList::toList() const + \fn template QList QList::toVector() const + \obsolete + + A no-op in Qt 6. Provided for backwards compatibility with + Qt 5, where QList and QVector where two different types. + + Returns this list. +*/ + +/*! \fn template QList QList::fromList(const QList &list) + \fn template QList QList::fromVector(const QList &list) + \obsolete + + A no-op in Qt 6. Provided for backwards compatibility with + Qt 5, where QList and QVector where two different types. + + Returns this list. +*/ + +/*! \fn template QList QList::fromStdVector(const std::vector &vector) + + Returns a QList object with the data contained in \a vector. The + order of the elements in the QList is the same as in \a vector. + + Example: + + \snippet code/src_corelib_tools_qlist.cpp 16 + + \include containers-range-constructor.qdocinc + + \sa toStdVector(), QList::fromStdList() +*/ + +/*! \fn template std::vector QList::toStdVector() const + + Returns a std::vector object with the data contained in this QList. + Example: + + \snippet code/src_corelib_tools_qlist.cpp 17 + + \include containers-range-constructor.qdocinc + + \sa fromStdVector(), QList::toStdList() +*/ + +/*! \fn template QDataStream &operator<<(QDataStream &out, const QList &vector) + \relates QList + + Writes the vector \a vector to stream \a out. + + This function requires the value type to implement \c operator<<(). + + \sa{Serializing Qt Data Types}{Format of the QDataStream operators} +*/ + +/*! \fn template QDataStream &operator>>(QDataStream &in, QList &vector) + \relates QList + + Reads a vector from stream \a in into \a vector. + + This function requires the value type to implement \c operator>>(). + + \sa{Serializing Qt Data Types}{Format of the QDataStream operators} +*/ diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index b502a86442..8d880407dd 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -1,7 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2019 Intel Corporation +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -41,758 +40,18 @@ #ifndef QVECTOR_H #define QVECTOR_H -#include -#include -#include -#include - -#include -#include -#include -#include +#include +#include QT_BEGIN_NAMESPACE -namespace QtPrivate { - template int indexOf(const QVector &list, const U &u, int from); - template int lastIndexOf(const QVector &list, const U &u, int from); -} - -template struct QVectorSpecialMethods -{ -protected: - ~QVectorSpecialMethods() = default; -}; -template <> struct QVectorSpecialMethods; -template <> struct QVectorSpecialMethods; - -template -class QVector -#ifndef Q_QDOC - : public QVectorSpecialMethods -#endif -{ - typedef QTypedArrayData Data; - typedef QArrayDataOps DataOps; - typedef QArrayDataPointer DataPointer; - class DisableRValueRefs {}; - - DataPointer d; - - template friend int QtPrivate::indexOf(const QVector &list, const U &u, int from); - template friend int QtPrivate::lastIndexOf(const QVector &list, const U &u, int from); - -public: - typedef T Type; - typedef T value_type; - typedef value_type *pointer; - typedef const value_type *const_pointer; - typedef value_type &reference; - typedef const value_type &const_reference; - typedef int size_type; - typedef qptrdiff difference_type; - typedef typename Data::iterator iterator; - typedef typename Data::const_iterator const_iterator; - typedef iterator Iterator; - typedef const_iterator ConstIterator; - typedef std::reverse_iterator reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; - typedef typename DataPointer::parameter_type parameter_type; - using rvalue_ref = typename std::conditional::type; - -private: - void resize_internal(int i, Qt::Initialization); - bool isValidIterator(const_iterator i) const - { - const std::less less = {}; - return !less(d->end(), i) && !less(i, d->begin()); - } -public: - QVector(DataPointer dd) noexcept - : d(dd) - { - } - -public: - constexpr inline QVector() noexcept { } - explicit QVector(int size) - : d(Data::allocate(size)) - { - if (size) - d->appendInitialize(size); - } - QVector(int size, const T &t) - : d(Data::allocate(size)) - { - if (size) - d->copyAppend(size, t); - } - - inline QVector(const QVector &other) noexcept : d(other.d) {} - QVector(QVector &&other) noexcept : d(std::move(other.d)) {} - inline QVector(std::initializer_list args) - : d(Data::allocate(args.size())) - { - if (args.size()) - d->copyAppend(args.begin(), args.end()); - } - - ~QVector() /*noexcept(std::is_nothrow_destructible::value)*/ {} - QVector &operator=(const QVector &other) { d = other.d; return *this; } - QVector &operator=(QVector &&other) noexcept(std::is_nothrow_destructible::value) - { - d = std::move(other.d); - return *this; - } - QVector &operator=(std::initializer_list args) - { - d = DataPointer(Data::allocate(args.size())); - if (args.size()) - d->copyAppend(args.begin(), args.end()); - return *this; - } - template = true> - QVector(InputIterator i1, InputIterator i2) - : d(Data::allocate(std::distance(i1, i2))) - { - if (std::distance(i1, i2)) - d->copyAppend(i1, i2); - } - - template = true> - QVector(InputIterator i1, InputIterator i2) - : QVector() - { - QtPrivate::reserveIfForwardIterator(this, i1, i2); - std::copy(i1, i2, std::back_inserter(*this)); - } - - void swap(QVector &other) noexcept { qSwap(d, other.d); } - - friend bool operator==(const QVector &l, const QVector &r) - { - if (l.size() != r.size()) - return false; - if (l.begin() == r.begin()) - return true; - - // do element-by-element comparison - return l.d->compare(l.begin(), r.begin(), l.size()); - } - friend bool operator!=(const QVector &l, const QVector &r) - { - return !(l == r); - } - - int size() const noexcept { return int(d->size); } - int count() const noexcept { return size(); } - int length() const noexcept { return size(); } - - inline bool isEmpty() const noexcept { return d->size == 0; } - - void resize(int size) - { - resize_internal(size, Qt::Uninitialized); - if (size > this->size()) - d->appendInitialize(size); - } - void resize(int size, parameter_type c) - { - resize_internal(size, Qt::Uninitialized); - if (size > this->size()) - d->copyAppend(size - this->size(), c); - } - - inline int capacity() const { return int(d->constAllocatedCapacity()); } - void reserve(int size); - inline void squeeze(); - - void detach() { d.detach(); } - bool isDetached() const noexcept { return !d->isShared(); } - - inline bool isSharedWith(const QVector &other) const { return d == other.d; } - - pointer data() { detach(); return d->data(); } - const_pointer data() const noexcept { return d->data(); } - const_pointer constData() const noexcept { return d->data(); } - void clear() { - if (!size()) - return; - if (d->needsDetach()) { - // must allocate memory - DataPointer detached(Data::allocate(d.allocatedCapacity(), d->detachFlags())); - d.swap(detached); - } else { - d->truncate(0); - } - } - - const_reference at(int i) const noexcept - { - Q_ASSERT_X(size_t(i) < size_t(d->size), "QVector::at", "index out of range"); - return data()[i]; - } - reference operator[](int i) - { - Q_ASSERT_X(size_t(i) < size_t(d->size), "QVector::operator[]", "index out of range"); - detach(); - return data()[i]; - } - const_reference operator[](int i) const noexcept { return at(i); } - void append(const_reference t) - { append(const_iterator(std::addressof(t)), const_iterator(std::addressof(t)) + 1); } - void append(const_iterator i1, const_iterator i2); - void append(rvalue_ref t) { emplaceBack(std::move(t)); } - void append(const QVector &l) { append(l.constBegin(), l.constEnd()); } - void prepend(rvalue_ref t); - void prepend(const T &t); - - template - reference emplaceBack(Args&&... args) { return *emplace(count(), std::forward(args)...); } - - iterator insert(int i, parameter_type t) - { return insert(i, 1, t); } - iterator insert(int i, int n, parameter_type t); - iterator insert(const_iterator before, parameter_type t) - { - Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid"); - return insert(before, 1, t); - } - iterator insert(const_iterator before, int n, parameter_type t) - { - Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid"); - return insert(std::distance(constBegin(), before), n, t); - } - iterator insert(const_iterator before, rvalue_ref t) - { - Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid"); - return insert(std::distance(constBegin(), before), std::move(t)); - } - iterator insert(int i, rvalue_ref t) { return emplace(i, std::move(t)); } - - template - iterator emplace(const_iterator before, Args&&... args) - { - Q_ASSERT_X(isValidIterator(before), "QVector::emplace", "The specified iterator argument 'before' is invalid"); - return emplace(std::distance(constBegin(), before), std::forward(args)...); - } - - template - iterator emplace(int i, Args&&... args); -#if 0 - template< class InputIt > - iterator insert( const_iterator pos, InputIt first, InputIt last ); - iterator insert( const_iterator pos, std::initializer_list ilist ); -#endif - void replace(int i, const T &t) - { - Q_ASSERT_X(i >= 0 && i < d->size, "QVector::replace", "index out of range"); - const T copy(t); - data()[i] = copy; - } - void replace(int i, rvalue_ref t) - { - Q_ASSERT_X(i >= 0 && i < d->size, "QVector::replace", "index out of range"); - const T copy(std::move(t)); - data()[i] = std::move(copy); - } - - void remove(int i, int n = 1); - void removeFirst() { Q_ASSERT(!isEmpty()); remove(0); } - void removeLast() { Q_ASSERT(!isEmpty()); remove(size() - 1); } - value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); remove(0); return v; } - value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); remove(size() - 1); return v; } - - QVector &fill(parameter_type t, int size = -1); - - int indexOf(const T &t, int from = 0) const noexcept; - int lastIndexOf(const T &t, int from = -1) const noexcept; - bool contains(const T &t) const noexcept - { - return indexOf(t) != -1; - } - int count(const T &t) const noexcept - { - return int(std::count(&*cbegin(), &*cend(), t)); - } - - // QList compatibility - void removeAt(int i) { remove(i); } - int removeAll(const T &t) - { - const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); - if (cit == ce) - return 0; - int index = cit - this->cbegin(); - // next operation detaches, so ce, cit, t may become invalidated: - const T tCopy = t; - const iterator e = end(), it = std::remove(begin() + index, e, tCopy); - const int result = std::distance(it, e); - erase(it, e); - return result; - } - bool removeOne(const T &t) - { - const int i = indexOf(t); - if (i < 0) - return false; - remove(i); - return true; - } - T takeAt(int i) { T t = std::move((*this)[i]); remove(i); return t; } - void move(int from, int to) - { - Q_ASSERT_X(from >= 0 && from < size(), "QVector::move(int,int)", "'from' is out-of-range"); - Q_ASSERT_X(to >= 0 && to < size(), "QVector::move(int,int)", "'to' is out-of-range"); - if (from == to) // don't detach when no-op - return; - detach(); - T * const b = d->begin(); - if (from < to) - std::rotate(b + from, b + from + 1, b + to + 1); - else - std::rotate(b + to, b + from, b + from + 1); - } - - // STL-style - iterator begin() { detach(); return d->begin(); } - iterator end() { detach(); return d->end(); } - - const_iterator begin() const noexcept { return d->constBegin(); } - const_iterator end() const noexcept { return d->constEnd(); } - const_iterator cbegin() const noexcept { return d->constBegin(); } - const_iterator cend() const noexcept { return d->constEnd(); } - const_iterator constBegin() const noexcept { return d->constBegin(); } - const_iterator constEnd() const noexcept { return d->constEnd(); } - reverse_iterator rbegin() { return reverse_iterator(end()); } - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } - const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } - - iterator erase(const_iterator begin, const_iterator end); - inline iterator erase(const_iterator pos) { return erase(pos, pos+1); } - - // more Qt - inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); } - inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); } - inline const T &constFirst() const { Q_ASSERT(!isEmpty()); return *begin(); } - inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); } - inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); } - inline const T &constLast() const { Q_ASSERT(!isEmpty()); return *(end()-1); } - inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; } - inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; } - QVector mid(int pos, int len = -1) const; - - T value(int i) const { return value(i, T()); } - T value(int i, const T &defaultValue) const; - - void swapItemsAt(int i, int j) { - Q_ASSERT_X(i >= 0 && i < size() && j >= 0 && j < size(), - "QVector::swap", "index out of range"); - detach(); - qSwap(d->begin()[i], d->begin()[j]); - } - - // STL compatibility - inline void push_back(const T &t) { append(t); } - void push_back(rvalue_ref t) { append(std::move(t)); } - void push_front(rvalue_ref t) { prepend(std::move(t)); } - inline void push_front(const T &t) { prepend(t); } - void pop_back() { removeLast(); } - void pop_front() { removeFirst(); } - - template - reference emplace_back(Args&&... args) { return emplaceBack(std::forward(args)...); } - - inline bool empty() const - { return d->size == 0; } - inline reference front() { return first(); } - inline const_reference front() const { return first(); } - inline reference back() { return last(); } - inline const_reference back() const { return last(); } - void shrink_to_fit() { squeeze(); } - - // comfort - QVector &operator+=(const QVector &l) { append(l.cbegin(), l.cend()); return *this; } - inline QVector operator+(const QVector &l) const - { QVector n = *this; n += l; return n; } - inline QVector &operator+=(const T &t) - { append(t); return *this; } - inline QVector &operator<< (const T &t) - { append(t); return *this; } - inline QVector &operator<<(const QVector &l) - { *this += l; return *this; } - inline QVector &operator+=(rvalue_ref t) - { append(std::move(t)); return *this; } - inline QVector &operator<<(rvalue_ref t) - { append(std::move(t)); return *this; } - - // Consider deprecating in 6.4 or later - static QVector fromList(const QVector &list) { return list; } - QVector toList() const { return *this; } - - static inline QVector fromVector(const QVector &vector) { return vector; } - inline QVector toVector() const { return *this; } -}; - -#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 -template ::value_type, - QtPrivate::IfIsInputIterator = true> -QVector(InputIterator, InputIterator) -> QVector; -#endif - -template -inline void QVector::resize_internal(int newSize, Qt::Initialization) -{ - Q_ASSERT(newSize >= 0); - - if (d->needsDetach() || newSize > capacity()) { - // must allocate memory - DataPointer detached(Data::allocate(d->detachCapacity(newSize), - d->detachFlags())); - if (size() && newSize) { - detached->copyAppend(constBegin(), constBegin() + qMin(newSize, size())); - } - d.swap(detached); - } - - if (newSize < size()) - d->truncate(newSize); -} - -template -void QVector::reserve(int asize) -{ - // capacity() == 0 for immutable data, so this will force a detaching below - if (asize <= capacity()) { - if (d->flags() & Data::CapacityReserved) - return; // already reserved, don't shrink - if (!d->isShared()) { - // accept current allocation, don't shrink - d->flags() |= Data::CapacityReserved; - return; - } - } - - DataPointer detached(Data::allocate(qMax(asize, size()), - d->detachFlags() | Data::CapacityReserved)); - detached->copyAppend(constBegin(), constEnd()); - d.swap(detached); -} - -template -inline void QVector::squeeze() -{ - if (d->needsDetach() || size() != capacity()) { - // must allocate memory - DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved)); - if (size()) { - detached->copyAppend(constBegin(), constEnd()); - } - d.swap(detached); - } -} - -template -inline void QVector::remove(int i, int n) -{ - Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QVector::remove", "index out of range"); - Q_ASSERT_X(n >= 0, "QVector::remove", "invalid count"); - - if (n == 0) - return; - - const size_t newSize = size() - n; - if (d->needsDetach() || - ((d->flags() & Data::CapacityReserved) == 0 - && newSize < d->allocatedCapacity()/2)) { - // allocate memory - DataPointer detached(Data::allocate(d->detachCapacity(newSize), - d->detachFlags() & ~(Data::GrowsBackwards | Data::GrowsForward))); - const_iterator where = constBegin() + i; - if (newSize) { - detached->copyAppend(constBegin(), where); - detached->copyAppend(where + n, constEnd()); - } - d.swap(detached); - } else { - // we're detached and we can just move data around - d->erase(d->begin() + i, d->begin() + i + n); - } -} - -template -inline void QVector::prepend(const T &t) -{ insert(0, 1, t); } -template -void QVector::prepend(rvalue_ref t) -{ insert(0, std::move(t)); } - +#if !defined(QT_NO_JAVA_STYLE_ITERATORS) template -inline T QVector::value(int i, const T &defaultValue) const -{ - return size_t(i) < size_t(d->size) ? at(i) : defaultValue; -} - -template -inline void QVector::append(const_iterator i1, const_iterator i2) -{ - if (i1 == i2) - return; - const size_t newSize = size() + std::distance(i1, i2); - if (d->needsDetach() || newSize > d->allocatedCapacity()) { - DataPointer detached(Data::allocate(d->detachCapacity(newSize), - d->detachFlags() | Data::GrowsForward)); - detached->copyAppend(constBegin(), constEnd()); - detached->copyAppend(i1, i2); - d.swap(detached); - } else { - // we're detached and we can just move data around - d->copyAppend(i1, i2); - } -} - -template -inline typename QVector::iterator -QVector::insert(int i, int n, parameter_type t) -{ - Q_ASSERT_X(size_t(i) <= size_t(d->size), "QVector::insert", "index out of range"); - - // we don't have a quick exit for n == 0 - // it's not worth wasting CPU cycles for that - - const size_t newSize = size() + n; - if (d->needsDetach() || newSize > d->allocatedCapacity()) { - typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; - if (size_t(i) <= newSize / 4) - flags |= Data::GrowsBackwards; - - DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags)); - const_iterator where = constBegin() + i; - detached->copyAppend(constBegin(), where); - detached->copyAppend(n, t); - detached->copyAppend(where, constEnd()); - d.swap(detached); - } else { - // we're detached and we can just move data around - if (i == size()) { - d->copyAppend(n, t); - } else { - T copy(t); - d->insert(d.begin() + i, n, copy); - } - } - return d.begin() + i; -} - -template -template -typename QVector::iterator -QVector::emplace(int i, Args&&... args) -{ - Q_ASSERT_X(i >= 0 && i <= d->size, "QVector::insert", "index out of range"); - - const size_t newSize = size() + 1; - if (d->needsDetach() || newSize > d->allocatedCapacity()) { - typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; - if (size_t(i) <= newSize / 4) - flags |= Data::GrowsBackwards; - - DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags)); - const_iterator where = constBegin() + i; - - // First, create an element to handle cases, when a user moves - // the element from a container to the same container - detached->createInPlace(detached.begin() + i, std::forward(args)...); - - // Then, put the first part of the elements to the new location - detached->copyAppend(constBegin(), where); - - // After that, increase the actual size, because we created - // one extra element - ++detached.size; - - // Finally, put the rest of the elements to the new location - detached->copyAppend(where, constEnd()); - - d.swap(detached); - } else { - d->emplace(d.begin() + i, std::forward(args)...); - } - return d.begin() + i; -} - -template -typename QVector::iterator QVector::erase(const_iterator abegin, const_iterator aend) -{ - Q_ASSERT_X(isValidIterator(abegin), "QVector::erase", "The specified iterator argument 'abegin' is invalid"); - Q_ASSERT_X(isValidIterator(aend), "QVector::erase", "The specified iterator argument 'aend' is invalid"); - Q_ASSERT(aend >= abegin); - - int i = std::distance(d.constBegin(), abegin); - int n = std::distance(abegin, aend); - remove(i, n); - - return d.begin() + i; -} - -template -inline QVector &QVector::fill(parameter_type t, int newSize) -{ - if (newSize == -1) - newSize = size(); - if (d->needsDetach() || newSize > capacity()) { - // must allocate memory - DataPointer detached(Data::allocate(d->detachCapacity(newSize), - d->detachFlags())); - detached->copyAppend(newSize, t); - d.swap(detached); - } else { - // we're detached - const T copy(t); - d->assign(d.begin(), d.begin() + qMin(size(), newSize), t); - if (newSize > size()) - d->copyAppend(newSize - size(), copy); - } - return *this; -} - -namespace QtPrivate { -template -int indexOf(const QVector &vector, const U &u, int from) -{ - if (from < 0) - from = qMax(from + vector.size(), 0); - if (from < vector.size()) { - auto n = vector.begin() + from - 1; - auto e = vector.end(); - while (++n != e) - if (*n == u) - return int(n - vector.begin()); - } - return -1; -} - -template -int lastIndexOf(const QVector &vector, const U &u, int from) -{ - if (from < 0) - from += vector.d->size; - else if (from >= vector.size()) - from = vector.size() - 1; - if (from >= 0) { - auto b = vector.begin(); - auto n = vector.begin() + from + 1; - while (n != b) { - if (*--n == u) - return int(n - b); - } - } - return -1; -} -} - -template -int QVector::indexOf(const T &t, int from) const noexcept -{ - return QtPrivate::indexOf(*this, t, from); -} - -template -int QVector::lastIndexOf(const T &t, int from) const noexcept -{ - return QtPrivate::lastIndexOf(*this, t, from); -} - -template -inline QVector QVector::mid(int pos, int len) const -{ - qsizetype p = pos; - qsizetype l = len; - using namespace QtPrivate; - switch (QContainerImplHelper::mid(d.size, &p, &l)) { - case QContainerImplHelper::Null: - case QContainerImplHelper::Empty: - return QVector(); - case QContainerImplHelper::Full: - return *this; - case QContainerImplHelper::Subset: - break; - } - - // Allocate memory - DataPointer copied(Data::allocate(l)); - copied->copyAppend(constBegin() + p, constBegin() + p + l); - return copied; -} - -Q_DECLARE_SEQUENTIAL_ITERATOR(Vector) -Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector) - -template -size_t qHash(const QVector &key, size_t seed = 0) - noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) -{ - return qHashRange(key.cbegin(), key.cend(), seed); -} - -template -auto operator<(const QVector &lhs, const QVector &rhs) - noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()))) - -> decltype(std::declval() < std::declval()) -{ - return std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()); -} - -template -auto operator>(const QVector &lhs, const QVector &rhs) - noexcept(noexcept(lhs < rhs)) - -> decltype(lhs < rhs) -{ - return rhs < lhs; -} - -template -auto operator<=(const QVector &lhs, const QVector &rhs) - noexcept(noexcept(lhs < rhs)) - -> decltype(lhs < rhs) -{ - return !(lhs > rhs); -} - -template -auto operator>=(const QVector &lhs, const QVector &rhs) - noexcept(noexcept(lhs < rhs)) - -> decltype(lhs < rhs) -{ - return !(lhs < rhs); -} - -/* - ### Qt 5: - ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because - ### Qt exports QPolygon and QPolygonF that inherit QVector and - ### QVector respectively. -*/ - -#if defined(Q_CC_MSVC) && !defined(QT_BUILD_CORE_LIB) -QT_BEGIN_INCLUDE_NAMESPACE -#include -QT_END_INCLUDE_NAMESPACE -extern template class Q_CORE_EXPORT QVector; -extern template class Q_CORE_EXPORT QVector; +using QMutableVectorIterator = QMutableListIterator; +template +using QVectorIterator = QListIterator; #endif -QVector QStringView::toUcs4() const { return QtPrivate::convertToUcs4(*this); } - QT_END_NAMESPACE -#include -#include - #endif // QVECTOR_H diff --git a/src/corelib/tools/qvector.qdoc b/src/corelib/tools/qvector.qdoc deleted file mode 100644 index 88baaab8c1..0000000000 --- a/src/corelib/tools/qvector.qdoc +++ /dev/null @@ -1,1473 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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 Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/*! - \class QVector - \inmodule QtCore - \brief The QVector class is a template class that provides a dynamic array. - - \ingroup tools - \ingroup shared - - \reentrant - - QVector\ is one of Qt's generic \l{container classes}. It - stores its items in adjacent memory locations and provides fast - index-based access. - - QList\, QVector\, and QVarLengthArray\ - provide similar APIs and functionality. They are often interchangeable, - but there are performance consequences. Here is an overview of use cases: - - \list - \li QVector should be your default first choice. - QVector\ will usually give better performance than QList\, - because QVector\ always stores its items sequentially in memory, - where QList\ will allocate its items on the heap unless - \c {sizeof(T) <= sizeof(void*)} and T has been declared to be - either a \c{Q_MOVABLE_TYPE} or a \c{Q_PRIMITIVE_TYPE} using - \l {Q_DECLARE_TYPEINFO}. See the \l {Pros and Cons of Using QList} - for an explanation. - \li However, QList is used throughout the Qt APIs for passing - parameters and for returning values. Use QList to interface with - those APIs. - \li If you need a real linked list, which guarantees - \l{Algorithmic Complexity}{constant time} insertions mid-list and - uses iterators to items rather than indexes, use std::list. - \endlist - - \note QVector and QVarLengthArray both guarantee C-compatible - array layout. QList does not. This might be important if your - application must interface with a C API. - - Here's an example of a QVector that stores integers and a QVector - that stores QString values: - - \snippet code/src_corelib_tools_qvector.cpp 0 - - QVector stores its items in a vector (array). Typically, vectors - are created with an initial size. For example, the following code - constructs a QVector with 200 elements: - - \snippet code/src_corelib_tools_qvector.cpp 1 - - The elements are automatically initialized with a - \l{default-constructed value}. If you want to initialize the - vector with a different value, pass that value as the second - argument to the constructor: - - \snippet code/src_corelib_tools_qvector.cpp 2 - - You can also call fill() at any time to fill the vector with a - value. - - QVector uses 0-based indexes, just like C++ arrays. To access the - item at a particular index position, you can use operator[](). On - non-const vectors, operator[]() returns a reference to the item - that can be used on the left side of an assignment: - - \snippet code/src_corelib_tools_qvector.cpp 3 - - For read-only access, an alternative syntax is to use at(): - - \snippet code/src_corelib_tools_qvector.cpp 4 - - at() can be faster than operator[](), because it never causes a - \l{deep copy} to occur. - - Another way to access the data stored in a QVector is to call - data(). The function returns a pointer to the first item in the - vector. You can use the pointer to directly access and modify the - elements stored in the vector. The pointer is also useful if you - need to pass a QVector to a function that accepts a plain C++ - array. - - If you want to find all occurrences of a particular value in a - vector, use indexOf() or lastIndexOf(). The former searches - forward starting from a given index position, the latter searches - backward. Both return the index of the matching item if they found - one; otherwise, they return -1. For example: - - \snippet code/src_corelib_tools_qvector.cpp 5 - - If you simply want to check whether a vector contains a - particular value, use contains(). If you want to find out how - many times a particular value occurs in the vector, use count(). - - QVector provides these basic functions to add, move, and remove - items: insert(), replace(), remove(), prepend(), append(). With - the exception of append() and replace(), these functions can be slow - (\l{linear time}) for large vectors, because they require moving many - items in the vector by one position in memory. If you want a container - class that provides fast insertion/removal in the middle, use - std::list instead. - - Unlike plain C++ arrays, QVectors can be resized at any time by - calling resize(). If the new size is larger than the old size, - QVector might need to reallocate the whole vector. QVector tries - to reduce the number of reallocations by preallocating up to twice - as much memory as the actual data needs. - - If you know in advance approximately how many items the QVector - will contain, you can call reserve(), asking QVector to - preallocate a certain amount of memory. You can also call - capacity() to find out how much memory QVector actually - allocated. - - Note that using non-const operators and functions can cause - QVector to do a deep copy of the data. This is due to \l{implicit sharing}. - - QVector's value type must be an \l{assignable data type}. This - covers most data types that are commonly used, but the compiler - won't let you, for example, store a QWidget as a value; instead, - store a QWidget *. A few functions have additional requirements; - for example, indexOf() and lastIndexOf() expect the value type to - support \c operator==(). These requirements are documented on a - per-function basis. - - Like the other container classes, QVector provides \l{Java-style - iterators} (QVectorIterator and QMutableVectorIterator) and - \l{STL-style iterators} (QVector::const_iterator and - QVector::iterator). In practice, these are rarely used, because - you can use indexes into the QVector. - - In addition to QVector, Qt also provides QVarLengthArray, a very - low-level class with little functionality that is optimized for - speed. - - QVector does \e not support inserting, prepending, appending or replacing - with references to its own values. Doing so will cause your application to - abort with an error message. - - \section2 More Information on Using Qt Containers - - For a detailed discussion comparing Qt containers with each other and - with STL containers, see \l {Understand the Qt Containers}. - - \section1 Maximum size and out-of-memory conditions - - The current version of QVector is limited to just under 2 GB (2^31 bytes) - in size. The exact value is architecture-dependent, since it depends on the - overhead required for managing the data block, but is no more than 32 - bytes. The number of elements that can be stored in a QVector is that size - divided by the size of each element. - - In case memory allocation fails, QVector will use the \l Q_CHECK_PTR macro, - which will throw a \c std::bad_alloc exception if the application is being - compiled with exception support. If exceptions are disabled, then running - out of memory is undefined behavior. - - Note that the operating system may impose further limits on applications - holding a lot of allocated memory, especially large, contiguous blocks. - Such considerations, the configuration of such behavior or any mitigation - are outside the scope of the Qt API. -*/ - -/*! - \fn template QVector QVector::mid(int pos, int length = -1) const - - Returns a sub-vector which contains elements from this vector, - starting at position \a pos. If \a length is -1 (the default), all - elements after \a pos are included; otherwise \a length elements (or - all remaining elements if there are less than \a length elements) - are included. -*/ - - -/*! \fn template QVector::QVector() - - Constructs an empty vector. - - \sa resize() -*/ - -/*! - \fn template QVector::QVector(QVector &&other) - - Move-constructs a QVector instance, making it point at the same - object that \a other was pointing to. - - \since 5.2 -*/ - -/*! \fn template QVector::QVector(int size) - - Constructs a vector with an initial size of \a size elements. - - The elements are initialized with a \l{default-constructed - value}. - - \sa resize() -*/ - -/*! \fn template QVector::QVector(int size, const T &value) - - Constructs a vector with an initial size of \a size elements. - Each element is initialized with \a value. - - \sa resize(), fill() -*/ - -/*! \fn template QVector::QVector(const QVector &other) - - Constructs a copy of \a other. - - This operation takes \l{Algorithmic Complexity}{constant time}, - because QVector is \l{implicitly shared}. This makes returning - a QVector from a function very fast. If a shared instance is - modified, it will be copied (copy-on-write), and that takes - \l{Algorithmic Complexity}{linear time}. - - \sa operator=() -*/ - -/*! \fn template QVector::QVector(std::initializer_list args) - \since 4.8 - - Constructs a vector from the std::initializer_list given by \a args. - - This constructor is only enabled if the compiler supports C++11 initializer - lists. -*/ - -/*! \fn template template QVector::QVector(InputIterator first, InputIterator last) - \since 5.14 - - Constructs a vector with the contents in the iterator range [\a first, \a last). - - The value type of \c InputIterator must be convertible to \c T. -*/ - -/*! - \fn template QVector::QVector(QArrayDataPointerRef ref) - \internal -*/ - -/*! \fn template QVector::~QVector() - - Destroys the vector. -*/ - -/*! \fn template QVector &QVector::operator=(const QVector &other) - - Assigns \a other to this vector and returns a reference to this - vector. -*/ - -/*! - \fn template QVector &QVector::operator=(QVector &&other) - - Move-assigns \a other to this QVector instance. - - \since 5.2 -*/ - -/*! - \fn template QVector &QVector::operator=(std::initializer_list args) - - Assigns the collection of values in \a args to this QVector instance. - - This operator is only enabled if the compiler supports C++11 initializer - lists. - - \since 5.14 -*/ - -/*! \fn template void QVector::swap(QVector &other) - \since 4.8 - - Swaps vector \a other with this vector. This operation is very fast and - never fails. -*/ - -/*! \fn template void QVector::swapItemsAt(int i, int j) - \since 5.14 - - Exchange the item at index position \a i with the item at index - position \a j. This function assumes that both \a i and \a j are - at least 0 but less than size(). To avoid failure, test that both - \a i and \a j are at least 0 and less than size(). -*/ - - -/*! \fn template bool QVector::operator==(const QVector &other) const - - Returns \c true if \a other is equal to this vector; otherwise - returns \c false. - - Two vectors are considered equal if they contain the same values - in the same order. - - This function requires the value type to have an implementation - of \c operator==(). - - \sa operator!=() -*/ - -/*! \fn template bool QVector::operator!=(const QVector &other) const - - Returns \c true if \a other is not equal to this vector; otherwise - returns \c false. - - Two vectors are considered equal if they contain the same values - in the same order. - - This function requires the value type to have an implementation - of \c operator==(). - - \sa operator==() -*/ - -/*! \fn template bool operator<(const QVector &lhs, const QVector &rhs) - \since 5.6 - \relates QVector - - Returns \c true if vector \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically less than} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template bool operator<=(const QVector &lhs, const QVector &rhs) - \since 5.6 - \relates QVector - - Returns \c true if vector \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically less than or equal to} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template bool operator>(const QVector &lhs, const QVector &rhs) - \since 5.6 - \relates QVector - - Returns \c true if vector \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically greater than} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template bool operator>=(const QVector &lhs, const QVector &rhs) - \since 5.6 - \relates QVector - - Returns \c true if vector \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically greater than or equal to} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! - \fn template size_t qHash(const QVector &key, size_t seed = 0) - \since 5.6 - \relates QVector - - Returns the hash value for \a key, - using \a seed to seed the calculation. - - This function requires qHash() to be overloaded for the value type \c T. -*/ - -/*! \fn template int QVector::size() const - - Returns the number of items in the vector. - - \sa isEmpty(), resize() -*/ - -/*! \fn template bool QVector::isEmpty() const - - Returns \c true if the vector has size 0; otherwise returns \c false. - - \sa size(), resize() -*/ - -/*! \fn template void QVector::resize(int size) - - Sets the size of the vector to \a size. If \a size is greater than the - current size, elements are added to the end; the new elements are - initialized with a \l{default-constructed value}. If \a size is less - than the current size, elements are removed from the end. - - Since Qt 5.6, resize() doesn't shrink the capacity anymore. - To shed excess capacity, use squeeze(). - - \sa size() -*/ - -/*! \fn template int QVector::capacity() const - - Returns the maximum number of items that can be stored in the - vector without forcing a reallocation. - - The sole purpose of this function is to provide a means of fine - tuning QVector's memory usage. In general, you will rarely ever - need to call this function. If you want to know how many items are - in the vector, call size(). - - \note a statically allocated vector will report a capacity of 0, - even if it's not empty. - - \sa reserve(), squeeze() -*/ - -/*! \fn template void QVector::reserve(int size) - - Attempts to allocate memory for at least \a size elements. If you - know in advance how large the vector will be, you should call this - function to prevent reallocations and memory fragmentation. - - If \a size is an underestimate, the worst that will happen is that - the QVector will be a bit slower. If \a size is an overestimate, you - may have used more memory than the normal QVector growth strategy - would have allocated—or you may have used less. - - An alternative to reserve() is calling resize(). Whether or not that is - faster than reserve() depends on the element type, because resize() - default-constructs all elements, and requires assignment to existing - entries rather than calling append(), which copy- or move-constructs. - For simple types, like \c int or \c double, resize() is typically faster, - but for anything more complex, you should prefer reserve(). - - \warning If the size passed to resize() was underestimated, you run out - of allocated space and into undefined behavior. This problem does not - exist with reserve(), because it treats the size as just a hint. - - \sa squeeze(), capacity() -*/ - -/*! \fn template void QVector::squeeze() - - Releases any memory not required to store the items. - - The sole purpose of this function is to provide a means of fine - tuning QVector's memory usage. In general, you will rarely ever - need to call this function. - - \sa reserve(), capacity() -*/ - -/*! \fn template void QVector::detach() - - \internal -*/ - -/*! \fn template bool QVector::isDetached() const - - \internal -*/ - -/*! \fn template void QVector::setSharable(bool sharable) - - \internal -*/ - -/*! \fn template bool QVector::isSharedWith(const QVector &other) const - - \internal -*/ - -/*! \fn template T *QVector::data() - - Returns a pointer to the data stored in the vector. The pointer - can be used to access and modify the items in the vector. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 6 - - The pointer remains valid as long as the vector isn't - reallocated. - - This function is mostly useful to pass a vector to a function - that accepts a plain C++ array. - - \sa constData(), operator[]() -*/ - -/*! \fn template const T *QVector::data() const - - \overload -*/ - -/*! \fn template const T *QVector::constData() const - - Returns a const pointer to the data stored in the vector. The - pointer can be used to access the items in the vector. - The pointer remains valid as long as the vector isn't - reallocated. - - This function is mostly useful to pass a vector to a function - that accepts a plain C++ array. - - \sa data(), operator[]() -*/ - -/*! \fn template void QVector::clear() - - Removes all the elements from the vector. - - \note Until Qt 5.6, this also released the memory used by - the vector. From Qt 5.7, the capacity is preserved. To shed - all capacity, swap with a default-constructed vector: - \code - QVector v ...; - QVector().swap(v); - Q_ASSERT(v.capacity() == 0); - \endcode - or call squeeze(). - - \sa squeeze() -*/ - -/*! \fn template const T &QVector::at(int i) const - - Returns the item at index position \a i in the vector. - - \a i must be a valid index position in the vector (i.e., 0 <= \a - i < size()). - - \sa value(), operator[]() -*/ - -/*! \fn template T &QVector::operator[](int i) - - Returns the item at index position \a i as a modifiable reference. - - \a i must be a valid index position in the vector (i.e., 0 <= \a i - < size()). - - Note that using non-const operators can cause QVector to do a deep - copy. - - \sa at(), value() -*/ - -/*! \fn template const T &QVector::operator[](int i) const - - \overload - - Same as at(\a i). -*/ - -/*! - \fn template void QVector::append(const T &value) - - Inserts \a value at the end of the vector. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 7 - - This is the same as calling resize(size() + 1) and assigning \a - value to the new last element in the vector. - - This operation is relatively fast, because QVector typically - allocates more memory than necessary, so it can grow without - reallocating the entire vector each time. - - \sa operator<<(), prepend(), insert() -*/ - -/*! - \fn template void QVector::append(T &&value) - \since 5.6 - - \overload - - Example: - \snippet code/src_corelib_tools_qvector.cpp move-append -*/ - -/*! \fn template void QVector::append(const QVector &value) - - \overload - - \since 5.5 - - Appends the items of the \a value vector to this vector. - - \sa operator<<(), operator+=() -*/ - - -/*! - \fn template void QVector::prepend(const T &value) - \fn template void QVector::prepend(T &&value) - - Inserts \a value at the beginning of the vector. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 8 - - This is the same as vector.insert(0, \a value). - - For large vectors, this operation can be slow (\l{linear time}), - because it requires moving all the items in the vector by one - position further in memory. If you want a container class that - provides a fast prepend operation, use std::list - instead. - - \sa append(), insert() -*/ - -/*! - \fn template template T &QVector::emplaceBack(Args&&... args) - \fn template template T &QVector::emplace_back(Args&&... args) - - Adds a new element to the end for the container. This new element - is constructed in-place using \a args as the arguments for its - construction. - - Returns a reference to the new element. - - Example: - \snippet code/src_corelib_tools_qvector.cpp emplace-back - - It is also possible to access a newly created object by using - returned reference: - \snippet code/src_corelib_tools_qvector.cpp emplace-back-ref - - This is the same as vector.emplace(vector.size(), \a args). - - \sa emplace -*/ - -/*! \fn template void QVector::insert(int i, const T &value) - \fn template void QVector::insert(int i, T &&value) - - Inserts \a value at index position \a i in the vector. If \a i is - 0, the value is prepended to the vector. If \a i is size(), the - value is appended to the vector. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 9 - - For large vectors, this operation can be slow (\l{linear time}), - because it requires moving all the items at indexes \a i and - above by one position further in memory. If you want a container - class that provides a fast insert() function, use std::list - instead. - - \sa append(), prepend(), remove() -*/ - -/*! \fn template void QVector::insert(int i, int count, const T &value) - - \overload - - Inserts \a count copies of \a value at index position \a i in the - vector. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 10 -*/ - -/*! - \fn template QVector::iterator QVector::insert(iterator before, const T &value) - \fn template QVector::iterator QVector::insert(iterator before, T &&value) - - \overload - - Inserts \a value in front of the item pointed to by the iterator - \a before. Returns an iterator pointing at the inserted item. -*/ - -/*! \fn template QVector::iterator QVector::insert(iterator before, int count, const T &value) - - Inserts \a count copies of \a value in front of the item pointed to - by the iterator \a before. Returns an iterator pointing at the - first of the inserted items. -*/ - -/*! - \fn template template QVector::iterator QVector::emplace(int i, Args&&... args) - - Extends the container by inserting a new element at position \a i. - This new element is constructed in-place using \a args as the - arguments for its construction. - - Returns an iterator to the new element. - - Example: - \snippet code/src_corelib_tools_qvector.cpp emplace - - \note It is garanteed that the element will be created in place - at the beginning, but after that it might be copied or - moved to the right position. - - \sa emplaceBack -*/ - - -/*! \fn template void QVector::replace(int i, const T &value) - - Replaces the item at index position \a i with \a value. - - \a i must be a valid index position in the vector (i.e., 0 <= \a - i < size()). - - \sa operator[](), remove() -*/ - -/*! \fn template void QVector::remove(int i) - - \overload - - Removes the element at index position \a i. - - \sa insert(), replace(), fill() -*/ - -/*! \fn template void QVector::remove(int i, int count) - - \overload - - Removes \a count elements from the middle of the vector, starting at - index position \a i. - - \sa insert(), replace(), fill() -*/ - -/*! \fn template void QVector::removeAt(int i) - \since 5.2 - - Removes the element at index position \a i. - Equivalent to - \code - remove(i); - \endcode - - Provided for compatibility with QList. - - \sa remove(), QList::removeAt() -*/ - -/*! \fn template int QVector::removeAll(const T &t) - \since 5.4 - - Removes all elements that compare equal to \a t from the - vector. Returns the number of elements removed, if any. - - Provided for compatibility with QList. - - \sa removeOne(), QList::removeAll() -*/ - -/*! \fn template bool QVector::removeOne(const T &t) - \since 5.4 - - Removes the first element that compares equal to \a t from the - vector. Returns whether an element was, in fact, removed. - - Provided for compatibility with QList. - - \sa removeAll(), QList::removeOne() -*/ - -/*! \fn template int QVector::length() const - \since 5.2 - - Same as size() and count(). - - Provided for compatibility with QList. - - \sa size(), count(), QList::length() -*/ - -/*! \fn template T QVector::takeAt(int i) - \since 5.2 - - Removes the element at index position \a i and returns it. - - Equivalent to - \code - T t = at(i); - remove(i); - return t; - \endcode - - Provided for compatibility with QList. - - \sa takeFirst(), takeLast(), QList::takeAt() -*/ - -/*! \fn template void QVector::move(int from, int to) - \since 5.6 - - Moves the item at index position \a from to index position \a to. - - Provided for compatibility with QList. - - \sa QList::move() -*/ - -/*! \fn template void QVector::removeFirst() - \since 5.1 - Removes the first item in the vector. Calling this function is - equivalent to calling remove(0). The vector must not be empty. If - the vector can be empty, call isEmpty() before calling this - function. - - \sa remove(), takeFirst(), isEmpty() -*/ - -/*! \fn template void QVector::removeLast() - \since 5.1 - Removes the last item in the vector. Calling this function is - equivalent to calling remove(size() - 1). The vector must not be - empty. If the vector can be empty, call isEmpty() before calling - this function. - - \sa remove(), takeLast(), removeFirst(), isEmpty() -*/ - -/*! \fn template T QVector::takeFirst() - \since 5.1 - - Removes the first item in the vector and returns it. This function - assumes the vector is not empty. To avoid failure, call isEmpty() - before calling this function. - - \sa takeLast(), removeFirst() -*/ - -/*! \fn template T QVector::takeLast() - \since 5.1 - - Removes the last item in the list and returns it. This function - assumes the vector is not empty. To avoid failure, call isEmpty() - before calling this function. - - If you don't use the return value, removeLast() is more - efficient. - - \sa takeFirst(), removeLast() -*/ - -/*! - \fn template template QVector::iterator QVector::emplace(QVector::iterator before, Args&&... args) - - \overload - - Creates a new element in front of the item pointed to by the - iterator \a before. This new element is constructed in-place - using \a args as the arguments for its construction. - - Returns an iterator to the new element. -*/ - -/*! \fn template QVector &QVector::fill(const T &value, int size = -1) - - Assigns \a value to all items in the vector. If \a size is - different from -1 (the default), the vector is resized to size \a - size beforehand. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 11 - - \sa resize() -*/ - -/*! \fn template int QVector::indexOf(const T &value, int from = 0) const - - Returns the index position of the first occurrence of \a value in - the vector, searching forward from index position \a from. - Returns -1 if no item matched. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 12 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa lastIndexOf(), contains() -*/ - -/*! \fn template int QVector::lastIndexOf(const T &value, int from = -1) const - - Returns the index position of the last occurrence of the value \a - value in the vector, searching backward from index position \a - from. If \a from is -1 (the default), the search starts at the - last item. Returns -1 if no item matched. - - Example: - \snippet code/src_corelib_tools_qvector.cpp 13 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa indexOf() -*/ - -/*! \fn template bool QVector::contains(const T &value) const - - Returns \c true if the vector contains an occurrence of \a value; - otherwise returns \c false. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa indexOf(), count() -*/ - -/*! \fn template bool QVector::startsWith(const T &value) const - \since 4.5 - - Returns \c true if this vector is not empty and its first - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), first() -*/ - -/*! \fn template bool QVector::endsWith(const T &value) const - \since 4.5 - - Returns \c true if this vector is not empty and its last - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), last() -*/ - - -/*! \fn template int QVector::count(const T &value) const - - Returns the number of occurrences of \a value in the vector. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa contains(), indexOf() -*/ - -/*! \fn template int QVector::count() const - - \overload - - Same as size(). -*/ - -/*! \fn template QVector::iterator QVector::begin() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in - the vector. - - \sa constBegin(), end() -*/ - -/*! \fn template QVector::const_iterator QVector::begin() const - - \overload -*/ - -/*! \fn template QVector::const_iterator QVector::cbegin() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the vector. - - \sa begin(), cend() -*/ - -/*! \fn template QVector::const_iterator QVector::constBegin() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the vector. - - \sa begin(), constEnd() -*/ - -/*! \fn template QVector::iterator QVector::end() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item - after the last item in the vector. - - \sa begin(), constEnd() -*/ - -/*! \fn template QVector::const_iterator QVector::end() const - - \overload -*/ - -/*! \fn template QVector::const_iterator QVector::cend() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the vector. - - \sa cbegin(), end() -*/ - -/*! \fn template QVector::const_iterator QVector::constEnd() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the vector. - - \sa constBegin(), end() -*/ - -/*! \fn template QVector::reverse_iterator QVector::rbegin() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the vector, in reverse order. - - \sa begin(), crbegin(), rend() -*/ - -/*! \fn template QVector::const_reverse_iterator QVector::rbegin() const - \since 5.6 - \overload -*/ - -/*! \fn template QVector::const_reverse_iterator QVector::crbegin() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the vector, in reverse order. - - \sa begin(), rbegin(), rend() -*/ - -/*! \fn template QVector::reverse_iterator QVector::rend() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past - the last item in the vector, in reverse order. - - \sa end(), crend(), rbegin() -*/ - -/*! \fn template QVector::const_reverse_iterator QVector::rend() const - \since 5.6 - \overload -*/ - -/*! \fn template QVector::const_reverse_iterator QVector::crend() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one - past the last item in the vector, in reverse order. - - \sa end(), rend(), rbegin() -*/ - -/*! \fn template QVector::iterator QVector::erase(iterator pos) - - Removes the item pointed to by the iterator \a pos from the - vector, and returns an iterator to the next item in the vector - (which may be end()). - - \sa insert(), remove() -*/ - -/*! \fn template QVector::iterator QVector::erase(iterator begin, iterator end) - - \overload - - Removes all the items from \a begin up to (but not including) \a - end. Returns an iterator to the same item that \a end referred to - before the call. -*/ - -/*! \fn template T& QVector::first() - - Returns a reference to the first item in the vector. This - function assumes that the vector isn't empty. - - \sa last(), isEmpty(), constFirst() -*/ - -/*! \fn template const T& QVector::first() const - - \overload -*/ - -/*! \fn template const T& QVector::constFirst() const - \since 5.6 - - Returns a const reference to the first item in the vector. This - function assumes that the vector isn't empty. - - \sa constLast(), isEmpty(), first() -*/ - -/*! \fn template T& QVector::last() - - Returns a reference to the last item in the vector. This function - assumes that the vector isn't empty. - - \sa first(), isEmpty(), constLast() -*/ - -/*! \fn template const T& QVector::last() const - - \overload -*/ - -/*! \fn template const T& QVector::constLast() const - \since 5.6 - - Returns a const reference to the last item in the vector. This function - assumes that the vector isn't empty. - - \sa constFirst(), isEmpty(), last() -*/ - -/*! \fn template T QVector::value(int i) const - - Returns the value at index position \a i in the vector. - - If the index \a i is out of bounds, the function returns - a \l{default-constructed value}. If you are certain that - \a i is within bounds, you can use at() instead, which is slightly - faster. - - \sa at(), operator[]() -*/ - -/*! \fn template T QVector::value(int i, const T &defaultValue) const - - \overload - - If the index \a i is out of bounds, the function returns - \a defaultValue. -*/ - -/*! \fn template void QVector::push_back(const T &value) - - This function is provided for STL compatibility. It is equivalent - to append(\a value). -*/ - -/*! \fn template void QVector::push_back(T &&value) - \since 5.6 - \overload -*/ - -/*! - \fn template void QVector::push_front(const T &value) - \fn template void QVector::push_front(T &&value) - - This function is provided for STL compatibility. It is equivalent - to prepend(\a value). -*/ - -/*! \fn template void QVector::pop_front() - - This function is provided for STL compatibility. It is equivalent - to removeFirst(). -*/ - -/*! \fn template void QVector::pop_back() - - This function is provided for STL compatibility. It is equivalent - to removeLast(). -*/ - -/*! \fn template T& QVector::front() - - This function is provided for STL compatibility. It is equivalent - to first(). -*/ - -/*! \fn template QVector::const_reference QVector::front() const - - \overload -*/ - -/*! \fn template QVector::reference QVector::back() - - This function is provided for STL compatibility. It is equivalent - to last(). -*/ - -/*! \fn template QVector::const_reference QVector::back() const - - \overload -*/ - -/*! \fn template void QVector::shrink_to_fit() - \since 5.10 - - This function is provided for STL compatibility. It is equivalent - to squeeze(). -*/ - -/*! \fn template bool QVector::empty() const - - This function is provided for STL compatibility. It is equivalent - to isEmpty(), returning \c true if the vector is empty; otherwise - returns \c false. -*/ - -/*! \fn template QVector &QVector::operator+=(const QVector &other) - - Appends the items of the \a other vector to this vector and - returns a reference to this vector. - - \sa operator+(), append() -*/ - -/*! \fn template void QVector::operator+=(const T &value) - - \overload - - Appends \a value to the vector. - - \sa append(), operator<<() -*/ - -/*! \fn template void QVector::operator+=(T &&value) - \since 5.11 - - \overload - - \sa append(), operator<<() -*/ - -/*! \fn template QVector QVector::operator+(const QVector &other) const - - Returns a vector that contains all the items in this vector - followed by all the items in the \a other vector. - - \sa operator+=() -*/ - -/*! \fn template QVector &QVector::operator<<(const T &value) - - Appends \a value to the vector and returns a reference to this - vector. - - \sa append(), operator+=() -*/ - -/*! \fn template QVector &QVector::operator<<(T &&value) - \since 5.11 - - \overload - - \sa append(), operator+=() -*/ - - -/*! \fn template QVector &QVector::operator<<(const QVector &other) - - Appends \a other to the vector and returns a reference to the - vector. -*/ - -/*! \typedef QVector::iterator - - The QVector::iterator typedef provides an STL-style non-const - iterator for QVector and QStack. - - QVector provides both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style non-const iterator is simply a typedef - for "T *" (pointer to T). - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QVector::begin(), QVector::end(), QVector::const_iterator, QMutableVectorIterator -*/ - -/*! \typedef QVector::const_iterator - - The QVector::const_iterator typedef provides an STL-style const - iterator for QVector and QStack. - - QVector provides both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style const iterator is simply a typedef for - "const T *" (pointer to const T). - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QVector::constBegin(), QVector::constEnd(), QVector::iterator, QVectorIterator -*/ - -/*! \typedef QVector::reverse_iterator - \since 5.6 - - The QVector::reverse_iterator typedef provides an STL-style non-const - reverse iterator for QVector. - - It is simply a typedef for \c{std::reverse_iterator}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QVector::rbegin(), QVector::rend(), QVector::const_reverse_iterator, QVector::iterator -*/ - -/*! \typedef QVector::const_reverse_iterator - \since 5.6 - - The QVector::const_reverse_iterator typedef provides an STL-style const - reverse iterator for QVector. - - It is simply a typedef for \c{std::reverse_iterator}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QVector::rbegin(), QVector::rend(), QVector::reverse_iterator, QVector::const_iterator -*/ - -/*! \typedef QVector::Iterator - - Qt-style synonym for QVector::iterator. -*/ - -/*! \typedef QVector::ConstIterator - - Qt-style synonym for QVector::const_iterator. -*/ - -/*! \typedef QVector::const_pointer - - Typedef for const T *. Provided for STL compatibility. -*/ - -/*! \typedef QVector::const_reference - - Typedef for T &. Provided for STL compatibility. -*/ - -/*! \typedef QVector::difference_type - - Typedef for ptrdiff_t. Provided for STL compatibility. -*/ - -/*! \typedef QVector::pointer - - Typedef for T *. Provided for STL compatibility. -*/ - -/*! \typedef QVector::reference - - Typedef for T &. Provided for STL compatibility. -*/ - -/*! \typedef QVector::size_type - - Typedef for int. Provided for STL compatibility. -*/ - -/*! \typedef QVector::value_type - - Typedef for T. Provided for STL compatibility. -*/ - -/*! \fn template QList QVector::toList() const - - Returns a QList object with the data contained in this QVector. - - Example: - - \snippet code/src_corelib_tools_qvector.cpp 14 - - \include containers-range-constructor.qdocinc - - \sa fromList(), QList::fromVector() -*/ - -/*! \fn template QVector QVector::fromList(const QList &list) - - Returns a QVector object with the data contained in \a list. - - Example: - - \snippet code/src_corelib_tools_qvector.cpp 15 - - \include containers-range-constructor.qdocinc - - \sa toList(), QList::toVector() -*/ - -/*! \fn template QVector QVector::fromStdVector(const std::vector &vector) - - Returns a QVector object with the data contained in \a vector. The - order of the elements in the QVector is the same as in \a vector. - - Example: - - \snippet code/src_corelib_tools_qvector.cpp 16 - - \include containers-range-constructor.qdocinc - - \sa toStdVector(), QList::fromStdList() -*/ - -/*! \fn template std::vector QVector::toStdVector() const - - Returns a std::vector object with the data contained in this QVector. - Example: - - \snippet code/src_corelib_tools_qvector.cpp 17 - - \include containers-range-constructor.qdocinc - - \sa fromStdVector(), QList::toStdList() -*/ - -/*! \fn template QDataStream &operator<<(QDataStream &out, const QVector &vector) - \relates QVector - - Writes the vector \a vector to stream \a out. - - This function requires the value type to implement \c operator<<(). - - \sa{Serializing Qt Data Types}{Format of the QDataStream operators} -*/ - -/*! \fn template QDataStream &operator>>(QDataStream &in, QVector &vector) - \relates QVector - - Reads a vector from stream \a in into \a vector. - - This function requires the value type to implement \c operator>>(). - - \sa{Serializing Qt Data Types}{Format of the QDataStream operators} -*/ -- cgit v1.2.3