-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashtable.c
124 lines (107 loc) · 2.57 KB
/
hashtable.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <string.h>
#include <stdlib.h>
#include "hashtable.h"
static uint32_t hashfunc(const char *key)
{
//murmur3_32
static const uint32_t c1 = 0xcc9e2d51;
static const uint32_t c2 = 0x1b873593;
static const uint32_t r1 = 15;
static const uint32_t r2 = 13;
static const uint32_t m = 5;
static const uint32_t n = 0xe6546b64;
uint32_t len = strlen(key);
uint32_t hash = 0;
const int nblocks = len / 4;
const uint32_t *blocks = (const uint32_t *) key;
int i;
for (i = 0; i < nblocks; i++) {
uint32_t k = blocks[i];
k *= c1;
k = (k << r1) | (k >> (32 - r1));
k *= c2;
hash ^= k;
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
}
const uint8_t *tail = (const uint8_t *) (key + nblocks * 4);
uint32_t k1 = 0;
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
case 2:
k1 ^= tail[1] << 8;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << r1) | (k1 >> (32 - r1));
k1 *= c2;
hash ^= k1;
}
hash ^= len;
hash ^= (hash >> 16);
hash *= 0x85ebca6b;
hash ^= (hash >> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >> 16);
return hash;
}
void ht_init(struct hashtable *h, size_t size)
{
h->count = 0;
h->size = size;
h->bucket = calloc(sizeof(struct hashnode), h->size);
}
void * ht_lookup(struct hashtable *h, const char *name)
{
size_t start = hashfunc(name);
size_t idx = (start + 1) % h->size;
for(; idx != start; idx = (idx + 1) % h->size){
if(h->bucket[idx].name){
if(!strcmp(h->bucket[idx].name, name)){
return h->bucket[idx].val;
}
}else{
break;
}
}
return NULL;
}
static void resize(struct hashtable *h)
{
struct hashtable newht;
ht_init(&newht, h->size*2 +1);
size_t i;
for(i = 0; i != h->size; i++){
if(h->bucket[i].name){
ht_put(&newht, h->bucket[i].name, h->bucket[i].val);
}
}
free(h->bucket);
h->bucket = newht.bucket;
h->size = newht.size;
h->count = newht.count;
}
bool ht_put(struct hashtable *h, const char *name, void *val)
{
if (ht_lookup(h, name) != NULL) {
return false;
}
size_t start = hashfunc(name);
size_t idx = (start + 1) % h->size;
for(; /*idx != start*/; idx = (idx + 1) % h->size){
if(!h->bucket[idx].name){
h->bucket[idx].name = name;
h->bucket[idx].val = val;
break;
}
}
h->count++;
if(h->count*3/2 > h->size){
resize(h);
}
return true;
}
void ht_clear(struct hashtable *h) {
memset(h->bucket, 0, h->size*sizeof(struct hashnode));
h->count = 0;
}