freebsd-dev/contrib/gperf/lib/hash.cc

28 lines
585 B
C++
Raw Normal View History

2000-03-25 07:45:29 +00:00
/*
2000-10-13 12:04:55 +00:00
Copyright (C) 1990, 2000 Free Software Foundation
2000-03-25 07:45:29 +00:00
written by Doug Lea (dl@rocky.oswego.edu)
*/
#include <hash.h>
/*
2000-10-13 12:04:55 +00:00
Some useful hash function.
It's not a particularly good hash function (<< 5 would be better than << 4),
but people believe in it because it comes from Dragon book.
2000-03-25 07:45:29 +00:00
*/
2000-10-13 12:04:55 +00:00
unsigned int
hashpjw (const char *x, unsigned int len) // From Dragon book, p436
2000-03-25 07:45:29 +00:00
{
unsigned int h = 0;
unsigned int g;
2000-10-13 12:04:55 +00:00
for (; len > 0; len--)
{
h = (h << 4) + (unsigned char) *x++;
if ((g = h & 0xf0000000) != 0)
h = (h ^ (g >> 24)) ^ g;
}
2000-03-25 07:45:29 +00:00
return h;
}