summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qlist.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r--src/corelib/tools/qlist.h988
1 files changed, 608 insertions, 380 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index 8a63e1283b..89e0e3f380 100644
--- a/src/corelib/tools/qlist.h
+++ b/src/corelib/tools/qlist.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Copyright (C) 2019 Intel Corporation
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2020 The Qt Company Ltd.
+// Copyright (C) 2019 Intel Corporation
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QLIST_H
#define QLIST_H
@@ -45,67 +9,262 @@
#include <QtCore/qnamespace.h>
#include <QtCore/qhashfunctions.h>
#include <QtCore/qiterator.h>
+#include <QtCore/qcontainertools_impl.h>
+#include <QtCore/qnamespace.h>
#include <functional>
#include <limits>
#include <initializer_list>
#include <type_traits>
+class tst_QList;
+
QT_BEGIN_NAMESPACE
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 V, typename U> qsizetype indexOf(const QList<V> &list, const U &u, qsizetype from) noexcept;
+ template <typename V, typename U> qsizetype lastIndexOf(const QList<V> &list, const U &u, qsizetype from) noexcept;
}
-template <typename T> struct QListSpecialMethods
+template <typename T> struct QListSpecialMethodsBase
+{
+protected:
+ ~QListSpecialMethodsBase() = default;
+
+ using Self = QList<T>;
+ Self *self() { return static_cast<Self *>(this); }
+ const Self *self() const { return static_cast<const Self *>(this); }
+
+public:
+ template <typename AT = T>
+ qsizetype indexOf(const AT &t, qsizetype from = 0) const noexcept;
+ template <typename AT = T>
+ qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const noexcept;
+
+ template <typename AT = T>
+ bool contains(const AT &t) const noexcept
+ {
+ return self()->indexOf(t) != -1;
+ }
+};
+template <typename T> struct QListSpecialMethods : QListSpecialMethodsBase<T>
{
protected:
~QListSpecialMethods() = default;
+public:
+ using QListSpecialMethodsBase<T>::indexOf;
+ using QListSpecialMethodsBase<T>::lastIndexOf;
+ using QListSpecialMethodsBase<T>::contains;
};
template <> struct QListSpecialMethods<QByteArray>;
template <> struct QListSpecialMethods<QString>;
+#if !defined(QT_STRICT_QLIST_ITERATORS) && (QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)) && !defined(Q_OS_WIN)
+#define QT_STRICT_QLIST_ITERATORS
+#endif
+
+#ifdef Q_QDOC // define QVector for QDoc
+template<typename T> class QVector : public QList<T> {};
+#endif
+
template <typename T>
class QList
#ifndef Q_QDOC
: public QListSpecialMethods<T>
#endif
{
- typedef QTypedArrayData<T> Data;
- typedef QArrayDataOps<T> DataOps;
- typedef QArrayDataPointer<T> DataPointer;
+ using Data = QTypedArrayData<T>;
+ using DataOps = QArrayDataOps<T>;
+ using DataPointer = QArrayDataPointer<T>;
class DisableRValueRefs {};
+ friend class ::tst_QList;
+
DataPointer d;
- 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);
+ template <typename V, typename U> friend qsizetype QtPrivate::indexOf(const QList<V> &list, const U &u, qsizetype from) noexcept;
+ template <typename V, typename U> friend qsizetype QtPrivate::lastIndexOf(const QList<V> &list, const U &u, qsizetype from) noexcept;
+ // This alias prevents the QtPrivate namespace from being exposed into the docs.
+ template <typename InputIterator>
+ using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
public:
- typedef T Type;
- typedef T value_type;
- typedef value_type *pointer;
- typedef const value_type *const_pointer;
- typedef value_type &reference;
- typedef const value_type &const_reference;
- typedef int size_type;
- typedef qptrdiff difference_type;
- typedef typename Data::iterator iterator;
- typedef typename Data::const_iterator const_iterator;
- typedef iterator Iterator;
- typedef const_iterator ConstIterator;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- typedef typename DataPointer::parameter_type parameter_type;
+ using Type = T;
+ using value_type = T;
+ using pointer = T *;
+ using const_pointer = const T *;
+ using reference = T &;
+ using const_reference = const T &;
+ using size_type = qsizetype;
+ using difference_type = qptrdiff;
+#ifndef Q_QDOC
+ using parameter_type = typename DataPointer::parameter_type;
using rvalue_ref = typename std::conditional<DataPointer::pass_parameter_by_value, DisableRValueRefs, T &&>::type;
+#else // simplified aliases for QDoc
+ using parameter_type = const T &;
+ using rvalue_ref = T &&;
+#endif
+
+ class const_iterator;
+ class iterator {
+ friend class QList<T>;
+ friend class const_iterator;
+ T *i = nullptr;
+#ifdef QT_STRICT_QLIST_ITERATORS
+ inline constexpr explicit iterator(T *n) : i(n) {}
+#endif
+
+ public:
+ using difference_type = qsizetype;
+ using value_type = T;
+#ifdef QT_COMPILER_HAS_LWG3346
+ using iterator_concept = std::contiguous_iterator_tag;
+ using element_type = value_type;
+#endif
+ using iterator_category = std::random_access_iterator_tag;
+ using pointer = T *;
+ using reference = T &;
+
+ inline constexpr iterator() = default;
+#ifndef QT_STRICT_QLIST_ITERATORS
+ inline constexpr explicit iterator(T *n) : i(n) {}
+#endif
+ inline T &operator*() const { return *i; }
+ inline T *operator->() const { return i; }
+ inline T &operator[](qsizetype j) const { return *(i + j); }
+ inline constexpr bool operator==(iterator o) const { return i == o.i; }
+ inline constexpr bool operator!=(iterator o) const { return i != o.i; }
+ inline constexpr bool operator<(iterator other) const { return i < other.i; }
+ inline constexpr bool operator<=(iterator other) const { return i <= other.i; }
+ inline constexpr bool operator>(iterator other) const { return i > other.i; }
+ inline constexpr bool operator>=(iterator other) const { return i >= other.i; }
+ inline constexpr bool operator==(const_iterator o) const { return i == o.i; }
+ inline constexpr bool operator!=(const_iterator o) const { return i != o.i; }
+ inline constexpr bool operator<(const_iterator other) const { return i < other.i; }
+ inline constexpr bool operator<=(const_iterator other) const { return i <= other.i; }
+ inline constexpr bool operator>(const_iterator other) const { return i > other.i; }
+ inline constexpr bool operator>=(const_iterator other) const { return i >= other.i; }
+ inline constexpr bool operator==(pointer p) const { return i == p; }
+ inline constexpr bool operator!=(pointer p) const { return i != p; }
+ inline iterator &operator++() { ++i; return *this; }
+ inline iterator operator++(int) { auto copy = *this; ++*this; return copy; }
+ inline iterator &operator--() { --i; return *this; }
+ inline iterator operator--(int) { auto copy = *this; --*this; return copy; }
+ inline qsizetype operator-(iterator j) const { return i - j.i; }
+#if QT_DEPRECATED_SINCE(6, 3) && !defined(QT_STRICT_QLIST_ITERATORS)
+ QT_DEPRECATED_VERSION_X_6_3("Use operator* or operator-> rather than relying on "
+ "the implicit conversion between a QList/QVector::iterator "
+ "and a raw pointer")
+ inline operator T*() const { return i; }
+
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, iterator>
+ &operator+=(Int j) { i+=j; return *this; }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, iterator>
+ &operator-=(Int j) { i-=j; return *this; }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, iterator>
+ operator+(Int j) const { return iterator(i+j); }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, iterator>
+ operator-(Int j) const { return iterator(i-j); }
+ template <typename Int> friend std::enable_if_t<std::is_integral_v<Int>, iterator>
+ operator+(Int j, iterator k) { return k + j; }
+#else
+ inline iterator &operator+=(qsizetype j) { i += j; return *this; }
+ inline iterator &operator-=(qsizetype j) { i -= j; return *this; }
+ inline iterator operator+(qsizetype j) const { return iterator(i + j); }
+ inline iterator operator-(qsizetype j) const { return iterator(i - j); }
+ friend inline iterator operator+(qsizetype j, iterator k) { return k + j; }
+#endif
+ };
+
+ class const_iterator {
+ friend class QList<T>;
+ friend class iterator;
+ const T *i = nullptr;
+#ifdef QT_STRICT_QLIST_ITERATORS
+ inline constexpr explicit const_iterator(const T *n) : i(n) {}
+#endif
+
+ public:
+ using difference_type = qsizetype;
+ using value_type = T;
+#ifdef QT_COMPILER_HAS_LWG3346
+ using iterator_concept = std::contiguous_iterator_tag;
+ using element_type = const value_type;
+#endif
+ using iterator_category = std::random_access_iterator_tag;
+ using pointer = const T *;
+ using reference = const T &;
+
+ inline constexpr const_iterator() = default;
+#ifndef QT_STRICT_QLIST_ITERATORS
+ inline constexpr explicit const_iterator(const T *n) : i(n) {}
+#endif
+ inline constexpr const_iterator(iterator o): i(o.i) {}
+ inline const T &operator*() const { return *i; }
+ inline const T *operator->() const { return i; }
+ inline const T &operator[](qsizetype j) const { return *(i + j); }
+ inline constexpr bool operator==(const_iterator o) const { return i == o.i; }
+ inline constexpr bool operator!=(const_iterator o) const { return i != o.i; }
+ inline constexpr bool operator<(const_iterator other) const { return i < other.i; }
+ inline constexpr bool operator<=(const_iterator other) const { return i <= other.i; }
+ inline constexpr bool operator>(const_iterator other) const { return i > other.i; }
+ inline constexpr bool operator>=(const_iterator other) const { return i >= other.i; }
+ inline constexpr bool operator==(iterator o) const { return i == o.i; }
+ inline constexpr bool operator!=(iterator o) const { return i != o.i; }
+ inline constexpr bool operator<(iterator other) const { return i < other.i; }
+ inline constexpr bool operator<=(iterator other) const { return i <= other.i; }
+ inline constexpr bool operator>(iterator other) const { return i > other.i; }
+ inline constexpr bool operator>=(iterator other) const { return i >= other.i; }
+ inline constexpr bool operator==(pointer p) const { return i == p; }
+ inline constexpr bool operator!=(pointer p) const { return i != p; }
+ inline const_iterator &operator++() { ++i; return *this; }
+ inline const_iterator operator++(int) { auto copy = *this; ++*this; return copy; }
+ inline const_iterator &operator--() { --i; return *this; }
+ inline const_iterator operator--(int) { auto copy = *this; --*this; return copy; }
+ inline qsizetype operator-(const_iterator j) const { return i - j.i; }
+#if QT_DEPRECATED_SINCE(6, 3) && !defined(QT_STRICT_QLIST_ITERATORS)
+ QT_DEPRECATED_VERSION_X_6_3("Use operator* or operator-> rather than relying on "
+ "the implicit conversion between a QList/QVector::const_iterator "
+ "and a raw pointer")
+ inline operator const T*() const { return i; }
+
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, const_iterator>
+ &operator+=(Int j) { i+=j; return *this; }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, const_iterator>
+ &operator-=(Int j) { i-=j; return *this; }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, const_iterator>
+ operator+(Int j) const { return const_iterator(i+j); }
+ template <typename Int> std::enable_if_t<std::is_integral_v<Int>, const_iterator>
+ operator-(Int j) const { return const_iterator(i-j); }
+ template <typename Int> friend std::enable_if_t<std::is_integral_v<Int>, const_iterator>
+ operator+(Int j, const_iterator k) { return k + j; }
+#else
+ inline const_iterator &operator+=(qsizetype j) { i += j; return *this; }
+ inline const_iterator &operator-=(qsizetype j) { i -= j; return *this; }
+ inline const_iterator operator+(qsizetype j) const { return const_iterator(i + j); }
+ inline const_iterator operator-(qsizetype j) const { return const_iterator(i - j); }
+ friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; }
+#endif
+ };
+ using Iterator = iterator;
+ using ConstIterator = const_iterator;
+ using reverse_iterator = std::reverse_iterator<iterator>;
+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private:
- void resize_internal(int i, Qt::Initialization);
+ void resize_internal(qsizetype i);
bool isValidIterator(const_iterator i) const
{
const std::less<const T*> less = {};
- return !less(d->end(), i) && !less(i, d->begin());
+ return !less(d->end(), i.i) && !less(i.i, d->begin());
+ }
+
+ void verify([[maybe_unused]] qsizetype pos = 0, [[maybe_unused]] qsizetype n = 1) const
+ {
+ Q_ASSERT(pos >= 0);
+ Q_ASSERT(pos <= size());
+ Q_ASSERT(n >= 0);
+ Q_ASSERT(n <= size() - pos);
}
public:
QList(DataPointer dd) noexcept
@@ -115,96 +274,153 @@ public:
public:
QList() = default;
- explicit QList(int size)
- : d(Data::allocate(size))
+ explicit QList(qsizetype size)
+ : d(size)
{
if (size)
d->appendInitialize(size);
}
- QList(int size, const T &t)
- : d(Data::allocate(size))
+ QList(qsizetype size, parameter_type t)
+ : d(size)
{
if (size)
d->copyAppend(size, t);
}
- inline QList(const QList<T> &other) noexcept : d(other.d) {}
- QList(QList<T> &&other) noexcept : d(std::move(other.d)) {}
inline QList(std::initializer_list<T> args)
- : d(Data::allocate(args.size()))
+ : d(qsizetype(args.size()))
{
if (args.size())
d->copyAppend(args.begin(), args.end());
}
- ~QList() /*noexcept(std::is_nothrow_destructible<T>::value)*/ {}
- QList<T> &operator=(const QList<T> &other) { d = other.d; return *this; }
- QList &operator=(QList &&other) noexcept(std::is_nothrow_destructible<T>::value)
- {
- d = std::move(other.d);
- return *this;
- }
QList<T> &operator=(std::initializer_list<T> args)
{
- d = DataPointer(Data::allocate(args.size()));
- if (args.size())
- d->copyAppend(args.begin(), args.end());
- return *this;
+ return assign(args);
}
- template <typename InputIterator, QtPrivate::IfIsForwardIterator<InputIterator> = true>
+
+ template <typename InputIterator, if_input_iterator<InputIterator> = true>
QList(InputIterator i1, InputIterator i2)
- : d(Data::allocate(std::distance(i1, i2)))
{
- if (std::distance(i1, i2))
- d->copyAppend(i1, i2);
+ if constexpr (!std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::forward_iterator_tag>) {
+ std::copy(i1, i2, std::back_inserter(*this));
+ } else {
+ const auto distance = std::distance(i1, i2);
+ if (distance) {
+ d = DataPointer(qsizetype(distance));
+ // appendIteratorRange can deal with contiguous iterators on its own,
+ // this is an optimization for C++17 code.
+ if constexpr (std::is_same_v<std::decay_t<InputIterator>, iterator> ||
+ std::is_same_v<std::decay_t<InputIterator>, const_iterator>) {
+ d->copyAppend(i1.i, i2.i);
+ } else {
+ d->appendIteratorRange(i1, i2);
+ }
+ }
+ }
}
- template <typename InputIterator, QtPrivate::IfIsNotForwardIterator<InputIterator> = true>
- QList(InputIterator i1, InputIterator i2)
- : QList()
+ // This constructor is here for compatibility with QStringList in Qt 5, that has a QStringList(const QString &) constructor
+ template<typename String, typename = std::enable_if_t<std::is_same_v<T, QString> && std::is_convertible_v<String, QString>>>
+ inline explicit QList(const String &str)
+ { append(str); }
+
+ QList(qsizetype size, Qt::Initialization)
+ : d(size)
{
- QtPrivate::reserveIfForwardIterator(this, i1, i2);
- std::copy(i1, i2, std::back_inserter(*this));
+ if (size)
+ d->appendUninitialized(size);
}
- void swap(QList<T> &other) noexcept { qSwap(d, other.d); }
+ // compiler-generated special member functions are fine!
+
+ void swap(QList &other) noexcept { d.swap(other.d); }
- friend bool operator==(const QList &l, const QList &r)
+#ifndef Q_QDOC
+ template <typename U = T>
+ QTypeTraits::compare_eq_result_container<QList, U> operator==(const QList &other) const
{
- if (l.size() != r.size())
+ if (size() != other.size())
return false;
- if (l.begin() == r.begin())
+ if (begin() == other.begin())
return true;
// do element-by-element comparison
- return l.d->compare(l.begin(), r.begin(), l.size());
+ return d->compare(data(), other.data(), size());
}
- friend bool operator!=(const QList &l, const QList &r)
+ template <typename U = T>
+ QTypeTraits::compare_eq_result_container<QList, U> operator!=(const QList &other) const
{
- return !(l == r);
+ return !(*this == other);
}
- int size() const noexcept { return int(d->size); }
- int count() const noexcept { return size(); }
- int length() const noexcept { return size(); }
+ template <typename U = T>
+ QTypeTraits::compare_lt_result_container<QList, U> operator<(const QList &other) const
+ noexcept(noexcept(std::lexicographical_compare<typename QList<U>::const_iterator,
+ typename QList::const_iterator>(
+ std::declval<QList<U>>().begin(), std::declval<QList<U>>().end(),
+ other.begin(), other.end())))
+ {
+ return std::lexicographical_compare(begin(), end(),
+ other.begin(), other.end());
+ }
+
+ template <typename U = T>
+ QTypeTraits::compare_lt_result_container<QList, U> operator>(const QList &other) const
+ noexcept(noexcept(other < std::declval<QList<U>>()))
+ {
+ return other < *this;
+ }
+
+ template <typename U = T>
+ QTypeTraits::compare_lt_result_container<QList, U> operator<=(const QList &other) const
+ noexcept(noexcept(other < std::declval<QList<U>>()))
+ {
+ return !(other < *this);
+ }
+
+ template <typename U = T>
+ QTypeTraits::compare_lt_result_container<QList, U> operator>=(const QList &other) const
+ noexcept(noexcept(std::declval<QList<U>>() < other))
+ {
+ return !(*this < other);
+ }
+#else
+ bool operator==(const QList &other) const;
+ bool operator!=(const QList &other) const;
+ bool operator<(const QList &other) const;
+ bool operator>(const QList &other) const;
+ bool operator<=(const QList &other) const;
+ bool operator>=(const QList &other) const;
+#endif // Q_QDOC
+
+ qsizetype size() const noexcept { return d->size; }
+ qsizetype count() const noexcept { return size(); }
+ qsizetype length() const noexcept { return size(); }
inline bool isEmpty() const noexcept { return d->size == 0; }
- void resize(int size)
+ void resize(qsizetype size)
{
- resize_internal(size, Qt::Uninitialized);
+ resize_internal(size);
if (size > this->size())
d->appendInitialize(size);
}
- void resize(int size, parameter_type c)
+ void resize(qsizetype size, parameter_type c)
{
- resize_internal(size, Qt::Uninitialized);
+ resize_internal(size);
if (size > this->size())
d->copyAppend(size - this->size(), c);
}
+ void resizeForOverwrite(qsizetype size)
+ {
+ resize_internal(size);
+ if (size > this->size())
+ d->appendUninitialized(size);
+ }
- inline int capacity() const { return int(d->constAllocatedCapacity()); }
- void reserve(int size);
+ inline qsizetype capacity() const { return qsizetype(d->constAllocatedCapacity()); }
+ void reserve(qsizetype size);
inline void squeeze();
void detach() { d.detach(); }
@@ -220,45 +436,64 @@ public:
return;
if (d->needsDetach()) {
// must allocate memory
- DataPointer detached(Data::allocate(d.allocatedCapacity(), d->detachFlags()));
+ DataPointer detached(d.allocatedCapacity());
d.swap(detached);
} else {
d->truncate(0);
}
}
- const_reference at(int i) const noexcept
+ const_reference at(qsizetype i) const noexcept
{
Q_ASSERT_X(size_t(i) < size_t(d->size), "QList::at", "index out of range");
return data()[i];
}
- reference operator[](int i)
+ reference operator[](qsizetype i)
{
Q_ASSERT_X(size_t(i) < size_t(d->size), "QList::operator[]", "index out of range");
- detach();
+ // don't detach() here, we detach in data below:
return data()[i];
}
- const_reference operator[](int i) const noexcept { return at(i); }
- void append(const_reference t)
- { append(const_iterator(std::addressof(t)), const_iterator(std::addressof(t)) + 1); }
+ const_reference operator[](qsizetype i) const noexcept { return at(i); }
+ void append(parameter_type t) { emplaceBack(t); }
void append(const_iterator i1, const_iterator i2);
- void append(rvalue_ref t) { emplaceBack(std::move(t)); }
- void append(const QList<T> &l) { append(l.constBegin(), l.constEnd()); }
- void prepend(rvalue_ref t);
- void prepend(const T &t);
+ void append(rvalue_ref t)
+ {
+ if constexpr (DataPointer::pass_parameter_by_value) {
+ Q_UNUSED(t);
+ } else {
+ emplaceBack(std::move(t));
+ }
+ }
+ void append(const QList<T> &l)
+ {
+ append(l.constBegin(), l.constEnd());
+ }
+ void append(QList<T> &&l);
+ void prepend(rvalue_ref t) {
+ if constexpr (DataPointer::pass_parameter_by_value) {
+ Q_UNUSED(t);
+ } else {
+ emplaceFront(std::move(t));
+ }
+ }
+ void prepend(parameter_type t) { emplaceFront(t); }
+
+ template<typename... Args>
+ inline reference emplaceBack(Args &&... args);
template <typename ...Args>
- reference emplaceBack(Args&&... args) { return *emplace(count(), std::forward<Args>(args)...); }
+ inline reference emplaceFront(Args&&... args);
- iterator insert(int i, parameter_type t)
- { return insert(i, 1, t); }
- iterator insert(int i, int n, parameter_type t);
+ iterator insert(qsizetype i, parameter_type t)
+ { return emplace(i, t); }
+ iterator insert(qsizetype i, qsizetype n, parameter_type t);
iterator insert(const_iterator before, parameter_type t)
{
Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid");
return insert(before, 1, t);
}
- iterator insert(const_iterator before, int n, parameter_type t)
+ iterator insert(const_iterator before, qsizetype n, parameter_type t)
{
Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid");
return insert(std::distance(constBegin(), before), n, t);
@@ -268,7 +503,28 @@ public:
Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid");
return insert(std::distance(constBegin(), before), std::move(t));
}
- iterator insert(int i, rvalue_ref t) { return emplace(i, std::move(t)); }
+ iterator insert(qsizetype i, rvalue_ref t) {
+ if constexpr (DataPointer::pass_parameter_by_value) {
+ Q_UNUSED(i);
+ Q_UNUSED(t);
+ return end();
+ } else {
+ return emplace(i, std::move(t));
+ }
+ }
+
+ QList &assign(qsizetype n, parameter_type t)
+ {
+ Q_ASSERT(n >= 0);
+ return fill(t, n);
+ }
+
+ template <typename InputIterator, if_input_iterator<InputIterator> = true>
+ QList &assign(InputIterator first, InputIterator last)
+ { d.assign(first, last); return *this; }
+
+ QList &assign(std::initializer_list<T> l)
+ { return assign(l.begin(), l.end()); }
template <typename ...Args>
iterator emplace(const_iterator before, Args&&... args)
@@ -278,72 +534,83 @@ public:
}
template <typename ...Args>
- iterator emplace(int i, Args&&... args);
+ iterator emplace(qsizetype i, Args&&... args);
#if 0
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
iterator insert( const_iterator pos, std::initializer_list<T> ilist );
#endif
- void replace(int i, const T &t)
+ void replace(qsizetype i, parameter_type t)
{
Q_ASSERT_X(i >= 0 && i < d->size, "QList<T>::replace", "index out of range");
- const T copy(t);
- data()[i] = copy;
+ DataPointer oldData;
+ d.detach(&oldData);
+ d.data()[i] = t;
}
- void replace(int i, rvalue_ref t)
+ void replace(qsizetype i, rvalue_ref t)
{
- Q_ASSERT_X(i >= 0 && i < d->size, "QList<T>::replace", "index out of range");
- const T copy(std::move(t));
- data()[i] = std::move(copy);
+ if constexpr (DataPointer::pass_parameter_by_value) {
+ Q_UNUSED(i);
+ Q_UNUSED(t);
+ } else {
+ Q_ASSERT_X(i >= 0 && i < d->size, "QList<T>::replace", "index out of range");
+ DataPointer oldData;
+ d.detach(&oldData);
+ d.data()[i] = std::move(t);
+ }
}
- void remove(int i, int n = 1);
- void removeFirst() { Q_ASSERT(!isEmpty()); remove(0); }
- void removeLast() { Q_ASSERT(!isEmpty()); remove(size() - 1); }
- value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); remove(0); return v; }
- value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); remove(size() - 1); return v; }
+ void remove(qsizetype i, qsizetype n = 1);
+ void removeFirst() noexcept;
+ void removeLast() noexcept;
+ value_type takeFirst() { Q_ASSERT(!isEmpty()); value_type v = std::move(first()); d->eraseFirst(); return v; }
+ value_type takeLast() { Q_ASSERT(!isEmpty()); value_type v = std::move(last()); d->eraseLast(); return v; }
- QList<T> &fill(parameter_type t, int size = -1);
+ QList<T> &fill(parameter_type t, qsizetype size = -1);
+
+#ifndef Q_QDOC
+ using QListSpecialMethods<T>::contains;
+ using QListSpecialMethods<T>::indexOf;
+ using QListSpecialMethods<T>::lastIndexOf;
+#else
+ template <typename AT>
+ qsizetype indexOf(const AT &t, qsizetype from = 0) const noexcept;
+ template <typename AT>
+ qsizetype lastIndexOf(const AT &t, qsizetype from = -1) const noexcept;
+ template <typename AT>
+ bool contains(const AT &t) const noexcept;
+#endif
- int indexOf(const T &t, int from = 0) const noexcept;
- int lastIndexOf(const T &t, int from = -1) const noexcept;
- bool contains(const T &t) const noexcept
+ template <typename AT = T>
+ qsizetype count(const AT &t) const noexcept
{
- return indexOf(t) != -1;
+ return qsizetype(std::count(data(), data() + size(), t));
}
- int count(const T &t) const noexcept
+
+ void removeAt(qsizetype i) { remove(i); }
+ template <typename AT = T>
+ qsizetype removeAll(const AT &t)
{
- return int(std::count(&*cbegin(), &*cend(), t));
+ return QtPrivate::sequential_erase_with_copy(*this, t);
}
- // QList compatibility
- void removeAt(int i) { remove(i); }
- int removeAll(const T &t)
+ template <typename AT = T>
+ bool removeOne(const AT &t)
{
- const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t);
- if (cit == ce)
- return 0;
- int index = cit - this->cbegin();
- // next operation detaches, so ce, cit, t may become invalidated:
- const T tCopy = t;
- const iterator e = end(), it = std::remove(begin() + index, e, tCopy);
- const int result = std::distance(it, e);
- erase(it, e);
- return result;
+ return QtPrivate::sequential_erase_one(*this, t);
}
- bool removeOne(const T &t)
+
+ template <typename Predicate>
+ qsizetype removeIf(Predicate pred)
{
- const int i = indexOf(t);
- if (i < 0)
- return false;
- remove(i);
- return true;
+ return QtPrivate::sequential_erase_if(*this, pred);
}
- T takeAt(int i) { T t = std::move((*this)[i]); remove(i); return t; }
- void move(int from, int to)
+
+ T takeAt(qsizetype i) { T t = std::move((*this)[i]); remove(i); return t; }
+ void move(qsizetype from, qsizetype to)
{
- Q_ASSERT_X(from >= 0 && from < size(), "QList::move(int,int)", "'from' is out-of-range");
- Q_ASSERT_X(to >= 0 && to < size(), "QList::move(int,int)", "'to' is out-of-range");
+ Q_ASSERT_X(from >= 0 && from < size(), "QList::move(qsizetype, qsizetype)", "'from' is out-of-range");
+ Q_ASSERT_X(to >= 0 && to < size(), "QList::move(qsizetype, qsizetype)", "'to' is out-of-range");
if (from == to) // don't detach when no-op
return;
detach();
@@ -355,15 +622,15 @@ public:
}
// STL-style
- iterator begin() { detach(); return d->begin(); }
- iterator end() { detach(); return d->end(); }
-
- const_iterator begin() const noexcept { return d->constBegin(); }
- const_iterator end() const noexcept { return d->constEnd(); }
- const_iterator cbegin() const noexcept { return d->constBegin(); }
- const_iterator cend() const noexcept { return d->constEnd(); }
- const_iterator constBegin() const noexcept { return d->constBegin(); }
- const_iterator constEnd() const noexcept { return d->constEnd(); }
+ iterator begin() { detach(); return iterator(d->begin()); }
+ iterator end() { detach(); return iterator(d->end()); }
+
+ const_iterator begin() const noexcept { return const_iterator(d->constBegin()); }
+ const_iterator end() const noexcept { return const_iterator(d->constEnd()); }
+ const_iterator cbegin() const noexcept { return const_iterator(d->constBegin()); }
+ const_iterator cend() const noexcept { return const_iterator(d->constEnd()); }
+ const_iterator constBegin() const noexcept { return const_iterator(d->constBegin()); }
+ const_iterator constEnd() const noexcept { return const_iterator(d->constEnd()); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
@@ -376,19 +643,28 @@ public:
// more Qt
inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
- inline const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); }
- inline const T &constFirst() const { Q_ASSERT(!isEmpty()); return *begin(); }
+ inline const T &first() const noexcept { Q_ASSERT(!isEmpty()); return *begin(); }
+ inline const T &constFirst() const noexcept { Q_ASSERT(!isEmpty()); return *begin(); }
inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
- inline const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
- inline const T &constLast() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
- inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
- inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
- QList<T> mid(int pos, int len = -1) const;
-
- T value(int i) const { return value(i, T()); }
- T value(int i, const T &defaultValue) const;
-
- void swapItemsAt(int i, int j) {
+ inline const T &last() const noexcept { Q_ASSERT(!isEmpty()); return *(end()-1); }
+ inline const T &constLast() const noexcept { Q_ASSERT(!isEmpty()); return *(end()-1); }
+ inline bool startsWith(parameter_type t) const { return !isEmpty() && first() == t; }
+ inline bool endsWith(parameter_type t) const { return !isEmpty() && last() == t; }
+ QList<T> mid(qsizetype pos, qsizetype len = -1) const;
+
+ QList<T> first(qsizetype n) const
+ { verify(0, n); return QList<T>(begin(), begin() + n); }
+ QList<T> last(qsizetype n) const
+ { verify(0, n); return QList<T>(end() - n, end()); }
+ QList<T> sliced(qsizetype pos) const
+ { verify(pos, 0); return QList<T>(begin() + pos, end()); }
+ QList<T> sliced(qsizetype pos, qsizetype n) const
+ { verify(pos, n); return QList<T>(begin() + pos, begin() + pos + n); }
+
+ T value(qsizetype i) const { return value(i, T()); }
+ T value(qsizetype i, parameter_type defaultValue) const;
+
+ void swapItemsAt(qsizetype i, qsizetype j) {
Q_ASSERT_X(i >= 0 && i < size() && j >= 0 && j < size(),
"QList<T>::swap", "index out of range");
detach();
@@ -396,108 +672,126 @@ public:
}
// STL compatibility
- inline void push_back(const T &t) { append(t); }
+ inline void push_back(parameter_type t) { append(t); }
void push_back(rvalue_ref t) { append(std::move(t)); }
void push_front(rvalue_ref t) { prepend(std::move(t)); }
- inline void push_front(const T &t) { prepend(t); }
- void pop_back() { removeLast(); }
- void pop_front() { removeFirst(); }
+ inline void push_front(parameter_type t) { prepend(t); }
+ void pop_back() noexcept { removeLast(); }
+ void pop_front() noexcept { removeFirst(); }
template <typename ...Args>
reference emplace_back(Args&&... args) { return emplaceBack(std::forward<Args>(args)...); }
- inline bool empty() const
+ inline bool empty() const noexcept
{ return d->size == 0; }
inline reference front() { return first(); }
- inline const_reference front() const { return first(); }
+ inline const_reference front() const noexcept { return first(); }
inline reference back() { return last(); }
- inline const_reference back() const { return last(); }
+ inline const_reference back() const noexcept { return last(); }
void shrink_to_fit() { squeeze(); }
+ static qsizetype max_size() noexcept
+ {
+ return Data::max_size();
+ }
// comfort
- QList<T> &operator+=(const QList<T> &l) { append(l.cbegin(), l.cend()); return *this; }
- inline QList<T> operator+(const QList<T> &l) const
+ QList<T> &operator+=(const QList<T> &l) { append(l); return *this; }
+ QList<T> &operator+=(QList<T> &&l) { append(std::move(l)); return *this; }
+ inline QList<T> operator+(const QList<T> &l) const &
{ QList n = *this; n += l; return n; }
- inline QList<T> &operator+=(const T &t)
+ QList<T> operator+(const QList<T> &l) &&
+ { return std::move(*this += l); }
+ inline QList<T> operator+(QList<T> &&l) const &
+ { QList n = *this; n += std::move(l); return n; }
+ QList<T> operator+(QList<T> &&l) &&
+ { return std::move(*this += std::move(l)); }
+ inline QList<T> &operator+=(parameter_type t)
{ append(t); return *this; }
- inline QList<T> &operator<< (const T &t)
+ inline QList<T> &operator<< (parameter_type t)
{ append(t); return *this; }
inline QList<T> &operator<<(const QList<T> &l)
{ *this += l; return *this; }
+ inline QList<T> &operator<<(QList<T> &&l)
+ { *this += std::move(l); return *this; }
inline QList<T> &operator+=(rvalue_ref t)
{ append(std::move(t)); return *this; }
inline QList<T> &operator<<(rvalue_ref t)
{ append(std::move(t)); return *this; }
// Consider deprecating in 6.4 or later
- static QList<T> fromList(const QList<T> &list) { return list; }
- QList<T> toList() const { return *this; }
+ static QList<T> fromList(const QList<T> &list) noexcept { return list; }
+ QList<T> toList() const noexcept { return *this; }
- static inline QList<T> fromVector(const QList<T> &vector) { return vector; }
- inline QList<T> toVector() const { return *this; }
+ static inline QList<T> fromVector(const QList<T> &vector) noexcept { return vector; }
+ inline QList<T> toVector() const noexcept { return *this; }
+
+ template<qsizetype N>
+ static QList<T> fromReadOnlyData(const T (&t)[N]) noexcept
+ {
+ return QList<T>({ nullptr, const_cast<T *>(t), N });
+ }
};
-#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
template <typename T>
-inline void QList<T>::resize_internal(int newSize, Qt::Initialization)
+inline void QList<T>::resize_internal(qsizetype newSize)
{
Q_ASSERT(newSize >= 0);
- if (d->needsDetach() || newSize > capacity()) {
- // must allocate memory
- DataPointer detached(Data::allocate(d->detachCapacity(newSize),
- d->detachFlags()));
- if (size() && newSize) {
- detached->copyAppend(constBegin(), constBegin() + qMin(newSize, size()));
- }
- d.swap(detached);
- }
-
- if (newSize < size())
+ if (d->needsDetach() || newSize > capacity() - d.freeSpaceAtBegin()) {
+ d.detachAndGrow(QArrayData::GrowsAtEnd, newSize - d.size, nullptr, nullptr);
+ } else if (newSize < size()) {
d->truncate(newSize);
+ }
}
template <typename T>
-void QList<T>::reserve(int asize)
+void QList<T>::reserve(qsizetype asize)
{
// capacity() == 0 for immutable data, so this will force a detaching below
- if (asize <= capacity()) {
+ if (asize <= capacity() - d.freeSpaceAtBegin()) {
if (d->flags() & Data::CapacityReserved)
return; // already reserved, don't shrink
if (!d->isShared()) {
// accept current allocation, don't shrink
- d->flags() |= Data::CapacityReserved;
+ d->setFlag(Data::CapacityReserved);
return;
}
}
- DataPointer detached(Data::allocate(qMax(asize, size()),
- d->detachFlags() | Data::CapacityReserved));
- detached->copyAppend(constBegin(), constEnd());
+ DataPointer detached(qMax(asize, size()));
+ detached->copyAppend(d->begin(), d->end());
+ if (detached.d_ptr())
+ detached->setFlag(Data::CapacityReserved);
d.swap(detached);
}
template <typename T>
inline void QList<T>::squeeze()
{
- if (d->needsDetach() || size() != capacity()) {
+ if (!d.isMutable())
+ return;
+ if (d->needsDetach() || size() < capacity()) {
// must allocate memory
- DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved));
+ DataPointer detached(size());
if (size()) {
- detached->copyAppend(constBegin(), constEnd());
+ if (d.needsDetach())
+ detached->copyAppend(d.data(), d.data() + d.size);
+ else
+ detached->moveAppend(d.data(), d.data() + d.size);
}
d.swap(detached);
}
+ // We're detached so this is fine
+ d->clearFlag(Data::CapacityReserved);
}
template <typename T>
-inline void QList<T>::remove(int i, int n)
+inline void QList<T>::remove(qsizetype i, qsizetype n)
{
Q_ASSERT_X(size_t(i) + size_t(n) <= size_t(d->size), "QList::remove", "index out of range");
Q_ASSERT_X(n >= 0, "QList::remove", "invalid count");
@@ -505,34 +799,29 @@ inline void QList<T>::remove(int i, int n)
if (n == 0)
return;
- const size_t newSize = size() - n;
- if (d->needsDetach() ||
- ((d->flags() & Data::CapacityReserved) == 0
- && newSize < d->allocatedCapacity()/2)) {
- // allocate memory
- DataPointer detached(Data::allocate(d->detachCapacity(newSize),
- d->detachFlags() & ~(Data::GrowsBackwards | Data::GrowsForward)));
- const_iterator where = constBegin() + i;
- if (newSize) {
- detached->copyAppend(constBegin(), where);
- detached->copyAppend(where + n, constEnd());
- }
- d.swap(detached);
- } else {
- // we're detached and we can just move data around
- d->erase(d->begin() + i, d->begin() + i + n);
- }
+ d.detach();
+ d->erase(d->begin() + i, n);
}
template <typename T>
-inline void QList<T>::prepend(const T &t)
-{ insert(0, 1, t); }
+inline void QList<T>::removeFirst() noexcept
+{
+ Q_ASSERT(!isEmpty());
+ d.detach();
+ d->eraseFirst();
+}
+
template <typename T>
-void QList<T>::prepend(rvalue_ref t)
-{ insert(0, std::move(t)); }
+inline void QList<T>::removeLast() noexcept
+{
+ Q_ASSERT(!isEmpty());
+ d.detach();
+ d->eraseLast();
+}
+
template<typename T>
-inline T QList<T>::value(int i, const T &defaultValue) const
+inline T QList<T>::value(qsizetype i, parameter_type defaultValue) const
{
return size_t(i) < size_t(d->size) ? at(i) : defaultValue;
}
@@ -540,89 +829,60 @@ inline T QList<T>::value(int i, const T &defaultValue) const
template <typename T>
inline void QList<T>::append(const_iterator i1, const_iterator i2)
{
- if (i1 == i2)
- return;
- const size_t newSize = size() + std::distance(i1, i2);
- if (d->needsDetach() || newSize > d->allocatedCapacity()) {
- DataPointer detached(Data::allocate(d->detachCapacity(newSize),
- d->detachFlags() | Data::GrowsForward));
- detached->copyAppend(constBegin(), constEnd());
- detached->copyAppend(i1, i2);
- d.swap(detached);
- } else {
- // we're detached and we can just move data around
- d->copyAppend(i1, i2);
- }
+ d->growAppend(i1.i, i2.i);
}
template <typename T>
-inline typename QList<T>::iterator
-QList<T>::insert(int i, int n, parameter_type t)
+inline void QList<T>::append(QList<T> &&other)
{
- Q_ASSERT_X(size_t(i) <= size_t(d->size), "QList<T>::insert", "index out of range");
+ Q_ASSERT(&other != this);
+ if (other.isEmpty())
+ return;
+ if (other.d->needsDetach() || !std::is_nothrow_move_constructible_v<T>)
+ return append(other);
- // we don't have a quick exit for n == 0
- // it's not worth wasting CPU cycles for that
+ // due to precondition &other != this, we can unconditionally modify 'this'
+ d.detachAndGrow(QArrayData::GrowsAtEnd, other.size(), nullptr, nullptr);
+ Q_ASSERT(d.freeSpaceAtEnd() >= other.size());
+ d->moveAppend(other.d->begin(), other.d->end());
+}
- const size_t newSize = size() + n;
- if (d->needsDetach() || newSize > d->allocatedCapacity()) {
- typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
- if (size_t(i) <= newSize / 4)
- flags |= Data::GrowsBackwards;
+template<typename T>
+template<typename... Args>
+inline typename QList<T>::reference QList<T>::emplaceFront(Args &&... args)
+{
+ d->emplace(0, std::forward<Args>(args)...);
+ return *d.begin();
+}
- DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags));
- const_iterator where = constBegin() + i;
- detached->copyAppend(constBegin(), where);
- detached->copyAppend(n, t);
- detached->copyAppend(where, constEnd());
- d.swap(detached);
- } else {
- // we're detached and we can just move data around
- if (i == size()) {
- d->copyAppend(n, t);
- } else {
- T copy(t);
- d->insert(d.begin() + i, n, copy);
- }
- }
- return d.begin() + i;
+
+template <typename T>
+inline typename QList<T>::iterator
+QList<T>::insert(qsizetype i, qsizetype n, parameter_type t)
+{
+ Q_ASSERT_X(size_t(i) <= size_t(d->size), "QList<T>::insert", "index out of range");
+ Q_ASSERT_X(n >= 0, "QList::insert", "invalid count");
+ if (Q_LIKELY(n))
+ d->insert(i, n, t);
+ return begin() + i;
}
template <typename T>
template <typename ...Args>
typename QList<T>::iterator
-QList<T>::emplace(int i, Args&&... args)
+QList<T>::emplace(qsizetype i, Args&&... args)
{
- Q_ASSERT_X(i >= 0 && i <= d->size, "QList<T>::insert", "index out of range");
-
- const size_t newSize = size() + 1;
- if (d->needsDetach() || newSize > d->allocatedCapacity()) {
- typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
- if (size_t(i) <= newSize / 4)
- flags |= Data::GrowsBackwards;
-
- DataPointer detached(Data::allocate(d->detachCapacity(newSize), flags));
- const_iterator where = constBegin() + i;
-
- // First, create an element to handle cases, when a user moves
- // the element from a container to the same container
- detached->createInPlace(detached.begin() + i, std::forward<Args>(args)...);
-
- // Then, put the first part of the elements to the new location
- detached->copyAppend(constBegin(), where);
-
- // After that, increase the actual size, because we created
- // one extra element
- ++detached.size;
-
- // Finally, put the rest of the elements to the new location
- detached->copyAppend(where, constEnd());
+ Q_ASSERT_X(i >= 0 && i <= d->size, "QList<T>::insert", "index out of range");
+ d->emplace(i, std::forward<Args>(args)...);
+ return begin() + i;
+}
- d.swap(detached);
- } else {
- d->emplace(d.begin() + i, std::forward<Args>(args)...);
- }
- return d.begin() + i;
+template<typename T>
+template<typename... Args>
+inline typename QList<T>::reference QList<T>::emplaceBack(Args &&... args)
+{
+ d->emplace(d->size, std::forward<Args>(args)...);
+ return *(end() - 1);
}
template <typename T>
@@ -632,52 +892,54 @@ typename QList<T>::iterator QList<T>::erase(const_iterator abegin, const_iterato
Q_ASSERT_X(isValidIterator(aend), "QList::erase", "The specified iterator argument 'aend' is invalid");
Q_ASSERT(aend >= abegin);
- int i = std::distance(d.constBegin(), abegin);
- int n = std::distance(abegin, aend);
+ qsizetype i = std::distance(constBegin(), abegin);
+ qsizetype n = std::distance(abegin, aend);
remove(i, n);
- return d.begin() + i;
+ return begin() + i;
}
template <typename T>
-inline QList<T> &QList<T>::fill(parameter_type t, int newSize)
+inline QList<T> &QList<T>::fill(parameter_type t, qsizetype newSize)
{
if (newSize == -1)
newSize = size();
if (d->needsDetach() || newSize > capacity()) {
// must allocate memory
- DataPointer detached(Data::allocate(d->detachCapacity(newSize),
- d->detachFlags()));
+ DataPointer detached(d->detachCapacity(newSize));
detached->copyAppend(newSize, t);
d.swap(detached);
} else {
// we're detached
const T copy(t);
d->assign(d.begin(), d.begin() + qMin(size(), newSize), t);
- if (newSize > size())
+ if (newSize > size()) {
d->copyAppend(newSize - size(), copy);
+ } else if (newSize < size()) {
+ d->truncate(newSize);
+ }
}
return *this;
}
namespace QtPrivate {
template <typename T, typename U>
-int indexOf(const QList<T> &vector, const U &u, int from)
+qsizetype indexOf(const QList<T> &vector, const U &u, qsizetype from) noexcept
{
if (from < 0)
- from = qMax(from + vector.size(), 0);
+ from = qMax(from + vector.size(), qsizetype(0));
if (from < vector.size()) {
auto n = vector.begin() + from - 1;
auto e = vector.end();
while (++n != e)
if (*n == u)
- return int(n - vector.begin());
+ return qsizetype(n - vector.begin());
}
return -1;
}
template <typename T, typename U>
-int lastIndexOf(const QList<T> &vector, const U &u, int from)
+qsizetype lastIndexOf(const QList<T> &vector, const U &u, qsizetype from) noexcept
{
if (from < 0)
from += vector.d->size;
@@ -688,7 +950,7 @@ int lastIndexOf(const QList<T> &vector, const U &u, int from)
auto n = vector.begin() + from + 1;
while (n != b) {
if (*--n == u)
- return int(n - b);
+ return qsizetype(n - b);
}
}
return -1;
@@ -696,19 +958,21 @@ int lastIndexOf(const QList<T> &vector, const U &u, int from)
}
template <typename T>
-int QList<T>::indexOf(const T &t, int from) const noexcept
+template <typename AT>
+qsizetype QListSpecialMethodsBase<T>::indexOf(const AT &t, qsizetype from) const noexcept
{
- return QtPrivate::indexOf<T, T>(*this, t, from);
+ return QtPrivate::indexOf(*self(), t, from);
}
template <typename T>
-int QList<T>::lastIndexOf(const T &t, int from) const noexcept
+template <typename AT>
+qsizetype QListSpecialMethodsBase<T>::lastIndexOf(const AT &t, qsizetype from) const noexcept
{
- return QtPrivate::lastIndexOf(*this, t, from);
+ return QtPrivate::lastIndexOf(*self(), t, from);
}
template <typename T>
-inline QList<T> QList<T>::mid(int pos, int len) const
+inline QList<T> QList<T>::mid(qsizetype pos, qsizetype len) const
{
qsizetype p = pos;
qsizetype l = len;
@@ -724,8 +988,8 @@ inline QList<T> QList<T>::mid(int pos, int len) const
}
// Allocate memory
- DataPointer copied(Data::allocate(l));
- copied->copyAppend(constBegin() + p, constBegin() + p + l);
+ DataPointer copied(l);
+ copied->copyAppend(data() + p, data() + p + l);
return copied;
}
@@ -739,55 +1003,19 @@ size_t qHash(const QList<T> &key, size_t seed = 0)
return qHashRange(key.cbegin(), key.cend(), seed);
}
-template <typename T>
-auto operator<(const QList<T> &lhs, const QList<T> &rhs)
- noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
- rhs.begin(), rhs.end())))
- -> decltype(std::declval<T>() < std::declval<T>())
-{
- return std::lexicographical_compare(lhs.begin(), lhs.end(),
- rhs.begin(), rhs.end());
-}
-
-template <typename T>
-auto operator>(const QList<T> &lhs, const QList<T> &rhs)
- noexcept(noexcept(lhs < rhs))
- -> decltype(lhs < rhs)
-{
- return rhs < lhs;
-}
-
-template <typename T>
-auto operator<=(const QList<T> &lhs, const QList<T> &rhs)
- noexcept(noexcept(lhs < rhs))
- -> decltype(lhs < rhs)
+template <typename T, typename AT>
+qsizetype erase(QList<T> &list, const AT &t)
{
- return !(lhs > rhs);
+ return QtPrivate::sequential_erase(list, t);
}
-template <typename T>
-auto operator>=(const QList<T> &lhs, const QList<T> &rhs)
- noexcept(noexcept(lhs < rhs))
- -> decltype(lhs < rhs)
+template <typename T, typename Predicate>
+qsizetype erase_if(QList<T> &list, Predicate pred)
{
- return !(lhs < rhs);
+ return QtPrivate::sequential_erase_if(list, pred);
}
-/*
- ### Qt 5:
- ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
- ### Qt exports QPolygon and QPolygonF that inherit QList<QPoint> and
- ### QList<QPointF> respectively.
-*/
-
-#if defined(Q_CC_MSVC) && !defined(QT_BUILD_CORE_LIB)
-QT_BEGIN_INCLUDE_NAMESPACE
-#include <QtCore/qpoint.h>
-QT_END_INCLUDE_NAMESPACE
-extern template class Q_CORE_EXPORT QList<QPointF>;
-extern template class Q_CORE_EXPORT QList<QPoint>;
-#endif
-
+// ### Qt 7 char32_t
QList<uint> QStringView::toUcs4() const { return QtPrivate::convertToUcs4(*this); }
QT_END_NAMESPACE