-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.c
59 lines (49 loc) · 818 Bytes
/
hash.c
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
/*
* Hash function
*
* File: hash.c
* Author: Andreas Behringer
*
* (c)2012 Andreas Behringer
* Copyright: GPL see included LICENSE file
*
* Created on January 10, 2013, 4:12 PM
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include "hash.h"
/**
* D.J. Bernsteins hash algorithm
*
* @param void * k
* @return unsigned int
*/
unsigned int djb2Hash(void *k) {
unsigned long h = 0;
int c;
char *key = (char *) k;
while ((c = *key++)) {
h = ((h << 5) + h) + c;
}
return h;
}
/**
* BDB Berkeley Database hash algorithm
*
* @param void * k
* @return unsigned int
*/
unsigned int sdbmHash(void *k) {
unsigned long h = 0;
int c;
char *key = (char *) k;
while ((c = *key++)) {
h = c + (h << 6) + (h << 16) - h;
}
return h;
}
#ifdef __cplusplus
}
#endif