|
From: Jamie A. <Jam...@sl...> - 2002-02-12 03:35:54
|
Here's a possible replacement for the hash function in the
Dictionary class - this one is much more forgiving on strings
where the only difference is at the end (for example URLs from
db-driven sites where the only difference in the URL is a
parameter at the end). It's also very slightly better for
/usr/dict/words too.
unsigned int hashCode2(const char *key)
{
char *test;
long conv_key = strtol(key, &test, 10);
if (key && *key && !*test) // Conversion succeeded
{
return conv_key;
}
char *base = (char*)malloc( strlen( key ) +2);
char *tmp_key = base;
strcpy( tmp_key, key );
unsigned int h = 0;
int length = strlen(tmp_key);
if (length >= 16)
{
tmp_key += strlen(tmp_key) - 15;
length = strlen(tmp_key);
}
for (int i = length; i > 0; i--)
{
h = (h * 37) + *tmp_key++;
}
free( base );
return h;
}
Jamie Anstice
Search Scientist, S.L.I. Systems, Inc
jam...@sl...
ph: 64 961 3262
mobile: 64 21 264 9347
|