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