improved qHash-functions and class constructors

NEW jkqtp_hash_combine() and jkqtp_combine_hash()
This commit is contained in:
jkriege2 2024-02-05 17:12:34 +01:00
parent 3b231273ef
commit 210c7aaa42
9 changed files with 81 additions and 23 deletions

View File

@ -105,7 +105,7 @@ void JKQTPEnhancedPainter::drawComplexRoundedRect(const QRectF &rin, double rTop
drawPath(path);
}
void JKQTPEnhancedPainter::drawRoundedRectOrRect(const QRectF &r, double radius, Qt::SizeMode mode)
void JKQTPEnhancedPainter::drawRoundedRectOrRect(const QRectF &r, double radius, Qt::SizeMode /*mode*/)
{
if (radius<=0) {
drawRect(r);

View File

@ -31,6 +31,7 @@
#include <QString>
#include <functional>
#include <type_traits>
#include <QHashFunctions>
#ifdef max
# undef max
@ -584,4 +585,40 @@ inline T jkqtp_reversed(const T& l) {
return T(l.rbegin(), l.rend());
}
/*! \brief can be used to build a hash-values from several hash-values
\ingroup jkqtptools_math_basic
\code
std::size_t seed=0;
jkqtp_hash_combine(seed, valA);
jkqtp_hash_combine(seed, valB);
//...
// finally seed contains the combined hash
\endcode
*/
template <class T>
inline void jkqtp_hash_combine(std::size_t& seed, const T& v)
{
const auto hsh=::qHash(v,0);
seed ^= hsh + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
/*! \brief can be used to build a hash-values from several hash-values
\ingroup jkqtptools_math_basic
\code
std::size_t seed=0;
jkqtp_combine_hash(seed, qHash(valA));
jkqtp_combine_hash(seed, qHash(valB));
//...
// finally seed contains the combined hash
\endcode
*/
inline void jkqtp_combine_hash(std::size_t& seed, std::size_t hsh)
{
seed ^= hsh + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
#endif // jkqtpmathtools_H_INCLUDED

View File

@ -31,6 +31,7 @@
#include <QColor>
#include <QDebug>
#include <QElapsedTimer>
#include <QHashFunctions>
#include <QIcon>
#include <QImage>
#include <QLine>

View File

@ -31,6 +31,7 @@
#include <QApplication>
#include <QFont>
#include <QReadWriteLock>
#include <QHashFunctions>
#include <mutex>
#include <functional>
@ -918,12 +919,18 @@ namespace {
};
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
inline size_t qHash(const JKQTMathTextCacheKeyBase& data, size_t seed=0) {
inline size_t qHash(const JKQTMathTextCacheKeyBase& data, size_t /*seedin=0*/) {
#else
inline uint qHash(const JKQTMathTextCacheKeyBase& data) {
inline uint qHash(const JKQTMathTextCacheKeyBase& data, uint /*seedin=0*/) {
const size_t seed=0;
#endif
return qHash(data.f)+::qHash(data.ldpiX,seed)+::qHash(data.ldpiY)+::qHash(data.pdpiX)+::qHash(data.pdpiY);
std::size_t seed=0;
jkqtp_combine_hash(seed, ::qHash(data.f,0));
jkqtp_combine_hash(seed, ::qHash(data.ldpiX,0));
jkqtp_combine_hash(seed, ::qHash(data.ldpiY,0));
jkqtp_combine_hash(seed, ::qHash(data.pdpiX,0));
jkqtp_combine_hash(seed, ::qHash(data.pdpiY,0));
return seed;
}
struct JKQTMathTextCacheKeyBaseExt: public JKQTMathTextCacheKeyBase {
@ -966,10 +973,17 @@ namespace {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
inline size_t qHash(const JKQTMathTextTBRDataH<TText>& data, size_t /*seed=0*/) {
#else
inline uint qHash(const JKQTMathTextTBRDataH<TText>& data) {
inline uint qHash(const JKQTMathTextTBRDataH<TText>& data, uint /*seed=0*/) {
#endif
return qHash(data.f)+qHash(data.text)+::qHash(data.ldpiX)+::qHash(data.ldpiY)+::qHash(data.pdpiX)+::qHash(data.pdpiY);
}
std::size_t seed=0;
jkqtp_combine_hash(seed, ::qHash(data.f,0));
jkqtp_combine_hash(seed, ::qHash(data.text,0));
jkqtp_combine_hash(seed, ::qHash(data.ldpiX,0));
jkqtp_combine_hash(seed, ::qHash(data.ldpiY,0));
jkqtp_combine_hash(seed, ::qHash(data.pdpiX,0));
jkqtp_combine_hash(seed, ::qHash(data.pdpiY,0));
return seed;
};
template <class TText=QString>
struct JKQTMathTextTBRDataHExt: public JKQTMathTextTBRDataH<TText> {

View File

@ -45,6 +45,7 @@
#include <QFontInfo>
#include <QFontMetricsF>
#include <QHash>
#include <QHashFunctions>
#include <QIcon>
#include <QImage>
#include <QLabel>

View File

@ -4808,11 +4808,9 @@ void JKQTBasePlotter::setEmittingSignalsEnabled(bool enabled)
JKQTBasePlotter::textSizeKey::textSizeKey(const QFont &f, const QString &text, QPaintDevice *pd):
text(), f(), ldpiX(0), ldpiY(0), pdpiX(0), pdpiY(0)
JKQTBasePlotter::textSizeKey::textSizeKey(const QFont &f_, const QString &text_, QPaintDevice *pd):
text(text_), f(f_), ldpiX(0), ldpiY(0), pdpiX(0), pdpiY(0)
{
this->text=text;
this->f=f;
if (pd) {
ldpiX=pd->logicalDpiX();
ldpiY=pd->logicalDpiY();
@ -4826,14 +4824,14 @@ JKQTBasePlotter::textSizeKey::textSizeKey(const QFont &f, const QString &text, Q
}
}
JKQTBasePlotter::textSizeKey::textSizeKey(const QString &fontName, double fontSize, const QString &text, QPaintDevice *pd):
text(), f(), ldpiX(0), ldpiY(0), pdpiX(0), pdpiY(0)
JKQTBasePlotter::textSizeKey::textSizeKey(const QString &fontName, double fontSize, const QString &text_, QPaintDevice *pd):
text(text_), f([&](){
QFont fnew;
fnew.setFamily(JKQTMathTextFontSpecifier::fromFontSpec(fontName).fontName());
fnew.setPointSizeF(fontSize);
return fnew;
}()), ldpiX(0), ldpiY(0), pdpiX(0), pdpiY(0)
{
QFont f;
f.setFamily(JKQTMathTextFontSpecifier::fromFontSpec(fontName).fontName());
f.setPointSizeF(fontSize);
this->text=text;
this->f=f;
if (pd) {
ldpiX=pd->logicalDpiX();
ldpiY=pd->logicalDpiY();

View File

@ -2821,7 +2821,10 @@ inline size_t qHash(const JKQTBasePlotter::textSizeKey& data, size_t /*seed=0*/)
#else
inline uint qHash(const JKQTBasePlotter::textSizeKey& data, uint /*seed=0*/) {
#endif
return qHash(data.f.family())+qHash(data.text);
std::size_t seed=0;
jkqtp_hash_combine(seed, data.f);
jkqtp_hash_combine(seed, data.text);
return seed;
}
#endif // JKQTPBASEPLOTTER_H

View File

@ -36,7 +36,7 @@
#include <QToolBar>
#include <QPointer>
#include <QTimer>
#include <QHash>
#include <vector>
#include <cmath>
#include <iostream>
@ -1720,7 +1720,10 @@ inline size_t qHash(const QPair<Qt::MouseButton,Qt::KeyboardModifiers> &key, siz
#else
inline uint qHash(const QPair<Qt::MouseButton,Qt::KeyboardModifiers> &key, uint /*seed=0*/) {
#endif
return static_cast<uint>(key.first)+static_cast<uint>(key.second);
std::size_t seed=0;
jkqtp_hash_combine(seed, key.first);
jkqtp_hash_combine(seed, key.second);
return seed;
}
/** \brief qHash-variant used by JKQTPlotter
@ -1732,7 +1735,7 @@ inline size_t qHash(const Qt::MouseButton &key, size_t /*seed=0*/) {
#else
inline uint qHash(const Qt::MouseButton &key, uint /*seed=0*/) {
#endif
return static_cast<uint>(key);
return qHash(static_cast<uint>(key),0);
}
/** \brief qHash-variant used by JKQTPlotter
@ -1744,7 +1747,7 @@ inline size_t qHash(const Qt::KeyboardModifiers &key, size_t /*seed=0*/) {
#else
inline uint qHash(const Qt::KeyboardModifiers &key, uint /*seed=0*/) {
#endif
return static_cast<uint>(key);
return qHash(static_cast<uint>(key),0);
}
QT_END_NAMESPACE

View File

@ -68,6 +68,7 @@
#include <QGridLayout>
#include <QGuiApplication>
#include <QHash>
#include <QHashFunctions>
#include <QIcon>
#include <QImage>
#include <QKeyEvent>