diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 52eafddf88..4f46764697 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -338,6 +338,17 @@ of \c operator<(). */ +/*! + \fn uint qHash(const QVector &key, uint seed = 0) + \since 5.6 + \relates QVector + + 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 QVector::size() const Returns the number of items in the vector. diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 58fbea50cc..12baecd37c 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -873,6 +874,13 @@ QList QList::fromVector(const QVector &vector) Q_DECLARE_SEQUENTIAL_ITERATOR(Vector) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector) +template +uint qHash(const QVector &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 QVector &lhs, const QVector &rhs) Q_DECL_NOEXCEPT_EXPR(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(), diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 67296054c9..3f95fa18f6 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -86,6 +86,8 @@ private: } }; +inline uint qHash(const Movable &key, uint seed = 0) { return qHash(key.i, seed); } + QAtomicInt Movable::counter = 0; QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(Movable, Q_MOVABLE_TYPE); @@ -155,6 +157,8 @@ private: }; QAtomicInt Custom::counter = 0; +inline uint qHash(const Custom &key, uint seed = 0) { return qHash(key.i, seed); } + Q_DECLARE_METATYPE(Custom); // tests depends on the fact that: @@ -237,6 +241,9 @@ private slots: void prependInt() const; void prependMovable() const; void prependCustom() const; + void qhashInt() const { qhash(); } + void qhashMovable() const { qhash(); } + void qhashCustom() const { qhash(); } void removeInt() const; void removeMovable() const; void removeCustom() const; @@ -302,6 +309,7 @@ private: template void fill() const; template void fromList() const; template void insert() const; + template void qhash() const; template void prepend() const; template void remove() const; template void size() const; @@ -1445,6 +1453,16 @@ void tst_QVector::mid() const QCOMPARE(list.mid(4), QVector() << "buck" << "hello" << "kitty"); } +template +void tst_QVector::qhash() const +{ + QVector l1, l2; + QCOMPARE(qHash(l1), qHash(l2)); + l1 << SimpleValue::at(0); + l2 << SimpleValue::at(0); + QCOMPARE(qHash(l1), qHash(l2)); +} + template void tst_QVector::prepend() const {