曲径通幽论坛

标题: gdbm hash 算法 [打印本页]

作者: beyes    时间: 2011-7-25 11:40
标题: gdbm hash 算法
原来使用于 gdbm 数据库的源码中,下面代码片段来自 linux 内核代码中的 modpost 工具源码 modpost.c :
[C++] 纯文本查看 复制代码

#define SYMBOL_HASH_SIZE 1024

static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
... ...
/* This is based on the hash agorithm from gdbm, via tdb */
static inline unsigned int tdb_hash(const char *name)
{
    unsigned value;    /* Used to compute the hash value.  */
    unsigned   i;    /* Used to cycle through random values. */

    /* Set the initial value from the key size. */
    for (value = 0x238F13AF * strlen(name), i = 0; name; i++)
        value = (value + (((unsigned char *)name) << (i*5 % 24)));

    return (1103515243 * value + 12345);
}

symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2