Back to index

plt-scheme  4.2.1
hash-string.h
Go to the documentation of this file.
00001 /* hash-string - Implements a string hashing function.
00002    Copyright (C) 1995 Free Software Foundation, Inc.
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 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
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 #ifdef HAVE_VALUES_H
00019 # include <values.h>
00020 #endif
00021 
00022 /* @@ end of prolog @@ */
00023 
00024 #ifndef PARAMS
00025 # if __STDC__
00026 #  define PARAMS(Args) Args
00027 # else
00028 #  define PARAMS(Args) ()
00029 # endif
00030 #endif
00031 
00032 /* We assume to have `unsigned long int' value with at least 32 bits.  */
00033 #define HASHWORDBITS 32
00034 
00035 
00036 /* Defines the so called `hashpjw' function by P.J. Weinberger
00037    [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
00038    1986, 1987 Bell Telephone Laboratories, Inc.]  */
00039 static unsigned long hash_string PARAMS ((const char *__str_param));
00040 
00041 static inline unsigned long
00042 hash_string (str_param)
00043      const char *str_param;
00044 {
00045   unsigned long int hval, g;
00046   const char *str = str_param;
00047 
00048   /* Compute the hash value for the given string.  */
00049   hval = 0;
00050   while (*str != '\0')
00051     {
00052       hval <<= 4;
00053       hval += (unsigned long) *str++;
00054       g = hval & ((unsigned long) 0xf << (HASHWORDBITS - 4));
00055       if (g != 0)
00056        {
00057          hval ^= g >> (HASHWORDBITS - 8);
00058          hval ^= g;
00059        }
00060     }
00061   return hval;
00062 }