summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-03-30 14:01:02 +0200
committerMarc Mutz <marc.mutz@kdab.com>2017-03-30 13:27:51 +0000
commitcb2246ad15241016063689de1600519202b87a93 (patch)
tree47f638eaf13a6acbf9f15269ac5a7e240b427910 /src/corelib/tools
parent58f9629a486ae757342bc7a85ad46f08c7d1dbbc (diff)
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>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qhash.cpp20
-rw-r--r--src/corelib/tools/qhashfunctions.h2
2 files changed, 10 insertions, 12 deletions
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp
index f83643e90a..662c304a2e 100644
--- a/src/corelib/tools/qhash.cpp
+++ b/src/corelib/tools/qhash.cpp
@@ -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:
diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h
index 2c3abe6af0..f75b310e4e 100644
--- a/src/corelib/tools/qhashfunctions.h
+++ b/src/corelib/tools/qhashfunctions.h
@@ -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
{