Skip to content

Commit

Permalink
Add qHash implementation for QPoint
Browse files Browse the repository at this point in the history
[ChangeLog][QtCore][QPoint] Added qHash() implementation.

Change-Id: I65332e7aafab53af40a6e11457b9b457196d584c
Fixes: QTBUG-86457
Reviewed-by: Thorbjørn Lindeijer <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
mitchcurtis committed Sep 15, 2020
1 parent 9815ebf commit fb65b32
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
13 changes: 0 additions & 13 deletions examples/embedded/lightmaps/slippymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions src/corelib/tools/qpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "qdatastream.h"

#include <private/qdebug_p.h>
#include <QtCore/qhashfunctions.h>

QT_BEGIN_NAMESPACE

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/corelib/tools/qpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;



Expand Down
3 changes: 3 additions & 0 deletions tests/auto/corelib/tools/qpoint/tst_qpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions tests/auto/gui/painting/qpainter/tst_qpainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fb65b32

Please sign in to comment.