File: hash.cc

package info (click to toggle)
gperf 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,924 kB
  • sloc: exp: 6,462; cpp: 5,795; perl: 2,118; sh: 1,007; ansic: 875; makefile: 669
file content (27 lines) | stat: -rw-r--r-- 584 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* 
Copyright (C) 1990, 2000, 2002 Free Software Foundation
    written by Doug Lea <dl@rocky.oswego.edu>
*/

#include <hash.h>

/*
 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.
*/

unsigned int
hashpjw (const unsigned char *x, unsigned int len) // From Dragon book, p436
{
  unsigned int h = 0;
  unsigned int g;

  for (; len > 0; len--)
    {
      h = (h << 4) + *x++;
      if ((g = h & 0xf0000000) != 0)
        h = (h ^ (g >> 24)) ^ g;
    }
  return h;
}