c_cpp C中的新哈希函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C中的新哈希函数相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

unsigned long chop_chop_hash(const char* string)
{
    unsigned long block = 5381;
    unsigned char* chops = (unsigned char*)(&block);
    size_t state = 0;
    while(*string)
    {
       chops[state] = chops[state++] ^ (*string++);
       state = state == sizeof(unsigned long) ? 0 : state;
    }
    return block;
}

/*chop chop hash of 'aa' is 353285
* chop chop hash of 'aab' is 1728406533
* chop chop hash of 'foods' is 127575075349253
* chop chop hash of 'Hello World!' is 3487834674689494277
* chop chop hash of 'mt14 is the best team in splunk' is * 2544596569758132229
 *
 */

int main(void) {
  printf("chop chop hash of '%s' is %lu\n", "aa", chop_chop_hash("aa"));
  printf("chop chop hash of '%s' is %lu\n", "aab", chop_chop_hash("aab"));
  printf("chop chop hash of '%s' is %lu\n", "foods", chop_chop_hash("foods"));
  printf("chop chop hash of '%s' is %lu\n", "Hello World!", chop_chop_hash("Hello World!"));
  printf("chop chop hash of '%s' is %lu\n", "mt14 is the best team in splunk", chop_chop_hash("mt14 is the best team in splunk"));
  return 0;
}

以上是关于c_cpp C中的新哈希函数的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C中的哈希映射实现,带有djb2哈希函数

c_cpp C中的哈希函数dbg2

c_cpp 为字符串实现哈希表和哈希函数

c_cpp 为字符串实现哈希表和哈希函数

c_cpp 尝试新的哈希函数

c_cpp C中的哈希表环境类