Allow to chain qt_hash() calls

There are some callers of qt_hash that first build a string just to hash it.
By allowing to pass an initial value for 'h', we can chain qt_hash() calls
to avoid having to allocate memory just to hash a two-part string.

Change-Id: Ifaca82d47b2fb8c707912342c3ddd84f91e70267
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2017-03-30 14:01:02 +02:00
parent 58f9629a48
commit cb2246ad15
2 changed files with 10 additions and 12 deletions

View File

@ -412,27 +412,25 @@ void qSetGlobalQHashSeed(int newSeed)
results.
The qt_hash functions must *never* change their results.
This function can hash discontiguous memory by invoking it on each chunk,
passing the previous's result in the next call's \a chained argument.
*/
static uint qt_hash(const QChar *p, size_t n) Q_DECL_NOTHROW
uint qt_hash(QStringView key, uint chained) Q_DECL_NOTHROW
{
uint h = 0;
auto n = key.size();
auto p = key.utf16();
uint h = chained;
while (n--) {
h = (h << 4) + (*p++).unicode();
h = (h << 4) + *p++;
h ^= (h & 0xf0000000) >> 23;
h &= 0x0fffffff;
}
return h;
}
/*!
\internal
*/
uint qt_hash(QStringView key) Q_DECL_NOTHROW
{
return qt_hash(key.data(), key.size());
}
/*
The prime_deltas array contains the difference between a power
of two and the next prime number:

View File

@ -102,7 +102,7 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed =
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QStringView key, uint seed = 0) Q_DECL_NOTHROW;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key) Q_DECL_NOTHROW;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) Q_DECL_NOTHROW;
template <class T> inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW
{