1

I'm trying to understand how this beauty works, can someone give me some clues?

struct VectorHash {
    size_t operator()(const std::vector<int>& v) const {
        std::hash<int> hasher;
        size_t seed = 0;
        for (int i : v) {
            seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }
        return seed;
    }
};

Used as:

using MySet = std::unordered_set<std::vector<int>, VectorHash>;

Why it use this number?

0x9e3779b9 = 10011110001101110111100110111001

Why it add the seed shifted 6 to the left and 2 to the rigth?

Why the use of Bitwise Exclusive OR assignment ^=?

Community
  • 1
  • 1
Rama
  • 3,222
  • 2
  • 11
  • 26
  • 1
    [Related](https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine). – Baum mit Augen Mar 20 '17 at 15:36
  • 1
    this is the implementation used by [`boost::hash_combine`](http://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine), which is explained in http://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values – Sander De Dycker Mar 20 '17 at 15:40
  • Thanks for the references, solved! – Rama Mar 20 '17 at 15:42

0 Answers0