diff options
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 141 |
1 files changed, 99 insertions, 42 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index f5ff952f97..32e0141d55 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -38,6 +38,7 @@ #include <QtCore/qiterator.h> #include <QtCore/qrefcount.h> #include <QtCore/qarraydata.h> +#include <QtCore/qhashfunctions.h> #include <iterator> #include <list> @@ -89,6 +90,7 @@ struct Q_CORE_EXPORT QListData { 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; @@ -102,11 +104,11 @@ struct Q_CORE_EXPORT QListData { void remove(int i); void remove(int i, int n); void move(int from, int to); - inline int size() const { return d->end - d->begin; } - inline bool isEmpty() const { return d->end == d->begin; } - inline void **at(int i) const { return d->array + d->begin + i; } - inline void **begin() const { return d->array + d->begin; } - inline void **end() const { return d->array + d->end; } + inline int size() const Q_DECL_NOTHROW { return d->end - d->begin; } + inline bool isEmpty() const Q_DECL_NOTHROW { return d->end == d->begin; } + inline void **at(int i) const Q_DECL_NOTHROW { return d->array + d->begin + i; } + inline void **begin() const Q_DECL_NOTHROW { return d->array + d->begin; } + inline void **end() const Q_DECL_NOTHROW { return d->array + d->end; } }; template <typename T> @@ -143,7 +145,7 @@ public: #ifdef Q_COMPILER_RVALUE_REFS inline QList(QList<T> &&other) : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); } inline QList &operator=(QList<T> &&other) - { qSwap(d, other.d); return *this; } + { QList moved(std::move(other)); swap(moved); return *this; } #endif inline void swap(QList<T> &other) { qSwap(d, other.d); } #ifdef Q_COMPILER_INITIALIZER_LISTS @@ -154,7 +156,7 @@ public: bool operator==(const QList<T> &l) const; inline bool operator!=(const QList<T> &l) const { return !(*this == l); } - inline int size() const { return p.size(); } + inline int size() const Q_DECL_NOTHROW { return p.size(); } inline void detach() { if (d->ref.isShared()) detach_helper(); } @@ -177,9 +179,9 @@ public: d->ref.setSharable(sharable); } #endif - inline bool isSharedWith(const QList<T> &other) const { return d == other.d; } + inline bool isSharedWith(const QList<T> &other) const Q_DECL_NOTHROW { return d == other.d; } - inline bool isEmpty() const { return p.isEmpty(); } + inline bool isEmpty() const Q_DECL_NOTHROW { return p.isEmpty(); } void clear(); @@ -218,30 +220,34 @@ public: typedef T *pointer; typedef T &reference; - inline iterator() : i(0) {} - inline iterator(Node *n) : i(n) {} - inline iterator(const iterator &o): i(o.i){} + inline iterator() Q_DECL_NOTHROW : i(Q_NULLPTR) {} + inline iterator(Node *n) Q_DECL_NOTHROW : 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) Q_DECL_NOTHROW : i(o.i){} +#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 { return i == o.i; } - inline bool operator!=(const iterator &o) const { return i != o.i; } - inline bool operator<(const iterator& other) const { return i < other.i; } - inline bool operator<=(const iterator& other) const { return i <= other.i; } - inline bool operator>(const iterator& other) const { return i > other.i; } - inline bool operator>=(const iterator& other) const { return i >= other.i; } + inline bool operator==(const iterator &o) const Q_DECL_NOTHROW { return i == o.i; } + inline bool operator!=(const iterator &o) const Q_DECL_NOTHROW { return i != o.i; } + inline bool operator<(const iterator& other) const Q_DECL_NOTHROW { return i < other.i; } + inline bool operator<=(const iterator& other) const Q_DECL_NOTHROW { return i <= other.i; } + inline bool operator>(const iterator& other) const Q_DECL_NOTHROW { return i > other.i; } + inline bool operator>=(const iterator& other) const Q_DECL_NOTHROW { return i >= other.i; } #ifndef QT_STRICT_ITERATORS - inline bool operator==(const const_iterator &o) const + inline bool operator==(const const_iterator &o) const Q_DECL_NOTHROW { return i == o.i; } - inline bool operator!=(const const_iterator &o) const + inline bool operator!=(const const_iterator &o) const Q_DECL_NOTHROW { return i != o.i; } - inline bool operator<(const const_iterator& other) const + inline bool operator<(const const_iterator& other) const Q_DECL_NOTHROW { return i < other.i; } - inline bool operator<=(const const_iterator& other) const + inline bool operator<=(const const_iterator& other) const Q_DECL_NOTHROW { return i <= other.i; } - inline bool operator>(const const_iterator& other) const + inline bool operator>(const const_iterator& other) const Q_DECL_NOTHROW { return i > other.i; } - inline bool operator>=(const const_iterator& other) const + inline bool operator>=(const const_iterator& other) const Q_DECL_NOTHROW { return i >= other.i; } #endif inline iterator &operator++() { ++i; return *this; } @@ -266,23 +272,27 @@ public: typedef const T *pointer; typedef const T &reference; - inline const_iterator() : i(0) {} - inline const_iterator(Node *n) : i(n) {} - inline const_iterator(const const_iterator &o): i(o.i) {} + inline const_iterator() Q_DECL_NOTHROW : i(Q_NULLPTR) {} + inline const_iterator(Node *n) Q_DECL_NOTHROW : 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) Q_DECL_NOTHROW : i(o.i) {} +#endif #ifdef QT_STRICT_ITERATORS - inline explicit const_iterator(const iterator &o): i(o.i) {} + inline explicit const_iterator(const iterator &o) Q_DECL_NOTHROW : i(o.i) {} #else - inline const_iterator(const iterator &o): i(o.i) {} + inline const_iterator(const iterator &o) Q_DECL_NOTHROW : 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 { return i == o.i; } - inline bool operator!=(const const_iterator &o) const { return i != o.i; } - inline bool operator<(const const_iterator& other) const { return i < other.i; } - inline bool operator<=(const const_iterator& other) const { return i <= other.i; } - inline bool operator>(const const_iterator& other) const { return i > other.i; } - inline bool operator>=(const const_iterator& other) const { return i >= other.i; } + inline bool operator==(const const_iterator &o) const Q_DECL_NOTHROW { return i == o.i; } + inline bool operator!=(const const_iterator &o) const Q_DECL_NOTHROW { return i != o.i; } + inline bool operator<(const const_iterator& other) const Q_DECL_NOTHROW { return i < other.i; } + inline bool operator<=(const const_iterator& other) const Q_DECL_NOTHROW { return i <= other.i; } + inline bool operator>(const const_iterator& other) const Q_DECL_NOTHROW { return i > other.i; } + inline bool operator>=(const const_iterator& other) const Q_DECL_NOTHROW { 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; } @@ -296,14 +306,22 @@ public: 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 { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator cbegin() const { return reinterpret_cast<Node *>(p.begin()); } - inline const_iterator constBegin() const { return reinterpret_cast<Node *>(p.begin()); } + inline const_iterator begin() const Q_DECL_NOTHROW { return reinterpret_cast<Node *>(p.begin()); } + inline const_iterator cbegin() const Q_DECL_NOTHROW { return reinterpret_cast<Node *>(p.begin()); } + inline const_iterator constBegin() const Q_DECL_NOTHROW { return reinterpret_cast<Node *>(p.begin()); } inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); } - inline const_iterator end() const { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator cend() const { return reinterpret_cast<Node *>(p.end()); } - inline const_iterator constEnd() const { return reinterpret_cast<Node *>(p.end()); } + inline const_iterator end() const Q_DECL_NOTHROW { return reinterpret_cast<Node *>(p.end()); } + inline const_iterator cend() const Q_DECL_NOTHROW { return reinterpret_cast<Node *>(p.end()); } + inline const_iterator constEnd() const Q_DECL_NOTHROW { 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 Q_DECL_NOTHROW { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } + const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); } + const_reverse_iterator crend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } iterator insert(iterator before, const T &t); iterator erase(iterator pos); iterator erase(iterator first, iterator last); @@ -314,9 +332,11 @@ public: 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; } @@ -378,7 +398,7 @@ private: void node_copy(Node *from, Node *to, Node *src); void node_destruct(Node *from, Node *to); - bool isValidIterator(const iterator &i) const + bool isValidIterator(const iterator &i) const Q_DECL_NOTHROW { return (constBegin().i <= i.i) && (i.i <= constEnd().i); } @@ -1020,6 +1040,43 @@ inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) co Q_DECLARE_SEQUENTIAL_ITERATOR(List) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) +template <typename T> +uint qHash(const QList<T> &key, uint seed = 0) + Q_DECL_NOEXCEPT_EXPR(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) + Q_DECL_NOEXCEPT_EXPR(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) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return rhs < lhs; +} + +template <typename T> +inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs > rhs); +} + +template <typename T> +inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs) + Q_DECL_NOEXCEPT_EXPR(noexcept(lhs < rhs)) +{ + return !(lhs < rhs); +} + QT_END_NAMESPACE #include <QtCore/qbytearraylist.h> |