如何创建与64位输出的好hash_combine(由升压启发:: hash_combine) [英] How to create a good hash_combine with 64 bit output (inspired by boost::hash_combine)

查看:819
本文介绍了如何创建与64位输出的好hash_combine(由升压启发:: hash_combine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前升压有输出32位无符号整数(为precise,为size_t)hash_combine功能。一些参考:

Currently Boost has hash_combine function that outputs 32 bit unsigned integer (to be precise, size_t). Some references:

<一个href=\"http://www.boost.org/doc/libs/1_43_0/doc/html/hash/reference.html#boost.hash_combine\">http://www.boost.org/doc/libs/1_43_0/doc/html/hash/reference.html#boost.hash_combine

<一个href=\"http://www.boost.org/doc/libs/1_43_0/doc/html/hash/combine.html\">http://www.boost.org/doc/libs/1_43_0/doc/html/hash/combine.html

在升压幻数:: hash_combine

我想探讨如何建立hash_combine的64位版本。

I'd like to explore on how to create 64 bit version of hash_combine.

的第一件事是让黄金比例或64任何其他无理数位。

The first thing is to get golden ratio or any other irrational number in 64 bit.

的第二部分是使用移位。这部分相当棘手,我想问问有没有最佳做法或指导使用的手段去哈希值?或者选择像原来的code班:

The second part is to use shifts. This part rather tricky and I'd like to ask if there are best practices or guide on using shifts to get hash values? Or choosing shifts like the original code:

seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); 

是完全随机的?

另外如何评价 hash_combine 的输出,以确保它不会产生比原来的散列函数 HASH_VALUE

Also how to evaluate the output of hash_combine to make sure that it doesn't create more collisions than the original hash function hash_value?

推荐答案

如果您只想说散列2 64位值形成一个hash_combine,你不需要为字符串的一个新的哈希函数,你可以只解除从CityHash code,像这样的点点(假设为size_t是64位无符号整数,添加自己喜欢的preprocessor或模板弄虚作假位验证):

If you only want a hash_combine that hashes 2 64 bit values into one, and you don't need a new hash function for strings, you could just lift a tiny bit of code from CityHash, something like this (assuming size_t is a 64 bit unsigned integer, add your favorite bit of preprocessor or template trickery to validate that):

template <class T> inline void hash_combine(std::size_t& seed, const T& v)
{
    std::hash<T> hasher;
    const std::size_t kMul = 0x9ddfea08eb382d69ULL;
    std::size_t a = (hasher(v) ^ seed) * kMul;
    a ^= (a >> 47);
    std::size_t b = (seed ^ a) * kMul;
    b ^= (b >> 47);
    seed = b * kMul;
}

(我觉得这里重现这个片段和其他地方是可以的,因为它不构成CityHash code的很大一部分,但请检查CityHash源&放大器;许可协议,以决定自己)

(I think reproducing this snippet here and elsewhere is OK because it doesn't constitute a 'substantial portion' of the CityHash code, but please check the CityHash sources & license agreement to decide for yourself)

这篇关于如何创建与64位输出的好hash_combine(由升压启发:: hash_combine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆