From 5357231c0a10eef558cc6aebfd172048dc010a96 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 24 May 2019 14:27:48 +0200 Subject: 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 --- src/corelib/tools/qcontainerfwd.h | 2 +- src/corelib/tools/qlist.cpp | 2025 ------------------------------------- src/corelib/tools/qlist.h | 1142 +-------------------- src/corelib/tools/qvector.h | 11 +- 4 files changed, 14 insertions(+), 3166 deletions(-) (limited to 'src/corelib/tools') 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 QHash; #ifndef QT_NO_LINKED_LIST template class QLinkedList; #endif -template class QList; template class QMap; template class QMultiHash; template class QMultiMap; @@ -60,6 +59,7 @@ template class QSet; template class QStack; template class QVarLengthArray; template class QVector; +template using QList = QVector; 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; template class Q_CORE_EXPORT QVector; #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(::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(::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(::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(::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\ 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\, QLinkedList\, and QVector\ provide similar - APIs and functionality. They are often interchangeable, but there - are performance consequences. Here is an overview of use cases: - - \list - \li QVector should be your default first choice. - QVector\ will usually give better performance than QList\, - because QVector\ always stores its items sequentially in memory, - where QList\ will allocate its items on the heap unless - \c {sizeof(T) <= sizeof(void*)} and T has been declared to be - either a \c{Q_MOVABLE_TYPE} or a \c{Q_PRIMITIVE_TYPE} using - \l {Q_DECLARE_TYPEINFO}. See the \l {Pros and Cons of Using QList} - for an explanation. - \li However, QList is used throughout the Qt APIs for passing - parameters and for returning values. Use QList to interface with - those APIs. - \li If you need a real linked list, which guarantees - \l {Algorithmic Complexity}{constant time} insertions mid-list and - uses iterators to items rather than indexes, use 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\ 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\ 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\ - 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 QList::QList(QList &&other) - - Move-constructs a QList instance, making it point at the same - object that \a other was pointing to. - - \since 5.2 -*/ - -/*! \fn template template QList::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 QList QList::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 QList::QList() - - Constructs an empty list. -*/ - -/*! \fn template QList::QList(const QList &other) - - Constructs a copy of \a other. - - This operation takes \l{Algorithmic Complexity}{constant time}, - because QList is \l{implicitly shared}. This makes returning a - QList from a function very fast. If a shared instance is modified, - it will be copied (copy-on-write), and that takes - \l{Algorithmic Complexity}{linear time}. - - \sa operator=() -*/ - -/*! \fn template QList::QList(std::initializer_list args) - \since 4.8 - - 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 QList::~QList() - - Destroys the list. References to the values in the list and all - iterators of this list become invalid. -*/ - -/*! \fn template QList &QList::operator=(const QList &other) - - Assigns \a other to this list and returns a reference to this - list. -*/ - -/*! - \fn template QList &QList::operator=(QList &&other) - - Move-assigns \a other to this QList instance. - - \since 5.2 -*/ - -/*! \fn template void QList::swap(QList &other) - \since 4.8 - - Swaps list \a other with this list. This operation is very - fast and never fails. -*/ - -/*! \fn template bool QList::operator==(const QList &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 bool QList::operator!=(const QList &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 bool operator<(const QList &lhs, const QList &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 bool operator<=(const QList &lhs, const QList &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 bool operator>(const QList &lhs, const QList &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 bool operator>=(const QList &lhs, const QList &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 uint qHash(const QList &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 int QList::size() const - - Returns the number of items in the list. - - \sa isEmpty(), count() -*/ - -/*! \fn template void QList::detach() - - \internal -*/ - -/*! \fn template void QList::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 bool QList::isDetached() const - - \internal -*/ - -/*! \fn template void QList::setSharable(bool sharable) - - \internal -*/ - -/*! \fn template bool QList::isSharedWith(const QList &other) const - - \internal -*/ - -/*! \fn template bool QList::isEmpty() const - - Returns \c true if the list contains no items; otherwise returns - false. - - \sa size() -*/ - -/*! \fn template void QList::clear() - - Removes all items from the list. - - \sa removeAll() -*/ - -/*! \fn template const T &QList::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 T &QList::operator[](int i) - - Returns the item at index position \a i as a modifiable reference. - \a i must be a valid index position in the 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 const T &QList::operator[](int i) const - - \overload - - Same as at(). This function runs in \l{Algorithmic Complexity}{constant time}. -*/ - -/*! \fn template void QList::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 void QList::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 void QList::append(const QList &value) - - \overload - - \since 4.5 - - Appends the items of the \a value list to this list. - - \sa operator<<(), operator+=() -*/ - -/*! \fn template void QList::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 void QList::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 QList::iterator QList::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 void QList::replace(int i, const T &value) - - Replaces the item at index position \a i with \a value. \a i must - be a valid index position in the list (i.e., 0 <= \a i < size()). - - \sa operator[](), removeAt() -*/ - -/*! - \fn template int QList::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 bool QList::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 void QList::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 T QList::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 T QList::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 T QList::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 void QList::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 void QList::swap(int i, int j) - - \obsolete Use swapItemsAt() - - \sa move(), swapItemsAt() -*/ - -/*! \fn template void QList::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 int QList::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 int QList::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 bool QList::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 int QList::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 bool QList::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 bool QList::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 QList::iterator QList::begin() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in - the list. - - \sa constBegin(), end() -*/ - -/*! \fn template QList::const_iterator QList::begin() const - - \overload -*/ - -/*! \fn template QList::const_iterator QList::cbegin() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), cend() -*/ - -/*! \fn template QList::const_iterator QList::constBegin() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template QList::iterator QList::end() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item - after the last item in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template const_iterator QList::end() const - - \overload -*/ - -/*! \fn template QList::const_iterator QList::cend() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the list. - - \sa cbegin(), end() -*/ - -/*! \fn template QList::const_iterator QList::constEnd() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the list. - - \sa constBegin(), end() -*/ - -/*! \fn template QList::reverse_iterator QList::rbegin() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), crbegin(), rend() -*/ - -/*! \fn template QList::const_reverse_iterator QList::rbegin() const - \since 5.6 - \overload -*/ - -/*! \fn template QList::const_reverse_iterator QList::crbegin() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), rbegin(), rend() -*/ - -/*! \fn template QList::reverse_iterator QList::rend() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past - the last item in the list, in reverse order. - - \sa end(), crend(), rbegin() -*/ - -/*! \fn template QList::const_reverse_iterator QList::rend() const - \since 5.6 - \overload -*/ - -/*! \fn template QList::const_reverse_iterator QList::crend() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one - past the last item in the list, in reverse order. - - \sa end(), rend(), rbegin() -*/ - -/*! \fn template QList::iterator QList::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 QList::iterator QList::erase(iterator begin, iterator end) - - \overload - - Removes all the items from \a begin up to (but not including) \a - end. Returns an iterator to the same item that \a end referred to - before the call. -*/ - -/*! \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}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator -*/ - -/*! \typedef QList::const_reverse_iterator - \since 5.6 - - The QList::const_reverse_iterator typedef provides an STL-style const - reverse iterator for QList. - - It is simply a typedef for \c{std::reverse_iterator}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator -*/ - -/*! \fn template int QList::count() const - - Returns the number of items in the list. This is effectively the - same as size(). -*/ - -/*! \fn template int QList::length() const - \since 4.5 - - This function is identical to count(). - - \sa count() -*/ - -/*! \fn template T& QList::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 const T& QList::first() const - - \overload -*/ - -/*! \fn template const T& QList::constFirst() const - \since 5.6 - - Returns a const reference to the first item in the 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 T& QList::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 const T& QList::last() const - - \overload -*/ - -/*! \fn template const T& QList::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 void QList::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 void QList::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 T QList::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 T QList::value(int i, const T &defaultValue) const - - \overload - - If the index \a i is out of bounds, the function returns - \a defaultValue. -*/ - -/*! \fn template void QList::push_back(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::append()}{append(\a value)}. -*/ - -/*! \fn template void QList::push_front(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::prepend()}{prepend(\a value)}. -*/ - -/*! \fn template T& QList::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 const T& QList::front() const - - \overload -*/ - -/*! \fn template T& QList::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 const T& QList::back() const - - \overload -*/ - -/*! \fn template void QList::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 void QList::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 bool QList::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 QList &QList::operator+=(const QList &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+(), append() -*/ - -/*! \fn template void QList::operator+=(const T &value) - - \overload - - Appends \a value to the list. - - \sa append(), operator<<() -*/ - -/*! \fn template QList QList::operator+(const QList &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 QList &QList::operator<<(const QList &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+=(), append() -*/ - -/*! \fn template void QList::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\::iterator allows you to iterate over a QList\ (or - QQueue\) 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\ 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\: - - \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 QList::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 QList::iterator::iterator(Node *node) - - \internal -*/ - -/*! \fn template QList::iterator::iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template T &QList::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 T *QList::iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template T &QList::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 bool QList::iterator::operator==(const iterator &other) const - \fn template bool QList::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 bool QList::iterator::operator!=(const iterator &other) const - \fn template bool QList::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 bool QList::iterator::operator<(const iterator& other) const - \fn template bool QList::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 bool QList::iterator::operator<=(const iterator& other) const - \fn template bool QList::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 bool QList::iterator::operator>(const iterator& other) const - \fn template bool QList::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 bool QList::iterator::operator>=(const iterator& other) const - \fn template bool QList::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 QList::iterator &QList::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 QList::iterator QList::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 QList::iterator &QList::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 QList::iterator QList::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 QList::iterator &QList::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 QList::iterator &QList::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 QList::iterator QList::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 QList::iterator QList::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 int QList::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\::const_iterator allows you to iterate over a - QList\ (or a QQueue\). 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\: - - \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 QList::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 QList::const_iterator::const_iterator(Node *node) - - \internal -*/ - -/*! \fn template QList::const_iterator::const_iterator(const const_iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template QList::const_iterator::const_iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template const T &QList::const_iterator::operator*() const - - Returns the current item. - - \sa operator->() -*/ - -/*! \fn template const T *QList::const_iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template const T &QList::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 bool QList::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 bool QList::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 bool QList::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 bool QList::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 bool QList::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 bool QList::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 QList::const_iterator &QList::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 QList::const_iterator QList::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 QList::const_iterator &QList::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 QList::const_iterator QList::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 QList::const_iterator &QList::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 QList::const_iterator &QList::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 QList::const_iterator QList::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 QList::const_iterator QList::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 int QList::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 QDataStream &operator<<(QDataStream &out, const QList &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 QDataStream &operator>>(QDataStream &in, QList &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 QList QList::fromVector(const QVector &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 QVector QList::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 QList QList::fromSet(const QSet &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 QSet QList::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 QList QList::fromStdList(const std::list &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 std::list QList::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 -#include -#include -#include -#include #include -#include - -#include -#include -#include -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) -#include -#endif - -#include -#include -#include -#include - -#ifdef Q_CC_MSVC -#pragma warning( push ) -#pragma warning( disable : 4127 ) // "conditional expression is constant" -#endif - -QT_BEGIN_NAMESPACE - - -template class QVector; -template 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 int indexOf(const QList &list, const U &u, int from); - template int lastIndexOf(const QList &list, const U &u, int from); -} - -template -class QList -{ -public: - struct MemoryLayout - : std::conditional< - // must stay isStatic until ### Qt 6 for BC reasons (don't use !isRelocatable)! - QTypeInfo::isStatic || QTypeInfo::isLarge, - QListData::IndirectLayout, - typename std::conditional< - sizeof(T) == sizeof(void*), - QListData::ArrayCompatibleLayout, - QListData::InlineWithPaddingLayout - >::type>::type {}; -private: - template friend int QtPrivate::indexOf(const QList &list, const U &u, int from); - template friend int QtPrivate::lastIndexOf(const QList &list, const U &u, int from); - struct Node { void *v; -#if defined(Q_CC_BOR) - Q_INLINE_TEMPLATE T &t(); -#else - Q_INLINE_TEMPLATE T &t() - { return *reinterpret_cast(QTypeInfo::isLarge || QTypeInfo::isStatic - ? v : this); } -#endif - }; - - union { QListData p; QListData::Data *d; }; - -public: - inline QList() noexcept : d(const_cast(&QListData::shared_null)) { } - QList(const QList &l); - ~QList(); - QList &operator=(const QList &l); - inline QList(QList &&other) noexcept - : d(other.d) { other.d = const_cast(&QListData::shared_null); } - inline QList &operator=(QList &&other) noexcept - { QList moved(std::move(other)); swap(moved); return *this; } - inline void swap(QList &other) noexcept { qSwap(d, other.d); } - inline QList(std::initializer_list args) - : QList(args.begin(), args.end()) {} - template = true> - QList(InputIterator first, InputIterator last); - bool operator==(const QList &l) const; - inline bool operator!=(const QList &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 &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); - 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::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 reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; - inline iterator begin() { detach(); return reinterpret_cast(p.begin()); } - inline const_iterator begin() const noexcept { return reinterpret_cast(p.begin()); } - inline const_iterator cbegin() const noexcept { return reinterpret_cast(p.begin()); } - inline const_iterator constBegin() const noexcept { return reinterpret_cast(p.begin()); } - inline iterator end() { detach(); return reinterpret_cast(p.end()); } - inline const_iterator end() const noexcept { return reinterpret_cast(p.end()); } - inline const_iterator cend() const noexcept { return reinterpret_cast(p.end()); } - inline const_iterator constEnd() const noexcept { return reinterpret_cast(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 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 &operator+=(const QList &l); - inline QList operator+(const QList &l) const - { QList n = *this; n += l; return n; } - inline QList &operator+=(const T &t) - { append(t); return *this; } - inline QList &operator<< (const T &t) - { append(t); return *this; } - inline QList &operator<<(const QList &l) - { *this += l; return *this; } - - static QList fromVector(const QVector &vector); - QVector toVector() const; - -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - Q_DECL_DEPRECATED_X("Use QList(set.begin(), set.end()) instead.") - static QList fromSet(const QSet &set); - Q_DECL_DEPRECATED_X("Use QSet(list.begin(), list.end()) instead.") - QSet toSet() const; - - Q_DECL_DEPRECATED_X("Use QList(list.begin(), list.end()) instead.") - static inline QList fromStdList(const std::list &list) - { return QList(list.begin(), list.end()); } - Q_DECL_DEPRECATED_X("Use std::list(list.begin(), list.end()) instead.") - inline std::list toStdList() const - { return std::list(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 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 ::value_type, - QtPrivate::IfIsInputIterator = true> -QList(InputIterator, InputIterator) -> QList; -#endif - -#if defined(Q_CC_BOR) -template -Q_INLINE_TEMPLATE T &QList::Node::t() -{ return QTypeInfo::isLarge || QTypeInfo::isStatic ? *(T*)v:*(T*)this; } -#endif - -template -Q_INLINE_TEMPLATE void QList::node_construct(Node *n, const T &t) -{ - if (QTypeInfo::isLarge || QTypeInfo::isStatic) n->v = new T(t); - else if (QTypeInfo::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(n) = t; -#else - // This is always safe, but penaltizes unoptimized builds a lot. - else ::memcpy(n, static_cast(&t), sizeof(T)); -#endif -} - -template -Q_INLINE_TEMPLATE void QList::node_destruct(Node *n) -{ - if (QTypeInfo::isLarge || QTypeInfo::isStatic) delete reinterpret_cast(n->v); - else if (QTypeInfo::isComplex) reinterpret_cast(n)->~T(); -} - -template -Q_INLINE_TEMPLATE void QList::node_copy(Node *from, Node *to, Node *src) -{ - Node *current = from; - if (QTypeInfo::isLarge || QTypeInfo::isStatic) { - QT_TRY { - while(current != to) { - current->v = new T(*reinterpret_cast(src->v)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - delete reinterpret_cast(current->v); - QT_RETHROW; - } - - } else if (QTypeInfo::isComplex) { - QT_TRY { - while(current != to) { - new (current) T(*reinterpret_cast(src)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - (reinterpret_cast(current))->~T(); - QT_RETHROW; - } - } else { - if (src != from && to - from > 0) - memcpy(from, src, (to - from) * sizeof(Node)); - } -} - -template -Q_INLINE_TEMPLATE void QList::node_destruct(Node *from, Node *to) -{ - if (QTypeInfo::isLarge || QTypeInfo::isStatic) - while(from != to) --to, delete reinterpret_cast(to->v); - else if (QTypeInfo::isComplex) - while (from != to) --to, reinterpret_cast(to)->~T(); -} - -template -Q_INLINE_TEMPLATE QList &QList::operator=(const QList &l) -{ - if (d != l.d) { - QList tmp(l); - tmp.swap(*this); - } - return *this; -} -template -inline typename QList::iterator QList::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(p.begin())); - Node *n = nullptr; - if (d->ref.isShared()) - n = detach_helper_grow(iBefore, 1); - else - n = reinterpret_cast(p.insert(iBefore)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(iBefore); - QT_RETHROW; - } - return n; -} -template -inline typename QList::iterator QList::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(p.begin())); - it = begin(); // implies detach() - it += offset; - } - node_destruct(it.i); - return reinterpret_cast(p.erase(reinterpret_cast(it.i))); -} -template -inline const T &QList::at(int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList::at", "index out of range"); - return reinterpret_cast(p.at(i))->t(); } -template -inline const T &QList::operator[](int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList::operator[]", "index out of range"); - return reinterpret_cast(p.at(i))->t(); } -template -inline T &QList::operator[](int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList::operator[]", "index out of range"); - detach(); return reinterpret_cast(p.at(i))->t(); } -template -inline void QList::removeAt(int i) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i < p.size(), "QList::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(p.at(i))); p.remove(i); -} -template -inline T QList::takeAt(int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList::take", "index out of range"); - detach(); Node *n = reinterpret_cast(p.at(i)); T t = std::move(n->t()); node_destruct(n); - p.remove(i); return t; } -template -inline T QList::takeFirst() -{ T t = std::move(first()); removeFirst(); return t; } -template -inline T QList::takeLast() -{ T t = std::move(last()); removeLast(); return t; } - -template -Q_OUTOFLINE_TEMPLATE void QList::reserve(int alloc) -{ - if (d->alloc < alloc) { - if (d->ref.isShared()) - detach_helper(alloc); - else - p.realloc(alloc); - } -} - -template -Q_OUTOFLINE_TEMPLATE void QList::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::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(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(p.append());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template -inline void QList::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::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(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(p.prepend());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template -inline void QList::insert(int i, const T &t) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i <= p.size(), "QList::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::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(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(p.insert(i));; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template -inline void QList::replace(int i, const T &t) -{ - Q_ASSERT_X(i >= 0 && i < p.size(), "QList::replace", "index out of range"); - detach(); - reinterpret_cast(p.at(i))->t() = t; -} - -template -inline void QList::swapItemsAt(int i, int j) -{ - Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(), - "QList::swap", "index out of range"); - detach(); - qSwap(d->array[d->begin + i], d->array[d->begin + j]); -} - -template -inline void QList::move(int from, int to) -{ - Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(), - "QList::move", "index out of range"); - detach(); - p.move(from, to); -} - -template -Q_OUTOFLINE_TEMPLATE QList QList::mid(int pos, int alength) const -{ - using namespace QtPrivate; - switch (QContainerImplHelper::mid(size(), &pos, &alength)) { - case QContainerImplHelper::Null: - case QContainerImplHelper::Empty: - return QList(); - case QContainerImplHelper::Full: - return *this; - case QContainerImplHelper::Subset: - break; - } - - QList cpy; - if (alength <= 0) - return cpy; - cpy.reserve(alength); - cpy.d->end = alength; - QT_TRY { - cpy.node_copy(reinterpret_cast(cpy.p.begin()), - reinterpret_cast(cpy.p.end()), - reinterpret_cast(p.begin() + pos)); - } QT_CATCH(...) { - // restore the old end - cpy.d->end = 0; - QT_RETHROW; - } - return cpy; -} +#include +#if !defined(QT_NO_JAVA_STYLE_ITERATORS) template -Q_OUTOFLINE_TEMPLATE T QList::value(int i) const -{ - if (i < 0 || i >= p.size()) { - return T(); - } - return reinterpret_cast(p.at(i))->t(); -} - +using QMutableListIterator = QMutableVectorIterator; template -Q_OUTOFLINE_TEMPLATE T QList::value(int i, const T& defaultValue) const -{ - return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast(p.at(i))->t()); -} - -template -Q_OUTOFLINE_TEMPLATE typename QList::Node *QList::detach_helper_grow(int i, int c) -{ - Node *n = reinterpret_cast(p.begin()); - QListData::Data *x = p.detach_grow(&i, c); - QT_TRY { - node_copy(reinterpret_cast(p.begin()), - reinterpret_cast(p.begin() + i), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - QT_TRY { - node_copy(reinterpret_cast(p.begin() + i + c), - reinterpret_cast(p.end()), n + i); - } QT_CATCH(...) { - node_destruct(reinterpret_cast(p.begin()), - reinterpret_cast(p.begin() + i)); - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); - - return reinterpret_cast(p.begin() + i); -} - -template -Q_OUTOFLINE_TEMPLATE void QList::detach_helper(int alloc) -{ - Node *n = reinterpret_cast(p.begin()); - QListData::Data *x = p.detach(alloc); - QT_TRY { - node_copy(reinterpret_cast(p.begin()), reinterpret_cast(p.end()), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); -} - -template -Q_OUTOFLINE_TEMPLATE void QList::detach_helper() -{ - detach_helper(d->alloc); -} - -template -Q_OUTOFLINE_TEMPLATE QList::QList(const QList &l) - : d(l.d) -{ - if (!d->ref.ref()) { - p.detach(d->alloc); - - QT_TRY { - node_copy(reinterpret_cast(p.begin()), - reinterpret_cast(p.end()), - reinterpret_cast(l.p.begin())); - } QT_CATCH(...) { - QListData::dispose(d); - QT_RETHROW; - } - } -} - -template -Q_OUTOFLINE_TEMPLATE QList::~QList() -{ - if (!d->ref.deref()) - dealloc(d); -} - -template -template > -QList::QList(InputIterator first, InputIterator last) - : QList() -{ - QtPrivate::reserveIfForwardIterator(this, first, last); - std::copy(first, last, std::back_inserter(*this)); -} - -template -Q_OUTOFLINE_TEMPLATE bool QList::operator==(const QList &l) const -{ - if (d == l.d) - return true; - if (p.size() != l.p.size()) - return false; - return this->op_eq_impl(l, MemoryLayout()); -} - -template -inline bool QList::op_eq_impl(const QList &l, QListData::NotArrayCompatibleLayout) const -{ - Node *i = reinterpret_cast(p.begin()); - Node *e = reinterpret_cast(p.end()); - Node *li = reinterpret_cast(l.p.begin()); - for (; i != e; ++i, ++li) { - if (!(i->t() == li->t())) - return false; - } - return true; -} - -template -inline bool QList::op_eq_impl(const QList &l, QListData::ArrayCompatibleLayout) const -{ - const T *lb = reinterpret_cast(l.p.begin()); - const T *b = reinterpret_cast(p.begin()); - const T *e = reinterpret_cast(p.end()); - return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(lb, l.p.size())); -} - -template -Q_OUTOFLINE_TEMPLATE void QList::dealloc(QListData::Data *data) -{ - node_destruct(reinterpret_cast(data->array + data->begin), - reinterpret_cast(data->array + data->end)); - QListData::dispose(data); -} - - -template -Q_OUTOFLINE_TEMPLATE void QList::clear() -{ - *this = QList(); -} - -template -Q_OUTOFLINE_TEMPLATE int QList::removeAll(const T &_t) -{ - int index = indexOf(_t); - if (index == -1) - return 0; - - const T t = _t; - detach(); - - Node *i = reinterpret_cast(p.at(index)); - Node *e = reinterpret_cast(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 -Q_OUTOFLINE_TEMPLATE bool QList::removeOne(const T &_t) -{ - int index = indexOf(_t); - if (index != -1) { - removeAt(index); - return true; - } - return false; -} - -template -Q_OUTOFLINE_TEMPLATE typename QList::iterator QList::erase(typename QList::iterator afirst, - typename QList::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(p.begin())); - int offsetlast = int(alast.i - reinterpret_cast(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 -Q_OUTOFLINE_TEMPLATE QList &QList::operator+=(const QList &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(p.append(l.p)); - QT_TRY { - node_copy(n, reinterpret_cast(p.end()), - reinterpret_cast(l.p.begin())); - } QT_CATCH(...) { - // restore the old end - d->end -= int(reinterpret_cast(p.end()) - n); - QT_RETHROW; - } - } - } - return *this; -} - -template -inline void QList::append(const QList &t) -{ - *this += t; -} - -template -Q_OUTOFLINE_TEMPLATE int QList::indexOf(const T &t, int from) const -{ - return QtPrivate::indexOf(*this, t, from); -} - -namespace QtPrivate -{ -template -int indexOf(const QList &list, const U &u, int from) -{ - typedef typename QList::Node Node; - - if (from < 0) - from = qMax(from + list.p.size(), 0); - if (from < list.p.size()) { - Node *n = reinterpret_cast(list.p.at(from -1)); - Node *e = reinterpret_cast(list.p.end()); - while (++n != e) - if (n->t() == u) - return int(n - reinterpret_cast(list.p.begin())); - } - return -1; -} -} - -template -Q_OUTOFLINE_TEMPLATE int QList::lastIndexOf(const T &t, int from) const -{ - return QtPrivate::lastIndexOf(*this, t, from); -} - -namespace QtPrivate -{ -template -int lastIndexOf(const QList &list, const U &u, int from) -{ - typedef typename QList::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(list.p.begin()); - Node *n = reinterpret_cast(list.p.at(from + 1)); - while (n-- != b) { - if (n->t() == u) - return int(n - b); - } - } - return -1; -} -} - -template -Q_OUTOFLINE_TEMPLATE bool QList::contains(const T &t) const -{ - return contains_impl(t, MemoryLayout()); -} - -template -inline bool QList::contains_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - Node *e = reinterpret_cast(p.end()); - Node *i = reinterpret_cast(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - return true; - return false; -} - -template -inline bool QList::contains_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - const T *b = reinterpret_cast(p.begin()); - const T *e = reinterpret_cast(p.end()); - return std::find(b, e, t) != e; -} - -template -Q_OUTOFLINE_TEMPLATE int QList::count(const T &t) const -{ - return this->count_impl(t, MemoryLayout()); -} - -template -inline int QList::count_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - int c = 0; - Node *e = reinterpret_cast(p.end()); - Node *i = reinterpret_cast(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - ++c; - return c; -} - -template -inline int QList::count_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - return int(std::count(reinterpret_cast(p.begin()), - reinterpret_cast(p.end()), - t)); -} - -template -Q_OUTOFLINE_TEMPLATE QVector QList::toVector() const -{ - return QVector(begin(), end()); -} - -template -QList QList::fromVector(const QVector &vector) -{ - return vector.toList(); -} - -template -Q_OUTOFLINE_TEMPLATE QList QVector::toList() const -{ - return QList(begin(), end()); -} - -template -QVector QVector::fromList(const QList &list) -{ - return list.toVector(); -} - -Q_DECLARE_SEQUENTIAL_ITERATOR(List) -Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) - -template -uint qHash(const QList &key, uint seed = 0) - noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) -{ - return qHashRange(key.cbegin(), key.cend(), seed); -} - -template -bool operator<(const QList &lhs, const QList &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 -inline bool operator>(const QList &lhs, const QList &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return rhs < lhs; -} - -template -inline bool operator<=(const QList &lhs, const QList &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs > rhs); -} - -template -inline bool operator>=(const QList &lhs, const QList &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs < rhs); -} - -QT_END_NAMESPACE +using QListIterator = QVectorIterator; +#endif #include #include -#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 &operator<<(T &&t) { append(std::move(t)); return *this; } - static QVector fromList(const QList &list); - QList toList() const; - #if QT_VERSION < QT_VERSION_CHECK(6,0,0) Q_DECL_DEPRECATED_X("Use QVector(vector.begin(), vector.end()) instead.") static inline QVector fromStdVector(const std::vector &vector) @@ -328,6 +325,14 @@ public: inline std::vector toStdVector() const { return std::vector(d->begin(), d->end()); } #endif + + // Consider deprecating in 6.4 or later + static QVector fromList(const QVector &list) { return list; } + QVector toList() const { return *this; } + + static inline QVector fromVector(const QVector &vector) { return vector; } + inline QVector toVector() const { return *this; } + private: // ### Qt6: remove methods, they are unused void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default); -- cgit v1.2.3