From 1299a2330ae14a6a901f574ea01fc63715e1b87d Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 22 May 2019 15:57:45 +0200 Subject: Move QListSpecialMethods over to QVector Extend QVector with special methods for QByteArray and QString, just as QList had them in Qt 5. This also means that QStringList and QByteArrayList are now implemented through a QVector, not a QList anymore. QListIterator is now slightly source incompatible as QStringList is a QVector, but that will be fixed in a follow-up change when QList will start mapping to a QVector. Change-Id: I7cfb8a72d4d95b347bbd386892f244b7203b41c2 Reviewed-by: Simon Hausmann --- src/corelib/kernel/qmetatype.h | 2 +- src/corelib/text/qbytearraylist.h | 16 ++++----- src/corelib/text/qstringlist.h | 74 +++++++++++++++++++-------------------- src/corelib/tools/qlist.h | 13 +------ src/corelib/tools/qvector.h | 68 ++++++++++++++++++++++++++--------- 5 files changed, 99 insertions(+), 74 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index d04f327223..8b621f0161 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -1999,7 +1999,7 @@ typedef QHash QVariantHash; #ifdef Q_CLANG_QDOC class QByteArrayList; #else -typedef QList QByteArrayList; +typedef QVector QByteArrayList; #endif #define Q_DECLARE_METATYPE_TEMPLATE_1ARG(SINGLE_ARG_TEMPLATE) \ diff --git a/src/corelib/text/qbytearraylist.h b/src/corelib/text/qbytearraylist.h index 0250b649b8..4b6c926960 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 QListIterator QByteArrayListIterator; -typedef QMutableListIterator QMutableByteArrayListIterator; +typedef QVectorIterator QByteArrayListIterator; +typedef QMutableVectorIterator QMutableByteArrayListIterator; #endif #ifndef Q_CLANG_QDOC -typedef QList QByteArrayList; +typedef QVector 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 QList +class QByteArrayList : public QVector #else -template <> struct QListSpecialMethods +template <> struct QVectorSpecialMethods #endif { #ifndef Q_CLANG_QDOC protected: - ~QListSpecialMethods() = default; + ~QVectorSpecialMethods() = default; #endif public: inline QByteArray join() const @@ -84,7 +84,7 @@ public: { return QtPrivate::QByteArrayList_indexOf(self(), needle, from); } private: - typedef QList Self; + typedef QVector Self; Self *self() { return static_cast(this); } const Self *self() const { return static_cast(this); } }; diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index a464d443dc..6e0940c488 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 @@ -55,21 +55,21 @@ class QRegExp; class QRegularExpression; #if !defined(QT_NO_JAVA_STYLE_ITERATORS) -typedef QListIterator QStringListIterator; -typedef QMutableListIterator QMutableStringListIterator; +typedef QVectorIterator QStringListIterator; +typedef QMutableVectorIterator QMutableStringListIterator; #endif class QStringList; #ifdef Q_QDOC -class QStringList : public QList +class QStringList : public QVector #else -template <> struct QListSpecialMethods +template <> struct QVectorSpecialMethods #endif { #ifndef Q_QDOC protected: - ~QListSpecialMethods() = default; + ~QVectorSpecialMethods() = default; #endif public: inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive); @@ -108,23 +108,23 @@ private: }; // ### Qt6: check if there's a better way -class QStringList : public QList +class QStringList : public QVector { #endif public: inline QStringList() noexcept { } inline explicit QStringList(const QString &i) { append(i); } - inline QStringList(const QList &l) : QList(l) { } - inline QStringList(QList &&l) noexcept : QList(std::move(l)) { } - inline QStringList(std::initializer_list args) : QList(args) { } + inline QStringList(const QVector &l) : QVector(l) { } + inline QStringList(QVector &&l) noexcept : QVector(std::move(l)) { } + inline QStringList(std::initializer_list args) : QVector(args) { } template = true> inline QStringList(InputIterator first, InputIterator last) - : QList(first, last) { } + : QVector(first, last) { } - QStringList &operator=(const QList &other) - { QList::operator=(other); return *this; } - QStringList &operator=(QList &&other) noexcept - { QList::operator=(std::move(other)); return *this; } + QStringList &operator=(const QVector &other) + { QVector::operator=(other); return *this; } + QStringList &operator=(QVector &&other) noexcept + { QVector::operator=(std::move(other)); return *this; } #if QT_STRINGVIEW_LEVEL < 2 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; @@ -138,7 +138,7 @@ public: { append(str); return *this; } inline QStringList &operator<<(const QStringList &l) { *this += l; return *this; } - inline QStringList &operator<<(const QList &l) + inline QStringList &operator<<(const QVector &l) { *this += l; return *this; } inline int indexOf(QStringView str, int from = 0) const; @@ -159,16 +159,16 @@ public: inline int lastIndexOf(const QRegularExpression &re, int from = -1) const; #endif // QT_CONFIG(regularexpression) - using QList::indexOf; - using QList::lastIndexOf; + using QVector::indexOf; + using QVector::lastIndexOf; }; Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE); #ifndef Q_QDOC -inline QStringList *QListSpecialMethods::self() +inline QStringList *QVectorSpecialMethods::self() { return static_cast(this); } -inline const QStringList *QListSpecialMethods::self() const +inline const QStringList *QVectorSpecialMethods::self() const { return static_cast(this); } namespace QtPrivate { @@ -213,45 +213,45 @@ namespace QtPrivate { #endif // QT_CONFIG(regularexpression) } -inline void QListSpecialMethods::sort(Qt::CaseSensitivity cs) +inline void QVectorSpecialMethods::sort(Qt::CaseSensitivity cs) { QtPrivate::QStringList_sort(self(), cs); } -inline int QListSpecialMethods::removeDuplicates() +inline int QVectorSpecialMethods::removeDuplicates() { return QtPrivate::QStringList_removeDuplicates(self()); } #if QT_STRINGVIEW_LEVEL < 2 -inline QString QListSpecialMethods::join(const QString &sep) const +inline QString QVectorSpecialMethods::join(const QString &sep) const { return QtPrivate::QStringList_join(self(), sep.constData(), sep.length()); } #endif -inline QString QListSpecialMethods::join(QStringView sep) const +inline QString QVectorSpecialMethods::join(QStringView sep) const { return QtPrivate::QStringList_join(self(), sep); } -QString QListSpecialMethods::join(QLatin1String sep) const +QString QVectorSpecialMethods::join(QLatin1String sep) const { return QtPrivate::QStringList_join(*self(), sep); } -inline QString QListSpecialMethods::join(QChar sep) const +inline QString QVectorSpecialMethods::join(QChar sep) const { return QtPrivate::QStringList_join(self(), &sep, 1); } -inline QStringList QListSpecialMethods::filter(QStringView str, Qt::CaseSensitivity cs) const +inline QStringList QVectorSpecialMethods::filter(QStringView str, Qt::CaseSensitivity cs) const { return QtPrivate::QStringList_filter(self(), str, cs); } #if QT_STRINGVIEW_LEVEL < 2 -inline QStringList QListSpecialMethods::filter(const QString &str, Qt::CaseSensitivity cs) const +inline QStringList QVectorSpecialMethods::filter(const QString &str, Qt::CaseSensitivity cs) const { return QtPrivate::QStringList_filter(self(), str, cs); } @@ -274,33 +274,33 @@ inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const return QtPrivate::QStringList_contains(this, str, cs); } -inline QStringList &QListSpecialMethods::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs) +inline QStringList &QVectorSpecialMethods::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); return *self(); } #if QT_STRINGVIEW_LEVEL < 2 -inline QStringList &QListSpecialMethods::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) +inline QStringList &QVectorSpecialMethods::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); return *self(); } -inline QStringList &QListSpecialMethods::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs) +inline QStringList &QVectorSpecialMethods::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, qToStringViewIgnoringNull(after), cs); return *self(); } -inline QStringList &QListSpecialMethods::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs) +inline QStringList &QVectorSpecialMethods::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), QStringView(before), after, cs); return *self(); } #endif -inline QStringList operator+(const QList &one, const QStringList &other) +inline QStringList operator+(const QVector &one, const QStringList &other) { QStringList n = one; n += other; @@ -328,13 +328,13 @@ inline int QStringList::lastIndexOf(QLatin1String string, int from) const } #ifndef QT_NO_REGEXP -inline QStringList &QListSpecialMethods::replaceInStrings(const QRegExp &rx, const QString &after) +inline QStringList &QVectorSpecialMethods::replaceInStrings(const QRegExp &rx, const QString &after) { QtPrivate::QStringList_replaceInStrings(self(), rx, after); return *self(); } -inline QStringList QListSpecialMethods::filter(const QRegExp &rx) const +inline QStringList QVectorSpecialMethods::filter(const QRegExp &rx) const { return QtPrivate::QStringList_filter(self(), rx); } @@ -361,13 +361,13 @@ inline int QStringList::lastIndexOf(QRegExp &rx, int from) const #endif #if QT_CONFIG(regularexpression) -inline QStringList &QListSpecialMethods::replaceInStrings(const QRegularExpression &rx, const QString &after) +inline QStringList &QVectorSpecialMethods::replaceInStrings(const QRegularExpression &rx, const QString &after) { QtPrivate::QStringList_replaceInStrings(self(), rx, after); return *self(); } -inline QStringList QListSpecialMethods::filter(const QRegularExpression &rx) const +inline QStringList QVectorSpecialMethods::filter(const QRegularExpression &rx) const { return QtPrivate::QStringList_filter(self(), rx); } diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index ffd470efcd..5dde80417d 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -71,14 +71,6 @@ QT_BEGIN_NAMESPACE template class QVector; template class QSet; -template struct QListSpecialMethods -{ -protected: - ~QListSpecialMethods() = default; -}; -template <> struct QListSpecialMethods; -template <> struct QListSpecialMethods; - struct Q_CORE_EXPORT QListData { // tags for tag-dispatching of QList implementations, // based on QList's three different memory layouts: @@ -126,9 +118,6 @@ namespace QtPrivate { template class QList -#ifndef Q_QDOC - : public QListSpecialMethods -#endif { public: struct MemoryLayout @@ -848,7 +837,7 @@ Q_OUTOFLINE_TEMPLATE void QList::detach_helper() template Q_OUTOFLINE_TEMPLATE QList::QList(const QList &l) - : QListSpecialMethods(l), d(l.d) + : d(l.d) { if (!d->ref.ref()) { p.detach(d->alloc); diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 62fbdb4a2a..45f7c5b76b 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -59,12 +59,31 @@ 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; Data *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: inline QVector() noexcept : d(Data::sharedNull()) { } explicit QVector(int size); @@ -1004,38 +1023,52 @@ QVector &QVector::operator+=(const QVector &l) return *this; } -template -int QVector::indexOf(const T &t, int from) const +namespace QtPrivate { +template +int indexOf(const QVector &vector, const U &u, int from) { if (from < 0) - from = qMax(from + d->size, 0); - if (from < d->size) { - T* n = d->begin() + from - 1; - T* e = d->end(); + 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 == t) - return n - d->begin(); + if (*n == u) + return n - vector.begin(); } return -1; } -template -int QVector::lastIndexOf(const T &t, int from) const +template +int lastIndexOf(const QVector &vector, const U &u, int from) { if (from < 0) - from += d->size; - else if (from >= d->size) - from = d->size-1; + from += vector.d->size; + else if (from >= vector.size()) + from = vector.size() - 1; if (from >= 0) { - T* b = d->begin(); - T* n = d->begin() + from + 1; + auto b = vector.begin(); + auto n = vector.begin() + from + 1; while (n != b) { - if (*--n == t) + if (*--n == u) return n - b; } } return -1; } +} + +template +int QVector::indexOf(const T &t, int from) const +{ + return QtPrivate::indexOf(*this, t, from); +} + +template +int QVector::lastIndexOf(const T &t, int from) const +{ + return QtPrivate::lastIndexOf(*this, t, from); +} template bool QVector::contains(const T &t) const @@ -1153,4 +1186,7 @@ QVector QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt: QT_END_NAMESPACE +#include +#include + #endif // QVECTOR_H -- cgit v1.2.3