path: root/src/corelib/doc
diff options
authorMarc Mutz <>2014-03-17 15:44:29 +0100
committerMarc Mutz <>2015-01-09 12:05:35 +0100
commit08373fb02d648544f091aa1aabfe5949ea83a0f8 (patch)
tree99a5705fbe45b2ae11fa3227ad8dde87ca2c2bd8 /src/corelib/doc
parent62b67092eadd97d739b05aeac68d556e02d22f85 (diff)
Add qHashRange and qHashRangeCommutative
qHashRange() takes an (input iterator) range and hashes each element, combining the hash values using the hash combiner from Boost/N1837 with the magic number 0x9e3779b9, as described here: qHashRangeCommutative() does the same but with a cummutative combiner (unsigned addition) to create hash values that are order-independent, e.g. for hashed containers. The obvious combiner, XOR, is a bad one because it eliminates duplicate elements. Signed addition cannot be used, since signed overflow leads to undefined behavior. [ChangeLog][QtCore] Added qHashRange() and qHashRangeCommutative() functions to aid implementing qHash() overloads for custom types. Change-Id: I3c2bbc9ce4bd0455262a70e0cf248486525e534f Reviewed-by: Thiago Macieira <>
Diffstat (limited to 'src/corelib/doc')
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
index aa0473964c..0195b07b54 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
@@ -307,3 +307,17 @@ inline uint qHash(const std::vector<int> &key, uint seed = 0)
return qHashBits(&key.front(), key.size() * sizeof(int), seed);
//! [qhashbits]
+//! [qhashrange]
+inline uint qHash(const std::vector<int> &key, uint seed = 0)
+ return qHashRange(key.begin(), key.end(), seed);
+//! [qhashrange]
+//! [qhashrangecommutative]
+inline uint qHash(const std::unordered_set<int> &key, uint seed = 0)
+ return qHashRangeCommutative(key.begin(), key.end(), seed);
+//! [qhashrangecommutative]