Add qHash(std::pair)

We already include <utility> in <qglobal.h>, so we might
as well provide a qHash() overload for std::pair.

[ChangeLog][QtCore] Added qHash(std::pair), defined in
<QHashFunctions>.

Change-Id: I0f61c513e82e05ce9d2e56bcf18f3be9e2da4da9
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
bb10
Marc Mutz 2015-12-28 15:58:03 +01:00 committed by Jędrzej Nowacki
parent ad74c95399
commit d681107f1f
3 changed files with 42 additions and 0 deletions

View File

@ -711,6 +711,23 @@ void QHashData::checkSanity()
Types \c T1 and \c T2 must be supported by qHash().
*/
/*!
\fn uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
\since 5.7
\relates QHash
Returns the hash value for the \a key, using \a seed to seed the calculation.
Types \c T1 and \c T2 must be supported by qHash().
\note The return type of this function is \e{not} the same as that of
\code
qHash(qMakePair(key.first, key.second), seed);
\endcode
The two functions use different hashing algorithms; due to binary compatibility
constraints, we cannot change the QPair algorithm to match the std::pair one before Qt 6.
*/
/*! \fn uint qHashRange(InputIterator first, InputIterator last, uint seed = 0)
\relates QHash
\since 5.5

View File

@ -147,6 +147,15 @@ template <typename T1, typename T2> inline uint qHash(const QPair<T1, T2> &key,
return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
}
template <typename T1, typename T2> inline uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
{
QtPrivate::QHashCombine hash;
seed = hash(seed, key.first);
seed = hash(seed, key.second);
return seed;
}
QT_END_NAMESPACE
#if defined(Q_CC_MSVC)

View File

@ -115,6 +115,22 @@ void tst_QHashFunctions::qhash()
QVERIFY(qHash(pA) != qHash(pB));
}
{
std::pair<int, int> p12(1, 2);
std::pair<int, int> p21(2, 1);
using QT_PREPEND_NAMESPACE(qHash);
QVERIFY(qHash(p12) == qHash(p12));
QVERIFY(qHash(p21) == qHash(p21));
QVERIFY(qHash(p12) != qHash(p21));
std::pair<int, int> pA(0x12345678, 0x12345678);
std::pair<int, int> pB(0x12345675, 0x12345675);
QVERIFY(qHash(pA) != qHash(pB));
}
}
void tst_QHashFunctions::fp_qhash_of_zero_is_zero()