diff options
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qarraydata.cpp | 26 | ||||
-rw-r--r-- | src/corelib/tools/qarraydata.h | 17 | ||||
-rw-r--r-- | src/corelib/tools/qarraydatapointer.h | 17 | ||||
-rw-r--r-- | src/corelib/tools/qcontainerfwd.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qcontiguouscache.h | 5 | ||||
-rw-r--r-- | src/corelib/tools/qeasingcurve.cpp | 5 | ||||
-rw-r--r-- | src/corelib/tools/qeasingcurve.h | 7 | ||||
-rw-r--r-- | src/corelib/tools/qhash.h | 25 | ||||
-rw-r--r-- | src/corelib/tools/qlinkedlist.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qlist.cpp | 2025 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 1151 | ||||
-rw-r--r-- | src/corelib/tools/qmap.h | 36 | ||||
-rw-r--r-- | src/corelib/tools/qrefcount.h | 25 | ||||
-rw-r--r-- | src/corelib/tools/qset.h | 33 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 138 | ||||
-rw-r--r-- | src/corelib/tools/qvector_msvc.cpp | 7 |
16 files changed, 89 insertions, 3433 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 36a221f728..ed7dfe2e41 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -164,8 +164,6 @@ static const QArrayData qt_array[3] = { QT_WARNING_POP static const QArrayData &qt_array_empty = qt_array[0]; -static const QArrayData &qt_array_unsharable_empty = qt_array[1]; - static inline size_t calculateBlockSize(size_t &capacity, size_t objectSize, size_t headerSize, uint options) { @@ -193,26 +191,21 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, size_t capacity, AllocationOptions options) noexcept { // Alignment is a power of two - Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData) + Q_ASSERT(alignment >= alignof(QArrayData) && !(alignment & (alignment - 1))); // Don't allocate empty headers - if (!(options & RawData) && !capacity) { -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - if (options & Unsharable) - return const_cast<QArrayData *>(&qt_array_unsharable_empty); -#endif + if (!(options & RawData) && !capacity) return const_cast<QArrayData *>(&qt_array_empty); - } size_t headerSize = sizeof(QArrayData); - // Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we + // Allocate extra (alignment - alignof(QArrayData)) padding bytes so we // can properly align the data array. This assumes malloc is able to // provide appropriate alignment for the header -- as it should! // Padding is skipped when allocating a header for RawData. if (!(options & RawData)) - headerSize += (alignment - Q_ALIGNOF(QArrayData)); + headerSize += (alignment - alignof(QArrayData)); if (headerSize > size_t(MaxAllocSize)) return nullptr; @@ -223,11 +216,7 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1); -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - header->ref.atomic.storeRelaxed(bool(!(options & Unsharable))); -#else header->ref.atomic.storeRelaxed(1); -#endif header->size = 0; header->alloc = capacity; header->capacityReserved = bool(options & CapacityReserved); @@ -256,15 +245,10 @@ void QArrayData::deallocate(QArrayData *data, size_t objectSize, size_t alignment) noexcept { // Alignment is a power of two - Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData) + Q_ASSERT(alignment >= alignof(QArrayData) && !(alignment & (alignment - 1))); Q_UNUSED(objectSize) Q_UNUSED(alignment) -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - if (data == &qt_array_unsharable_empty) - return; -#endif - Q_ASSERT_X(data == 0 || !data->ref.isStatic(), "QArrayData::deallocate", "Static data cannot be deleted"); ::free(data); diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index dcd95924c1..0063cf046f 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -78,9 +78,6 @@ struct Q_CORE_EXPORT QArrayData enum AllocationOption { CapacityReserved = 0x1, -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - Unsharable = 0x2, -#endif RawData = 0x4, Grow = 0x8, @@ -222,7 +219,7 @@ struct QTypedArrayData { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); return static_cast<QTypedArrayData *>(QArrayData::allocate(sizeof(T), - Q_ALIGNOF(AlignmentDummy), capacity, options)); + alignof(AlignmentDummy), capacity, options)); } static QTypedArrayData *reallocateUnaligned(QTypedArrayData *data, size_t capacity, @@ -236,7 +233,7 @@ struct QTypedArrayData static void deallocate(QArrayData *data) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - QArrayData::deallocate(data, sizeof(T), Q_ALIGNOF(AlignmentDummy)); + QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy)); } static QTypedArrayData *fromRawData(const T *data, size_t n, @@ -265,14 +262,6 @@ struct QTypedArrayData Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); return allocate(/* capacity */ 0); } - -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - static QTypedArrayData *unsharableEmpty() - { - Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - return allocate(/* capacity */ 0, Unsharable); - } -#endif }; template <class T, size_t N> @@ -295,7 +284,7 @@ struct QArrayDataPointerRef #define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(type, size) \ Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size,\ - ((sizeof(QArrayData) + (Q_ALIGNOF(type) - 1)) & ~(Q_ALIGNOF(type) - 1) )) \ + ((sizeof(QArrayData) + (alignof(type) - 1)) & ~(alignof(type) - 1) )) \ /**/ //////////////////////////////////////////////////////////////////////////////// diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index af5173c9ad..b8a1ef5d68 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -131,23 +131,6 @@ public: return (!d->isMutable() || d->ref.isShared()); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - void setSharable(bool sharable) - { - if (needsDetach()) { - Data *detached = clone(sharable - ? d->detachFlags() & ~QArrayData::Unsharable - : d->detachFlags() | QArrayData::Unsharable); - QArrayDataPointer old(d); - d = detached; - } else { - d->ref.setSharable(sharable); - } - } - - bool isSharable() const { return d->isSharable(); } -#endif - void swap(QArrayDataPointer &other) noexcept { qSwap(d, other.d); diff --git a/src/corelib/tools/qcontainerfwd.h b/src/corelib/tools/qcontainerfwd.h index 532b4c95ce..f6efa99b6a 100644 --- a/src/corelib/tools/qcontainerfwd.h +++ b/src/corelib/tools/qcontainerfwd.h @@ -50,7 +50,6 @@ template <class Key, class T> class QHash; #ifndef QT_NO_LINKED_LIST template <class T> class QLinkedList; #endif -template <class T> class QList; template <class Key, class T> class QMap; template <class Key, class T> class QMultiHash; template <class Key, class T> class QMultiMap; @@ -60,6 +59,7 @@ template <class T> class QSet; template <class T> class QStack; template<class T, int Prealloc = 256> class QVarLengthArray; template <class T> class QVector; +template<typename T> using QList = QVector<T>; QT_END_NAMESPACE diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h index 7b74b4f526..8cae7d1651 100644 --- a/src/corelib/tools/qcontiguouscache.h +++ b/src/corelib/tools/qcontiguouscache.h @@ -102,9 +102,6 @@ public: inline void detach() { if (d->ref.loadRelaxed() != 1) detach_helper(); } inline bool isDetached() const { return d->ref.loadRelaxed() == 1; } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; } -#endif QContiguousCache<T> &operator=(const QContiguousCache<T> &other); inline QContiguousCache<T> &operator=(QContiguousCache<T> &&other) noexcept @@ -166,7 +163,7 @@ private: } int alignOfTypedData() const { - return qMax<int>(sizeof(void*), Q_ALIGNOF(Data)); + return qMax<int>(sizeof(void*), alignof(Data)); } }; diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index 52c8d13fe3..d7f69d3e0a 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -1382,11 +1382,6 @@ void QEasingCurve::addTCBSegment(const QPointF &nextPoint, qreal t, qreal c, qre } /*! - \fn QList<QPointF> QEasingCurve::cubicBezierSpline() const - \obsolete Use toCubicSpline() instead. - */ - -/*! \since 5.0 Returns the cubicBezierSpline that defines a custom easing curve. diff --git a/src/corelib/tools/qeasingcurve.h b/src/corelib/tools/qeasingcurve.h index d3a468bb22..81833a758f 100644 --- a/src/corelib/tools/qeasingcurve.h +++ b/src/corelib/tools/qeasingcurve.h @@ -46,10 +46,6 @@ QT_REQUIRE_CONFIG(easingcurve); #include <QtCore/qobjectdefs.h> #include <QtCore/qvector.h> -#if QT_DEPRECATED_SINCE(5, 0) -# include <QtCore/qlist.h> -# include <QtCore/qpoint.h> -#endif QT_BEGIN_NAMESPACE @@ -105,9 +101,6 @@ public: void addCubicBezierSegment(const QPointF & c1, const QPointF & c2, const QPointF & endPoint); void addTCBSegment(const QPointF &nextPoint, qreal t, qreal c, qreal b); QVector<QPointF> toCubicSpline() const; -#if QT_DEPRECATED_SINCE(5, 0) - QT_DEPRECATED QList<QPointF> cubicBezierSpline() const { return toCubicSpline().toList(); } -#endif Type type() const; void setType(Type type); diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 9108be4dc6..045dcf6e18 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -235,7 +235,7 @@ class QHash return reinterpret_cast<Node *>(node); } - static inline int alignOfNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(Node)); } + static inline int alignOfNode() { return qMax<int>(sizeof(void*), alignof(Node)); } public: inline QHash() noexcept : d(const_cast<QHashData *>(&QHashData::shared_null)) { } @@ -290,9 +290,6 @@ public: inline void detach() { if (d->ref.isShared()) detach_helper(); } inline bool isDetached() const { return !d->ref.isShared(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QHashData::shared_null) d->sharable = sharable; } -#endif bool isSharedWith(const QHash &other) const { return d == other.d; } void clear(); @@ -374,13 +371,8 @@ public: friend inline QT_DEPRECATED iterator operator+(int j, iterator k) { return k + j; } #endif -#ifndef QT_STRICT_ITERATORS - public: - inline bool operator==(const const_iterator &o) const - { return i == o.i; } - inline bool operator!=(const const_iterator &o) const - { return i != o.i; } -#endif + inline bool operator==(const const_iterator &o) const { return i == o.i; } + inline bool operator!=(const const_iterator &o) const { return i != o.i; } }; friend class iterator; @@ -405,11 +397,7 @@ public: Q_DECL_CONSTEXPR inline const_iterator() : i(nullptr) { } explicit inline const_iterator(void *node) : i(reinterpret_cast<QHashData::Node *>(node)) { } -#ifdef QT_STRICT_ITERATORS - explicit inline const_iterator(const iterator &o) -#else inline const_iterator(const iterator &o) -#endif { i = o.i; } inline const Key &key() const { return concrete(i)->key; } @@ -450,13 +438,6 @@ public: return k + j; } #endif - - // ### Qt 5: not sure this is necessary anymore -#ifdef QT_STRICT_ITERATORS - private: - inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); } - inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); } -#endif }; friend class const_iterator; diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 8970d39be0..e3713d2c1c 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -107,9 +107,6 @@ public: inline void detach() { if (d->ref.isShared()) detach_helper2(this->e); } inline bool isDetached() const { return !d->ref.isShared(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QLinkedListData::shared_null) d->sharable = sharable; } -#endif inline bool isSharedWith(const QLinkedList<T> &other) const { return d == other.d; } inline bool isEmpty() const { return d->size == 0; } diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 5d5da20752..62201fd5d6 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -63,2029 +63,4 @@ template class Q_CORE_EXPORT QVector<QPointF>; template class Q_CORE_EXPORT QVector<QPoint>; #endif - -/* - QList as an array-list combines the easy-of-use of a random - access interface with fast list operations and the low memory - management overhead of an array. Accessing elements by index, - appending, prepending, and removing elements from both the front - and the back all happen in constant time O(1). Inserting or - removing elements at random index positions \ai happens in linear - time, or more precisly in O(min{i,n-i}) <= O(n/2), with n being - the number of elements in the list. -*/ - -const QListData::Data QListData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, { nullptr } }; - -/*! - * Detaches the QListData by allocating new memory for a list which will be bigger - * than the copied one and is expected to grow further. - * *idx is the desired insertion point and is clamped to the actual size of the list. - * num is the number of new elements to insert at the insertion point. - * Returns the old (shared) data, it is up to the caller to deref() and free(). - * For the new data node_copy needs to be called. - * - * \internal - */ -QListData::Data *QListData::detach_grow(int *idx, int num) -{ - Data *x = d; - int l = x->end - x->begin; - int nl = l + num; - auto blockInfo = qCalculateGrowingBlockSize(nl, sizeof(void *), DataHeaderSize); - Data* t = static_cast<Data *>(::malloc(blockInfo.size)); - Q_CHECK_PTR(t); - t->alloc = int(uint(blockInfo.elementCount)); - - t->ref.initializeOwned(); - // The space reservation algorithm's optimization is biased towards appending: - // Something which looks like an append will put the data at the beginning, - // while something which looks like a prepend will put it in the middle - // instead of at the end. That's based on the assumption that prepending - // is uncommon and even an initial prepend will eventually be followed by - // at least some appends. - int bg; - if (*idx < 0) { - *idx = 0; - bg = (t->alloc - nl) >> 1; - } else if (*idx > l) { - *idx = l; - bg = 0; - } else if (*idx < (l >> 1)) { - bg = (t->alloc - nl) >> 1; - } else { - bg = 0; - } - t->begin = bg; - t->end = bg + nl; - d = t; - - return x; -} - -/*! - * Detaches the QListData by allocating new memory for a list which possibly - * has a different size than the copied one. - * Returns the old (shared) data, it is up to the caller to deref() and free() - * For the new data node_copy needs to be called. - * - * \internal - */ -QListData::Data *QListData::detach(int alloc) -{ - Data *x = d; - Data* t = static_cast<Data *>(::malloc(qCalculateBlockSize(alloc, sizeof(void*), DataHeaderSize))); - Q_CHECK_PTR(t); - - t->ref.initializeOwned(); - t->alloc = alloc; - if (!alloc) { - t->begin = 0; - t->end = 0; - } else { - t->begin = x->begin; - t->end = x->end; - } - d = t; - - return x; -} - -void QListData::realloc(int alloc) -{ - Q_ASSERT(!d->ref.isShared()); - Data *x = static_cast<Data *>(::realloc(d, qCalculateBlockSize(alloc, sizeof(void *), DataHeaderSize))); - Q_CHECK_PTR(x); - - d = x; - d->alloc = alloc; - if (!alloc) - d->begin = d->end = 0; -} - -void QListData::realloc_grow(int growth) -{ - Q_ASSERT(!d->ref.isShared()); - auto r = qCalculateGrowingBlockSize(d->alloc + growth, sizeof(void *), DataHeaderSize); - Data *x = static_cast<Data *>(::realloc(d, r.size)); - Q_CHECK_PTR(x); - - d = x; - d->alloc = int(uint(r.elementCount)); -} - -void QListData::dispose(Data *d) -{ - Q_ASSERT(!d->ref.isShared()); - free(d); -} - -// ensures that enough space is available to append n elements -void **QListData::append(int n) -{ - Q_ASSERT(!d->ref.isShared()); - int e = d->end; - if (e + n > d->alloc) { - int b = d->begin; - if (b - n >= 2 * d->alloc / 3) { - // we have enough space. Just not at the end -> move it. - e -= b; - ::memcpy(d->array, d->array + b, e * sizeof(void *)); - d->begin = 0; - } else { - realloc_grow(n); - } - } - d->end = e + n; - return d->array + e; -} - -// ensures that enough space is available to append one element -void **QListData::append() -{ - return append(1); -} - -// ensures that enough space is available to append the list -void **QListData::append(const QListData& l) -{ - return append(l.d->end - l.d->begin); -} - -void **QListData::prepend() -{ - Q_ASSERT(!d->ref.isShared()); - if (d->begin == 0) { - if (d->end >= d->alloc / 3) - realloc_grow(1); - - if (d->end < d->alloc / 3) - d->begin = d->alloc - 2 * d->end; - else - d->begin = d->alloc - d->end; - - ::memmove(d->array + d->begin, d->array, d->end * sizeof(void *)); - d->end += d->begin; - } - return d->array + --d->begin; -} - -void **QListData::insert(int i) -{ - Q_ASSERT(!d->ref.isShared()); - if (i <= 0) - return prepend(); - int size = d->end - d->begin; - if (i >= size) - return append(); - - bool leftward = false; - - if (d->begin == 0) { - if (d->end == d->alloc) { - // If the array is full, we expand it and move some items rightward - realloc_grow(1); - } else { - // If there is free space at the end of the array, we move some items rightward - } - } else { - if (d->end == d->alloc) { - // If there is free space at the beginning of the array, we move some items leftward - leftward = true; - } else { - // If there is free space at both ends, we move as few items as possible - leftward = (i < size - i); - } - } - - if (leftward) { - --d->begin; - ::memmove(d->array + d->begin, d->array + d->begin + 1, i * sizeof(void *)); - } else { - ::memmove(d->array + d->begin + i + 1, d->array + d->begin + i, - (size - i) * sizeof(void *)); - ++d->end; - } - return d->array + d->begin + i; -} - -void QListData::remove(int i) -{ - Q_ASSERT(!d->ref.isShared()); - i += d->begin; - if (i - d->begin < d->end - i) { - if (int offset = i - d->begin) - ::memmove(d->array + d->begin + 1, d->array + d->begin, offset * sizeof(void *)); - d->begin++; - } else { - if (int offset = d->end - i - 1) - ::memmove(d->array + i, d->array + i + 1, offset * sizeof(void *)); - d->end--; - } -} - -void QListData::remove(int i, int n) -{ - Q_ASSERT(!d->ref.isShared()); - i += d->begin; - int middle = i + n/2; - if (middle - d->begin < d->end - middle) { - ::memmove(d->array + d->begin + n, d->array + d->begin, - (i - d->begin) * sizeof(void*)); - d->begin += n; - } else { - ::memmove(d->array + i, d->array + i + n, - (d->end - i - n) * sizeof(void*)); - d->end -= n; - } -} - -void QListData::move(int from, int to) -{ - Q_ASSERT(!d->ref.isShared()); - if (from == to) - return; - - from += d->begin; - to += d->begin; - void *t = d->array[from]; - - if (from < to) { - if (d->end == d->alloc || 3 * (to - from) < 2 * (d->end - d->begin)) { - ::memmove(d->array + from, d->array + from + 1, (to - from) * sizeof(void *)); - } else { - // optimization - if (int offset = from - d->begin) - ::memmove(d->array + d->begin + 1, d->array + d->begin, offset * sizeof(void *)); - if (int offset = d->end - (to + 1)) - ::memmove(d->array + to + 2, d->array + to + 1, offset * sizeof(void *)); - ++d->begin; - ++d->end; - ++to; - } - } else { - if (d->begin == 0 || 3 * (from - to) < 2 * (d->end - d->begin)) { - ::memmove(d->array + to + 1, d->array + to, (from - to) * sizeof(void *)); - } else { - // optimization - if (int offset = to - d->begin) - ::memmove(d->array + d->begin - 1, d->array + d->begin, offset * sizeof(void *)); - if (int offset = d->end - (from + 1)) - ::memmove(d->array + from, d->array + from + 1, offset * sizeof(void *)); - --d->begin; - --d->end; - --to; - } - } - d->array[to] = t; -} - -void **QListData::erase(void **xi) -{ - Q_ASSERT(!d->ref.isShared()); - int i = xi - (d->array + d->begin); - remove(i); - return d->array + d->begin + i; -} - -/*! \class QList - \inmodule QtCore - \brief The QList class is a template class that provides lists. - - \ingroup tools - \ingroup shared - - \reentrant - - QList\<T\> is one of Qt's generic \l{container classes}. It - stores items in a list that provides fast index-based access - and index-based insertions and removals. - - QList\<T\>, QLinkedList\<T\>, and QVector\<T\> provide similar - APIs and functionality. They are often interchangeable, but there - are performance consequences. Here is an overview of use cases: - - \list - \li QVector should be your default first choice. - QVector\<T\> will usually give better performance than QList\<T\>, - because QVector\<T\> always stores its items sequentially in memory, - where QList\<T\> will allocate its items on the heap unless - \c {sizeof(T) <= sizeof(void*)} and T has been declared to be - either a \c{Q_MOVABLE_TYPE} or a \c{Q_PRIMITIVE_TYPE} using - \l {Q_DECLARE_TYPEINFO}. See the \l {Pros and Cons of Using QList} - for an explanation. - \li However, QList is used throughout the Qt APIs for passing - parameters and for returning values. Use QList to interface with - those APIs. - \li If you need a real linked list, which guarantees - \l {Algorithmic Complexity}{constant time} insertions mid-list and - uses iterators to items rather than indexes, use QLinkedList. - \endlist - - \note QVector and QVarLengthArray both guarantee C-compatible - array layout. QList does not. This might be important if your - application must interface with a C API. - - \note Iterators into a QLinkedList and references into - heap-allocating QLists remain valid as long as the referenced items - remain in the container. This is not true for iterators and - references into a QVector and non-heap-allocating QLists. - - Internally, QList\<T\> is represented as an array of T if - \c{sizeof(T) <= sizeof(void*)} and T has been declared to be - either a \c{Q_MOVABLE_TYPE} or a \c{Q_PRIMITIVE_TYPE} using - \l {Q_DECLARE_TYPEINFO}. Otherwise, QList\<T\> is represented - as an array of T* and the items are allocated on the heap. - - The array representation allows very fast insertions and - index-based access. The prepend() and append() operations are - also very fast because QList preallocates memory at both - ends of its internal array. (See \l{Algorithmic Complexity} for - details. - - Note, however, that when the conditions specified above are not met, - each append or insert of a new item requires allocating the new item - on the heap, and this per item allocation will make QVector a better - choice for use cases that do a lot of appending or inserting, because - QVector can allocate memory for many items in a single heap allocation. - - Note that the internal array only ever gets bigger over the life - of the list. It never shrinks. The internal array is deallocated - by the destructor and by the assignment operator, when one list - is assigned to another. - - Here's an example of a QList that stores integers and - a QList that stores QDate values: - - \snippet code/src_corelib_tools_qlistdata.cpp 0 - - Qt includes a QStringList class that inherits QList\<QString\> - and adds a few convenience functions, such as QStringList::join() - and QStringList::filter(). QString::split() creates QStringLists - from strings. - - QList stores a list of items. The default constructor creates an - empty list. You can use the initializer-list constructor to create - a list with elements: - - \snippet code/src_corelib_tools_qlistdata.cpp 1a - - QList provides these basic functions to add, move, and remove - items: insert(), replace(), removeAt(), move(), and swap(). In - addition, it provides the following convenience functions: - append(), \l{operator<<()}, \l{operator+=()}, prepend(), removeFirst(), - and removeLast(). - - \l{operator<<()} allows to conveniently add multiple elements to a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 1b - - QList uses 0-based indexes, just like C++ arrays. To access the - item at a particular index position, you can use operator[](). On - non-const lists, operator[]() returns a reference to the item and - can be used on the left side of an assignment: - - \snippet code/src_corelib_tools_qlistdata.cpp 2 - - Because QList is implemented as an array of pointers for types - that are larger than a pointer or are not movable, this operation - requires (\l{Algorithmic Complexity}{constant time}). For read-only - access, an alternative syntax is to use at(): - - \snippet code/src_corelib_tools_qlistdata.cpp 3 - - at() can be faster than operator[](), because it never causes a - \l{deep copy} to occur. - - A common requirement is to remove an item from a list and do - something with it. For this, QList provides takeAt(), takeFirst(), - and takeLast(). Here's a loop that removes the items from a list - one at a time and calls \c delete on them: - - \snippet code/src_corelib_tools_qlistdata.cpp 4 - - Inserting and removing items at either end of the list is very - fast (\l{Algorithmic Complexity}{constant time} in most cases), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - If you want to find all occurrences of a particular value in a - list, use indexOf() or lastIndexOf(). The former searches forward - starting from a given index position, the latter searches - backward. Both return the index of a matching item if they find - it; otherwise, they return -1. For example: - - \snippet code/src_corelib_tools_qlistdata.cpp 5 - - If you simply want to check whether a list contains a particular - value, use contains(). If you want to find out how many times a - particular value occurs in the list, use count(). If you want to - replace all occurrences of a particular value with another, use - replace(). - - QList's value type must be an \l{assignable data type}. This - covers most data types that are commonly used, but the compiler - won't let you, for example, store a QWidget as a value; instead, - store a QWidget *. A few functions have additional requirements; - for example, indexOf() and lastIndexOf() expect the value type to - support \c operator==(). These requirements are documented on a - per-function basis. - - Like the other container classes, QList provides \l{Java-style - iterators} (QListIterator and QMutableListIterator) and - \l{STL-style iterators} (QList::const_iterator and - QList::iterator). In practice, these are rarely used, because you - can use indexes into the QList. QList is implemented in such a way - that direct index-based access is just as fast as using iterators. - - QList does \e not support inserting, prepending, appending or - replacing with references to its own values. Doing so will cause - your application to abort with an error message. - - To make QList as efficient as possible, its member functions don't - validate their input before using it. Except for isEmpty(), member - functions always assume the list is \e not empty. Member functions - that take index values as parameters always assume their index - value parameters are in the valid range. This means QList member - functions can fail. If you define QT_NO_DEBUG when you compile, - failures will not be detected. If you \e don't define QT_NO_DEBUG, - failures will be detected using Q_ASSERT() or Q_ASSERT_X() with an - appropriate message. - - To avoid failures when your list can be empty, call isEmpty() - before calling other member functions. If you must pass an index - value that might not be in the valid range, check that it is less - than the value returned by size() but \e not less than 0. - - \section1 More Members - - If T is a QByteArray, this class has a couple more members that can be - used. See the documentation for QByteArrayList for more information. - - If T is QString, this class has the following additional members: - \l{QStringList::filter()}{filter}, - \l{QStringList::join()}{join}, - \l{QStringList::removeDuplicates()}{removeDuplicates}, - \l{QStringList::sort()}{sort}. - - \section1 More Information on Using Qt Containers - - For a detailed discussion comparing Qt containers with each other and - with STL containers, see \l {Understand the Qt Containers}. - - \sa QListIterator, QMutableListIterator, QLinkedList, QVector -*/ - -/*! - \fn template <class T> QList<T>::QList(QList<T> &&other) - - Move-constructs a QList instance, making it point at the same - object that \a other was pointing to. - - \since 5.2 -*/ - -/*! \fn template <class T> template<typename InputIterator> QList<T>::QList(InputIterator first, InputIterator last) - \since 5.14 - - Constructs a QList with the contents in the iterator range [\a first, \a last). - - The value type of \c InputIterator must be convertible to \c T. -*/ - -/*! - \fn template <class T> QList<T> QList<T>::mid(int pos, int length) const - - Returns a sub-list which includes elements from this list, - starting at position \a pos. If \a length is -1 (the default), all - elements from \a pos are included; otherwise \a length elements (or - all remaining elements if there are less than \a length elements) - are included. -*/ - -/*! \fn template <class T> QList<T>::QList() - - Constructs an empty list. -*/ - -/*! \fn template <class T> QList<T>::QList(const QList<T> &other) - - Constructs a copy of \a other. - - This operation takes \l{Algorithmic Complexity}{constant time}, - because QList is \l{implicitly shared}. This makes returning a - QList from a function very fast. If a shared instance is modified, - it will be copied (copy-on-write), and that takes - \l{Algorithmic Complexity}{linear time}. - - \sa operator=() -*/ - -/*! \fn template <class T> QList<T>::QList(std::initializer_list<T> args) - \since 4.8 - - Construct a list from the std::initializer_list specified by \a args. - - This constructor is only enabled if the compiler supports C++11 initializer - lists. -*/ - -/*! \fn template <class T> QList<T>::~QList() - - Destroys the list. References to the values in the list and all - iterators of this list become invalid. -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator=(const QList<T> &other) - - Assigns \a other to this list and returns a reference to this - list. -*/ - -/*! - \fn template <class T> QList &QList<T>::operator=(QList<T> &&other) - - Move-assigns \a other to this QList instance. - - \since 5.2 -*/ - -/*! \fn template <class T> void QList<T>::swap(QList<T> &other) - \since 4.8 - - Swaps list \a other with this list. This operation is very - fast and never fails. -*/ - -/*! \fn template <class T> bool QList<T>::operator==(const QList<T> &other) const - - Returns \c true if \a other is equal to this list; otherwise returns - false. - - Two lists are considered equal if they contain the same values in - the same order. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa operator!=() -*/ - -/*! \fn template <class T> bool QList<T>::operator!=(const QList<T> &other) const - - Returns \c true if \a other is not equal to this list; otherwise - returns \c false. - - Two lists are considered equal if they contain the same values in - the same order. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa operator==() -*/ - -/*! \fn template <class T> bool operator<(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically less than} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template <class T> bool operator<=(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically less than or equal to} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template <class T> bool operator>(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically greater than} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! \fn template <class T> bool operator>=(const QList<T> &lhs, const QList<T> &rhs) - \since 5.6 - \relates QList - - Returns \c true if list \a lhs is - \l{http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare} - {lexicographically greater than or equal to} \a rhs; otherwise returns \c false. - - This function requires the value type to have an implementation - of \c operator<(). -*/ - -/*! - \fn template <class T> uint qHash(const QList<T> &key, uint seed = 0) - \since 5.6 - \relates QList - - Returns the hash value for \a key, - using \a seed to seed the calculation. - - This function requires qHash() to be overloaded for the value type \c T. -*/ - -/*! - \fn template <class T> int QList<T>::size() const - - Returns the number of items in the list. - - \sa isEmpty(), count() -*/ - -/*! \fn template <class T> void QList<T>::detach() - - \internal -*/ - -/*! \fn template <class T> void QList<T>::detachShared() - - \internal - - like detach(), but does nothing if we're shared_null. - This prevents needless mallocs, and makes QList more exception safe - in case of cleanup work done in destructors on empty lists. -*/ - -/*! \fn template <class T> bool QList<T>::isDetached() const - - \internal -*/ - -/*! \fn template <class T> void QList<T>::setSharable(bool sharable) - - \internal -*/ - -/*! \fn template <class T> bool QList<T>::isSharedWith(const QList<T> &other) const - - \internal -*/ - -/*! \fn template <class T> bool QList<T>::isEmpty() const - - Returns \c true if the list contains no items; otherwise returns - false. - - \sa size() -*/ - -/*! \fn template <class T> void QList<T>::clear() - - Removes all items from the list. - - \sa removeAll() -*/ - -/*! \fn template <class T> const T &QList<T>::at(int i) const - - Returns the item at index position \a i in the list. \a i must be - a valid index position in the list (i.e., 0 <= \a i < size()). - - This function is very fast (\l{Algorithmic Complexity}{constant time}). - - \sa value(), operator[]() -*/ - -/*! \fn template <class T> T &QList<T>::operator[](int i) - - Returns the item at index position \a i as a modifiable reference. - \a i must be a valid index position in the list (i.e., 0 <= \a i < - size()). - - If this function is called on a list that is currently being shared, it - will trigger a copy of all elements. Otherwise, this function runs in - \l{Algorithmic Complexity}{constant time}. If you do not want to modify - the list you should use QList::at(). - - \sa at(), value() -*/ - -/*! \fn template <class T> const T &QList<T>::operator[](int i) const - - \overload - - Same as at(). This function runs in \l{Algorithmic Complexity}{constant time}. -*/ - -/*! \fn template <class T> void QList<T>::reserve(int alloc) - - Reserve space for \a alloc elements. - - If \a alloc is smaller than the current size of the list, nothing will happen. - - Use this function to avoid repetetive reallocation of QList's internal - data if you can predict how many elements will be appended. - Note that the reservation applies only to the internal pointer array. - - \since 4.7 -*/ - -/*! \fn template <class T> void QList<T>::append(const T &value) - - Inserts \a value at the end of the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 6 - - This is the same as list.insert(size(), \a value). - - If this list is not shared, this operation is typically - very fast (amortized \l{Algorithmic Complexity}{constant time}), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - \sa operator<<(), prepend(), insert() -*/ - -/*! \fn template <class T> void QList<T>::append(const QList<T> &value) - - \overload - - \since 4.5 - - Appends the items of the \a value list to this list. - - \sa operator<<(), operator+=() -*/ - -/*! \fn template <class T> void QList<T>::prepend(const T &value) - - Inserts \a value at the beginning of the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 7 - - This is the same as list.insert(0, \a value). - - If this list is not shared, this operation is typically - very fast (amortized \l{Algorithmic Complexity}{constant time}), - because QList preallocates extra space on both sides of its - internal buffer to allow for fast growth at both ends of the list. - - \sa append(), insert() -*/ - -/*! \fn template <class T> void QList<T>::insert(int i, const T &value) - - Inserts \a value at index position \a i in the list. - - If \a i == 0, the value is prepended to the list. If \a i == size(), - the value is appended to the list. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 8 - - \sa append(), prepend(), replace(), removeAt() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::insert(iterator before, const T &value) - - \overload - - Inserts \a value in front of the item pointed to by the - iterator \a before. Returns an iterator pointing at the inserted - item. Note that the iterator passed to the function will be - invalid after the call; the returned iterator should be used - instead. -*/ - -/*! \fn template <class T> void QList<T>::replace(int i, const T &value) - - Replaces the item at index position \a i with \a value. \a i must - be a valid index position in the list (i.e., 0 <= \a i < size()). - - \sa operator[](), removeAt() -*/ - -/*! - \fn template <class T> int QList<T>::removeAll(const T &value) - - Removes all occurrences of \a value in the list and returns the - number of entries removed. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 9 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa removeOne(), removeAt(), takeAt(), replace() -*/ - -/*! - \fn template <class T> bool QList<T>::removeOne(const T &value) - \since 4.4 - - Removes the first occurrence of \a value in the list and returns - true on success; otherwise returns \c false. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 10 - - This function requires the value type to have an implementation of - \c operator==(). - - \sa removeAll(), removeAt(), takeAt(), replace() -*/ - -/*! \fn template <class T> void QList<T>::removeAt(int i) - - Removes the item at index position \a i. \a i must be a valid - index position in the list (i.e., 0 <= \a i < size()). - - \sa takeAt(), removeFirst(), removeLast(), removeOne() -*/ - -/*! \fn template <class T> T QList<T>::takeAt(int i) - - Removes the item at index position \a i and returns it. \a i must - be a valid index position in the list (i.e., 0 <= \a i < size()). - - If you don't use the return value, removeAt() is more efficient. - - \sa removeAt(), takeFirst(), takeLast() -*/ - -/*! \fn template <class T> T QList<T>::takeFirst() - - Removes the first item in the list and returns it. This is the - same as takeAt(0). This function assumes the list is not empty. To - avoid failure, call isEmpty() before calling this function. - - If this list is not shared, this operation takes - \l {Algorithmic Complexity}{constant time}. - - If you don't use the return value, removeFirst() is more - efficient. - - \sa takeLast(), takeAt(), removeFirst() -*/ - -/*! \fn template <class T> T QList<T>::takeLast() - - Removes the last item in the list and returns it. This is the - same as takeAt(size() - 1). This function assumes the list is - not empty. To avoid failure, call isEmpty() before calling this - function. - - If this list is not shared, this operation takes - \l {Algorithmic Complexity}{constant time}. - - If you don't use the return value, removeLast() is more - efficient. - - \sa takeFirst(), takeAt(), removeLast() -*/ - -/*! \fn template <class T> void QList<T>::move(int from, int to) - - Moves the item at index position \a from to index position \a to. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 11 - - This is the same as insert(\a{to}, takeAt(\a{from})).This function - assumes that both \a from and \a to are at least 0 but less than - size(). To avoid failure, test that both \a from and \a to are at - least 0 and less than size(). - - \sa swap(), insert(), takeAt() -*/ - -/*! \fn template <class T> void QList<T>::swap(int i, int j) - - \obsolete Use swapItemsAt() - - \sa move(), swapItemsAt() -*/ - -/*! \fn template <class T> void QList<T>::swapItemsAt(int i, int j) - \since 5.13 - - Exchange the item at index position \a i with the item at index - position \a j. This function assumes that both \a i and \a j are - at least 0 but less than size(). To avoid failure, test that both - \a i and \a j are at least 0 and less than size(). - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 12 - - \sa move() -*/ - -/*! \fn template <class T> int QList<T>::indexOf(const T &value, int from = 0) const - - Returns the index position of the first occurrence of \a value in - the list, searching forward from index position \a from. Returns - -1 if no item matched. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 13 - - This function requires the value type to have an implementation of - \c operator==(). - - Note that QList uses 0-based indexes, just like C++ arrays. Negative - indexes are not supported with the exception of the value mentioned - above. - - \sa lastIndexOf(), contains() -*/ - -/*! \fn template <class T> int QList<T>::lastIndexOf(const T &value, int from = -1) const - - Returns the index position of the last occurrence of \a value in - the list, searching backward from index position \a from. If \a - from is -1 (the default), the search starts at the last item. - Returns -1 if no item matched. - - Example: - \snippet code/src_corelib_tools_qlistdata.cpp 14 - - This function requires the value type to have an implementation of - \c operator==(). - - Note that QList uses 0-based indexes, just like C++ arrays. Negative - indexes are not supported with the exception of the value mentioned - above. - - \sa indexOf() -*/ - -/*! \fn template <class T> bool QList<T>::contains(const T &value) const - - Returns \c true if the list contains an occurrence of \a value; - otherwise returns \c false. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa indexOf(), count() -*/ - -/*! \fn template <class T> int QList<T>::count(const T &value) const - - Returns the number of occurrences of \a value in the list. - - This function requires the value type to have an implementation of - \c operator==(). - - \sa contains(), indexOf() -*/ - -/*! \fn template <class T> bool QList<T>::startsWith(const T &value) const - \since 4.5 - - Returns \c true if this list is not empty and its first - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), contains() -*/ - -/*! \fn template <class T> bool QList<T>::endsWith(const T &value) const - \since 4.5 - - Returns \c true if this list is not empty and its last - item is equal to \a value; otherwise returns \c false. - - \sa isEmpty(), contains() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::begin() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in - the list. - - \sa constBegin(), end() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::begin() const - - \overload -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::cbegin() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), cend() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::constBegin() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item - in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::end() - - Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item - after the last item in the list. - - \sa begin(), constEnd() -*/ - -/*! \fn template <class T> const_iterator QList<T>::end() const - - \overload -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::cend() const - \since 5.0 - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the list. - - \sa cbegin(), end() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::constEnd() const - - Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary - item after the last item in the list. - - \sa constBegin(), end() -*/ - -/*! \fn template <class T> QList<T>::reverse_iterator QList<T>::rbegin() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), crbegin(), rend() -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::rbegin() const - \since 5.6 - \overload -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::crbegin() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first - item in the list, in reverse order. - - \sa begin(), rbegin(), rend() -*/ - -/*! \fn template <class T> QList<T>::reverse_iterator QList<T>::rend() - \since 5.6 - - Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past - the last item in the list, in reverse order. - - \sa end(), crend(), rbegin() -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::rend() const - \since 5.6 - \overload -*/ - -/*! \fn template <class T> QList<T>::const_reverse_iterator QList<T>::crend() const - \since 5.6 - - Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one - past the last item in the list, in reverse order. - - \sa end(), rend(), rbegin() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::erase(iterator pos) - - Removes the item associated with the iterator \a pos from the - list, and returns an iterator to the next item in the list (which - may be end()). - - \sa insert(), removeAt() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::erase(iterator begin, iterator end) - - \overload - - Removes all the items from \a begin up to (but not including) \a - end. Returns an iterator to the same item that \a end referred to - before the call. -*/ - -/*! \typedef QList::Iterator - - Qt-style synonym for QList::iterator. -*/ - -/*! \typedef QList::ConstIterator - - Qt-style synonym for QList::const_iterator. -*/ - -/*! - \typedef QList::size_type - - Typedef for int. Provided for STL compatibility. -*/ - -/*! - \typedef QList::value_type - - Typedef for T. Provided for STL compatibility. -*/ - -/*! - \typedef QList::difference_type - - Typedef for ptrdiff_t. Provided for STL compatibility. -*/ - -/*! - \typedef QList::pointer - - Typedef for T *. Provided for STL compatibility. -*/ - -/*! - \typedef QList::const_pointer - - Typedef for const T *. Provided for STL compatibility. -*/ - -/*! - \typedef QList::reference - - Typedef for T &. Provided for STL compatibility. -*/ - -/*! - \typedef QList::const_reference - - Typedef for const T &. Provided for STL compatibility. -*/ - -/*! \typedef QList::reverse_iterator - \since 5.6 - - The QList::reverse_iterator typedef provides an STL-style non-const - reverse iterator for QList. - - It is simply a typedef for \c{std::reverse_iterator<iterator>}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator -*/ - -/*! \typedef QList::const_reverse_iterator - \since 5.6 - - The QList::const_reverse_iterator typedef provides an STL-style const - reverse iterator for QList. - - It is simply a typedef for \c{std::reverse_iterator<const_iterator>}. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator -*/ - -/*! \fn template <class T> int QList<T>::count() const - - Returns the number of items in the list. This is effectively the - same as size(). -*/ - -/*! \fn template <class T> int QList<T>::length() const - \since 4.5 - - This function is identical to count(). - - \sa count() -*/ - -/*! \fn template <class T> T& QList<T>::first() - - Returns a reference to the first item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constFirst(), last(), isEmpty() -*/ - -/*! \fn template <class T> const T& QList<T>::first() const - - \overload -*/ - -/*! \fn template <class T> const T& QList<T>::constFirst() const - \since 5.6 - - Returns a const reference to the first item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constLast(), isEmpty(), first() -*/ - -/*! \fn template <class T> T& QList<T>::last() - - Returns a reference to the last item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constLast(), first(), isEmpty() -*/ - -/*! \fn template <class T> const T& QList<T>::last() const - - \overload -*/ - -/*! \fn template <class T> const T& QList<T>::constLast() const - \since 5.6 - - Returns a reference to the last item in the list. The list must - not be empty. If the list can be empty, call isEmpty() before - calling this function. - - \sa constFirst(), isEmpty(), last() -*/ - -/*! \fn template <class T> void QList<T>::removeFirst() - - Removes the first item in the list. Calling this function is - equivalent to calling removeAt(0). The list must not be empty. If - the list can be empty, call isEmpty() before calling this - function. - - \sa removeAt(), takeFirst() -*/ - -/*! \fn template <class T> void QList<T>::removeLast() - - Removes the last item in the list. Calling this function is - equivalent to calling removeAt(size() - 1). The list must not be - empty. If the list can be empty, call isEmpty() before calling - this function. - - \sa removeAt(), takeLast() -*/ - -/*! \fn template <class T> T QList<T>::value(int i) const - - Returns the value at index position \a i in the list. - - If the index \a i is out of bounds, the function returns a - \l{default-constructed value}. If you are certain that the index - is going to be within bounds, you can use at() instead, which is - slightly faster. - - \sa at(), operator[]() -*/ - -/*! \fn template <class T> T QList<T>::value(int i, const T &defaultValue) const - - \overload - - If the index \a i is out of bounds, the function returns - \a defaultValue. -*/ - -/*! \fn template <class T> void QList<T>::push_back(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::append()}{append(\a value)}. -*/ - -/*! \fn template <class T> void QList<T>::push_front(const T &value) - - This function is provided for STL compatibility. It is equivalent - to \l{QList::prepend()}{prepend(\a value)}. -*/ - -/*! \fn template <class T> T& QList<T>::front() - - This function is provided for STL compatibility. It is equivalent - to first(). The list must not be empty. If the list can be empty, - call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> const T& QList<T>::front() const - - \overload -*/ - -/*! \fn template <class T> T& QList<T>::back() - - This function is provided for STL compatibility. It is equivalent - to last(). The list must not be empty. If the list can be empty, - call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> const T& QList<T>::back() const - - \overload -*/ - -/*! \fn template <class T> void QList<T>::pop_front() - - This function is provided for STL compatibility. It is equivalent - to removeFirst(). The list must not be empty. If the list can be - empty, call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> void QList<T>::pop_back() - - This function is provided for STL compatibility. It is equivalent - to removeLast(). The list must not be empty. If the list can be - empty, call isEmpty() before calling this function. -*/ - -/*! \fn template <class T> bool QList<T>::empty() const - - This function is provided for STL compatibility. It is equivalent - to isEmpty() and returns \c true if the list is empty. -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator+=(const QList<T> &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+(), append() -*/ - -/*! \fn template <class T> void QList<T>::operator+=(const T &value) - - \overload - - Appends \a value to the list. - - \sa append(), operator<<() -*/ - -/*! \fn template <class T> QList<T> QList<T>::operator+(const QList<T> &other) const - - Returns a list that contains all the items in this list followed - by all the items in the \a other list. - - \sa operator+=() -*/ - -/*! \fn template <class T> QList<T> &QList<T>::operator<<(const QList<T> &other) - - Appends the items of the \a other list to this list and returns a - reference to this list. - - \sa operator+=(), append() -*/ - -/*! \fn template <class T> void QList<T>::operator<<(const T &value) - - \overload - - Appends \a value to the list. -*/ - -/*! \class QList::iterator - \inmodule QtCore - \brief The QList::iterator class provides an STL-style non-const iterator for QList and QQueue. - - QList features both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style iterators are more low-level and more - cumbersome to use; on the other hand, they are slightly faster - and, for developers who already know STL, have the advantage of - familiarity. - - QList\<T\>::iterator allows you to iterate over a QList\<T\> (or - QQueue\<T\>) and to modify the list item associated with the - iterator. If you want to iterate over a const QList, use - QList::const_iterator instead. It is generally good practice to - use QList::const_iterator on a non-const QList as well, unless - you need to change the QList through the iterator. Const - iterators are slightly faster, and can improve code readability. - - The default QList::iterator constructor creates an uninitialized - iterator. You must initialize it using a QList function like - QList::begin(), QList::end(), or QList::insert() before you can - start iterating. Here's a typical loop that prints all the items - stored in a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 15 - - Let's see a few examples of things we can do with a - QList::iterator that we cannot do with a QList::const_iterator. - Here's an example that increments every value stored in a - QList\<int\> by 2: - - \snippet code/src_corelib_tools_qlistdata.cpp 16 - - Most QList functions accept an integer index rather than an - iterator. For that reason, iterators are rarely useful in - connection with QList. One place where STL-style iterators do - make sense is as arguments to \l{generic algorithms}. - - For example, here's how to delete all the widgets stored in a - QList\<QWidget *\>: - - \snippet code/src_corelib_tools_qlistdata.cpp 17 - - Multiple iterators can be used on the same list. However, be - aware that any non-const function call performed on the QList - will render all existing iterators undefined. If you need to keep - iterators over a long period of time, we recommend that you use - QLinkedList rather than QList. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::const_iterator, QMutableListIterator -*/ - -/*! \typedef QList::iterator::iterator_category - - A synonym for \e {std::random_access_iterator_tag} indicating - this iterator is a random access iterator. -*/ - -/*! \typedef QList::iterator::difference_type - - \internal -*/ - -/*! \typedef QList::iterator::value_type - - \internal -*/ - -/*! \typedef QList::iterator::pointer - - \internal -*/ - -/*! \typedef QList::iterator::reference - - \internal -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator() - - Constructs an uninitialized iterator. - - Functions like operator*() and operator++() should not be called - on an uninitialized iterator. Use operator=() to assign a value - to it before using it. - - \sa QList::begin(), QList::end() -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator(Node *node) - - \internal -*/ - -/*! \fn template <class T> QList<T>::iterator::iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> T &QList<T>::iterator::operator*() const - - Returns a modifiable reference to the current item. - - You can change the value of an item by using operator*() on the - left side of an assignment, for example: - - \snippet code/src_corelib_tools_qlistdata.cpp 18 - - \sa operator->() -*/ - -/*! \fn template <class T> T *QList<T>::iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template <class T> T &QList<T>::iterator::operator[](difference_type j) const - - Returns a modifiable reference to the item at position *this + - \a{j}. - - This function is provided to make QList iterators behave like C++ - pointers. - - \sa operator+() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator==(const iterator &other) const - \fn template <class T> bool QList<T>::iterator::operator==(const const_iterator &other) const - - Returns \c true if \a other points to the same item as this - iterator; otherwise returns \c false. - - \sa operator!=() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator!=(const iterator &other) const - \fn template <class T> bool QList<T>::iterator::operator!=(const const_iterator &other) const - - Returns \c true if \a other points to a different item than this - iterator; otherwise returns \c false. - - \sa operator==() -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator<(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator<(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator<=(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator<=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator>(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator>(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::iterator::operator>=(const iterator& other) const - \fn template <class T> bool QList<T>::iterator::operator>=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator++() - - The prefix ++ operator (\c{++it}) advances the iterator to the - next item in the list and returns an iterator to the new current - item. - - Calling this function on QList::end() leads to undefined results. - - \sa operator--() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator++(int) - - \overload - - The postfix ++ operator (\c{it++}) advances the iterator to the - next item in the list and returns an iterator to the previously - current item. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator--() - - The prefix -- operator (\c{--it}) makes the preceding item - current and returns an iterator to the new current item. - - Calling this function on QList::begin() leads to undefined results. - - \sa operator++() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator--(int) - - \overload - - The postfix -- operator (\c{it--}) makes the preceding item - current and returns an iterator to the previously current item. -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator+=(difference_type j) - - Advances the iterator by \a j items. (If \a j is negative, the - iterator goes backward.) - - \sa operator-=(), operator+() -*/ - -/*! \fn template <class T> QList<T>::iterator &QList<T>::iterator::operator-=(difference_type j) - - Makes the iterator go back by \a j items. (If \a j is negative, - the iterator goes forward.) - - \sa operator+=(), operator-() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator+(difference_type j) const - - Returns an iterator to the item at \a j positions forward from - this iterator. (If \a j is negative, the iterator goes backward.) - - \sa operator-(), operator+=() -*/ - -/*! \fn template <class T> QList<T>::iterator QList<T>::iterator::operator-(difference_type j) const - - Returns an iterator to the item at \a j positions backward from - this iterator. (If \a j is negative, the iterator goes forward.) - - \sa operator+(), operator-=() -*/ - -/*! \fn template <class T> int QList<T>::iterator::operator-(iterator other) const - - Returns the number of items between the item pointed to by \a - other and the item pointed to by this iterator. -*/ - -/*! \class QList::const_iterator - \inmodule QtCore - \brief The QList::const_iterator class provides an STL-style const iterator for QList and QQueue. - - QList provides both \l{STL-style iterators} and \l{Java-style - iterators}. The STL-style iterators are more low-level and more - cumbersome to use; on the other hand, they are slightly faster - and, for developers who already know STL, have the advantage of - familiarity. - - QList\<T\>::const_iterator allows you to iterate over a - QList\<T\> (or a QQueue\<T\>). If you want to modify the QList as - you iterate over it, use QList::iterator instead. It is generally - good practice to use QList::const_iterator on a non-const QList - as well, unless you need to change the QList through the - iterator. Const iterators are slightly faster, and can improve - code readability. - - The default QList::const_iterator constructor creates an - uninitialized iterator. You must initialize it using a QList - function like QList::constBegin(), QList::constEnd(), or - QList::insert() before you can start iterating. Here's a typical - loop that prints all the items stored in a list: - - \snippet code/src_corelib_tools_qlistdata.cpp 19 - - Most QList functions accept an integer index rather than an - iterator. For that reason, iterators are rarely useful in - connection with QList. One place where STL-style iterators do - make sense is as arguments to \l{generic algorithms}. - - For example, here's how to delete all the widgets stored in a - QList\<QWidget *\>: - - \snippet code/src_corelib_tools_qlistdata.cpp 20 - - Multiple iterators can be used on the same list. However, be - aware that any non-const function call performed on the QList - will render all existing iterators undefined. If you need to keep - iterators over a long period of time, we recommend that you use - QLinkedList rather than QList. - - \warning Iterators on implicitly shared containers do not work - exactly like STL-iterators. You should avoid copying a container - while iterators are active on that container. For more information, - read \l{Implicit sharing iterator problem}. - - \sa QList::iterator, QListIterator -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator() - - Constructs an uninitialized iterator. - - Functions like operator*() and operator++() should not be called - on an uninitialized iterator. Use operator=() to assign a value - to it before using it. - - \sa QList::constBegin(), QList::constEnd() -*/ - -/*! \typedef QList::const_iterator::iterator_category - - A synonym for \e {std::random_access_iterator_tag} indicating - this iterator is a random access iterator. -*/ - -/*! \typedef QList::const_iterator::difference_type - - \internal -*/ - -/*! \typedef QList::const_iterator::value_type - - \internal -*/ - -/*! \typedef QList::const_iterator::pointer - - \internal -*/ - -/*! \typedef QList::const_iterator::reference - - \internal -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(Node *node) - - \internal -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(const const_iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> QList<T>::const_iterator::const_iterator(const iterator &other) - - Constructs a copy of \a other. -*/ - -/*! \fn template <class T> const T &QList<T>::const_iterator::operator*() const - - Returns the current item. - - \sa operator->() -*/ - -/*! \fn template <class T> const T *QList<T>::const_iterator::operator->() const - - Returns a pointer to the current item. - - \sa operator*() -*/ - -/*! \fn template <class T> const T &QList<T>::const_iterator::operator[](difference_type j) const - - Returns the item at position *this + \a{j}. - - This function is provided to make QList iterators behave like C++ - pointers. - - \sa operator+() -*/ - -/*! \fn template <class T> bool QList<T>::const_iterator::operator==(const const_iterator &other) const - - Returns \c true if \a other points to the same item as this - iterator; otherwise returns \c false. - - \sa operator!=() -*/ - -/*! \fn template <class T> bool QList<T>::const_iterator::operator!=(const const_iterator &other) const - - Returns \c true if \a other points to a different item than this - iterator; otherwise returns \c false. - - \sa operator==() -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator<(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator<=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator>(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. -*/ - -/*! - \fn template <class T> bool QList<T>::const_iterator::operator>=(const const_iterator& other) const - - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator++() - - The prefix ++ operator (\c{++it}) advances the iterator to the - next item in the list and returns an iterator to the new current - item. - - Calling this function on QList::end() leads to undefined results. - - \sa operator--() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator++(int) - - \overload - - The postfix ++ operator (\c{it++}) advances the iterator to the - next item in the list and returns an iterator to the previously - current item. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator--() - - The prefix -- operator (\c{--it}) makes the preceding item - current and returns an iterator to the new current item. - - Calling this function on QList::begin() leads to undefined results. - - \sa operator++() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator--(int) - - \overload - - The postfix -- operator (\c{it--}) makes the preceding item - current and returns an iterator to the previously current item. -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator+=(difference_type j) - - Advances the iterator by \a j items. (If \a j is negative, the - iterator goes backward.) - - \sa operator-=(), operator+() -*/ - -/*! \fn template <class T> QList<T>::const_iterator &QList<T>::const_iterator::operator-=(difference_type j) - - Makes the iterator go back by \a j items. (If \a j is negative, - the iterator goes forward.) - - \sa operator+=(), operator-() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator+(difference_type j) const - - Returns an iterator to the item at \a j positions forward from - this iterator. (If \a j is negative, the iterator goes backward.) - - \sa operator-(), operator+=() -*/ - -/*! \fn template <class T> QList<T>::const_iterator QList<T>::const_iterator::operator-(difference_type j) const - - Returns an iterator to the item at \a j positions backward from - this iterator. (If \a j is negative, the iterator goes forward.) - - \sa operator+(), operator-=() -*/ - -/*! \fn template <class T> int QList<T>::const_iterator::operator-(const_iterator other) const - - Returns the number of items between the item pointed to by \a - other and the item pointed to by this iterator. -*/ - -/*! \fn template <class T> QDataStream &operator<<(QDataStream &out, const QList<T> &list) - \relates QList - - Writes the list \a list to stream \a out. - - This function requires the value type to implement \c - operator<<(). - - \sa{Serializing Qt Data Types}{Format of the QDataStream operators} -*/ - -/*! \fn template <class T> QDataStream &operator>>(QDataStream &in, QList<T> &list) - \relates QList - - Reads a list from stream \a in into \a list. - - This function requires the value type to implement \c - operator>>(). - - \sa{Serializing Qt Data Types}{Format of the QDataStream operators} -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromVector(const QVector<T> &vector) - - Returns a QList object with the data contained in \a vector. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 21 - - \include containers-range-constructor.qdocinc - - \sa fromSet(), toVector(), QVector::toList() -*/ - -/*! \fn template <class T> QVector<T> QList<T>::toVector() const - - Returns a QVector object with the data contained in this QList. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 22 - - \include containers-range-constructor.qdocinc - - \sa toSet(), fromVector(), QVector::fromList() -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromSet(const QSet<T> &set) - - Returns a QList object with the data contained in \a set. The - order of the elements in the QList is undefined. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 23 - - \include containers-range-constructor.qdocinc - - \sa fromVector(), toSet(), QSet::toList() -*/ - -/*! \fn template <class T> QSet<T> QList<T>::toSet() const - - Returns a QSet object with the data contained in this QList. - Since QSet doesn't allow duplicates, the resulting QSet might be - smaller than the original list was. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 24 - - \include containers-range-constructor.qdocinc - - \sa toVector(), fromSet(), QSet::fromList() -*/ - -/*! \fn template <class T> QList<T> QList<T>::fromStdList(const std::list<T> &list) - - Returns a QList object with the data contained in \a list. The - order of the elements in the QList is the same as in \a list. - - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 25 - - \include containers-range-constructor.qdocinc - - \sa toStdList(), QVector::fromStdVector() -*/ - -/*! \fn template <class T> std::list<T> QList<T>::toStdList() const - - Returns a std::list object with the data contained in this QList. - Example: - - \snippet code/src_corelib_tools_qlistdata.cpp 26 - - \include containers-range-constructor.qdocinc - - \sa fromStdList(), QVector::toStdVector() -*/ - QT_END_NAMESPACE diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index ffd470efcd..515fba6530 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -40,1160 +40,19 @@ #ifndef QLIST_H #define QLIST_H -#include <QtCore/qalgorithms.h> -#include <QtCore/qiterator.h> -#include <QtCore/qrefcount.h> -#include <QtCore/qarraydata.h> -#include <QtCore/qhashfunctions.h> #include <QtCore/qvector.h> -#include <QtCore/qcontainertools_impl.h> - -#include <algorithm> -#include <initializer_list> -#include <iterator> -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) -#include <list> -#endif - -#include <stdlib.h> -#include <new> -#include <limits.h> -#include <string.h> - -#ifdef Q_CC_MSVC -#pragma warning( push ) -#pragma warning( disable : 4127 ) // "conditional expression is constant" -#endif +#include <QtCore/qcontainerfwd.h> +#if !defined(QT_NO_JAVA_STYLE_ITERATORS) QT_BEGIN_NAMESPACE - - -template <typename T> class QVector; -template <typename T> class QSet; - -template <typename T> struct QListSpecialMethods -{ -protected: - ~QListSpecialMethods() = default; -}; -template <> struct QListSpecialMethods<QByteArray>; -template <> struct QListSpecialMethods<QString>; - -struct Q_CORE_EXPORT QListData { - // tags for tag-dispatching of QList implementations, - // based on QList's three different memory layouts: - struct NotArrayCompatibleLayout {}; - struct NotIndirectLayout {}; - struct ArrayCompatibleLayout : NotIndirectLayout {}; // data laid out like a C array - struct InlineWithPaddingLayout : NotArrayCompatibleLayout, NotIndirectLayout {}; // data laid out like a C array with padding - struct IndirectLayout : NotArrayCompatibleLayout {}; // data allocated on the heap - - struct Data { - QtPrivate::RefCount ref; - int alloc, begin, end; - void *array[1]; - }; - enum { DataHeaderSize = sizeof(Data) - sizeof(void *) }; - - Data *detach(int alloc); - Data *detach_grow(int *i, int n); - void realloc(int alloc); - void realloc_grow(int growth); - inline void dispose() { dispose(d); } - static void dispose(Data *d); - static const Data shared_null; - Data *d; - void **erase(void **xi); - void **append(int n); - void **append(); - void **append(const QListData &l); - void **prepend(); - void **insert(int i); - void remove(int i); - void remove(int i, int n); - void move(int from, int to); - inline int size() const noexcept { return int(d->end - d->begin); } // q6sizetype - inline bool isEmpty() const noexcept { return d->end == d->begin; } - inline void **at(int i) const noexcept { return d->array + d->begin + i; } - inline void **begin() const noexcept { return d->array + d->begin; } - inline void **end() const noexcept { return d->array + d->end; } -}; - -namespace QtPrivate { - template <typename V, typename U> int indexOf(const QList<V> &list, const U &u, int from); - template <typename V, typename U> int lastIndexOf(const QList<V> &list, const U &u, int from); -} - -template <typename T> -class QList -#ifndef Q_QDOC - : public QListSpecialMethods<T> -#endif -{ -public: - struct MemoryLayout - : std::conditional< - // must stay isStatic until ### Qt 6 for BC reasons (don't use !isRelocatable)! - QTypeInfo<T>::isStatic || QTypeInfo<T>::isLarge, - QListData::IndirectLayout, - typename std::conditional< - sizeof(T) == sizeof(void*), - QListData::ArrayCompatibleLayout, - QListData::InlineWithPaddingLayout - >::type>::type {}; -private: - template <typename V, typename U> friend int QtPrivate::indexOf(const QList<V> &list, const U &u, int from); - template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QList<V> &list, const U &u, int from); - struct Node { void *v; -#if defined(Q_CC_BOR) - Q_INLINE_TEMPLATE T &t(); -#else - Q_INLINE_TEMPLATE T &t() - { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic - ? v : this); } -#endif - }; - - union { QListData p; QListData::Data *d; }; - -public: - inline QList() noexcept : d(const_cast<QListData::Data *>(&QListData::shared_null)) { } - QList(const QList<T> &l); - ~QList(); - QList<T> &operator=(const QList<T> &l); - inline QList(QList<T> &&other) noexcept - : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); } - inline QList &operator=(QList<T> &&other) noexcept - { QList moved(std::move(other)); swap(moved); return *this; } - inline void swap(QList<T> &other) noexcept { qSwap(d, other.d); } - inline QList(std::initializer_list<T> args) - : QList(args.begin(), args.end()) {} - template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> - QList(InputIterator first, InputIterator last); - bool operator==(const QList<T> &l) const; - inline bool operator!=(const QList<T> &l) const { return !(*this == l); } - - inline int size() const noexcept { return p.size(); } - - inline void detach() { if (d->ref.isShared()) detach_helper(); } - - inline void detachShared() - { - // The "this->" qualification is needed for GCCE. - if (d->ref.isShared() && this->d != &QListData::shared_null) - detach_helper(); - } - - inline bool isDetached() const { return !d->ref.isShared(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) - { - if (sharable == d->ref.isSharable()) - return; - if (!sharable) - detach(); - if (d != &QListData::shared_null) - d->ref.setSharable(sharable); - } -#endif - inline bool isSharedWith(const QList<T> &other) const noexcept { return d == other.d; } - - inline bool isEmpty() const noexcept { return p.isEmpty(); } - - void clear(); - - const T &at(int i) const; - const T &operator[](int i) const; - T &operator[](int i); - - void reserve(int size); - void append(const T &t); - void append(const QList<T> &t); - void prepend(const T &t); - void insert(int i, const T &t); - void replace(int i, const T &t); - void removeAt(int i); - int removeAll(const T &t); - bool removeOne(const T &t); - T takeAt(int i); - T takeFirst(); - T takeLast(); - void move(int from, int to); - void swapItemsAt(int i, int j); -#if QT_DEPRECATED_SINCE(5, 13) && QT_VERSION < QT_VERSION_CHECK(6,0,0) - QT_DEPRECATED_X("Use QList<T>::swapItemsAt()") - void swap(int i, int j) { swapItemsAt(i, j); } -#endif - int indexOf(const T &t, int from = 0) const; - int lastIndexOf(const T &t, int from = -1) const; - bool contains(const T &t) const; - int count(const T &t) const; - - class const_iterator; - - class iterator { - public: - Node *i; - typedef std::random_access_iterator_tag iterator_category; - // ### Qt6: use int - typedef qptrdiff difference_type; - typedef T value_type; - typedef T *pointer; - typedef T &reference; - - inline iterator() noexcept : i(nullptr) {} - inline iterator(Node *n) noexcept : i(n) {} -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - // can't remove it in Qt 5, since doing so would make the type trivial, - // which changes the way it's passed to functions by value. - inline iterator(const iterator &o) noexcept : i(o.i){} - inline iterator &operator=(const iterator &o) noexcept - { i = o.i; return *this; } -#endif - inline T &operator*() const { return i->t(); } - inline T *operator->() const { return &i->t(); } - inline T &operator[](difference_type j) const { return i[j].t(); } - inline bool operator==(const iterator &o) const noexcept { return i == o.i; } - inline bool operator!=(const iterator &o) const noexcept { return i != o.i; } - inline bool operator<(const iterator& other) const noexcept { return i < other.i; } - inline bool operator<=(const iterator& other) const noexcept { return i <= other.i; } - inline bool operator>(const iterator& other) const noexcept { return i > other.i; } - inline bool operator>=(const iterator& other) const noexcept { return i >= other.i; } -#ifndef QT_STRICT_ITERATORS - inline bool operator==(const const_iterator &o) const noexcept - { return i == o.i; } - inline bool operator!=(const const_iterator &o) const noexcept - { return i != o.i; } - inline bool operator<(const const_iterator& other) const noexcept - { return i < other.i; } - inline bool operator<=(const const_iterator& other) const noexcept - { return i <= other.i; } - inline bool operator>(const const_iterator& other) const noexcept - { return i > other.i; } - inline bool operator>=(const const_iterator& other) const noexcept - { return i >= other.i; } -#endif - inline iterator &operator++() { ++i; return *this; } - inline iterator operator++(int) { Node *n = i; ++i; return n; } - inline iterator &operator--() { i--; return *this; } - inline iterator operator--(int) { Node *n = i; i--; return n; } - inline iterator &operator+=(difference_type j) { i+=j; return *this; } - inline iterator &operator-=(difference_type j) { i-=j; return *this; } - inline iterator operator+(difference_type j) const { return iterator(i+j); } - inline iterator operator-(difference_type j) const { return iterator(i-j); } - friend inline iterator operator+(difference_type j, iterator k) { return k + j; } - inline int operator-(iterator j) const { return int(i - j.i); } - }; - friend class iterator; - - class const_iterator { - public: - Node *i; - typedef std::random_access_iterator_tag iterator_category; - // ### Qt6: use int - typedef qptrdiff difference_type; - typedef T value_type; - typedef const T *pointer; - typedef const T &reference; - - inline const_iterator() noexcept : i(nullptr) {} - inline const_iterator(Node *n) noexcept : i(n) {} -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - // can't remove it in Qt 5, since doing so would make the type trivial, - // which changes the way it's passed to functions by value. - inline const_iterator(const const_iterator &o) noexcept : i(o.i) {} - inline const_iterator &operator=(const const_iterator &o) noexcept - { i = o.i; return *this; } -#endif -#ifdef QT_STRICT_ITERATORS - inline explicit const_iterator(const iterator &o) noexcept : i(o.i) {} -#else - inline const_iterator(const iterator &o) noexcept : i(o.i) {} -#endif - inline const T &operator*() const { return i->t(); } - inline const T *operator->() const { return &i->t(); } - inline const T &operator[](difference_type j) const { return i[j].t(); } - inline bool operator==(const const_iterator &o) const noexcept { return i == o.i; } - inline bool operator!=(const const_iterator &o) const noexcept { return i != o.i; } - inline bool operator<(const const_iterator& other) const noexcept { return i < other.i; } - inline bool operator<=(const const_iterator& other) const noexcept { return i <= other.i; } - inline bool operator>(const const_iterator& other) const noexcept { return i > other.i; } - inline bool operator>=(const const_iterator& other) const noexcept { return i >= other.i; } - inline const_iterator &operator++() { ++i; return *this; } - inline const_iterator operator++(int) { Node *n = i; ++i; return n; } - inline const_iterator &operator--() { i--; return *this; } - inline const_iterator operator--(int) { Node *n = i; i--; return n; } - inline const_iterator &operator+=(difference_type j) { i+=j; return *this; } - inline const_iterator &operator-=(difference_type j) { i-=j; return *this; } - inline const_iterator operator+(difference_type j) const { return const_iterator(i+j); } - inline const_iterator operator-(difference_type j) const { return const_iterator(i-j); } - friend inline const_iterator operator+(difference_type j, const_iterator k) { return k + j; } - inline int operator-(const_iterator j) const { return int(i - j.i); } - }; - friend class const_iterator; - - // stl style - typedef std::reverse_iterator<iterator> reverse_iterator; - typedef std::reverse_iterator<const_iterator> const_reverse_iterator; - inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator begin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator cbegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator constBegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); } - inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); } - inline const_iterator end() const noexcept { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator cend() const noexcept { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator constEnd() const noexcept { return reinterpret_cast<Node *>(p.end()); } - reverse_iterator rbegin() { return reverse_iterator(end()); } - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } - const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } - iterator insert(iterator before, const T &t); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); - - // more Qt - typedef iterator Iterator; - typedef const_iterator ConstIterator; - inline int count() const { return p.size(); } - inline int length() const { return p.size(); } // Same as count() - inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); } - inline const T& constFirst() const { return first(); } - inline const T& first() const { Q_ASSERT(!isEmpty()); return at(0); } - T& last() { Q_ASSERT(!isEmpty()); return *(--end()); } - const T& last() const { Q_ASSERT(!isEmpty()); return at(count() - 1); } - inline const T& constLast() const { return last(); } - inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); } - inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); } - inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; } - inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; } - QList<T> mid(int pos, int length = -1) const; - - T value(int i) const; - T value(int i, const T &defaultValue) const; - - // stl compatibility - inline void push_back(const T &t) { append(t); } - inline void push_front(const T &t) { prepend(t); } - inline T& front() { return first(); } - inline const T& front() const { return first(); } - inline T& back() { return last(); } - inline const T& back() const { return last(); } - inline void pop_front() { removeFirst(); } - inline void pop_back() { removeLast(); } - inline bool empty() const { return isEmpty(); } - typedef int size_type; - typedef T value_type; - typedef value_type *pointer; - typedef const value_type *const_pointer; - typedef value_type &reference; - typedef const value_type &const_reference; - // ### Qt6: use int - typedef qptrdiff difference_type; - - // comfort - QList<T> &operator+=(const QList<T> &l); - inline QList<T> operator+(const QList<T> &l) const - { QList n = *this; n += l; return n; } - inline QList<T> &operator+=(const T &t) - { append(t); return *this; } - inline QList<T> &operator<< (const T &t) - { append(t); return *this; } - inline QList<T> &operator<<(const QList<T> &l) - { *this += l; return *this; } - - static QList<T> fromVector(const QVector<T> &vector); - QVector<T> toVector() const; - -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - Q_DECL_DEPRECATED_X("Use QList<T>(set.begin(), set.end()) instead.") - static QList<T> fromSet(const QSet<T> &set); - Q_DECL_DEPRECATED_X("Use QSet<T>(list.begin(), list.end()) instead.") - QSet<T> toSet() const; - - Q_DECL_DEPRECATED_X("Use QList<T>(list.begin(), list.end()) instead.") - static inline QList<T> fromStdList(const std::list<T> &list) - { return QList<T>(list.begin(), list.end()); } - Q_DECL_DEPRECATED_X("Use std::list<T>(list.begin(), list.end()) instead.") - inline std::list<T> toStdList() const - { return std::list<T>(begin(), end()); } -#endif - -private: - Node *detach_helper_grow(int i, int n); - void detach_helper(int alloc); - void detach_helper(); - void dealloc(QListData::Data *d); - - void node_construct(Node *n, const T &t); - void node_destruct(Node *n); - void node_copy(Node *from, Node *to, Node *src); - void node_destruct(Node *from, Node *to); - - bool isValidIterator(const iterator &i) const noexcept - { - const std::less<const Node *> less = {}; - return !less(i.i, cbegin().i) && !less(cend().i, i.i); - } - -private: - inline bool op_eq_impl(const QList &other, QListData::NotArrayCompatibleLayout) const; - inline bool op_eq_impl(const QList &other, QListData::ArrayCompatibleLayout) const; - inline bool contains_impl(const T &, QListData::NotArrayCompatibleLayout) const; - inline bool contains_impl(const T &, QListData::ArrayCompatibleLayout) const; - inline int count_impl(const T &, QListData::NotArrayCompatibleLayout) const; - inline int count_impl(const T &, QListData::ArrayCompatibleLayout) const; -}; - -#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 -template <typename InputIterator, - typename ValueType = typename std::iterator_traits<InputIterator>::value_type, - QtPrivate::IfIsInputIterator<InputIterator> = true> -QList(InputIterator, InputIterator) -> QList<ValueType>; -#endif - -#if defined(Q_CC_BOR) -template <typename T> -Q_INLINE_TEMPLATE T &QList<T>::Node::t() -{ return QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic ? *(T*)v:*(T*)this; } -#endif - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t); - else if (QTypeInfo<T>::isComplex) new (n) T(t); -#if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__) - // This violates pointer aliasing rules, but it is known to be safe (and silent) - // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which - // set the same define are assumed to be safe. - else *reinterpret_cast<T*>(n) = t; -#else - // This is always safe, but penaltizes unoptimized builds a lot. - else ::memcpy(n, static_cast<const void *>(&t), sizeof(T)); -#endif -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v); - else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T(); -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src) -{ - Node *current = from; - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - QT_TRY { - while(current != to) { - current->v = new T(*reinterpret_cast<T*>(src->v)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - delete reinterpret_cast<T*>(current->v); - QT_RETHROW; - } - - } else if (QTypeInfo<T>::isComplex) { - QT_TRY { - while(current != to) { - new (current) T(*reinterpret_cast<T*>(src)); - ++current; - ++src; - } - } QT_CATCH(...) { - while (current-- != from) - (reinterpret_cast<T*>(current))->~T(); - QT_RETHROW; - } - } else { - if (src != from && to - from > 0) - memcpy(from, src, (to - from) * sizeof(Node)); - } -} - -template <typename T> -Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to) -{ - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) - while(from != to) --to, delete reinterpret_cast<T*>(to->v); - else if (QTypeInfo<T>::isComplex) - while (from != to) --to, reinterpret_cast<T*>(to)->~T(); -} - -template <typename T> -Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l) -{ - if (d != l.d) { - QList<T> tmp(l); - tmp.swap(*this); - } - return *this; -} -template <typename T> -inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) -{ - Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); - - int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin())); - Node *n = nullptr; - if (d->ref.isShared()) - n = detach_helper_grow(iBefore, 1); - else - n = reinterpret_cast<Node *>(p.insert(iBefore)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(iBefore); - QT_RETHROW; - } - return n; -} -template <typename T> -inline typename QList<T>::iterator QList<T>::erase(iterator it) -{ - Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid"); - if (d->ref.isShared()) { - int offset = int(it.i - reinterpret_cast<Node *>(p.begin())); - it = begin(); // implies detach() - it += offset; - } - node_destruct(it.i); - return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); -} -template <typename T> -inline const T &QList<T>::at(int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range"); - return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline const T &QList<T>::operator[](int i) const -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); - return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline T &QList<T>::operator[](int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); - detach(); return reinterpret_cast<Node *>(p.at(i))->t(); } -template <typename T> -inline void QList<T>::removeAt(int i) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::removeAt", "index out of range"); -#elif !defined(QT_NO_DEBUG) - if (i < 0 || i >= p.size()) - qWarning("QList::removeAt(): Index out of range."); -#endif - detach(); - node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); -} -template <typename T> -inline T QList<T>::takeAt(int i) -{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range"); - detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = std::move(n->t()); node_destruct(n); - p.remove(i); return t; } -template <typename T> -inline T QList<T>::takeFirst() -{ T t = std::move(first()); removeFirst(); return t; } -template <typename T> -inline T QList<T>::takeLast() -{ T t = std::move(last()); removeLast(); return t; } - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc) -{ - if (d->alloc < alloc) { - if (d->ref.isShared()) - detach_helper(alloc); - else - p.realloc(alloc); - } -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t) -{ - if (d->ref.isShared()) { - Node *n = detach_helper_grow(INT_MAX, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - --d->end; - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.append()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - --d->end; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.append());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::prepend(const T &t) -{ - if (d->ref.isShared()) { - Node *n = detach_helper_grow(0, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - ++d->begin; - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.prepend()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - ++d->begin; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.prepend());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::insert(int i, const T &t) -{ -#if !QT_DEPRECATED_SINCE(5, 15) - Q_ASSERT_X(i >= 0 && i <= p.size(), "QList<T>::insert", "index out of range"); -#elif !defined(QT_NO_DEBUG) - if (i < 0 || i > p.size()) - qWarning("QList::insert(): Index out of range."); -#endif - if (d->ref.isShared()) { - Node *n = detach_helper_grow(i, 1); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(i); - QT_RETHROW; - } - } else { - if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { - Node *n = reinterpret_cast<Node *>(p.insert(i)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(i); - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast<Node *>(p.insert(i));; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; - } - } -} - -template <typename T> -inline void QList<T>::replace(int i, const T &t) -{ - Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range"); - detach(); - reinterpret_cast<Node *>(p.at(i))->t() = t; -} - -template <typename T> -inline void QList<T>::swapItemsAt(int i, int j) -{ - Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(), - "QList<T>::swap", "index out of range"); - detach(); - qSwap(d->array[d->begin + i], d->array[d->begin + j]); -} - -template <typename T> -inline void QList<T>::move(int from, int to) -{ - Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(), - "QList<T>::move", "index out of range"); - detach(); - p.move(from, to); -} - -template<typename T> -Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int alength) const -{ - using namespace QtPrivate; - switch (QContainerImplHelper::mid(size(), &pos, &alength)) { - case QContainerImplHelper::Null: - case QContainerImplHelper::Empty: - return QList<T>(); - case QContainerImplHelper::Full: - return *this; - case QContainerImplHelper::Subset: - break; - } - - QList<T> cpy; - if (alength <= 0) - return cpy; - cpy.reserve(alength); - cpy.d->end = alength; - QT_TRY { - cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()), - reinterpret_cast<Node *>(cpy.p.end()), - reinterpret_cast<Node *>(p.begin() + pos)); - } QT_CATCH(...) { - // restore the old end - cpy.d->end = 0; - QT_RETHROW; - } - return cpy; -} - template<typename T> -Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const -{ - if (i < 0 || i >= p.size()) { - return T(); - } - return reinterpret_cast<Node *>(p.at(i))->t(); -} - +using QMutableListIterator = QMutableVectorIterator<T>; template<typename T> -Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const -{ - return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t()); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i, int c) -{ - Node *n = reinterpret_cast<Node *>(p.begin()); - QListData::Data *x = p.detach_grow(&i, c); - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.begin() + i), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin() + i + c), - reinterpret_cast<Node *>(p.end()), n + i); - } QT_CATCH(...) { - node_destruct(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.begin() + i)); - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); - - return reinterpret_cast<Node *>(p.begin() + i); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(int alloc) -{ - Node *n = reinterpret_cast<Node *>(p.begin()); - QListData::Data *x = p.detach(alloc); - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n); - } QT_CATCH(...) { - p.dispose(); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - dealloc(x); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper() -{ - detach_helper(d->alloc); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l) - : QListSpecialMethods<T>(l), d(l.d) -{ - if (!d->ref.ref()) { - p.detach(d->alloc); - - QT_TRY { - node_copy(reinterpret_cast<Node *>(p.begin()), - reinterpret_cast<Node *>(p.end()), - reinterpret_cast<Node *>(l.p.begin())); - } QT_CATCH(...) { - QListData::dispose(d); - QT_RETHROW; - } - } -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T>::~QList() -{ - if (!d->ref.deref()) - dealloc(d); -} - -template <typename T> -template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator>> -QList<T>::QList(InputIterator first, InputIterator last) - : QList() -{ - QtPrivate::reserveIfForwardIterator(this, first, last); - std::copy(first, last, std::back_inserter(*this)); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const -{ - if (d == l.d) - return true; - if (p.size() != l.p.size()) - return false; - return this->op_eq_impl(l, MemoryLayout()); -} - -template <typename T> -inline bool QList<T>::op_eq_impl(const QList &l, QListData::NotArrayCompatibleLayout) const -{ - Node *i = reinterpret_cast<Node *>(p.begin()); - Node *e = reinterpret_cast<Node *>(p.end()); - Node *li = reinterpret_cast<Node *>(l.p.begin()); - for (; i != e; ++i, ++li) { - if (!(i->t() == li->t())) - return false; - } - return true; -} - -template <typename T> -inline bool QList<T>::op_eq_impl(const QList &l, QListData::ArrayCompatibleLayout) const -{ - const T *lb = reinterpret_cast<const T*>(l.p.begin()); - const T *b = reinterpret_cast<const T*>(p.begin()); - const T *e = reinterpret_cast<const T*>(p.end()); - return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(lb, l.p.size())); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::dealloc(QListData::Data *data) -{ - node_destruct(reinterpret_cast<Node *>(data->array + data->begin), - reinterpret_cast<Node *>(data->array + data->end)); - QListData::dispose(data); -} - - -template <typename T> -Q_OUTOFLINE_TEMPLATE void QList<T>::clear() -{ - *this = QList<T>(); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t) -{ - int index = indexOf(_t); - if (index == -1) - return 0; - - const T t = _t; - detach(); - - Node *i = reinterpret_cast<Node *>(p.at(index)); - Node *e = reinterpret_cast<Node *>(p.end()); - Node *n = i; - node_destruct(i); - while (++i != e) { - if (i->t() == t) - node_destruct(i); - else - *n++ = *i; - } - - int removedCount = int(e - n); - d->end -= removedCount; - return removedCount; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::removeOne(const T &_t) -{ - int index = indexOf(_t); - if (index != -1) { - removeAt(index); - return true; - } - return false; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst, - typename QList<T>::iterator alast) -{ - Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid"); - Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid"); - - if (d->ref.isShared()) { - // ### A block is erased and a detach is needed. We should shrink and only copy relevant items. - int offsetfirst = int(afirst.i - reinterpret_cast<Node *>(p.begin())); - int offsetlast = int(alast.i - reinterpret_cast<Node *>(p.begin())); - afirst = begin(); // implies detach() - alast = afirst; - afirst += offsetfirst; - alast += offsetlast; - } - - for (Node *n = afirst.i; n < alast.i; ++n) - node_destruct(n); - int idx = afirst - begin(); - p.remove(idx, alast - afirst); - return begin() + idx; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l) -{ - if (!l.isEmpty()) { - if (d == &QListData::shared_null) { - *this = l; - } else { - Node *n = (d->ref.isShared()) - ? detach_helper_grow(INT_MAX, l.size()) - : reinterpret_cast<Node *>(p.append(l.p)); - QT_TRY { - node_copy(n, reinterpret_cast<Node *>(p.end()), - reinterpret_cast<Node *>(l.p.begin())); - } QT_CATCH(...) { - // restore the old end - d->end -= int(reinterpret_cast<Node *>(p.end()) - n); - QT_RETHROW; - } - } - } - return *this; -} - -template <typename T> -inline void QList<T>::append(const QList<T> &t) -{ - *this += t; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const -{ - return QtPrivate::indexOf<T, T>(*this, t, from); -} - -namespace QtPrivate -{ -template <typename T, typename U> -int indexOf(const QList<T> &list, const U &u, int from) -{ - typedef typename QList<T>::Node Node; - - if (from < 0) - from = qMax(from + list.p.size(), 0); - if (from < list.p.size()) { - Node *n = reinterpret_cast<Node *>(list.p.at(from -1)); - Node *e = reinterpret_cast<Node *>(list.p.end()); - while (++n != e) - if (n->t() == u) - return int(n - reinterpret_cast<Node *>(list.p.begin())); - } - return -1; -} -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const -{ - return QtPrivate::lastIndexOf<T, T>(*this, t, from); -} - -namespace QtPrivate -{ -template <typename T, typename U> -int lastIndexOf(const QList<T> &list, const U &u, int from) -{ - typedef typename QList<T>::Node Node; - - if (from < 0) - from += list.p.size(); - else if (from >= list.p.size()) - from = list.p.size()-1; - if (from >= 0) { - Node *b = reinterpret_cast<Node *>(list.p.begin()); - Node *n = reinterpret_cast<Node *>(list.p.at(from + 1)); - while (n-- != b) { - if (n->t() == u) - return int(n - b); - } - } - return -1; -} -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const -{ - return contains_impl(t, MemoryLayout()); -} - -template <typename T> -inline bool QList<T>::contains_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - Node *e = reinterpret_cast<Node *>(p.end()); - Node *i = reinterpret_cast<Node *>(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - return true; - return false; -} - -template <typename T> -inline bool QList<T>::contains_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - const T *b = reinterpret_cast<const T*>(p.begin()); - const T *e = reinterpret_cast<const T*>(p.end()); - return std::find(b, e, t) != e; -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const -{ - return this->count_impl(t, MemoryLayout()); -} - -template <typename T> -inline int QList<T>::count_impl(const T &t, QListData::NotArrayCompatibleLayout) const -{ - int c = 0; - Node *e = reinterpret_cast<Node *>(p.end()); - Node *i = reinterpret_cast<Node *>(p.begin()); - for (; i != e; ++i) - if (i->t() == t) - ++c; - return c; -} - -template <typename T> -inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) const -{ - return int(std::count(reinterpret_cast<const T*>(p.begin()), - reinterpret_cast<const T*>(p.end()), - t)); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const -{ - return QVector<T>(begin(), end()); -} - -template <typename T> -QList<T> QList<T>::fromVector(const QVector<T> &vector) -{ - return vector.toList(); -} - -template <typename T> -Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const -{ - return QList<T>(begin(), end()); -} - -template <typename T> -QVector<T> QVector<T>::fromList(const QList<T> &list) -{ - return list.toVector(); -} - -Q_DECLARE_SEQUENTIAL_ITERATOR(List) -Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) - -template <typename T> -uint qHash(const QList<T> &key, uint seed = 0) - noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) -{ - return qHashRange(key.cbegin(), key.cend(), seed); -} - -template <typename T> -bool operator<(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()))) -{ - return std::lexicographical_compare(lhs.begin(), lhs.end(), - rhs.begin(), rhs.end()); -} - -template <typename T> -inline bool operator>(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return rhs < lhs; -} - -template <typename T> -inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs > rhs); -} - -template <typename T> -inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs) - noexcept(noexcept(lhs < rhs)) -{ - return !(lhs < rhs); -} - +using QListIterator = QVectorIterator<T>; QT_END_NAMESPACE +#endif #include <QtCore/qbytearraylist.h> #include <QtCore/qstringlist.h> -#ifdef Q_CC_MSVC -#pragma warning( pop ) -#endif - #endif // QLIST_H diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 18c681581f..f169ed5e49 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -219,7 +219,7 @@ struct QMapData : public QMapDataBase Node *createNode(const Key &k, const T &v, Node *parent = nullptr, bool left = false) { - Node *n = static_cast<Node *>(QMapDataBase::createNode(sizeof(Node), Q_ALIGNOF(Node), + Node *n = static_cast<Node *>(QMapDataBase::createNode(sizeof(Node), alignof(Node), parent, left)); QT_TRY { new (&n->key) Key(k); @@ -243,7 +243,7 @@ struct QMapData : public QMapDataBase void destroy() { if (root()) { root()->destroySubTree(); - freeTree(header.left, Q_ALIGNOF(Node)); + freeTree(header.left, alignof(Node)); } freeData(this); } @@ -356,17 +356,6 @@ public: inline void detach() { if (d->ref.isShared()) 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(); - // Don't call on shared_null - d->ref.setSharable(sharable); - } -#endif inline bool isSharedWith(const QMap<Key, T> &other) const { return d == other.d; } void clear(); @@ -444,13 +433,8 @@ public: inline iterator &operator-=(int j) { return *this = *this - j; } friend inline iterator operator+(int j, iterator k) { return k + j; } -#ifndef QT_STRICT_ITERATORS - public: - inline bool operator==(const const_iterator &o) const - { return i == o.i; } - inline bool operator!=(const const_iterator &o) const - { return i != o.i; } -#endif + inline bool operator==(const const_iterator &o) const { return i == o.i; } + inline bool operator!=(const const_iterator &o) const { return i != o.i; } friend class QMap<Key, T>; }; friend class iterator; @@ -469,12 +453,7 @@ public: Q_DECL_CONSTEXPR inline const_iterator() : i(nullptr) { } inline const_iterator(const Node *node) : i(node) { } -#ifdef QT_STRICT_ITERATORS - explicit inline const_iterator(const iterator &o) -#else - inline const_iterator(const iterator &o) -#endif - { i = o.i; } + inline const_iterator(const iterator &o) { i = o.i; } inline const Key &key() const { return i->key; } inline const T &value() const { return i->value; } @@ -508,11 +487,6 @@ public: inline const_iterator &operator-=(int j) { return *this = *this - j; } friend inline const_iterator operator+(int j, const_iterator k) { return k + j; } -#ifdef QT_STRICT_ITERATORS - private: - inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); } - inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); } -#endif friend class QMap<Key, T>; }; friend class const_iterator; diff --git a/src/corelib/tools/qrefcount.h b/src/corelib/tools/qrefcount.h index 2e5388ad9a..982a9c2bbf 100644 --- a/src/corelib/tools/qrefcount.h +++ b/src/corelib/tools/qrefcount.h @@ -53,10 +53,6 @@ class RefCount public: inline bool ref() noexcept { int count = atomic.loadRelaxed(); -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - if (count == 0) // !isSharable - return false; -#endif if (count != -1) // !isStatic atomic.ref(); return true; @@ -64,32 +60,11 @@ public: inline bool deref() noexcept { int count = atomic.loadRelaxed(); -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - if (count == 0) // !isSharable - return false; -#endif if (count == -1) // isStatic return true; return atomic.deref(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - bool setSharable(bool sharable) noexcept - { - Q_ASSERT(!isShared()); - if (sharable) - return atomic.testAndSetRelaxed(0, 1); - else - return atomic.testAndSetRelaxed(1, 0); - } - - bool isSharable() const noexcept - { - // Sharable === Shared ownership. - return atomic.loadRelaxed() != 0; - } -#endif - bool isStatic() const noexcept { // Persistent object, never deleted diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index 879e2176f2..f98bb051ec 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -86,9 +86,6 @@ public: inline void detach() { q_hash.detach(); } inline bool isDetached() const { return q_hash.isDetached(); } -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - inline void setSharable(bool sharable) { q_hash.setSharable(sharable); } -#endif inline void clear() { q_hash.clear(); } @@ -260,12 +257,6 @@ public: { QSet<T> result = *this; result -= other; return result; } QList<T> values() const; -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) - Q_DECL_DEPRECATED_X("Use values() instead.") - QList<T> toList() const { return values(); } - Q_DECL_DEPRECATED_X("Use QSet<T>(list.begin(), list.end()) instead.") - static QSet<T> fromList(const QList<T> &list); -#endif private: Hash q_hash; @@ -397,30 +388,6 @@ Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const return result; } -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) -template <typename T> -Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const -{ - QSet<T> result; - result.reserve(size()); - for (int i = 0; i < size(); ++i) - result.insert(at(i)); - return result; -} - -template <typename T> -QSet<T> QSet<T>::fromList(const QList<T> &list) -{ - return list.toSet(); -} - -template <typename T> -QList<T> QList<T>::fromSet(const QSet<T> &set) -{ - return set.toList(); -} -#endif - Q_DECLARE_SEQUENTIAL_ITERATOR(Set) #if !defined(QT_NO_JAVA_STYLE_ITERATORS) diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 62fbdb4a2a..5def2eceb2 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -59,12 +59,31 @@ QT_BEGIN_NAMESPACE +namespace QtPrivate { + template <typename V, typename U> int indexOf(const QVector<V> &list, const U &u, int from); + template <typename V, typename U> int lastIndexOf(const QVector<V> &list, const U &u, int from); +} + +template <typename T> struct QVectorSpecialMethods +{ +protected: + ~QVectorSpecialMethods() = default; +}; +template <> struct QVectorSpecialMethods<QByteArray>; +template <> struct QVectorSpecialMethods<QString>; + template <typename T> class QVector +#ifndef Q_QDOC + : public QVectorSpecialMethods<T> +#endif { typedef QTypedArrayData<T> Data; Data *d; + template <typename V, typename U> friend int QtPrivate::indexOf(const QVector<V> &list, const U &u, int from); + template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QVector<V> &list, const U &u, int from); + public: inline QVector() noexcept : d(Data::sharedNull()) { } explicit QVector(int size); @@ -111,23 +130,6 @@ public: inline void detach(); 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 == Data::unsharableEmpty()) { - if (sharable) - d = Data::sharedNull(); - } else { - d->ref.setSharable(sharable); - } - Q_ASSERT(d->ref.isSharable() == sharable); - } -#endif inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; } @@ -206,7 +208,6 @@ public: typedef typename Data::const_iterator const_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; -#if !defined(QT_STRICT_ITERATORS) || defined(Q_CLANG_QDOC) inline iterator begin() { detach(); return d->begin(); } inline const_iterator begin() const noexcept { return d->constBegin(); } inline const_iterator cbegin() const noexcept { return d->constBegin(); } @@ -215,16 +216,6 @@ public: inline const_iterator end() const noexcept { return d->constEnd(); } inline const_iterator cend() const noexcept { return d->constEnd(); } inline const_iterator constEnd() const noexcept { return d->constEnd(); } -#else - inline iterator begin(iterator = iterator()) { detach(); return d->begin(); } - inline const_iterator begin(const_iterator = const_iterator()) const noexcept { return d->constBegin(); } - inline const_iterator cbegin(const_iterator = const_iterator()) const noexcept { return d->constBegin(); } - inline const_iterator constBegin(const_iterator = const_iterator()) const noexcept { return d->constBegin(); } - inline iterator end(iterator = iterator()) { detach(); return d->end(); } - inline const_iterator end(const_iterator = const_iterator()) const noexcept { return d->constEnd(); } - inline const_iterator cend(const_iterator = const_iterator()) const noexcept { return d->constEnd(); } - inline const_iterator constEnd(const_iterator = const_iterator()) const noexcept { return d->constEnd(); } -#endif 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()); } @@ -298,9 +289,6 @@ public: inline QVector<T> &operator<<(T &&t) { append(std::move(t)); return *this; } - static QVector<T> fromList(const QList<T> &list); - QList<T> toList() const; - #if QT_VERSION < QT_VERSION_CHECK(6,0,0) Q_DECL_DEPRECATED_X("Use QVector<T>(vector.begin(), vector.end()) instead.") static inline QVector<T> fromStdVector(const std::vector<T> &vector) @@ -309,6 +297,14 @@ public: inline std::vector<T> toStdVector() const { return std::vector<T>(d->begin(), d->end()); } #endif + + // Consider deprecating in 6.4 or later + static QVector<T> fromList(const QVector<T> &list) { return list; } + QVector<T> toList() const { return *this; } + + static inline QVector<T> fromVector(const QVector<T> &vector) { return vector; } + inline QVector<T> toVector() const { return *this; } + private: // ### Qt6: remove methods, they are unused void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default); @@ -402,14 +398,11 @@ inline QVector<T>::QVector(const QVector<T> &v) template <typename T> void QVector<T>::detach() { - if (!isDetached()) { -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - if (!d->alloc) - d = Data::unsharableEmpty(); - else -#endif - realloc(int(d->alloc)); - } + if (d->ref.isStatic()) + return; + + if (!isDetached()) + realloc(int(d->alloc)); Q_ASSERT(isDetached()); } @@ -418,11 +411,7 @@ void QVector<T>::reserve(int asize) { if (asize > int(d->alloc)) realloc(asize); - if (isDetached() -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - && d != Data::unsharableEmpty() -#endif - ) + if (isDetached()) d->capacityReserved = 1; Q_ASSERT(capacity() >= asize); } @@ -605,9 +594,6 @@ void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::Allo x = Data::allocate(aalloc, options); Q_CHECK_PTR(x); // aalloc is bigger then 0 so it is not [un]sharedEmpty -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - Q_ASSERT(x->ref.isSharable() || options.testFlag(QArrayData::Unsharable)); -#endif Q_ASSERT(!x->ref.isStatic()); x->size = asize; @@ -688,9 +674,6 @@ void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::Allo Q_ASSERT(d->data()); Q_ASSERT(uint(d->size) <= d->alloc); -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - Q_ASSERT(d != Data::unsharableEmpty()); -#endif Q_ASSERT(aalloc ? d != Data::sharedNull() : d == Data::sharedNull()); Q_ASSERT(d->alloc >= uint(aalloc)); Q_ASSERT(d->size == asize); @@ -709,9 +692,6 @@ void QVector<T>::realloc(int aalloc, QArrayData::AllocationOptions options) x = Data::allocate(aalloc, options); Q_CHECK_PTR(x); // aalloc is bigger then 0 so it is not [un]sharedEmpty -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - Q_ASSERT(x->ref.isSharable() || options.testFlag(QArrayData::Unsharable)); -#endif Q_ASSERT(!x->ref.isStatic()); x->size = d->size; @@ -759,9 +739,6 @@ void QVector<T>::realloc(int aalloc, QArrayData::AllocationOptions options) Q_ASSERT(d->data()); Q_ASSERT(uint(d->size) <= d->alloc); -#if !defined(QT_NO_UNSHARABLE_CONTAINERS) - Q_ASSERT(d != Data::unsharableEmpty()); -#endif Q_ASSERT(d != Data::sharedNull()); Q_ASSERT(d->alloc >= uint(aalloc)); } @@ -1004,38 +981,52 @@ QVector<T> &QVector<T>::operator+=(const QVector &l) return *this; } -template <typename T> -int QVector<T>::indexOf(const T &t, int from) const +namespace QtPrivate { +template <typename T, typename U> +int indexOf(const QVector<T> &vector, const U &u, int from) { if (from < 0) - from = qMax(from + d->size, 0); - if (from < d->size) { - T* n = d->begin() + from - 1; - T* e = d->end(); + from = qMax(from + vector.size(), 0); + if (from < vector.size()) { + auto n = vector.begin() + from - 1; + auto e = vector.end(); while (++n != e) - if (*n == t) - return n - d->begin(); + if (*n == u) + return n - vector.begin(); } return -1; } -template <typename T> -int QVector<T>::lastIndexOf(const T &t, int from) const +template <typename T, typename U> +int lastIndexOf(const QVector<T> &vector, const U &u, int from) { if (from < 0) - from += d->size; - else if (from >= d->size) - from = d->size-1; + from += vector.d->size; + else if (from >= vector.size()) + from = vector.size() - 1; if (from >= 0) { - T* b = d->begin(); - T* n = d->begin() + from + 1; + auto b = vector.begin(); + auto n = vector.begin() + from + 1; while (n != b) { - if (*--n == t) + if (*--n == u) return n - b; } } return -1; } +} + +template <typename T> +int QVector<T>::indexOf(const T &t, int from) const +{ + return QtPrivate::indexOf<T, T>(*this, t, from); +} + +template <typename T> +int QVector<T>::lastIndexOf(const T &t, int from) const +{ + return QtPrivate::lastIndexOf(*this, t, from); +} template <typename T> bool QVector<T>::contains(const T &t) const @@ -1153,4 +1144,7 @@ QVector<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt: QT_END_NAMESPACE +#include <QtCore/qbytearraylist.h> +#include <QtCore/qstringlist.h> + #endif // QVECTOR_H diff --git a/src/corelib/tools/qvector_msvc.cpp b/src/corelib/tools/qvector_msvc.cpp index cee343e72b..7e87467d42 100644 --- a/src/corelib/tools/qvector_msvc.cpp +++ b/src/corelib/tools/qvector_msvc.cpp @@ -47,12 +47,5 @@ # error "This file must be compiled with no precompiled headers" #endif -// invert the setting of QT_STRICT_ITERATORS, whichever it was -#ifdef QT_STRICT_ITERATORS -# undef QT_STRICT_ITERATORS -#else -# define QT_STRICT_ITERATORS -#endif - // the Q_TEMPLATE_EXTERN at the bottom of qvector.h will do the trick #include <QtCore/qvector.h> |