-
Notifications
You must be signed in to change notification settings - Fork 3
/
hash.h
91 lines (73 loc) · 1.98 KB
/
hash.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef HASH_H
#define HASH_H
#include <stdint.h>
#include <string.h>
#include "util.h"
static inline unsigned int hashdjb_string(const void *key)
{
unsigned int hash = 5381;
char *val = (char *) key;
while (*val != '\0') {
hash = ((hash << 5) + hash) + *val++;
}
return hash;
}
static inline unsigned int hashfnv_string(const void *key)
{
unsigned int hash = 2166136261;
char *val = (char *) key;
while (*val != '\0')
hash = (hash ^ *val++) * 16777619;
return hash;
}
static inline int compare_string(const void *e1, const void *e2)
{
return strcmp((char *) e1, (char *) e2);
}
static inline unsigned int hashdjb_uint16(const void *key)
{
unsigned int hash = 5381;
uint16_t val = PTR_TO_UINT(key);
for (unsigned int i = 0; i < 2; i++) {
hash = ((hash << 5) + hash) + ((val >> (i * 8)) & 0xff);
}
return hash;
}
static inline unsigned int hashdjb_uint32(const void *key)
{
unsigned int hash = 5381;
uint32_t val = PTR_TO_UINT(key);
for (unsigned int i = 0; i < 4; i++) {
hash = ((hash << 5) + hash) + ((val >> (i * 8)) & 0xff);
}
return hash;
}
static inline unsigned int hashfnv_uint16(const void *key)
{
unsigned int hash = 2166136261;
uint16_t val = PTR_TO_UINT(key);
for (int i = 0; i < 2; i++)
hash = (hash ^ ((val >> (8 * i)) & 0xff)) * 16777619;
return hash;
}
static inline unsigned int hashfnv_uint32(const void *key)
{
unsigned int hash = 2166136261;
uint32_t val = PTR_TO_UINT(key);
for (int i = 0; i < 4; i++)
hash = (hash ^ ((val >> (8 * i)) & 0xff)) * 16777619;
return hash;
}
static inline unsigned int hashfnv_uint64(const void *key)
{
uint64_t hash = 14695981039346656037U;
uint64_t val = PTR_TO_UINT(key);
for (int i = 0; i < 8; i++)
hash = (hash ^ ((val >> (8 * i)) & 0xff)) * 1099511628211;
return hash;
}
static inline int compare_uint(const void *e1, const void *e2)
{
return PTR_TO_UINT(e1) - PTR_TO_UINT(e2);
}
#endif