Graphviz
12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
dtstrhash.c
Go to the documentation of this file.
1
#include <assert.h>
2
#include <
cdt/dthdr.h
>
3
#include <limits.h>
4
#include <string.h>
5
6
/* Hashing a string into an unsigned integer.
7
** The basic method is to continuously accumulate bytes and multiply
8
** with some given prime. The length n of the string is added last.
9
** The recurrent equation is like this:
10
** h[k] = (h[k-1] + bytes)*prime for 0 <= k < n
11
** h[n] = (h[n-1] + n)*prime
12
** The prime is chosen to have a good distribution of 1-bits so that
13
** the multiplication will distribute the bits in the accumulator well.
14
** The below code accumulates 2 bytes at a time for speed.
15
**
16
** Written by Kiem-Phong Vo (02/28/03)
17
*/
18
19
unsigned
dtstrhash
(
void
*args,
int
n) {
20
unsigned
h = 0;
21
unsigned
char
*
s
= args;
22
23
if
(n <= 0)
24
{
for
(; *
s
!= 0;
s
+=
s
[1] ? 2 : 1)
25
h = (h + ((
unsigned
)
s
[0] << 8u) + (
unsigned
)
s
[1]) *
DT_PRIME
;
26
assert(strlen(args) <= INT_MAX);
27
n = (int)(
s
- (
unsigned
char
*)args);
28
}
29
else
30
{
unsigned
char
* ends;
31
for
(ends =
s
+n-1;
s
< ends;
s
+= 2)
32
h = (h + ((
unsigned
)
s
[0] << 8u) + (unsigned)
s
[1]) *
DT_PRIME
;
33
if
(
s
<= ends)
34
h = (h + ((unsigned)
s
[0] << 8u)) *
DT_PRIME
;
35
}
36
assert(n >= 0);
37
return
(h + (
unsigned
)n) *
DT_PRIME
;
38
}
DT_PRIME
#define DT_PRIME
Definition
cdt.h:198
dthdr.h
dtstrhash
unsigned dtstrhash(void *args, int n)
Definition
dtstrhash.c:19
s
Definition
grammar.c:93
lib
cdt
dtstrhash.c
Generated by
1.9.8