Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine hash values in C++0x?

C++0x adds hash<...>(...).

I could not find a hash_combine function though, as presented in boost. What is the cleanest way to implement something like this? Perhaps, using C++0x xor_combine?

like image 540
Neil G Avatar asked Apr 07 '10 07:04

Neil G


People also ask

What is hash combine?

Calls to hash_combine incrementally build the hash from the different members of point, it can be repeatedly called for any number of elements. It calls hash_value on the supplied element, and combines it with the seed. Full code for this example is at /libs/functional/hash/examples/point. cpp. Note.

Why XOR is used for hash?

That is, XOR is most suitable for the purpose of combining hashes because we want to capture entropy from both a and b. Here is a paper that explains that combining hashes securely where each function is called only once is not possible without outputting less bits than the sum of number of bits in each hash value.


1 Answers

Well, just do it like the boost guys did it:

template <class T> inline void hash_combine(std::size_t& seed, const T& v) {     std::hash<T> hasher;     seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } 
like image 159
Karl von Moor Avatar answered Sep 21 '22 08:09

Karl von Moor