diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-03-17 15:44:29 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-01-09 12:05:35 +0100 |
commit | 08373fb02d648544f091aa1aabfe5949ea83a0f8 (patch) | |
tree | 99a5705fbe45b2ae11fa3227ad8dde87ca2c2bd8 /src/corelib/doc | |
parent | 62b67092eadd97d739b05aeac68d556e02d22f85 (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:
http://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp | 14 |
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] |