首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

这个哈希函数是怎么实现映射的

2012-03-01 
这个哈希函数是如何实现映射的?在某开源软件上扣下来的:/*Hashingofnumericvalues,suchaspointersandinteg

这个哈希函数是如何实现映射的?
在某开源软件上扣下来的:

/*   Hashing   of   numeric   values,   such   as   pointers   and   integers.

      This   implementation   is   the   Robert   Jenkins '   32   bit   Mix   Function,
      with   a   simple   adaptation   for   64-bit   values.     It   offers   excellent
      spreading   of   values   and   doesn 't   need   to   know   the   hash   table   size   to
      work   (unlike   the   very   popular   Knuth 's   multiplication   hash).     */
/*哈希函数*/
unsigned   long
hash_pointer   (const   void   *ptr)
{
    unsigned   long   key   =   (unsigned   long)ptr;
    key   +=   (key   < <   12);
    key   ^=   (key   > >   22);
    key   +=   (key   < <   4);
    key   ^=   (key   > >   9);
    key   +=   (key   < <   10);
    key   ^=   (key   > >   2);
    key   +=   (key   < <   7);
    key   ^=   (key   > >   12);
#if   SIZEOF_LONG   >   4
    key   +=   (key   < <   44);
    key   ^=   (key   > >   54);
    key   +=   (key   < <   36);
    key   ^=   (key   > >   41);
    key   +=   (key   < <   42);
    key   ^=   (key   > >   34);
    key   +=   (key   < <   39);
    key   ^=   (key   > >   44);
#endif
    return   key;
}

[解决办法]
把一个字符串的地址当作哈希表的初始值

热点排行