c - good hash function -



c - good hash function -

i have been failing understand design of hash functions. going through example. can see in function comments, why should take 31 number multiplied. how decide? random or mean something?

unsigned int hash(hash_table_t *hashtable, char *str) { unsigned int hashval; /* start our hash out @ 0 */ hashval = 0; /* each character, multiply old hash 31 , add together current * character. remember shifting number left equivalent * multiplying 2 raised number of places shifted. * in effect multiplying hashval 32 , subtracting hashval. * why this? because shifting , subtraction much more * efficient operations multiplication. */ for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval; /* homecoming hash value mod hashtable size * fit necessary range */ homecoming hashval % hashtable->size; }

the hash in question known bernstein hash, torek hash, or "times 33" hash. pretty popular due simplicity, speed, , decent distribution english string data.

your comments note multiplying 31, seemed arbitrary , is bit arbitrary. apache portable runtime has comment in hash algorithm source notes many possible constants work (33 beingness common). odd , close powerfulness of 2, meaning translate shifts , add-on or subtraction.

some other resources help understand hashing:

bob jenkins' hash function hash tables eternally confuzzled: fine art of hashing

c hashcode hash-function

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -