From 55d68a16aafb93aa15bcdbd78892006777b6067a Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 18 Jul 2011 12:12:57 +0200 Subject: Add a qHash for QUuid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib0d9a765b8db3b34aa20d510de501c72a54cacbe Reviewed-on: http://codereview.qt.nokia.com/1743 Reviewed-by: Qt Sanity Bot Reviewed-by: Xizhi Zhu Reviewed-by: João Abecasis --- src/corelib/plugin/quuid.cpp | 11 +++++++++++ src/corelib/plugin/quuid.h | 2 ++ tests/auto/quuid/tst_quuid.cpp | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index ad182eb9bf..6fca1b6709 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -927,4 +927,15 @@ QUuid QUuid::createUuid() guid; otherwise returns false. */ +/** + Returns a hash of the QUuid + */ +uint qHash(const QUuid &uuid) +{ + return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) + ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) + ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]); +} + + QT_END_NAMESPACE diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index 28feff13d2..b941079df0 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -189,6 +189,8 @@ Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUuid &); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &); #endif +Q_CORE_EXPORT uint qHash(const QUuid &uuid); + QT_END_NAMESPACE QT_END_HEADER diff --git a/tests/auto/quuid/tst_quuid.cpp b/tests/auto/quuid/tst_quuid.cpp index 4948312fd4..5761ea1a8c 100644 --- a/tests/auto/quuid/tst_quuid.cpp +++ b/tests/auto/quuid/tst_quuid.cpp @@ -82,6 +82,8 @@ private slots: void threadUniqueness(); void processUniqueness(); + void hash(); + public: // Variables QUuid uuidA; @@ -322,5 +324,14 @@ void tst_QUuid::processUniqueness() QVERIFY(processOneOutput != processTwoOutput); } +void tst_QUuid::hash() +{ + uint h = qHash(uuidA); + QCOMPARE(qHash(uuidA), h); + QCOMPARE(qHash(QUuid(uuidA.toString())), h); +} + + + QTEST_MAIN(tst_QUuid) #include "tst_quuid.moc" -- cgit v1.2.3