Back to index

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