Created
September 13, 2023 18:19
-
-
Save keithel/84bae121e7c57bf95a26cefbb2d6b672 to your computer and use it in GitHub Desktop.
QPointF hash function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace std { | |
template<> struct hash<QPointF> | |
{ | |
size_t operator()(const QPointF &point, size_t seed) const | |
{ | |
size_t x = static_cast<size_t>(point.x()); | |
size_t y = static_cast<size_t>(point.y()); | |
size_t half_qreal = sizeof(qreal) / 2; | |
size_t half_bits = half_qreal*8; | |
size_t half1s = pow(2, half_qreal*8)-1; | |
size_t swappedX = (x & half1s) << half_bits | (x & (half1s << half_bits)) >> half_bits; | |
return (swappedX | y) ^ seed; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From Marc Mutz' presentation on Qt Container changes between Qt 5 and Qt 6, 20230913.
He asked us to implement a qHash function for hashing QPointF, which meets all the requirements for QHash.
Note that this is a bit odd because it does not make the assumption that qreal is a 64 bit type, since this can be altered when compiling Qt using the -qreal option.