From fb65b32d7621f065ec854f1e72828720bffb3b39 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 7 Sep 2020 15:57:15 +0200 Subject: [PATCH] Add qHash implementation for QPoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ChangeLog][QtCore][QPoint] Added qHash() implementation. Change-Id: I65332e7aafab53af40a6e11457b9b457196d584c Fixes: QTBUG-86457 Reviewed-by: Thorbjørn Lindeijer Reviewed-by: Thiago Macieira --- examples/embedded/lightmaps/slippymap.cpp | 13 ------------- src/corelib/tools/qpoint.cpp | 14 ++++++++++++++ src/corelib/tools/qpoint.h | 1 + tests/auto/corelib/tools/qpoint/tst_qpoint.cpp | 3 +++ tests/auto/gui/painting/qpainter/tst_qpainter.cpp | 5 ----- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/embedded/lightmaps/slippymap.cpp b/examples/embedded/lightmaps/slippymap.cpp index 038738c69e3..bd97aef7ee0 100644 --- a/examples/embedded/lightmaps/slippymap.cpp +++ b/examples/embedded/lightmaps/slippymap.cpp @@ -53,19 +53,6 @@ #include "slippymap.h" #include "qmath.h" -#ifdef QT_NAMESPACE -QT_BEGIN_NAMESPACE -size_t qHash(const QT_NAMESPACE::QPoint& p) -#else -size_t qHash(const QPoint& p) -#endif -{ - return p.x() * 17 ^ p.y(); -} -#ifdef QT_NAMESPACE -QT_END_NAMESPACE -#endif - // tile size in pixels const int tdim = 256; diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index 432fb332974..eca1b5a0ac5 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -41,6 +41,7 @@ #include "qdatastream.h" #include +#include QT_BEGIN_NAMESPACE @@ -483,6 +484,19 @@ QDebug operator<<(QDebug dbg, const QPointF &p) } #endif +/*! + \fn size_t qHash(QPoint key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the + calculation. +*/ +size_t qHash(QPoint key, size_t seed) noexcept +{ + return qHashMulti(seed, key.x(), key.y()); +} + /*! \class QPointF \inmodule QtCore diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 725787265fb..e854e771984 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -214,6 +214,7 @@ constexpr inline const QPoint operator/(const QPoint &p, qreal c) Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &); #endif +Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept; diff --git a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp b/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp index f25492d2db1..0f3edb3eeda 100644 --- a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp +++ b/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp @@ -346,6 +346,9 @@ void tst_QPoint::operator_eq() QCOMPARE(equal, expectEqual); bool notEqual = point1 != point2; QCOMPARE(notEqual, !expectEqual); + + if (equal) + QCOMPARE(qHash(point1), qHash(point2)); } #ifndef QT_NO_DATASTREAM diff --git a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp index 5d4e9def832..9c5a52f2d9a 100644 --- a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp +++ b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp @@ -3256,11 +3256,6 @@ void tst_QPainter::imageScaling_task206785() #define FOR_EACH_NEIGHBOR_8 for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) if (dx != 0 || dy != 0) #define FOR_EACH_NEIGHBOR_4 for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) if ((dx == 0) != (dy == 0)) -size_t qHash(const QPoint &point) -{ - return qHash(qMakePair(point.x(), point.y())); -} - bool verifyOutlineFillConsistency(const QImage &img, QRgb outside, QRgb inside, QRgb outline) { if (img.pixel(img.width() / 2, img.height() / 2) != inside)