diff options
author | Lars Knoll <lars.knoll@qt.io> | 2019-05-24 14:27:48 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-10-30 11:52:48 +0100 |
commit | 5357231c0a10eef558cc6aebfd172048dc010a96 (patch) | |
tree | 47d13d107665105bc93734c064b11e47ad5392e2 /src | |
parent | 1299a2330ae14a6a901f574ea01fc63715e1b87d (diff) |
Make QList an alias to QVector
This is almost 100% source compatible with Qt 5. Exceptions are
* Stability of references for large or non movable types
* taking a PMF for types that are now overloaded with r-value references
in QVector
* The missing prepend optimization in QVector (that is still planned
to come for Qt 6)
Change-Id: I96d44553304dd623def9c70d6fea8fa2fb0373b0
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/global/qtypeinfo.h | 1 | ||||
-rw-r--r-- | src/corelib/io/qdebug.h | 6 | ||||
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.h | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 4 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobject_moc_p.h | 3 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.h | 9 | ||||
-rw-r--r-- | src/corelib/serialization/qcborvalue_p.h | 2 | ||||
-rw-r--r-- | src/corelib/serialization/qdatastream.h | 12 | ||||
-rw-r--r-- | src/corelib/tools/qcontainerfwd.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qlist.cpp | 2025 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 1142 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 11 | ||||
-rw-r--r-- | src/dbus/qdbusmisc.cpp | 5 | ||||
-rw-r--r-- | src/tools/bootstrap/bootstrap.pro | 1 | ||||
-rw-r--r-- | src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp | 3 | ||||
-rw-r--r-- | src/widgets/itemviews/qlistwidget.cpp | 4 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreewidget.cpp | 2 |
17 files changed, 32 insertions, 3202 deletions
diff --git a/src/corelib/global/qtypeinfo.h b/src/corelib/global/qtypeinfo.h index 30be47296e..34cf1de4f5 100644 --- a/src/corelib/global/qtypeinfo.h +++ b/src/corelib/global/qtypeinfo.h @@ -209,7 +209,6 @@ public: \ }; \ } -Q_DECLARE_MOVABLE_CONTAINER(QList); Q_DECLARE_MOVABLE_CONTAINER(QVector); Q_DECLARE_MOVABLE_CONTAINER(QQueue); Q_DECLARE_MOVABLE_CONTAINER(QStack); diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 421c5d933b..2314b36bda 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -234,12 +234,6 @@ inline QDebug printSequentialContainer(QDebug debug, const char *which, const Se } // namespace QtPrivate -template <class T> -inline QDebug operator<<(QDebug debug, const QList<T> &list) -{ - return QtPrivate::printSequentialContainer(debug, "" /*for historical reasons*/, list); -} - template <typename T> inline QDebug operator<<(QDebug debug, const QVector<T> &vec) { diff --git a/src/corelib/itemmodels/qitemselectionmodel.h b/src/corelib/itemmodels/qitemselectionmodel.h index 5820695592..5421eb2afa 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.h +++ b/src/corelib/itemmodels/qitemselectionmodel.h @@ -241,7 +241,7 @@ inline uint qHash(const QItemSelectionRange &) { return 0; } # define Q_TEMPLATE_EXTERN extern # endif # endif -Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList<QItemSelectionRange>; +Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QItemSelectionRange>; #endif // Q_CC_MSVC class Q_CORE_EXPORT QItemSelection : public QList<QItemSelectionRange> diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index a8e8866339..b1e1bb8b4a 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -682,7 +682,9 @@ static void argumentTypesFromString(const char *str, const char *end, --level; ++str; } - types += QArgumentType(QByteArray(begin, str - begin)); + QByteArray argType(begin, str - begin); + argType.replace("QList<", "QVector<"); + types += QArgumentType(std::move(argType)); } } diff --git a/src/corelib/kernel/qmetaobject_moc_p.h b/src/corelib/kernel/qmetaobject_moc_p.h index 8c7900767b..471e43f85b 100644 --- a/src/corelib/kernel/qmetaobject_moc_p.h +++ b/src/corelib/kernel/qmetaobject_moc_p.h @@ -213,6 +213,9 @@ static QByteArray normalizeTypeInternal(const char *t, const char *e, bool fixSc } } + // Qt 5 compatibility, make sure we use the correct type name for QList + result.replace("QList<", "QVector<"); + return result; } diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 8b621f0161..7b985be9fd 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -207,10 +207,10 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId(); F(UInt, -1, uint, "quint32") \ F(LongLong, -1, qlonglong, "qint64") \ F(ULongLong, -1, qulonglong, "quint64") \ - F(QVariantList, -1, QVariantList, "QList<QVariant>") \ + F(QVariantList, -1, QVariantList, "QVector<QVariant>") \ F(QVariantMap, -1, QVariantMap, "QMap<QString,QVariant>") \ F(QVariantHash, -1, QVariantHash, "QHash<QString,QVariant>") \ - F(QByteArrayList, -1, QByteArrayList, "QList<QByteArray>") \ + F(QByteArrayList, -1, QByteArrayList, "QVector<QByteArray>") \ #define QT_FOR_EACH_STATIC_TYPE(F)\ QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F)\ @@ -225,7 +225,6 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId(); TypeName = Id, #define QT_FOR_EACH_AUTOMATIC_TEMPLATE_1ARG(F) \ - F(QList) \ F(QVector) \ F(QQueue) \ F(QStack) \ @@ -1004,10 +1003,6 @@ struct ContainerAPI : CapabilitiesImpl<T> }; template<typename T> -struct ContainerAPI<QList<T> > : CapabilitiesImpl<QList<T> > -{ static int size(const QList<T> *t) { return t->size(); } }; - -template<typename T> struct ContainerAPI<QVector<T> > : CapabilitiesImpl<QVector<T> > { static int size(const QVector<T> *t) { return t->size(); } }; diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index 38ad54047f..d18d108969 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -215,7 +215,7 @@ public: } void insertAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp = CopyContainer) { - replaceAt_internal(*elements.insert(elements.begin() + idx, {}), value, disp); + replaceAt_internal(*elements.insert(elements.begin() + int(idx), {}), value, disp); } void append(QtCbor::Undefined) diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h index bcfcd47ccf..20d3953652 100644 --- a/src/corelib/serialization/qdatastream.h +++ b/src/corelib/serialization/qdatastream.h @@ -389,18 +389,6 @@ typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type& operator>>(QDataStream &s, T &t) { return s >> reinterpret_cast<typename std::underlying_type<T>::type &>(t); } -template <typename T> -inline QDataStream &operator>>(QDataStream &s, QList<T> &l) -{ - return QtPrivate::readArrayBasedContainer(s, l); -} - -template <typename T> -inline QDataStream &operator<<(QDataStream &s, const QList<T> &l) -{ - return QtPrivate::writeSequentialContainer(s, l); -} - template<typename T> inline QDataStream &operator>>(QDataStream &s, QVector<T> &v) { diff --git a/src/corelib/tools/qcontainerfwd.h b/src/corelib/tools/qcontainerfwd.h index 532b4c95ce..f6efa99b6a 100644 --- a/src/corelib/tools/qcontainerfwd.h +++ b/src/corelib/tools/qcontainerfwd.h @@ -50,7 +50,6 @@ template <class Key, class T> class QHash; #ifndef QT_NO_LINKED_LIST template <class T> class QLinkedList; #endif -template <class T> class QList; template <class Key, class T> class QMap; template <class Key, class T> class QMultiHash; template <class Key, class T> class QMultiMap; @@ -60,6 +59,7 @@ template <class T> class QSet; template <class T> class QStack; template<class T, int Prealloc = 256> class QVarLengthArray; template <class T> class QVector; +template<typename T> using QList = QVector<T>; QT_END_NAMESPACE diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 5d5da20752..62201fd5d6 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -63,2029 +63,4 @@ template class Q_CORE_EXPORT QVector<QPointF>; template class Q_CORE_EXPORT QVector<QPoint>; #endif - -/* - QList as an array-list combines the easy-of-use of a random - access interface with fast list operations and the low memory - management overhead of an array. Accessing elements by index, - appending, prepending, and removing elements from both the front - and the back all happen in constant time O(1). Inserting or - removing elements at random index positions \ai happens in linear - time, or more precisly in O(min{i,n-i}) <= O(n/2), with n being - the number of elements in the list. -*/ - -const QListData::Data QListData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, { nullptr } }; - -/*! - * Detaches the QListData by allocating new memory for a list which will be bigger - * than the copied one and is expected to grow further. - * *idx is the desired insertion point and is clamped to the actual size of the list. - * num is the number of new elements to insert at the insertion point. - * Returns the old (shared) data, it is up to the caller to deref() and free(). - * For the new data node_copy needs to be called. - * - * \internal - */ -QListData::Data *QListData::detach_grow(int *idx, int num) -{ - Data *x = d; - int l = x->end - x->begin; - int nl = l + num; - auto blockInfo = qCalculateGrowingBlockSize(nl, sizeof(void *), DataHeaderSize); - Data* t = static_cast<Data *>(::malloc(blockInfo.size)); - Q_CHECK_PTR(t); - t->alloc = int(uint(blockInfo.elementCount)); - - t->ref.initializeOwned(); - // The space reservation algorithm's optimization is biased towards appending: - // Something which looks like an append will put the data at the beginning, - // while something which looks like a prepend will put it in the middle - // instead of at the end. That's based on the assumption that prepending - // is uncommon and even an initial prepend will eventually be followed by - // at least some appends. - int bg; - if (*idx < 0) { - *idx = 0; - bg = (t->alloc - nl) >> 1; - } else if (*idx > l) { - *idx = l; - bg = 0; - } else if (*idx < (l >> 1)) { - bg = (t->alloc - nl) >> 1; - } else { - bg = 0; - } - t->begin = bg; - t->end = bg + nl; - d = t; - - return x; -} - -/*! - * Detaches the QListData by allocating new memory for a list which possibly - * has a different size than the copied one. - * Returns the old (shared) data, it is up to the caller to deref() and free() - * For the new data node_copy needs to be called. - * - * \internal - */ -QListData::Data *QListData::detach(int alloc) -{ - Data *x = d; - Data* t = static_cast<Data *>(::malloc(qCalculateBlockSize(alloc, sizeof(void*), DataHeaderSize))); - Q_CHECK_PTR(t); - - t->ref.initializeOwned(); - t->alloc = alloc; - if (!alloc) { - t->begin = 0; - t->end = 0; - } else { - t->begin = x->begin; - t->end = x->end; - } - d = t; - - return x; -} - -void QListData::realloc(int alloc) -{ - Q_ASSERT(!d->ref.isShared()); - Data *x = static_cast<Data *>(::realloc(d, qCalculateBlockSize(alloc, sizeof(void *), DataHeaderSize))); - Q_CHECK_PTR(x); - - d = x; - d->alloc = alloc; - if (!alloc) - d->begin = d->end = 0; -} - -void QListData::realloc_grow(int growth) -{ - Q_ASSERT(!d->ref.isShared()); - auto r = qCalculateGrowingBlockSize(d->alloc + growth, sizeof(void *), DataHeaderSize); - Data *x = static_cast<Data *>(::realloc(d, r.size)); - Q_CHECK_PTR(x); - - d = x; - d->alloc = int(uint(r.elementCount)); -} - -void QListData::dispose(Data *d) -{ - Q_ASSERT(!d->ref.isShared()); - free(d); -} - -// ensures that enough space is available to append n elements -void **QListData::append(int n) -{ - Q_ASSERT(!d->ref.isShared()); - int e = d->end; - if (e + n > d->alloc) { - int b = d->begin; - if (b - n >= 2 * d->alloc / 3) { - // we have enough space. Just not at the end -> move it. - e -= b; - ::memcpy(d->array, d->array + b, e * sizeof(void *)); - d->begin = 0; - } else { - realloc_grow(n); - } - } - d->end = e + n; - return d->array + e; -} - -// ensures that enough space is available to append one element -void **QListData::append() -{ - return append(1); -} - -// ensures that enough space is available to append the list -void **QListData::append(const QListData& l) -{ - return append(l.d->end - l.d->begin); -} - -void **QListData::prepend() -{ - Q_ASSERT(!d->ref.isShared()); - if (d->begin == 0) { - if (d->end >= d->alloc / 3) - realloc_grow(1); - - if (d->end < d->alloc / 3) - d->begin = d->alloc - 2 * d->end; - else - d->begin = d->alloc - d->end; - - ::memmove(d->array + d->begin, d->array, d->end * sizeof(void *)); - d->end += d->begin; - } - return d->array + --d->begin; -} - -void **QListData::insert(int i) -{ - Q_ASSERT(!d->ref.isShared()); - if (i <= 0) - return prepend(); - int size = d->end - d->begin; - if (i >= size) - return append(); - - bool leftward = false; - - if (d->begin == 0) { - if (d->end == d->alloc) { - // If the array is full, we expand it and move some items rightward - realloc_grow(1); - } else { - // If there is free space at the end of the array, we move some items rightward - } - } else { - if (d->end == d->alloc) { - // If there is free space at the beginning of the array, we move some items leftward - leftward = true; - } else { - // If there is free space at both ends, we move as few items as possible - leftward = (i < size - i); - } - } - - if (leftward) { - --d->begin; - ::memmove(d->array + d->begin, d->array + d->begin + 1, i * sizeof(void *)); - } else { - ::memmove(d->array + d->begin + i + 1, d->array + d->begin + i, - (size - i) * sizeof(void *)); - ++d->end; - } - return d->array + d->begin + i; -} - -void QListData::remove(int i) -{ - Q_ASSERT(!d->ref.isShared()); - i += d->begin; - if (i - d->begin < d->end - i) { - if (int offset = i - d->begin) - ::memmove(d->array + d->begin + 1, d->array + d->begin, offset * sizeof(void *)); - d->begin++; - } else { - if (int offset = d->end - i - 1) - ::memmove(d->array + i, d->array + i + 1, offset * sizeof(void *)); - d->end--; - } -} - -void QListData::remove(int i, int n) -{ - Q_ASSERT(!d->ref.isShared()); - i += d->begin; - int middle = i + n/2; - if (middle - d->begin < d->end - middle) { - ::memmove(d->array + d->begin + n, d->array + d->begin, - (i - d->begin) * sizeof(void*)); - d->begin += n; - } else { - ::memmove(d->array + i, d->array + i + n, - (d->end - i - n) * sizeof(void*)); - d->end -= n; - } -} - -void QListData::move(int from, int to) -{ - Q_ASSERT(!d->ref.isShared()); - if (from == to) - return; - - from += d->begin; - to += d->begin; - void *t = d->array[from]; - - if (from < to) { - if (d->end == d->alloc || 3 * (to - from) < 2 * (d->end - d->begin)) { - ::memmove(d->array + from, d->array + from + 1, (to - from) * sizeof(void *)); - } else { - // optimization - if (int offset = from - d->begin) - ::memmove(d->array + d->begin + 1, d->array + d->begin, offset * sizeof(void *)); - if (int offset = d->end - (to + 1)) - ::memmove(d->array + to + 2, d->array + to + 1, offset * sizeof(void *)); - ++d->begin; - ++d->end; - ++to; - } - } else { - if (d->begin == 0 || 3 * (from - to) < 2 * (d->end - d->begin)) { - ::memmove(d->array + to + 1, d->array + to, (from - to) * sizeof(void *)); - } else { - // optimization - if (int offset = to - d->begin) - ::memmove(d->array + d->begin - 1, d->array + d->begin, offset * sizeof(void *)); - if (int offset = d->end - (from + 1)) - ::memmove(d->array + from, d->array + from + 1, offset * sizeof(void *)); - --d->begin; - --d->end; - --to; - } - } - d->array[to] = t; -} - -void **QListData::erase(void **xi) -{ - Q_ASSERT(!d->ref.isShared()); - int i = xi - (d->array + d->begin); - remove(i); - return d->array + d->begin + i; -} - -/*! \class QList - \inmodule QtCore - \brief The QList class is a template class that provides lists. - - \ingroup tools - \ingroup shared - - \reentrant - - QList\<T\> is one of Qt's generic \l{container classes}. It - stores items in a list that provides fast index-based access - and index-based insertions and removals. - - QList\<T\>, QLinkedList\<T\>, and QVector\<T\> 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\<T\> will usually give better performance than QList\<T\>, - because QVector\<T\> always stores its items sequentially in memory, - where QList\<T\> 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 QLinkedList. - \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. - - \note Iterators into a QLinkedList and references into - heap-allocating QLists remain valid as long as the referenced items - remain in the container. This is not true for iterators and - references into a QVector and non-heap-allocating QLists. - - Internally, QList\<T\> is represented as an array of T if - \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}. Otherwise, QList\<T\> is represented - as an array of T* and the items are allocated on the heap. - - The array representation allows very fast insertions and - index-based access. The prepend() and append() operations are - also very fast because QList preallocates memory at both - ends of its internal array. (See \l{Algorithmic Complexity} for - details. - - Note, however, that when the conditions specified above are not met, - each append or insert of a new item requires allocating the new item - on the heap, and this per item allocation will make QVector a better - choice for use cases that do a lot of appending or inserting, because - QVector can allocate memory for many items in a single heap allocation. - - Note that the internal array only ever gets bigger over the life - of the list. It never shrinks. The internal array is deallocated - by the destructor and by the assignment operator, when one list - is assigned to another. - - Here's an example of a QList that stores integers and - a QList that stores QDate values: - - \snippet code/src_corelib_tools_qlistdata.cpp 0 - - Qt includes a QStringList class that inherits QList\<QString\> - and adds a few convenience functions, such as QStringList::join() - and QStringList::filter(). QString::split() creates QStringLists - from strings. - - QList stores a list of items. The default constructor creates an - empty list. You can use the initializer-list constructor to create - a list with elements: - - \snippet code/src_corelib_tools_qlistdata.cpp 1a - - QList provides these basic functions to add, move, and remove - items: insert(), replace(), removeAt(), move(), and swap(). In - addition, it provides the following convenience functions: - append(), \l{operator<<()}, \l{operator+=()}, prepend(), removeFirst(), - and removeLast(). - - \l{operator<<()} allows to conveniently add multiple elements to a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 1b - - 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 lists, operator[]() returns a reference to the item and - can be used on the left side of an assignment: - - \snippet code/src_corelib_tools_qlistdata.cpp 2 - - Because QList is implemented as an array of pointers for types - that are larger than a pointer or are not movable, this operation - requires (\l{Algorithmic Complexity}{constant time}). For read-only - access, an alternative syntax is to use at(): - - \snippet code/src_corelib_tools_qlistdata.cpp 3 - - at() can be faster than operator[](), because it never causes a - \l{deep copy} to occur. - - A common requirement is to remove an item from a list and do - something with it. For this, QList provides takeAt(), takeFirst(), - and takeLast(). Here's a loop that removes the items from a list - one at a time and calls \c delete on them: - - \snippet code/src_corelib_tools_qlistdata.cpp 4 - - Inserting and removing items at either end of the list is very - fast (\l{Algorithmic Complexity}{constant time} in most cases), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - If you want to find all occurrences of a particular value in a - list, use indexOf() or lastIndexOf(). The former searches forward - starting from a given index position, the latter searches - backward. Both return the index of a matching item if they find - it; otherwise, they return -1. For example: - - \snippet code/src_corelib_tools_qlistdata.cpp 5 - - If you simply want to check whether a list contains a particular - value, use contains(). If you want to find out how many times a - particular value occurs in the list, use count(). If you want to - replace all occurrences of a particular value with another, use - replace(). - - 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 QMutableListIterator) 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. QList is implemented in such a way - that direct index-based access is just as fast as using iterators. - - 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. - - To make QList as efficient as possible, its member functions don't - validate their input before using it. Except for isEmpty(), member - functions always assume the list is \e not empty. Member functions - that take index values as parameters always assume their index - value parameters are in the valid range. This means QList member - functions can fail. If you define QT_NO_DEBUG when you compile, - failures will not be detected. If you \e don't define QT_NO_DEBUG, - failures will be detected using Q_ASSERT() or Q_ASSERT_X() with an - appropriate message. - - To avoid failures when your list can be empty, call isEmpty() - before calling other member functions. If you must pass an index - value that might not be in the valid range, check that it is less - than the value returned by size() but \e not less than 0. - - \section1 More Members - - If T is a QByteArray, this class has a couple more members that can be - used. See the documentation for QByteArrayList for more information. - - If T is QString, this class has the following additional members: - \l{QStringList::filter()}{filter}, - \l{QStringList::join()}{join}, - \l{QStringList::removeDuplicates()}{removeDuplicates}, - \l{QStringList::sort()}{sort}. - - \section1 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}. - - \sa QListIterator, QMutableListIterator, QLinkedList, QVector -*/ - -/*! - \fn template <class T> QList<T>::QList(QList<T> &&other) - - Move-constructs a QList instance, making it point at the same - object that \a other was pointing to. - - \since 5.2 -*/ - -/*! \fn template <class T> template<typename InputIterator> QList<T>::QList(InputIterator first, InputIterator last) - \since 5.14 - - Constructs a QList 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 <class T> QList<T> QList<T>::mid(int pos, int length) const - - Returns a sub-list which includes elements from this list, - starting at position \a pos. If \a length is -1 (the default), all - elements from \a pos are included; otherwise \a length elements (or - all remaining elements if there are less than \a length elements) - are included. -*/ - -/*! \fn template <class T> QList<T>::QList() - - Constructs an empty list. -*/ - -/*! \fn template <class T> QList<T>::QList(const QList<T> &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 <class T> QList<T>::QList(std::initializer_list<T> args) - \since 4.8 - - Construct a list from the std::initializer_list specified by \a args. - - This constructor is only enabled if the compiler supports C++11 initializer - lists. -*/ - -/*! \fn template <class T> QList<T>::~QList() - - Destroys the list. References to the values in the list and all - iterators of this list become invalid. -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator=(const QList<T> &other) - - Assigns \a other to this list and returns a reference to this - list. -*/ - -/*! - \fn template <class T> QList &QList<T>::operator=(QList<T> &&other) - - Move-assigns \a other to this QList instance. - - \since 5.2 -*/ - -/*! \fn template <class T> void QList<T>::swap(QList<T> &other) - \since 4.8 - - Swaps list \a other with this list. This operation is very - fast and never fails. -*/ - -/*! \fn template <class T> bool QList<T>::operator==(const QList<T> &other) const - - Returns \c true if \a other is equal to this list; otherwise returns - false. - - Two lists 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 <class T> bool QList<T>::operator!=(const QList<T> &other) const - - Returns \c true if \a other is not equal to this list; otherwise - returns \c false. - - Two lists 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 <class T> bool operator<(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \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 <class T> bool operator<=(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \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 <class T> bool operator>(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \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 <class T> bool operator>=(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \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 <class T> uint qHash(const QList<T> &key, uint 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 <class T> int QList<T>::size() const - - Returns the number of items in the list. - - \sa isEmpty(), count() -*/ - -/*! \fn template <class T> void QList<T>::detach() - - \internal -*/ - -/*! \fn template <class T> void QList<T>::detachShared() - - \internal - - like detach(), but does nothing if we're shared_null. - This prevents needless mallocs, and makes QList more exception safe - in case of cleanup work done in destructors on empty lists. -*/ - -/*! \fn template <class T> bool QList<T>::isDetached() const - - \internal -*/ - -/*! \fn template <class T> void QList<T>::setSharable(bool sharable) - - \internal -*/ - -/*! \fn template <class T> bool QList<T>::isSharedWith(const QList<T> &other) const - - \internal -*/ - -/*! \fn template <class T> bool QList<T>::isEmpty() const - - Returns \c true if the list contains no items; otherwise returns - false. - - \sa size() -*/ - -/*! \fn template <class T> void QList<T>::clear() - - Removes all items from the list. - - \sa removeAll() -*/ - -/*! \fn template <class T> const T &QList<T>::at(int i) const - - Returns the item at index position \a i in the list. \a i must be - a valid index position in the list (i.e., 0 <= \a i < size()). - - This function is very fast (\l{Algorithmic Complexity}{constant time}). - - \sa value(), operator[]() -*/ - -/*! \fn template <class T> T &QList<T>::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 list (i.e., 0 <= \a i < - size()). - - If this function is called on a list that is currently being shared, it - will trigger a copy of all elements. Otherwise, this function runs in - \l{Algorithmic Complexity}{constant time}. If you do not want to modify - the list you should use QList::at(). - - \sa at(), value() -*/ - -/*! \fn template <class T> const T &QList<T>::operator[](int i) const - - \overload - - Same as at(). This function runs in \l{Algorithmic Complexity}{constant time}. -*/ - -/*! \fn template <class T> void QList<T>::reserve(int alloc) - - Reserve space for \a alloc elements. - - If \a alloc is smaller than the current size of the list, nothing will happen. - - Use this function to avoid repetetive reallocation of QList's internal - data if you can predict how many elements will be appended. - Note that the reservation applies only to the internal pointer array. - - \since 4.7 -*/ - -/*! \fn template <class T> void QList<T>::append(const T &value) - - Inserts \a value at the end of the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 6 - - This is the same as list.insert(size(), \a value). - - If this list is not shared, this operation is typically - very fast (amortized \l{Algorithmic Complexity}{constant time}), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - \sa operator<<(), prepend(), insert() -*/ - -/*! \fn template <class T> void QList<T>::append(const QList<T> &value) - - \overload - - \since 4.5 - - Appends the items of the \a value list to this list. - - \sa operator<<(), operator+=() -*/ - -/*! \fn template <class T> void QList<T>::prepend(const T &value) - - Inserts \a value at the beginning of the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 7 - - This is the same as list.insert(0, \a value). - - If this list is not shared, this operation is typically - very fast (amortized \l{Algorithmic Complexity}{constant time}), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - \sa append(), insert() -*/ - -/*! \fn template <class T> void QList<T>::insert(int i, const T &value) - - Inserts \a value at index position \a i in the list. - - If \a i == 0, the value is prepended to the list. If \a i == size(), - the value is appended to the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 8 - - \sa append(), prepend(), replace(), removeAt() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::insert(iterator before, const 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. Note that the iterator passed to the function will be - invalid after the call; the returned iterator should be used - instead. -*/ - -/*! \fn template <class T> void QList<T>::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 list (i.e., 0 <= \a i < size()). - - \sa operator[](), removeAt() -*/ - -/*! - \fn template <class T> int QList<T>::removeAll(const T &value) - - Removes all occurrences of \a value in the list and returns the - number of entries removed. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 9 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa removeOne(), removeAt(), takeAt(), replace() -*/ - -/*! - \fn template <class T> bool QList<T>::removeOne(const T &value) - \since 4.4 - - Removes the first occurrence of \a value in the list and returns - true on success; otherwise returns \c false. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 10 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa removeAll(), removeAt(), takeAt(), replace() -*/ - -/*! \fn template <class T> void QList<T>::removeAt(int i) - - Removes the item at index position \a i. \a i must be a valid - index position in the list (i.e., 0 <= \a i < size()). - - \sa takeAt(), removeFirst(), removeLast(), removeOne() -*/ - -/*! \fn template <class T> T QList<T>::takeAt(int i) - - Removes the item at index position \a i and returns it. \a i must - be a valid index position in the list (i.e., 0 <= \a i < size()). - - If you don't use the return value, removeAt() is more efficient. - - \sa removeAt(), takeFirst(), takeLast() -*/ - -/*! \fn template <class T> T QList<T>::takeFirst() - - Removes the first item in the list and returns it. This is the - same as takeAt(0). This function assumes the list is not empty. To - avoid failure, call isEmpty() before calling this function. - - If this list is not shared, this operation takes - \l {Algorithmic Complexity}{constant time}. - - If you don't use the return value, removeFirst() is more - efficient. - - \sa takeLast(), takeAt(), removeFirst() -*/ - -/*! \fn template <class T> T QList<T>::takeLast() - - Removes the last item in the list and returns it. This is the - same as takeAt(size() - 1). This function assumes the list is - not empty. To avoid failure, call isEmpty() before calling this - function. - - If this list is not shared, this operation takes - \l {Algorithmic Complexity}{constant time}. - - If you don't use the return value, removeLast() is more - efficient. - - \sa takeFirst(), takeAt(), removeLast() -*/ - -/*! \fn template <class T> void QList<T>::move(int from, int to) - - Moves the item at index position \a from to index position \a to. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 11 - - This is the same as insert(\a{to}, takeAt(\a{from})).This function - assumes that both \a from and \a to are at least 0 but less than - size(). To avoid failure, test that both \a from and \a to are at - least 0 and less than size(). - - \sa swap(), insert(), takeAt() -*/ - -/*! \fn template <class T> void QList<T>::swap(int i, int j) - - \obsolete Use swapItemsAt() - - \sa move(), swapItemsAt() -*/ - -/*! \fn template <class T> void QList<T>::swapItemsAt(int i, int j) - \since 5.13 - - 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(). - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 12 - - \sa move() -*/ - -/*! \fn template <class T> int QList<T>::indexOf(const T &value, int from = 0) const - - Returns the index position of the first occurrence of \a value in - the list, searching forward from index position \a from. Returns - -1 if no item matched. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 13 - - This function requires the value type to have an implementation of - \c operator==(). - - Note that QList uses 0-based indexes, just like C++ arrays. Negative - indexes are not supported with the exception of the value mentioned - above. - - \sa lastIndexOf(), contains() -*/ - -/*! \fn template <class T> int QList<T>::lastIndexOf(const T &value, int from = -1) const - - Returns the index position of the last occurrence of \a value in - the list, 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_qlistdata.cpp 14 - - This function requires the value type to have an implementation of - \c operator==(). - - Note that QList uses 0-based indexes, just like C++ arrays. Negative - indexes are not supported with the exception of the value mentioned - above. - - \sa indexOf() -*/ - -/*! \fn template <class T> bool QList<T>::contains(const T &value) const - - Returns \c true if the list 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 <class T> int QList<T>::count(const T &value) const - - Returns the number of occurrences of \a value in the list. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa contains(), indexOf() -*/ - -/*! \fn template <class T> bool QList<T>::startsWith(const T &value) const - \since 4.5 - - Returns \c true if this list is not empty and its first - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), contains() -*/ - -/*! \fn template <class T> bool QList<T>::endsWith(const T &value) const - \since 4.5 - - Returns \c true if this list is not empty and its last - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), contains() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::begin() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in - the list. - - \sa constBegin(), end() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::begin() const - - \overload -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::cbegin() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), cend() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::constBegin() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::end() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item - after the last item in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template <class T> const_iterator QList<T>::end() const - - \overload -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::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 list. - - \sa cbegin(), end() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::constEnd() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the list. - - \sa constBegin(), end() -*/ - -/*! \fn template <class T> QList<T>::reverse_iterator QList<T>::rbegin() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), crbegin(), rend() -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::rbegin() const - \since 5.6 - \overload -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::crbegin() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), rbegin(), rend() -*/ - -/*! \fn template <class T> QList<T>::reverse_iterator QList<T>::rend() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past - the last item in the list, in reverse order. - - \sa end(), crend(), rbegin() -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::rend() const - \since 5.6 - \overload -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::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 list, in reverse order. - - \sa end(), rend(), rbegin() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::erase(iterator pos) - - Removes the item associated with the iterator \a pos from the - list, and returns an iterator to the next item in the list (which - may be end()). - - \sa insert(), removeAt() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::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. -*/ - -/*! \typedef QList::Iterator - - Qt-style synonym for QList::iterator. -*/ - -/*! \typedef QList::ConstIterator - - Qt-style synonym for QList::const_iterator. -*/ - -/*! - \typedef QList::size_type - - Typedef for int. Provided for STL compatibility. -*/ - -/*! - \typedef QList::value_type - - 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::const_pointer - - Typedef for const T *. Provided for STL compatibility. -*/ - -/*! - \typedef QList::reference - - Typedef for T &. Provided for STL compatibility. -*/ - -/*! - \typedef QList::const_reference - - Typedef for const T &. Provided for STL compatibility. -*/ - -/*! \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<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<const_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 -*/ - -/*! \fn template <class T> int QList<T>::count() const - - Returns the number of items in the list. This is effectively the - same as size(). -*/ - -/*! \fn template <class T> int QList<T>::length() const - \since 4.5 - - This function is identical to count(). - - \sa count() -*/ - -/*! \fn template <class T> T& QList<T>::first() - - Returns a reference to the first item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constFirst(), last(), isEmpty() -*/ - -/*! \fn template <class T> const T& QList<T>::first() const - - \overload -*/ - -/*! \fn template <class T> const T& QList<T>::constFirst() const - \since 5.6 - - Returns a const reference to the first item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constLast(), isEmpty(), first() -*/ - -/*! \fn template <class T> T& QList<T>::last() - - Returns a reference to the last item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constLast(), first(), isEmpty() -*/ - -/*! \fn template <class T> const T& QList<T>::last() const - - \overload -*/ - -/*! \fn template <class T> const T& QList<T>::constLast() const - \since 5.6 - - Returns a reference to the last item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constFirst(), isEmpty(), last() -*/ - -/*! \fn template <class T> void QList<T>::removeFirst() - - Removes the first item in the list. Calling this function is - equivalent to calling removeAt(0). The list must not be empty. If - the list can be empty, call isEmpty() before calling this - function. - - \sa removeAt(), takeFirst() -*/ - -/*! \fn template <class T> void QList<T>::removeLast() - - Removes the last item in the list. Calling this function is - equivalent to calling removeAt(size() - 1). The list must not be - empty. If the list can be empty, call isEmpty() before calling - this function. - - \sa removeAt(), takeLast() -*/ - -/*! \fn template <class T> T QList<T>::value(int i) const - - Returns the value at index position \a i in the list. - - If the index \a i is out of bounds, the function returns a - \l{default-constructed value}. If you are certain that the index - is going to be within bounds, you can use at() instead, which is - slightly faster. - - \sa at(), operator[]() -*/ - -/*! \fn template <class T> T QList<T>::value(int i, const T &defaultValue) const - - \overload - - If the index \a i is out of bounds, the function returns - \a defaultValue. -*/ - -/*! \fn template <class T> void QList<T>::push_back(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::append()}{append(\a value)}. -*/ - -/*! \fn template <class T> void QList<T>::push_front(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::prepend()}{prepend(\a value)}. -*/ - -/*! \fn template <class T> T& QList<T>::front() - - This function is provided for STL compatibility. It is equivalent - to first(). The list must not be empty. If the list can be empty, - call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> const T& QList<T>::front() const - - \overload -*/ - -/*! \fn template <class T> T& QList<T>::back() - - This function is provided for STL compatibility. It is equivalent - to last(). The list must not be empty. If the list can be empty, - call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> const T& QList<T>::back() const - - \overload -*/ - -/*! \fn template <class T> void QList<T>::pop_front() - - This function is provided for STL compatibility. It is equivalent - to removeFirst(). The list must not be empty. If the list can be - empty, call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> void QList<T>::pop_back() - - This function is provided for STL compatibility. It is equivalent - to removeLast(). The list must not be empty. If the list can be - empty, call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> bool QList<T>::empty() const - - This function is provided for STL compatibility. It is equivalent - to isEmpty() and returns \c true if the list is empty. -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator+=(const QList<T> &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+(), append() -*/ - -/*! \fn template <class T> void QList<T>::operator+=(const T &value) - - \overload - - Appends \a value to the list. - - \sa append(), operator<<() -*/ - -/*! \fn template <class T> QList<T> QList<T>::operator+(const QList<T> &other) const - - Returns a list that contains all the items in this list followed - by all the items in the \a other list. - - \sa operator+=() -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator<<(const QList<T> &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+=(), append() -*/ - -/*! \fn template <class T> void QList<T>::operator<<(const T &value) - - \overload - - Appends \a value to the list. -*/ - -/*! \class QList::iterator - \inmodule QtCore - \brief The QList::iterator class provides an STL-style non-const iterator for QList and QQueue. - - QList features both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style iterators are more low-level and more - cumbersome to use; on the other hand, they are slightly faster - and, for developers who already know STL, have the advantage of - familiarity. - - QList\<T\>::iterator allows you to iterate over a QList\<T\> (or - QQueue\<T\>) and to modify the list item associated with the - iterator. If you want to iterate over a const QList, use - QList::const_iterator instead. It is generally good practice to - use QList::const_iterator on a non-const QList as well, unless - you need to change the QList through the iterator. Const - iterators are slightly faster, and can improve code readability. - - The default QList::iterator constructor creates an uninitialized - iterator. You must initialize it using a QList function like - QList::begin(), QList::end(), or QList::insert() before you can - start iterating. Here's a typical loop that prints all the items - stored in a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 15 - - Let's see a few examples of things we can do with a - QList::iterator that we cannot do with a QList::const_iterator. - Here's an example that increments every value stored in a - QList\<int\> by 2: - - \snippet code/src_corelib_tools_qlistdata.cpp 16 - - Most QList functions accept an integer index rather than an - iterator. For that reason, iterators are rarely useful in - connection with QList. One place where STL-style iterators do - make sense is as arguments to \l{generic algorithms}. - - For example, here's how to delete all the widgets stored in a - QList\<QWidget *\>: - - \snippet code/src_corelib_tools_qlistdata.cpp 17 - - Multiple iterators can be used on the same list. However, be - aware that any non-const function call performed on the QList - will render all existing iterators undefined. If you need to keep - iterators over a long period of time, we recommend that you use - QLinkedList rather than QList. - - \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::const_iterator, QMutableListIterator -*/ - -/*! \typedef QList::iterator::iterator_category - - A synonym for \e {std::random_access_iterator_tag} indicating - this iterator is a random access iterator. -*/ - -/*! \typedef QList::iterator::difference_type - - \internal -*/ - -/*! \typedef QList::iterator::value_type - - \internal -*/ - -/*! \typedef QList::iterator::pointer - - \internal -*/ - -/*! \typedef QList::iterator::reference - - \internal -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator() - - Constructs an uninitialized iterator. - - Functions like operator*() and operator++() should not be called - on an uninitialized iterator. Use operator=() to assign a value - to it before using it. - - \sa QList::begin(), QList::end() -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator(Node *node) - - \internal -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> T &QList<T>::iterator::operator*() const - - Returns a modifiable reference to the current item. - - You can change the value of an item by using operator*() on the - left side of an assignment, for example: - - \snippet code/src_corelib_tools_qlistdata.cpp 18 - - \sa operator->() -*/ - -/*! \fn template <class T> T *QList<T>::iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template <class T> T &QList<T>::iterator::operator[](difference_type j) const - - Returns a modifiable reference to the item at position *this + - \a{j}. - - This function is provided to make QList iterators behave like C++ - pointers. - - \sa operator+() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator==(const iterator &other) const - \fn template <class T> bool QList<T>::iterator::operator==(const const_iterator &other) const - - Returns \c true if \a other points to the same item as this - iterator; otherwise returns \c false. - - \sa operator!=() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator!=(const iterator &other) const - \fn template <class T> bool QList<T>::iterator::operator!=(const const_iterator &other) const - - Returns \c true if \a other points to a different item than this - iterator; otherwise returns \c false. - - \sa operator==() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator<(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator<(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator<=(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator<=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator>(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator>(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator>=(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator>=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator++() - - The prefix ++ operator (\c{++it}) advances the iterator to the - next item in the list and returns an iterator to the new current - item. - - Calling this function on QList::end() leads to undefined results. - - \sa operator--() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator++(int) - - \overload - - The postfix ++ operator (\c{it++}) advances the iterator to the - next item in the list and returns an iterator to the previously - current item. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator--() - - The prefix -- operator (\c{--it}) makes the preceding item - current and returns an iterator to the new current item. - - Calling this function on QList::begin() leads to undefined results. - - \sa operator++() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator--(int) - - \overload - - The postfix -- operator (\c{it--}) makes the preceding item - current and returns an iterator to the previously current item. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator+=(difference_type j) - - Advances the iterator by \a j items. (If \a j is negative, the - iterator goes backward.) - - \sa operator-=(), operator+() -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator-=(difference_type j) - - Makes the iterator go back by \a j items. (If \a j is negative, - the iterator goes forward.) - - \sa operator+=(), operator-() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator+(difference_type j) const - - Returns an iterator to the item at \a j positions forward from - this iterator. (If \a j is negative, the iterator goes backward.) - - \sa operator-(), operator+=() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator-(difference_type j) const - - Returns an iterator to the item at \a j positions backward from - this iterator. (If \a j is negative, the iterator goes forward.) - - \sa operator+(), operator-=() -*/ - -/*! \fn template <class T> int QList<T>::iterator::operator-(iterator other) const - - Returns the number of items between the item pointed to by \a - other and the item pointed to by this iterator. -*/ - -/*! \class QList::const_iterator - \inmodule QtCore - \brief The QList::const_iterator class provides an STL-style const iterator for QList and QQueue. - - QList provides both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style iterators are more low-level and more - cumbersome to use; on the other hand, they are slightly faster - and, for developers who already know STL, have the advantage of - familiarity. - - QList\<T\>::const_iterator allows you to iterate over a - QList\<T\> (or a QQueue\<T\>). If you want to modify the QList as - you iterate over it, use QList::iterator instead. It is generally - good practice to use QList::const_iterator on a non-const QList - as well, unless you need to change the QList through the - iterator. Const iterators are slightly faster, and can improve - code readability. - - The default QList::const_iterator constructor creates an - uninitialized iterator. You must initialize it using a QList - function like QList::constBegin(), QList::constEnd(), or - QList::insert() before you can start iterating. Here's a typical - loop that prints all the items stored in a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 19 - - Most QList functions accept an integer index rather than an - iterator. For that reason, iterators are rarely useful in - connection with QList. One place where STL-style iterators do - make sense is as arguments to \l{generic algorithms}. - - For example, here's how to delete all the widgets stored in a - QList\<QWidget *\>: - - \snippet code/src_corelib_tools_qlistdata.cpp 20 - - Multiple iterators can be used on the same list. However, be - aware that any non-const function call performed on the QList - will render all existing iterators undefined. If you need to keep - iterators over a long period of time, we recommend that you use - QLinkedList rather than QList. - - \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::iterator, QListIterator -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator() - - Constructs an uninitialized iterator. - - Functions like operator*() and operator++() should not be called - on an uninitialized iterator. Use operator=() to assign a value - to it before using it. - - \sa QList::constBegin(), QList::constEnd() -*/ - -/*! \typedef QList::const_iterator::iterator_category - - A synonym for \e {std::random_access_iterator_tag} indicating - this iterator is a random access iterator. -*/ - -/*! \typedef QList::const_iterator::difference_type - - \internal -*/ - -/*! \typedef QList::const_iterator::value_type - - \internal -*/ - -/*! \typedef QList::const_iterator::pointer - - \internal -*/ - -/*! \typedef QList::const_iterator::reference - - \internal -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(Node *node) - - \internal -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(const const_iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> const T &QList<T>::const_iterator::operator*() const - - Returns the current item. - - \sa operator->() -*/ - -/*! \fn template <class T> const T *QList<T>::const_iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template <class T> const T &QList<T>::const_iterator::operator[](difference_type j) const - - Returns the item at position *this + \a{j}. - - This function is provided to make QList iterators behave like C++ - pointers. - - \sa operator+() -*/ - -/*! \fn template <class T> bool QList<T>::const_iterator::operator==(const const_iterator &other) const - - Returns \c true if \a other points to the same item as this - iterator; otherwise returns \c false. - - \sa operator!=() -*/ - -/*! \fn template <class T> bool QList<T>::const_iterator::operator!=(const const_iterator &other) const - - Returns \c true if \a other points to a different item than this - iterator; otherwise returns \c false. - - \sa operator==() -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator<(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator<=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator>(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator>=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator++() - - The prefix ++ operator (\c{++it}) advances the iterator to the - next item in the list and returns an iterator to the new current - item. - - Calling this function on QList::end() leads to undefined results. - - \sa operator--() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator++(int) - - \overload - - The postfix ++ operator (\c{it++}) advances the iterator to the - next item in the list and returns an iterator to the previously - current item. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator--() - - The prefix -- operator (\c{--it}) makes the preceding item - current and returns an iterator to the new current item. - - Calling this function on QList::begin() leads to undefined results. - - \sa operator++() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator--(int) - - \overload - - The postfix -- operator (\c{it--}) makes the preceding item - current and returns an iterator to the previously current item. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator+=(difference_type j) - - Advances the iterator by \a j items. (If \a j is negative, the - iterator goes backward.) - - \sa operator-=(), operator+() -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator-=(difference_type j) - - Makes the iterator go back by \a j items. (If \a j is negative, - the iterator goes forward.) - - \sa operator+=(), operator-() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator+(difference_type j) const - - Returns an iterator to the item at \a j positions forward from - this iterator. (If \a j is negative, the iterator goes backward.) - - \sa operator-(), operator+=() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator-(difference_type j) const - - Returns an iterator to the item at \a j positions backward from - this iterator. (If \a j is negative, the iterator goes forward.) - - \sa operator+(), operator-=() -*/ - -/*! \fn template <class T> int QList<T>::const_iterator::operator-(const_iterator other) const - - Returns the number of items between the item pointed to by \a - other and the item pointed to by this iterator. -*/ - -/*! \fn template <class T> QDataStream &operator<<(QDataStream &out, const QList<T> &list) - \relates QList - - Writes the list \a list 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 <class T> QDataStream &operator>>(QDataStream &in, QList<T> &list) - \relates QList - - Reads a list from stream \a in into \a list. - - This function requires the value type to implement \c - operator>>(). - - \sa{Serializing Qt Data Types}{Format of the QDataStream operators} -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromVector(const QVector<T> &vector) - - Returns a QList object with the data contained in \a vector. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 21 - - \include containers-range-constructor.qdocinc - - \sa fromSet(), toVector(), QVector::toList() -*/ - -/*! \fn template <class T> QVector<T> QList<T>::toVector() const - - Returns a QVector object with the data contained in this QList. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 22 - - \include containers-range-constructor.qdocinc - - \sa toSet(), fromVector(), QVector::fromList() -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromSet(const QSet<T> &set) - - Returns a QList object with the data contained in \a set. The - order of the elements in the QList is undefined. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 23 - - \include containers-range-constructor.qdocinc - - \sa fromVector(), toSet(), QSet::toList() -*/ - -/*! \fn template <class T> QSet<T> QList<T>::toSet() const - - Returns a QSet object with the data contained in this QList. - Since QSet doesn't allow duplicates, the resulting QSet might be - smaller than the original list was. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 24 - - \include containers-range-constructor.qdocinc - - \sa toVector(), fromSet(), QSet::fromList() -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromStdList(const std::list<T> &list) - - Returns a QList object with the data contained in \a list. The - order of the elements in the QList is the same as in \a list. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 25 - - \include containers-range-constructor.qdocinc - - \sa toStdList(), QVector::fromStdVector() -*/ - -/*! \fn template <class T> std::list<T> QList<T>::toStdList() const - - Returns a std::list object with the data contained in this QList. - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 26 - - \include containers-range-constructor.qdocinc - - \sa fromStdList(), QVector::toStdVector() -*/ - QT_END_NAMESPACE diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 5dde80417d..2e9fad3bfe 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -40,1149 +40,17 @@ #ifndef QLIST_H #define QLIST_H -#include <QtCore/qalgorithms.h> -#include <QtCore/qiterator.h> -#include <QtCore/qrefcount.h> -#include <QtCore/qarraydata.h> -#include <QtCore/qhashfunctions.h> #include <QtCore/qvector.h> -#include <QtCore/qcontainertools_impl.h> - -#include <algorithm> -#include <initializer_list> -#include <iterator> -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) -#include <list> -#endif - -#include <stdlib.h> -#include <new> -#include <limits.h> -#include <string.h> - -#ifdef Q_CC_MSVC -#pragma warning( push ) -#pragma warning( disable : 4127 ) // "conditional expression is constant" -#endif - -QT_BEGIN_NAMESPACE - - -template <typename T> class QVector; -template <typename T> class QSet; - -struct Q_CORE_EXPORT QListData { - // tags for tag-dispatching of QList implementations, - // based on QList's three different memory layouts: - struct NotArrayCompatibleLayout {}; - struct NotIndirectLayout {}; - struct ArrayCompatibleLayout : NotIndirectLayout {}; // data laid out like a C array - struct InlineWithPaddingLayout : NotArrayCompatibleLayout, NotIndirectLayout {}; // data laid out like a C array with padding - struct IndirectLayout : NotArrayCompatibleLayout {}; // data allocated on the heap - - struct Data { - QtPrivate::RefCount ref; - int alloc, begin, end; - void *array[1]; - }; - enum { DataHeaderSize = sizeof(Data) - sizeof(void *) }; - - Data *detach(int alloc); - Data *detach_grow(int *i, int n); - void realloc(int alloc); - void realloc_grow(int growth); - inline void dispose() { dispose(d); } - static void dispose(Data *d); - static const Data shared_null; - Data *d; - void **erase(void **xi); - void **append(int n); - void **append(); - void **append(const QListData &l); - void **prepend(); - void **insert(int i); - void remove(int i); - void remove(int i, int n); - void move(int from, int to); - inline int size() const noexcept { return int(d->end - d->begin); } // q6sizetype - inline bool isEmpty() const noexcept { return d->end == d->begin; } - inline void **at(int i) const noexcept { return d->array + d->begin + i; } - inline void **begin() const noexcept { return d->array + d->begin; } - inline void **end() const noexcept { return d->array + d->end; } -}; - -namespace QtPrivate { - template <typename V, typename U> int indexOf(const QList<V> &list, const U &u, int from); - template <typename V, typename U> int lastIndexOf(const QList<V> &list, const U &u, int from); -} - -template <typename T> -class QList -{ -public: - struct MemoryLayout - : std::conditional< - // must stay isStatic until ### Qt 6 for BC reasons (don't use !isRelocatable)! - QTypeInfo<T>::isStatic || QTypeInfo<T>::isLarge, - QListData::IndirectLayout, - typename std::conditional< - sizeof(T) == sizeof(void*), - QListData::ArrayCompatibleLayout, - QListData::InlineWithPaddingLayout - >::type>::type {}; -private: - template <typename V, typename U> friend int QtPrivate::indexOf(const QList<V> &list, const U &u, int from); - template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QList<V> &list, const U &u, int from); - struct Node { void *v; -#if defined(Q_CC_BOR) - Q_INLINE_TEMPLATE T &t(); -#else - Q_INLINE_TEMPLATE T &t() - { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic - ? v : this); } -#endif - }; - - union { QListData p; QListData::Data *d; }; - -public: - inline QList() noexcept : d(const_cast<QListData::Data *>(&QListData::shared_null)) { } - QList(const QList<T> &l); - ~QList(); - QList<T> &operator=(const QList<T> &l); - inline QList(QList<T> &&other) noexcept - : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); } - inline QList &operator=(QList<T> &&other) noexcept - { QList moved(std::move(other)); swap(moved); return *this; } - inline void swap(QList<T> &other) noexcept { qSwap(d, other.d); } - inline QList(std::initializer_list<T> args) - : QList(args.begin(), args.end()) {} - template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> - QList(InputIterator first, InputIterator last); - bool operator==(const QList<T> &l) const; - inline bool operator!=(const QList<T> &l) const { return !(*this == l); } - - inline int size() const noexcept { return p.size(); } - - inline void detach() { if (d->ref.isShared()) detach_helper(); } - - inline void detachShared() - { - // The "this->" qualification is needed for GCCE. - if (d->ref.isShared() && this->d != &QListData::shared_null) - detach_helper(); - } - - inline bool isDetached() const { return !d->ref.isShared(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) - { - if (sharable == d->ref.isSharable()) - return; - if (!sharable) - detach(); - if (d != &QListData::shared_null) - d->ref.setSharable(sharable); - } -#endif - inline bool isSharedWith(const QList<T> &other) const noexcept { return d == other.d; } - - inline bool isEmpty() const noexcept { return p.isEmpty(); } - - void clear(); - - const T &at(int i) const; - const T &operator[](int i) const; - T &operator[](int i); - - void reserve(int size); - void append(const T &t); - void append(const QList<T> &t); - void prepend(const T &t); - void insert(int i, const T &t); - void replace(int i, const T &t); - void removeAt(int i); - int removeAll(const T &t); - bool removeOne(const T &t); - T takeAt(int i); - T takeFirst(); - T takeLast(); - void move(int from, int to); - void swapItemsAt(int i, int j); -#if QT_DEPRECATED_SINCE(5, 13) && QT_VERSION < QT_VERSION_CHECK(6,0,0) - QT_DEPRECATED_X("Use QList<T>::swapItemsAt()") - void swap(int i, int j) { swapItemsAt(i, j); } -#endif - int indexOf(const T &t, int from = 0) const; - int lastIndexOf(const T &t, int from = -1) const; - bool contains(const T &t) const; - int count(const T &t) const; - - class const_iterator; - - class iterator { - public: - Node *i; - typedef std::random_access_iterator_tag iterator_category; - // ### Qt6: use int - typedef qptrdiff difference_type; - typedef T value_type; - typedef T *pointer; - typedef T &reference; - - inline iterator() noexcept : i(nullptr) {} - inline iterator(Node *n) noexcept : i(n) {} -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - // can't remove it in Qt 5, since doing so would make the type trivial, - // which changes the way it's passed to functions by value. - inline iterator(const iterator &o) noexcept : i(o.i){} - inline iterator &operator=(const iterator &o) noexcept - { i = o.i; return *this; } -#endif - inline T &operator*() const { return i->t(); } - inline T *operator->() const { return &i->t(); } - inline T &operator[](difference_type j) const { return i[j].t(); } - inline bool operator==(const iterator &o) const noexcept { return i == o.i; } - inline bool operator!=(const iterator &o) const noexcept { return i != o.i; } - inline bool operator<(const iterator& other) const noexcept { return i < other.i; } - inline bool operator<=(const iterator& other) const noexcept { return i <= other.i; } - inline bool operator>(const iterator& other) const noexcept { return i > other.i; } - inline bool operator>=(const iterator& other) const noexcept { return i >= other.i; } -#ifndef QT_STRICT_ITERATORS - inline bool operator==(const const_iterator &o) const noexcept - { return i == o.i; } - inline bool operator!=(const const_iterator &o) const noexcept - { return i != o.i; } - inline bool operator<(const const_iterator& other) const noexcept - { return i < other.i; } - inline bool operator<=(const const_iterator& other) const noexcept - { return i <= other.i; } - inline bool operator>(const const_iterator& other) const noexcept - { return i > other.i; } - inline bool operator>=(const const_iterator& other) const noexcept - { return i >= other.i; } -#endif - inline iterator &operator++() { ++i; return *this; } - inline iterator operator++(int) { Node *n = i; ++i; return n; } - inline iterator &operator--() { i--; return *this; } - inline iterator operator--(int) { Node *n = i; i--; return n; } - inline iterator &operator+=(difference_type j) { i+=j; return *this; } - inline iterator &operator-=(difference_type j) { i-=j; return *this; } - inline iterator operator+(difference_type j) const { return iterator(i+j); } - inline iterator operator-(difference_type j) const { return iterator(i-j); } - friend inline iterator operator+(difference_type j, iterator k) { return k + j; } - inline int operator-(iterator j) const { return int(i - j.i); } - }; - friend class iterator; - - class const_iterator { - public: - Node *i; - typedef std::random_access_iterator_tag iterator_category; - // ### Qt6: use int - typedef qptrdiff difference_type; - typedef T value_type; - typedef const T *pointer; - typedef const T &reference; - - inline const_iterator() noexcept : i(nullptr) {} - inline const_iterator(Node *n) noexcept : i(n) {} -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - // can't remove it in Qt 5, since doing so would make the type trivial, - // which changes the way it's passed to functions by value. - inline const_iterator(const const_iterator &o) noexcept : i(o.i) {} - inline const_iterator &operator=(const const_iterator &o) noexcept - { i = o.i; return *this; } -#endif -#ifdef QT_STRICT_ITERATORS - inline explicit const_iterator(const iterator &o) noexcept : i(o.i) {} -#else - inline const_iterator(const iterator &o) noexcept : i(o.i) {} -#endif - inline const T &operator*() const { return i->t(); } - inline const T *operator->() const { return &i->t(); } - inline const T &operator[](difference_type j) const { return i[j].t(); } - inline bool operator==(const const_iterator &o) const noexcept { return i == o.i; } - inline bool operator!=(const const_iterator &o) const noexcept { return i != o.i; } - inline bool operator<(const const_iterator& other) const noexcept { return i < other.i; } - inline bool operator<=(const const_iterator& other) const noexcept { return i <= other.i; } - inline bool operator>(const const_iterator& other) const noexcept { return i > other.i; } - inline bool operator>=(const const_iterator& other) const noexcept { return i >= other.i; } - inline const_iterator &operator++() { ++i; return *this; } - inline const_iterator operator++(int) { Node *n = i; ++i; return n; } - inline const_iterator &operator--() { i--; return *this; } - inline const_iterator operator--(int) { Node *n = i; i--; return n; } - inline const_iterator &operator+=(difference_type j) { i+=j; return *this; } - inline const_iterator &operator-=(difference_type j) { i-=j; return *this; } - inline const_iterator operator+(difference_type j) const { return const_iterator(i+j); } - inline const_iterator operator-(difference_type j) const { return const_iterator(i-j); } - friend inline const_iterator operator+(difference_type j, const_iterator k) { return k + j; } - inline int operator-(const_iterator j) const { return int(i - j.i); } - }; - friend class const_iterator; - - // stl style - typedef std::reverse_iterator<iterator> reverse_iterator; - typedef std::reverse_iterator<const_iterator> const_reverse_iterator; - inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator begin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator cbegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator constBegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); } - inline const_iterator end() const noexcept { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator cend() const noexcept { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator constEnd() const noexcept { return reinterpret_cast<Node *>(p.end()); } - 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 insert(iterator before, const T &t); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); - - // more Qt - typedef iterator Iterator; - typedef const_iterator ConstIterator; - inline int count() const { return p.size(); } - inline int length() const { return p.size(); } // Same as count() - inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); } - inline const T& constFirst() const { return first(); } - inline const T& first() const { Q_ASSERT(!isEmpty()); return at(0); } - T& last() { Q_ASSERT(!isEmpty()); return *(--end()); } - const T& last() const { Q_ASSERT(!isEmpty()); return at(count() - 1); } - inline const T& constLast() const { return last(); } - inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); } - inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); } - inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; } - inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; } - QList<T> mid(int pos, int length = -1) const; - - T value(int i) const; - T value(int i, const T &defaultValue) const; - - // stl compatibility - inline void push_back(const T &t) { append(t); } - inline void push_front(const T &t) { prepend(t); } - inline T& front() { return first(); } - inline const T& front() const { return first(); } - inline T& back() { return last(); } - inline const T& back() const { return last(); } - inline void pop_front() { removeFirst(); } - inline void pop_back() { removeLast(); } - inline bool empty() const { return isEmpty(); } - typedef int size_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; - // ### Qt6: use int - typedef qptrdiff difference_type; - - // comfort - QList<T> &operator+=(const QList<T> &l); - inline QList<T> operator+(const QList<T> &l) const - { QList n = *this; n += l; return n; } - inline QList<T> &operator+=(const T &t) - { append(t); return *this; } - inline QList<T> &operator<< (const T &t) - { append(t); return *this; } - inline QList<T> &operator<<(const QList<T> &l) - { *this += l; return *this; } - - static QList<T> fromVector(const QVector<T> &vector); - QVector<T> toVector() const; - -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - Q_DECL_DEPRECATED_X("Use QList<T>(set.begin(), set.end()) instead.") - static QList<T> fromSet(const QSet<T> &set); - Q_DECL_DEPRECATED_X("Use QSet<T>(list.begin(), list.end()) instead.") - QSet<T> toSet() const; - - Q_DECL_DEPRECATED_X("Use QList<T>(list.begin(), list.end()) instead.") - static inline QList<T> fromStdList(const std::list<T> &list) - { return QList<T>(list.begin(), list.end()); } - Q_DECL_DEPRECATED_X("Use std::list<T>(list.begin(), list.end()) instead.") - inline std::list<T> toStdList() const - { return std::list<T>(begin(), end()); } -#endif - -private: - Node *detach_helper_grow(int i, int n); - void detach_helper(int alloc); - void detach_helper(); - void dealloc(QListData::Data *d); - - void node_construct(Node *n, const T &t); - void node_destruct(Node *n); - void node_copy(Node *from, Node *to, Node *src); - void node_destruct(Node *from, Node *to); - - bool isValidIterator(const iterator &i) const noexcept - { - const std::less<const Node *> less = {}; - return !less(i.i, cbegin().i) && !less(cend().i, i.i); - } - -private: - inline bool op_eq_impl(const QList &other, QListData::NotArrayCompatibleLayout) const; - inline bool op_eq_impl(const QList &other, QListData::ArrayCompatibleLayout) const; - inline bool contains_impl(const T &, QListData::NotArrayCompatibleLayout) const; - inline bool contains_impl(const T &, QListData::ArrayCompatibleLayout) const; - inline int count_impl(const T &, QListData::NotArrayCompatibleLayout) const; - inline int count_impl(const T &, QListData::ArrayCompatibleLayout) const; -}; - -#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 -template <typename InputIterator, - typename ValueType = typename std::iterator_traits<InputIterator>::value_type, - QtPrivate::IfIsInputIterator<InputIterator> = true> -QList(InputIterator, InputIterator) -> QList<ValueType>; -#endif - -#if defined(Q_CC_BOR) -template <typename T> -Q_INLINE_TEMPLATE T &QList<T>::Node::t() -{ return QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic ? *(T*)v:*(T*)this; } -#endif - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t); - else if (QTypeInfo<T>::isComplex) new (n) T(t); -#if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__) - // This violates pointer aliasing rules, but it is known to be safe (and silent) - // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which - // set the same define are assumed to be safe. - else *reinterpret_cast<T*>(n) = t; -#else - // This is always safe, but penaltizes unoptimized builds a lot. - else ::memcpy(n, static_cast<const void *>(&t), sizeof(T)); -#endif -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v); - else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T(); -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src) -{ - Node *current = from; - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - QT_TRY { - while(current != to) { - current->v = new T(*reinterpret_cast<T*>(src->v)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - delete reinterpret_cast<T*>(current->v); - QT_RETHROW; - } - - } else if (QTypeInfo<T>::isComplex) { - QT_TRY { - while(current != to) { - new (current) T(*reinterpret_cast<T*>(src)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - (reinterpret_cast<T*>(current))->~T(); - QT_RETHROW; - } - } else { - if (src != from && to - from > 0) - memcpy(from, src, (to - from) * sizeof(Node)); - } -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) - while(from != to) --to, delete reinterpret_cast<T*>(to->v); - else if (QTypeInfo<T>::isComplex) - while (from != to) --to, reinterpret_cast<T*>(to)->~T(); -} - -template <typename T> -Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l) -{ - if (d != l.d) { - QList<T> tmp(l); - tmp.swap(*this); - } - return *this; -} -template <typename T> -inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) -{ - Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); - - int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin())); - Node *n = nullptr; - if (d->ref.isShared()) - n = detach_helper_grow(iBefore, 1); - else - n = reinterpret_cast<Node *>(p.insert(iBefore)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(iBefore); - QT_RETHROW; - } - return n; -} -template <typename T> -inline typename QList<T>::iterator QList<T>::erase(iterator it) -{ - Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid"); - if (d->ref.isShared()) { - int offset = int(it.i - reinterpret_cast<Node *>(p.begin())); - it = begin(); // implies detach() - it += offset; - } - node_destruct(it.i); - return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); -} -template <typename T> -inline const T &QList<T>::at(int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range"); - return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline const T &QList<T>::operator[](int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); - return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline T &QList<T>::operator[](int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); - detach(); return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline void QList<T>::removeAt(int i) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::removeAt", "index out of range"); -#elif !defined(QT_NO_DEBUG) - if (i < 0 || i >= p.size()) - qWarning("QList::removeAt(): Index out of range."); -#endif - detach(); - node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); -} -template <typename T> -inline T QList<T>::takeAt(int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range"); - detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = std::move(n->t()); node_destruct(n); - p.remove(i); return t; } -template <typename T> -inline T QList<T>::takeFirst() -{ T t = std::move(first()); removeFirst(); return t; } -template <typename T> -inline T QList<T>::takeLast() -{ T t = std::move(last()); removeLast(); return t; } - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc) -{ - if (d->alloc < alloc) { - if (d->ref.isShared()) - detach_helper(alloc); - else - p.realloc(alloc); - } -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t) -{ - if (d->ref.isShared()) { - Node *n = detach_helper_grow(INT_MAX, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - --d->end; - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.append()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - --d->end; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.append());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::prepend(const T &t) -{ - if (d->ref.isShared()) { - Node *n = detach_helper_grow(0, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - ++d->begin; - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.prepend()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - ++d->begin; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.prepend());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::insert(int i, const T &t) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i <= p.size(), "QList<T>::insert", "index out of range"); -#elif !defined(QT_NO_DEBUG) - if (i < 0 || i > p.size()) - qWarning("QList::insert(): Index out of range."); -#endif - if (d->ref.isShared()) { - Node *n = detach_helper_grow(i, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(i); - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.insert(i)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(i); - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.insert(i));; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::replace(int i, const T &t) -{ - Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range"); - detach(); - reinterpret_cast<Node *>(p.at(i))->t() = t; -} - -template <typename T> -inline void QList<T>::swapItemsAt(int i, int j) -{ - Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(), - "QList<T>::swap", "index out of range"); - detach(); - qSwap(d->array[d->begin + i], d->array[d->begin + j]); -} - -template <typename T> -inline void QList<T>::move(int from, int to) -{ - Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(), - "QList<T>::move", "index out of range"); - detach(); - p.move(from, to); -} - -template<typename T> -Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int alength) const -{ - using namespace QtPrivate; - switch (QContainerImplHelper::mid(size(), &pos, &alength)) { - case QContainerImplHelper::Null: - case QContainerImplHelper::Empty: - return QList<T>(); - case QContainerImplHelper::Full: - return *this; - case QContainerImplHelper::Subset: - break; - } - - QList<T> cpy; - if (alength <= 0) - return cpy; - cpy.reserve(alength); - cpy.d->end = alength; - QT_TRY { - cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()), - reinterpret_cast<Node *>(cpy.p.end()), - reinterpret_cast<Node *>(p.begin() + pos)); - } QT_CATCH(...) { - // restore the old end - cpy.d->end = 0; - QT_RETHROW; - } - return cpy; -} +#include <QtCore/qcontainerfwd.h> +#if !defined(QT_NO_JAVA_STYLE_ITERATORS) template<typename T> -Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const -{ - if (i < 0 || i >= p.size()) { - return T(); - } - return reinterpret_cast<Node *>(p.at(i))->t(); -} - +using QMutableListIterator = QMutableVectorIterator<T>; template<typename T> -Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const -{ - return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t()); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i, int c) -{ - Node *n = reinterpret_cast<Node *>(p.begin()); - QListData::Data *x = p.detach_grow(&i, c); - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.begin() + i), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin() + i + c), - reinterpret_cast<Node *>(p.end()), n + i); - } QT_CATCH(...) { - node_destruct(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.begin() + i)); - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); - - return reinterpret_cast<Node *>(p.begin() + i); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(int alloc) -{ - Node *n = reinterpret_cast<Node *>(p.begin()); - QListData::Data *x = p.detach(alloc); - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper() -{ - detach_helper(d->alloc); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l) - : d(l.d) -{ - if (!d->ref.ref()) { - p.detach(d->alloc); - - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.end()), - reinterpret_cast<Node *>(l.p.begin())); - } QT_CATCH(...) { - QListData::dispose(d); - QT_RETHROW; - } - } -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T>::~QList() -{ - if (!d->ref.deref()) - dealloc(d); -} - -template <typename T> -template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator>> -QList<T>::QList(InputIterator first, InputIterator last) - : QList() -{ - QtPrivate::reserveIfForwardIterator(this, first, last); - std::copy(first, last, std::back_inserter(*this)); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const -{ - if (d == l.d) - return true; - if (p.size() != l.p.size()) - return false; - return this->op_eq_impl(l, MemoryLayout()); -} - -template <typename T> -inline bool QList<T>::op_eq_impl(const QList &l, QListData::NotArrayCompatibleLayout) const -{ - Node *i = reinterpret_cast<Node *>(p.begin()); - Node *e = reinterpret_cast<Node *>(p.end()); - Node *li = reinterpret_cast<Node *>(l.p.begin()); - for (; i != e; ++i, ++li) { - if (!(i->t() == li->t())) - return false; - } - return true; -} - -template <typename T> -inline bool QList<T>::op_eq_impl(const QList &l, QListData::ArrayCompatibleLayout) const -{ - const T *lb = reinterpret_cast<const T*>(l.p.begin()); - const T *b = reinterpret_cast<const T*>(p.begin()); - const T *e = reinterpret_cast<const T*>(p.end()); - return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(lb, l.p.size())); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::dealloc(QListData::Data *data) -{ - node_destruct(reinterpret_cast<Node *>(data->array + data->begin), - reinterpret_cast<Node *>(data->array + data->end)); - QListData::dispose(data); -} - - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::clear() -{ - *this = QList<T>(); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t) -{ - int index = indexOf(_t); - if (index == -1) - return 0; - - const T t = _t; - detach(); - - Node *i = reinterpret_cast<Node *>(p.at(index)); - Node *e = reinterpret_cast<Node *>(p.end()); - Node *n = i; - node_destruct(i); - while (++i != e) { - if (i->t() == t) - node_destruct(i); - else - *n++ = *i; - } - - int removedCount = int(e - n); - d->end -= removedCount; - return removedCount; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::removeOne(const T &_t) -{ - int index = indexOf(_t); - if (index != -1) { - removeAt(index); - return true; - } - return false; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst, - typename QList<T>::iterator alast) -{ - Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid"); - Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid"); - - if (d->ref.isShared()) { - // ### A block is erased and a detach is needed. We should shrink and only copy relevant items. - int offsetfirst = int(afirst.i - reinterpret_cast<Node *>(p.begin())); - int offsetlast = int(alast.i - reinterpret_cast<Node *>(p.begin())); - afirst = begin(); // implies detach() - alast = afirst; - afirst += offsetfirst; - alast += offsetlast; - } - - for (Node *n = afirst.i; n < alast.i; ++n) - node_destruct(n); - int idx = afirst - begin(); - p.remove(idx, alast - afirst); - return begin() + idx; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l) -{ - if (!l.isEmpty()) { - if (d == &QListData::shared_null) { - *this = l; - } else { - Node *n = (d->ref.isShared()) - ? detach_helper_grow(INT_MAX, l.size()) - : reinterpret_cast<Node *>(p.append(l.p)); - QT_TRY { - node_copy(n, reinterpret_cast<Node *>(p.end()), - reinterpret_cast<Node *>(l.p.begin())); - } QT_CATCH(...) { - // restore the old end - d->end -= int(reinterpret_cast<Node *>(p.end()) - n); - QT_RETHROW; - } - } - } - return *this; -} - -template <typename T> -inline void QList<T>::append(const QList<T> &t) -{ - *this += t; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const -{ - return QtPrivate::indexOf<T, T>(*this, t, from); -} - -namespace QtPrivate -{ -template <typename T, typename U> -int indexOf(const QList<T> &list, const U &u, int from) -{ - typedef typename QList<T>::Node Node; - - if (from < 0) - from = qMax(from + list.p.size(), 0); - if (from < list.p.size()) { - Node *n = reinterpret_cast<Node *>(list.p.at(from -1)); - Node *e = reinterpret_cast<Node *>(list.p.end()); - while (++n != e) - if (n->t() == u) - return int(n - reinterpret_cast<Node *>(list.p.begin())); - } - return -1; -} -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const -{ - return QtPrivate::lastIndexOf<T, T>(*this, t, from); -} - -namespace QtPrivate -{ -template <typename T, typename U> -int lastIndexOf(const QList<T> &list, const U &u, int from) -{ - typedef typename QList<T>::Node Node; - - if (from < 0) - from += list.p.size(); - else if (from >= list.p.size()) - from = list.p.size()-1; - if (from >= 0) { - Node *b = reinterpret_cast<Node *>(list.p.begin()); - Node *n = reinterpret_cast<Node *>(list.p.at(from + 1)); - while (n-- != b) { - if (n->t() == u) - return int(n - b); - } - } - return -1; -} -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const -{ - return contains_impl(t, MemoryLayout()); -} - -template <typename T> -inline bool QList<T>::contains_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - Node *e = reinterpret_cast<Node *>(p.end()); - Node *i = reinterpret_cast<Node *>(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - return true; - return false; -} - -template <typename T> -inline bool QList<T>::contains_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - const T *b = reinterpret_cast<const T*>(p.begin()); - const T *e = reinterpret_cast<const T*>(p.end()); - return std::find(b, e, t) != e; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const -{ - return this->count_impl(t, MemoryLayout()); -} - -template <typename T> -inline int QList<T>::count_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - int c = 0; - Node *e = reinterpret_cast<Node *>(p.end()); - Node *i = reinterpret_cast<Node *>(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - ++c; - return c; -} - -template <typename T> -inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - return int(std::count(reinterpret_cast<const T*>(p.begin()), - reinterpret_cast<const T*>(p.end()), - t)); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const -{ - return QVector<T>(begin(), end()); -} - -template <typename T> -QList<T> QList<T>::fromVector(const QVector<T> &vector) -{ - return vector.toList(); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const -{ - return QList<T>(begin(), end()); -} - -template <typename T> -QVector<T> QVector<T>::fromList(const QList<T> &list) -{ - return list.toVector(); -} - -Q_DECLARE_SEQUENTIAL_ITERATOR(List) -Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) - -template <typename T> -uint qHash(const QList<T> &key, uint seed = 0) - noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) -{ - return qHashRange(key.cbegin(), key.cend(), seed); -} - -template <typename T> -bool operator<(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()))) -{ - return std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()); -} - -template <typename T> -inline bool operator>(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return rhs < lhs; -} - -template <typename T> -inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs > rhs); -} - -template <typename T> -inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs < rhs); -} - -QT_END_NAMESPACE +using QListIterator = QVectorIterator<T>; +#endif #include <QtCore/qbytearraylist.h> #include <QtCore/qstringlist.h> -#ifdef Q_CC_MSVC -#pragma warning( pop ) -#endif - #endif // QLIST_H diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 45f7c5b76b..366eb15813 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -317,9 +317,6 @@ public: inline QVector<T> &operator<<(T &&t) { append(std::move(t)); return *this; } - static QVector<T> fromList(const QList<T> &list); - QList<T> toList() const; - #if QT_VERSION < QT_VERSION_CHECK(6,0,0) Q_DECL_DEPRECATED_X("Use QVector<T>(vector.begin(), vector.end()) instead.") static inline QVector<T> fromStdVector(const std::vector<T> &vector) @@ -328,6 +325,14 @@ public: inline std::vector<T> toStdVector() const { return std::vector<T>(d->begin(), d->end()); } #endif + + // Consider deprecating in 6.4 or later + static QVector<T> fromList(const QVector<T> &list) { return list; } + QVector<T> toList() const { return *this; } + + static inline QVector<T> fromVector(const QVector<T> &vector) { return vector; } + inline QVector<T> toVector() const { return *this; } + private: // ### Qt6: remove methods, they are unused void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default); diff --git a/src/dbus/qdbusmisc.cpp b/src/dbus/qdbusmisc.cpp index eb8f61c783..ce1546db92 100644 --- a/src/dbus/qdbusmisc.cpp +++ b/src/dbus/qdbusmisc.cpp @@ -153,7 +153,7 @@ int qDBusParametersForMethod(const QList<QByteArray> ¶meterTypes, QVector<in QList<QByteArray>::ConstIterator it = parameterTypes.constBegin(); QList<QByteArray>::ConstIterator end = parameterTypes.constEnd(); for ( ; it != end; ++it) { - const QByteArray &type = *it; + QByteArray type = *it; if (type.endsWith('*')) { errorMsg = QLatin1String("Pointers are not supported: ") + QLatin1String(type); return -1; @@ -180,6 +180,9 @@ int qDBusParametersForMethod(const QList<QByteArray> ¶meterTypes, QVector<in return -1; // not allowed } + if (type.startsWith("QList<")) + type = "QVector<" + type.mid(sizeof("QList<") - 1); + int id = QMetaType::type(type); #ifdef QT_BOOTSTRAPPED // in bootstrap mode QDBusMessage isn't included, thus we need to resolve it manually here diff --git a/src/tools/bootstrap/bootstrap.pro b/src/tools/bootstrap/bootstrap.pro index 6230cc081d..c784e35607 100644 --- a/src/tools/bootstrap/bootstrap.pro +++ b/src/tools/bootstrap/bootstrap.pro @@ -97,7 +97,6 @@ SOURCES += \ ../../corelib/tools/qcommandlineoption.cpp \ ../../corelib/tools/qcryptographichash.cpp \ ../../corelib/tools/qhash.cpp \ - ../../corelib/tools/qlist.cpp \ ../../corelib/tools/qmap.cpp \ ../../corelib/tools/qringbuffer.cpp \ ../../corelib/tools/qpoint.cpp \ diff --git a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp index ce4232f3e8..019004e126 100644 --- a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp +++ b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp @@ -72,9 +72,8 @@ static const char includeList[] = static const char forwardDeclarations[] = "QT_BEGIN_NAMESPACE\n" + "#include <QtCore/qcontainerfwd.h>\n" "class QByteArray;\n" - "template<class T> class QList;\n" - "template<class Key, class Value> class QMap;\n" "class QString;\n" "class QStringList;\n" "class QVariant;\n" diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index e7dcfac403..c45d41dab5 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -112,7 +112,7 @@ void QListModel::insert(int row, QListWidgetItem *item) QList<QListWidgetItem*>::iterator it; it = sortedInsertionIterator(items.begin(), items.end(), item->view->sortOrder(), item); - row = qMax(it - items.begin(), 0); + row = qMax<qsizetype>(it - items.begin(), 0); } else { if (row < 0) row = 0; @@ -391,7 +391,7 @@ void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int en --tmpitepos; lit = tmp.begin() + tmpitepos; lit = sortedInsertionIterator(lit, tmp.end(), order, item); - int newRow = qMax(lit - tmp.begin(), 0); + int newRow = qMax<qsizetype>(lit - tmp.begin(), 0); lit = tmp.insert(lit, item); if (newRow != oldRow) { changed = true; diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index a2d6e7798d..68338228b2 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -646,7 +646,7 @@ void QTreeModel::ensureSorted(int column, Qt::SortOrder order, lit = lst.begin() + tmpitepos; lit = sortedInsertionIterator(lit, lst.end(), order, item); - int newRow = qMax(lit - lst.begin(), 0); + int newRow = qMax<qsizetype>(lit - lst.begin(), 0); if ((newRow < oldRow) && !(*item < *lst.at(oldRow - 1)) && !(*lst.at(oldRow - 1) < *item )) newRow = oldRow; |