Hash function

一些据说还凑活的hash function,需要的时候可以拿来主义~

1. 最简单的

unsigned long hash(unsigned char *str)
{
    unsigned int hash = 0;
    int c;
    
    while (c = *str++)
        hash += c;
    
    return hash;
}

2. djb2

unsigned long hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

3. sdbm

static unsigned long sdbm(unsigned char *str)
{
    unsigned long hash = 0;
    int c;

    while (c = *str++)
        hash = c + (hash << 6) + (hash << 16) - hash;

    return hash;
}

我不是伟大技术的创造者,我只是互联网的搬运工…

Tags :

0 thoughts on “Hash function”

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Click the right image To submit your comment: