Navit SVN

/work/compile/navit/src/navit/intl_cmake/hash-string.h

00001 /* Description of GNU message catalog format: string hashing function.
00002    Copyright (C) 1995, 1997-1998, 2000-2003 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017    USA.  */
00018 
00019 /* @@ end of prolog @@ */
00020 
00021 /* We assume to have `unsigned long int' value with at least 32 bits.  */
00022 #define HASHWORDBITS 32
00023 
00024 
00025 /* Defines the so called `hashpjw' function by P.J. Weinberger
00026    [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
00027    1986, 1987 Bell Telephone Laboratories, Inc.]  */
00028 static unsigned long int
00029 hash_string (const char *str_param)
00030 {
00031   unsigned long int hval, g;
00032   const char *str = str_param;
00033 
00034   /* Compute the hash value for the given string.  */
00035   hval = 0;
00036   while (*str != '\0')
00037     {
00038       hval <<= 4;
00039       hval += (unsigned char) *str++;
00040       g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
00041       if (g != 0)
00042         {
00043           hval ^= g >> (HASHWORDBITS - 8);
00044           hval ^= g;
00045         }
00046     }
00047   return hval;
00048 }