Skip to content

Instantly share code, notes, and snippets.

@keithel
Created September 13, 2023 18:19
Show Gist options
  • Save keithel/84bae121e7c57bf95a26cefbb2d6b672 to your computer and use it in GitHub Desktop.
Save keithel/84bae121e7c57bf95a26cefbb2d6b672 to your computer and use it in GitHub Desktop.
QPointF hash function
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;
   }
};
}
@keithel
Copy link
Author

keithel commented Sep 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment