Skip to content

Instantly share code, notes, and snippets.

@FRex
Created June 14, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FRex/bbbbdb34f16e353d7173 to your computer and use it in GitHub Desktop.
Save FRex/bbbbdb34f16e353d7173 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
unsigned fnv_hash(const char * str)
{
unsigned hash = 2166136261u;
unsigned i;
for (i = 0u; str[i]; ++i)
hash = (hash * 16777619u) ^ str[i];
return hash;
}
unsigned gd_hash(const char * str)
{
unsigned hash = 0u;
unsigned i;
for (i = 0u; str[i]; ++i)
hash += (str[i] << ((i & 1) * 8));
return hash;
}
char word[1024];
int main(int argc, char ** argv)
{
if(argc == 1)
{
while(1)
{
fgets(word, sizeof(word), stdin);
if(feof(stdin))
break;
if(strlen(word))
{
word[strlen(word) - 1] = '\0';
printf("%u\n", gd_hash(word));
}
}
}
else
{
while(1)
{
fgets(word, sizeof(word), stdin);
if(feof(stdin))
break;
if(strlen(word))
{
word[strlen(word) - 1] = '\0';
printf("%u\n", fnv_hash(word));
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment