Skip to content

Instantly share code, notes, and snippets.

@rjauquet
Last active September 13, 2015 01:47
Show Gist options
  • Select an option

  • Save rjauquet/ff483aa45347b59da09d to your computer and use it in GitHub Desktop.

Select an option

Save rjauquet/ff483aa45347b59da09d to your computer and use it in GitHub Desktop.
#include <stdio.h>
unsigned int jauquet(char *str) {
//jauquet prime = 79087987342985798987987
//mod 2 ^ 32 = 2053222611
//mod 2 ^ 64 = 6795498992951210195
unsigned int hash = 6795498992951210195;
unsigned int shift = 611;
int c;
while (c = *str++)
hash = hash * shift + c;
return hash;
}
int main(){
int wordCount = 109583;
int tableSize = wordCount * 2;
int jauquet_hashTable[tableSize];
int jauquet_collisions = 0;
unsigned int jauquet_temp;
int size = 1024, pos;
int c;
char *buffer = (char *)malloc(size);
FILE *f = fopen("wordsEn.txt", "r");
if(f) {
do { // read all lines in file
pos = 0;
do{ // read one line
c = fgetc(f);
if(c != EOF) buffer[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffer = (char*)realloc(buffer, size);
}
}while(c != EOF && c != '\n');
buffer[pos] = 0;
// line is now in buffer
jauquet_temp = jauquet(buffer) % tableSize;
if(!jauquet_hashTable[jauquet_temp]){
jauquet_hashTable[jauquet_temp] = 1;
} else {
jauquet_collisions++;
}
} while(c != EOF);
fclose(f);
}
free(buffer);
printf("djb2 %i\n", jauquet_collisions);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment