Andy's insights

Opinions, thoughts, archivements

Sat, 30 Sep 2006

String Hash Function

I played a bit with string hashing functions for application like dictionaries. The result was a quite simple function with not too bad results: few collisions.


uint32_t
computeHash(const char *name, size_t namelen)
{ #define CONSIDER 32 uint32_t value = 0; int i;

if (name == NULL) return 0; /* take the first bytes of the string as initial value */ memcpy(&value, name, MIN)); /* include the length in start value somehow */ value ^= namelen & 0xaaaa; value ^= (namelen & 0×5555) << 16; for (i = sizeof(value); i < MIN; i++) value ^= name[i] + (name[i] << (i & 0×0f)); return value; }

posted at: 18:11 | path: /development | permanent link to this entry