unsigned int hash = 0; while (*s) hash = hash * 33 + *s++; #### /* MT_gen_hash_bits() * Dan Bernstein's hash function - works great for short ASCII strings! * This is an empirical thing that "just works" without mathematical * proof. Noteworthy: this is algorithm used by Perl. * http://burtleburtle.net/bob/hash/doobs.html */ unsigned int MT_gen_hash_bits (unsigned long int address) { char ascii[21]; // max 64 bit unsigned int is 20 decimal digits sprintf (ascii, "%lu", address); char *s = ascii; unsigned int hash = 0; while (*s) hash = hash * 33 + *s++; return hash; } /* MT_gen_hash_bits_version2() * Thomas Wang's 64-bit hash function - works well for integers, and is significantly * faster than the DJB function since the key is not ASCII. It is also slightly * better in distributing keys. * http://www.concentric.net/~Ttwang/tech/inthash.htm */ unsigned int MT_gen_hash_bits (unsigned long int address) { address = (~address) + (address << 21); // address = (address << 21) - address - 1; address = address ^ (address >> 24); address = (address + (address << 3)) + (address << 8); // address * 265 address = address ^ (address >> 14); address = (address + (address << 2)) + (address << 4); // address * 21 address = address ^ (address >> 28); address = address + (address << 31); return (unsigned int)address; }