From 58ec01e278496bdaaee05546beb543a119de4277 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 2 Jul 2012 09:01:56 +0100 Subject: [PATCH] More qHash(T, uint) support Add the seed to QPair, QUuid, QPersistentModelIndex's qHash(), and fix qHash documentation for them and for many other datatypes. Change-Id: I1386f3ed42ee1a832371a242ee5c82895ba92c2b Reviewed-by: Thiago Macieira --- src/corelib/itemmodels/qabstractitemmodel.cpp | 10 ++- src/corelib/itemmodels/qabstractitemmodel.h | 6 +- src/corelib/plugin/quuid.cpp | 15 ++-- src/corelib/plugin/quuid.h | 2 +- src/corelib/tools/qhash.cpp | 106 +++++++++++++++++--------- src/corelib/tools/qhash.h | 8 +- 6 files changed, 94 insertions(+), 53 deletions(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 9e0e9c0..87bcb6d 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -3517,11 +3517,13 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti */ /*! - \fn uint qHash(const QPersistentModelIndex &index) - \since 4.5 + \fn uint qHash(const QPersistentModelIndex &index, uint seed = 0) + \since 5.0 + \relates QPersistentModelIndex - Returns a hash of the QPersistentModelIndex - */ + Returns a hash of the QPersistentModelIndex \a index, using \a seed to + seed the calculation. +*/ /*! diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index 66af606..4cdafdd 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -134,15 +134,15 @@ public: bool isValid() const; private: QPersistentModelIndexData *d; - friend uint qHash(const QPersistentModelIndex &); + friend uint qHash(const QPersistentModelIndex &, uint seed = 0); #ifndef QT_NO_DEBUG_STREAM friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &); #endif }; Q_DECLARE_SHARED(QPersistentModelIndex) -inline uint qHash(const QPersistentModelIndex &index) -{ return qHash(index.d); } +inline uint qHash(const QPersistentModelIndex &index, uint seed) +{ return qHash(index.d, seed); } #ifndef QT_NO_DEBUG_STREAM diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index bfe8dfc..952e418 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -999,14 +999,17 @@ QDebug operator<<(QDebug dbg, const QUuid &id) } #endif -/** - Returns a hash of the QUuid - */ -uint qHash(const QUuid &uuid) +/*! + \since 5.0 + \relates QUuid + Returns a hash of the UUID \a uuid, using \a seed to seed the calculation. +*/ +uint qHash(const QUuid &uuid, uint seed) { return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) - ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) - ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]); + ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) + ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) + ^ seed; } diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index e595613..7fd32a3 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -227,7 +227,7 @@ Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &); Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &); #endif -Q_CORE_EXPORT uint qHash(const QUuid &uuid); +Q_CORE_EXPORT uint qHash(const QUuid &uuid, uint seed = 0); QT_END_NAMESPACE diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 0a6d51c..97f846c 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -1,3 +1,4 @@ + /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). @@ -570,104 +571,139 @@ void QHashData::checkSanity() #endif /*! - \fn uint qHash(const QPair &key) - \since 4.3 + \fn uint qHash(const QPair &key, uint seed = 0) + \since 5.0 \relates QHash - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. Types \c T1 and \c T2 must be supported by qHash(). */ -/*! \fn uint qHash(char key) +/*! \fn uint qHash(char key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(uchar key) +/*! \fn uint qHash(uchar key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(signed char key) +/*! \fn uint qHash(signed char key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(ushort key) +/*! \fn uint qHash(ushort key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(short key) +/*! \fn uint qHash(short key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(uint key) +/*! \fn uint qHash(uint key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(int key) +/*! \fn uint qHash(int key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(ulong key) +/*! \fn uint qHash(ulong key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(long key) +/*! \fn uint qHash(long key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(quint64 key) +/*! \fn uint qHash(quint64 key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(qint64 key) +/*! \fn uint qHash(qint64 key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(QChar key) +/*! \fn uint qHash(QChar key, uint seed = 0) \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ /*! \fn uint qHash(const QByteArray &key, uint seed = 0) - \fn uint qHash(const QBitArray &key, uint seed = 0) - \fn uint qHash(const QString &key, uint seed = 0) - \fn uint qHash(const QStringRef &key, uint seed = 0) - \fn uint qHash(QLatin1String key, uint seed = 0) + \relates QHash + \since 5.0 + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn uint qHash(const QBitArray &key, uint seed = 0) \relates QHash \since 5.0 - Returns the hash value for the \a key, using \a seed to - seed the calculation. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ -/*! \fn uint qHash(const T *key) +/*! \fn uint qHash(const QString &key, uint seed = 0) \relates QHash + \since 5.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn uint qHash(const QStringRef &key, uint seed = 0) + \relates QHash + \since 5.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn uint qHash(QLatin1String key, uint seed = 0) + \relates QHash + \since 5.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn uint qHash(const T *key, uint seed = 0) + \relates QHash + \since 5.0 - Returns the hash value for the \a key. + Returns the hash value for the \a key, using \a seed to seed the calculation. */ /*! diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 8f01cd2..0c322cd 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -103,11 +103,11 @@ template inline uint qHash(const T *key, uint seed = 0) #pragma warning( pop ) #endif -template inline uint qHash(const QPair &key) +template inline uint qHash(const QPair &key, uint seed = 0) { - uint h1 = qHash(key.first); - uint h2 = qHash(key.second); - return ((h1 << 16) | (h1 >> 16)) ^ h2; + uint h1 = qHash(key.first, seed); + uint h2 = qHash(key.second, seed); + return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed; } template inline uint qHash(const T &t, uint seed) { return (qHash(t) ^ seed); } -- 2.7.4