Created
June 14, 2015 21:48
-
-
Save FRex/bbbbdb34f16e353d7173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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