From 08373fb02d648544f091aa1aabfe5949ea83a0f8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 17 Mar 2014 15:44:29 +0100 Subject: 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 --- src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/corelib/doc') 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 &key, uint seed = 0) return qHashBits(&key.front(), key.size() * sizeof(int), seed); } //! [qhashbits] + +//! [qhashrange] +inline uint qHash(const std::vector &key, uint seed = 0) +{ + return qHashRange(key.begin(), key.end(), seed); +} +//! [qhashrange] + +//! [qhashrangecommutative] +inline uint qHash(const std::unordered_set &key, uint seed = 0) +{ + return qHashRangeCommutative(key.begin(), key.end(), seed); +} +//! [qhashrangecommutative] -- cgit v1.2.3