forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
63 lines (56 loc) · 1 KB
/
Copy pathhash.c
File metadata and controls
63 lines (56 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
author: Christian Bender
This is the implementation unit of the hash-functions.
Overview about hash-functions:
- sdbm
- djb2
- xor8 (8 bits)
- adler_32 (32 bits)
*/
long long sdbm(char s[])
{
long long hash = 0;
int i = 0;
while (s[i] != '\0')
{
hash = s[i] + (hash << 6) + (hash << 16) - hash;
i++;
}
return hash;
}
long long djb2(char s[])
{
long long hash = 5381; /* init value */
int i = 0;
while (s[i] != '\0')
{
hash = ((hash << 5) + hash) + s[i];
i++;
}
return hash;
}
char xor8(char s[])
{
int hash = 0;
int i = 0;
while (s[i] != '\0')
{
hash = (hash + s[i]) & 0xff;
i++;
}
return (((hash ^ 0xff) + 1) & 0xff);
}
int adler_32(char s[])
{
int a = 1;
int b = 0;
const int MODADLER = 65521;
int i = 0;
while (s[i] != '\0')
{
a = (a + s[i]) % MODADLER;
b = (b + a) % MODADLER;
i++;
}
return (b << 16) | a;
}