开发者

Alternatives to boost::hash_combine that have associative property?

开发者 https://www.devze.com 2023-03-07 02:03 出处:网络
I am looking for a hash_combine function that has the associative property. For example, I\'d like to be able to either combine the values a, b, c, d one after another to get the hash key for the se

I am looking for a hash_combine function that has the associative property.

For example, I'd like to be able to either combine the values a, b, c, d one after another to get the hash key for the sequence, or combine a and b, then c and d, and combine the results. The two methods should give the same result.

boost::hash_combine does not have that property:

  // a * b * c * d                                                                                                                                                                开发者_如何学C                        
  std::size_t seed = 0;
  boost::hash_combine(seed, 234);
  boost::hash_combine(seed, 62);
  boost::hash_combine(seed, 675);
  boost::hash_combine(seed, 916);
  std::cout << seed << std::endl; // 706245846748881

  // (a * b) * (c * d)                                                                                                                                                                                    
  std::size_t seed1 = 0;
  boost::hash_combine(seed1, 234);
  boost::hash_combine(seed1, 62);
  std::size_t seed2 = 0;
  boost::hash_combine(seed2, 675);
  boost::hash_combine(seed2, 916);
  boost::hash_combine(seed1, seed2); // 11337801211148

Is there any good hash_combine function that has it?

P.S.: The reason for doing this is that I assign hash keys to sequences that I find in a DAG. I'm running dynamic programming to find the hash keys for (the sequences between) all pairs of states.


How about plain xor?

std::size_t seed = 0;
seed ^= boost::hash_value(234);
seed ^= boost::hash_value(62);
...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号