-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
hash_string.h
48 lines (40 loc) · 1.04 KB
/
hash_string.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* STRING HASH FUNCTIONS
*
* 1. http://en.wikipedia.org/wiki/Java_hashCode()
* 2. http://www.isthe.com/chongo/tech/comp/fnv/
*
******************************************************************************/
#ifndef ALGO_STRING_HASH_H__
#define ALGO_STRING_HASH_H__
#include <stdint.h>
namespace alg {
/**
* hash a string into integer
* using java's hashCode() implementation
*/
static uint32_t hash_string(const char * str, uint32_t len) {
uint32_t hash=0;
uint32_t i;
for (i=0;i<len;i++) {
hash = 31*hash + (unsigned char)str[i];
}
return hash;
}
static uint32_t hash_fnv1a(const char * str, uint32_t len) {
uint32_t prime = 16777619U;
uint32_t hash = 2166136261U;
for (uint32_t i=0;i<len;i++) {
hash = hash ^ str[i];
hash = hash * prime;
}
return hash;
}
}
#endif //