Mercurial > hg > octave-kai > gnulib-hg
annotate lib/hash-pjw.c @ 17342:c75939cb6254
merge with default branch
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Thu, 21 Feb 2013 14:57:31 +0000 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
3541 | 1 /* hash-pjw.c -- compute a hash value from a NUL-terminated string. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
3 Copyright (C) 2001, 2003, 2006, 2009-2013 Free Software Foundation, Inc. |
3541 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
3541 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 (at your option) any later version. |
3541 | 9 |
10 This program 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 | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
3541 | 17 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
18 #include <config.h> |
3541 | 19 |
20 #include "hash-pjw.h" | |
21 | |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
22 #include <limits.h> |
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
23 |
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
24 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT) |
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
25 |
3541 | 26 /* A hash function for NUL-terminated char* strings using |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
27 the method described by Bruno Haible. |
4177
6a34d03dc4e3
Add comment about low quality of this function.
Bruno Haible <bruno@clisp.org>
parents:
3541
diff
changeset
|
28 See http://www.haible.de/bruno/hashfunc.html. */ |
3541 | 29 |
14612
6ef4f1f39105
Revert "use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE"
Jim Meyering <meyering@redhat.com>
parents:
14610
diff
changeset
|
30 size_t |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
31 hash_pjw (const void *x, size_t tablesize) |
3541 | 32 { |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
33 const char *s; |
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
34 size_t h = 0; |
3541 | 35 |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
36 for (s = x; *s; s++) |
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
37 h = *s + ((h << 9) | (h >> (SIZE_BITS - 9))); |
3541 | 38 |
4813
04eb43f18dc7
Fix several address-calculation bugs in the hash modules,
Paul Eggert <eggert@cs.ucla.edu>
parents:
4177
diff
changeset
|
39 return h % tablesize; |
3541 | 40 } |