]>
Commit | Line | Data |
---|---|---|
a7b65cdc | 1 | /* Implement simple hashing table with string based keys. |
2b778ceb | 2 | Copyright (C) 1994-2021 Free Software Foundation, Inc. |
a7b65cdc | 3 | This file is part of the GNU C Library. |
a7b65cdc UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
a7b65cdc | 18 | |
a3f9038c RM |
19 | #ifndef hashval_t |
20 | # define hashval_t unsigned long int | |
405550bf | 21 | #endif |
a3f9038c | 22 | #include <limits.h> /* For CHAR_BIT. */ |
405550bf | 23 | |
a3f9038c RM |
24 | hashval_t |
25 | compute_hashval (const void *key, size_t keylen) | |
a7b65cdc UD |
26 | { |
27 | size_t cnt; | |
a3f9038c | 28 | hashval_t hval; |
a7b65cdc UD |
29 | |
30 | /* Compute the hash value for the given string. The algorithm | |
31 | is taken from [Aho,Sethi,Ullman], modified to reduce the number of | |
32 | collisions for short strings with very varied bit patterns. | |
33 | See http://www.clisp.org/haible/hashfunc.html. */ | |
34 | cnt = 0; | |
35 | hval = keylen; | |
36 | while (cnt < keylen) | |
37 | { | |
a3f9038c | 38 | hval = (hval << 9) | (hval >> (sizeof hval * CHAR_BIT - 9)); |
a3a6c312 | 39 | hval += (hashval_t) ((const unsigned char *) key)[cnt++]; |
a7b65cdc | 40 | } |
a3f9038c | 41 | return hval != 0 ? hval : ~((hashval_t) 0); |
a7b65cdc | 42 | } |