我试图在 C 中实现哈希表来存储英文单词。所以我在互联网上搜索了一些最好的非加密哈希函数。其中一些是Murmurhash、Seahash、xxHash,但它们似乎都很难实现。所以我搜索了一些更简单的,然后我发现了DJB2, sdbm, lost loss。在实施 sdbm 我得到了这个
try.c:12:18: error: using the result of an assignment as a condition without
parentheses [-Werror,-Wparentheses]
while (c = *str++)
~~^~~~~~~~
try.c:12:18: note: place parentheses around the assignment to silence this
warning
while (c = *str++)
^
( )
try.c:12:18: note: use '==' to turn this assignment into an equality
comparison
while (c = *str++)
^
==
try.c:26:31: error: passing 'char *' to parameter of type 'unsigned char *'
converts between pointers to integer types with
different sign [-Werror,-Wpointer-sign]
unsigned long hash = sdbm(argv[1]);
^~~~~~~
2 errors generated.
我的代码是
#include <cs50.h>
#include <string.h>
#include <stdio.h>
static unsigned long
sdbm(str)
unsigned char *str;
{
unsigned long hash = 0;
int c;
while (c = *str++)
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
printf("Enter the second command line argument\n");
return 1;
}
unsigned long hash = sdbm(argv[1]);
printf("The returned hashcode is %lu\n", hash);
}
如果你也可以帮助我处理 Murmurhash、Seahash 或 xxHash,请这样做。