I am using uthash.h for hash implementation in C. I am using the hash-table for a basic word count exercise. I have a file containing words and I have to count frequency of each word. The implementation of uthash.h requires me to generate an integer id for each entry, and I wanted to calculate a unique integer corresponding to each string. I tried using md5 hash algorithm, but it generates strings with digits and alphabets, so its no use.Can anybody suggest me such an algorithm.
1 Answer
Use Robert Sedgewick's algorithm for hashing
unsigned int GenerateHash(char* str, unsigned int len) { unsigned int result = 0; unsigned int b = 378551; unsigned int a = 63689; unsigned int i = 0; for(i=0; i<len; str++, i++) { result = result*a + (*str); a = a*b; } return result; }
uthash.himplementation the id needs to be integer. I am not wether this will work or not. I will try this approach and post my results once done. In the mean time if you have any more suggestion, please post them.