From 5fdc64f5666d094e34e1373e75ae6e50239d9622 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 8 Apr 2015 19:44:48 +0200 Subject: [PATCH] Add qHash(QList) QLists can be compared for equality, so qHash should be overloaded, too. [ChangeLog][QtCore][QList] Added qHash(QList). Change-Id: I9ad91811f12479764cc17d87192539612ceb0b4c Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/tools/qlist.cpp | 11 +++++++++++ src/corelib/tools/qlist.h | 8 ++++++++ tests/auto/corelib/tools/qlist/tst_qlist.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 126147716c..f4901d336e 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -632,6 +632,17 @@ void **QListData::erase(void **xi) of \c operator<(). */ +/*! + \fn uint qHash(const QList &key, uint seed = 0) + \since 5.6 + \relates QList + + Returns the hash value for \a key, + using \a seed to seed the calculation. + + This function requires qHash() to be overloaded for the value type \c T. +*/ + /*! \fn int QList::size() const diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 34ebedce3b..85e4570f45 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -1021,6 +1022,13 @@ inline int QList::count_impl(const T &t, QListData::ArrayCompatibleLayout) co Q_DECLARE_SEQUENTIAL_ITERATOR(List) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List) +template +uint qHash(const QList &key, uint seed = 0) + Q_DECL_NOEXCEPT_EXPR(noexcept(qHashRange(key.cbegin(), key.cend(), seed))) +{ + return qHashRange(key.cbegin(), key.cend(), seed); +} + template bool operator<(const QList &lhs, const QList &rhs) Q_DECL_NOEXCEPT_EXPR(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 9b12083b18..dd9371fbe1 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -399,6 +399,9 @@ private slots: void eraseValidIteratorsOnSharedList() const; void insertWithValidIteratorsOnSharedList() const; + void qhashOptimal() const { qhash(); } + void qhashMovable() const { qhash(); } + void qhashComplex() const { qhash(); } void reserve() const; private: template void length() const; @@ -415,6 +418,7 @@ private: template void endsWith() const; template void lastIndexOf() const; template void move() const; + template void qhash() const; template void removeAll() const; template void removeAt() const; template void removeOne() const; @@ -1908,6 +1912,16 @@ void tst_QList::insertWithValidIteratorsOnSharedList() const QCOMPARE(a.at(1), 15); } +template +void tst_QList::qhash() const +{ + QList l1, l2; + QCOMPARE(qHash(l1), qHash(l2)); + l1 << T_BAR; + l2 << T_BAR; + QCOMPARE(qHash(l1), qHash(l2)); +} + void tst_QList::reserve() const { // Note: