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