Add qHash(QMatrix) and qHash(QTransform)

QMatrix and QTransform can be compared for equality,
so qHash should be overloaded, too.

[ChangeLog][QtCore][QMatrix] Added qHash(QMatrix).
[ChangeLog][QtCore][QTransform] Added qHash(QTransform).

Change-Id: I1ce925ebe258c9d7e35b68e5ac5c3373f1460c58
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2015-04-08 19:44:48 +02:00
parent 299350f668
commit 05d1693793
5 changed files with 68 additions and 1 deletions

View File

@ -31,9 +31,11 @@
** **
****************************************************************************/ ****************************************************************************/
#include "qmatrix.h"
#include "qdatastream.h" #include "qdatastream.h"
#include "qdebug.h" #include "qdebug.h"
#include "qmatrix.h" #include "qhashfunctions.h"
#include "qregion.h" #include "qregion.h"
#include "qpainterpath.h" #include "qpainterpath.h"
#include "qpainterpath_p.h" #include "qpainterpath_p.h"
@ -972,6 +974,26 @@ bool QMatrix::operator==(const QMatrix &m) const
_dy == m._dy; _dy == m._dy;
} }
/*!
\since 5.6
\relates QMatrix
Returns the hash value for \a key, using
\a seed to seed the calculation.
*/
uint qHash(const QMatrix &key, uint seed) Q_DECL_NOTHROW
{
QtPrivate::QHashCombine hash;
seed = hash(key.m11(), seed);
seed = hash(key.m12(), seed);
seed = hash(key.m21(), seed);
seed = hash(key.m22(), seed);
seed = hash(key.dx(), seed);
seed = hash(key.dy(), seed);
return seed;
}
/*! /*!
\fn bool QMatrix::operator!=(const QMatrix &matrix) const \fn bool QMatrix::operator!=(const QMatrix &matrix) const

View File

@ -126,6 +126,8 @@ private:
}; };
Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE);
Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QMatrix &key, uint seed = 0) Q_DECL_NOTHROW;
// mathematical semantics // mathematical semantics
inline QPoint operator*(const QPoint &p, const QMatrix &m) inline QPoint operator*(const QPoint &p, const QMatrix &m)
{ return m.map(p); } { return m.map(p); }

View File

@ -34,6 +34,7 @@
#include "qdatastream.h" #include "qdatastream.h"
#include "qdebug.h" #include "qdebug.h"
#include "qhashfunctions.h"
#include "qmatrix.h" #include "qmatrix.h"
#include "qregion.h" #include "qregion.h"
#include "qpainterpath.h" #include "qpainterpath.h"
@ -775,6 +776,29 @@ bool QTransform::operator==(const QTransform &o) const
m_33 == o.m_33; m_33 == o.m_33;
} }
/*!
\since 5.6
\relates QTransform
Returns the hash value for \a key, using
\a seed to seed the calculation.
*/
uint qHash(const QTransform &key, uint seed) Q_DECL_NOTHROW
{
QtPrivate::QHashCombine hash;
seed = hash(key.m11(), seed);
seed = hash(key.m12(), seed);
seed = hash(key.m21(), seed);
seed = hash(key.m22(), seed);
seed = hash(key.dx(), seed);
seed = hash(key.dy(), seed);
seed = hash(key.m13(), seed);
seed = hash(key.m23(), seed);
seed = hash(key.m33(), seed);
return seed;
}
/*! /*!
\fn bool QTransform::operator!=(const QTransform &matrix) const \fn bool QTransform::operator!=(const QTransform &matrix) const
Returns \c true if this matrix is not equal to the given \a matrix, Returns \c true if this matrix is not equal to the given \a matrix,

View File

@ -180,6 +180,8 @@ private:
}; };
Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE);
Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QTransform &key, uint seed = 0) Q_DECL_NOTHROW;
/******* inlines *****/ /******* inlines *****/
inline QTransform::TransformationType QTransform::inline_type() const inline QTransform::TransformationType QTransform::inline_type() const
{ {

View File

@ -57,6 +57,7 @@ private slots:
void mapRect(); void mapRect();
void assignments(); void assignments();
void mapToPolygon(); void mapToPolygon();
void qhash();
void translate(); void translate();
void scale(); void scale();
void matrix(); void matrix();
@ -361,6 +362,22 @@ void tst_QTransform::mapToPolygon()
QVERIFY(equal); QVERIFY(equal);
} }
void tst_QTransform::qhash()
{
QMatrix m1;
m1.shear(3.0, 2.0);
m1.rotate(44);
QMatrix m2 = m1;
QTransform t1(m1);
QTransform t2(m2);
// not really much to test here, so just the bare minimum:
QCOMPARE(qHash(m1), qHash(m2));
QCOMPARE(qHash(t1), qHash(t2));
}
void tst_QTransform::translate() void tst_QTransform::translate()
{ {