A good hash function for a vector

So, when not wanting to use boost, Michael Blurr’s comment led to the following hash function implementation:

std::size_t operator()(std::vector<uint32_t> const& vec) const {
  std::size_t seed = vec.size();
  for(auto& i : vec) {
    seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
  return seed;
}

Seems to work.

Edit: see’s answer is a little bit slower, but indeed yields a better hash distribution. I’d go with that one.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)