-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
114 lines (102 loc) · 3.15 KB
/
hashtable.cpp
File metadata and controls
114 lines (102 loc) · 3.15 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
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
#include "hashtable.h"
#include <assert.h>
#include <stdlib.h>
// n>0 and n must be power of 2
static void h_init(HTab* htab, size_t n) {
assert(n > 0 && (n & (n - 1)) == 0);
htab->tab = (HNode**)calloc(n, sizeof(HNode*));
htab->mask = n - 1;
htab->size = 0;
}
// hashtable insertion
static void h_insert(HTab* htab, HNode* node) {
size_t pos = node->hcode & htab->mask; // hask(key) & (n-1)
// insert node at beginning of its slot (htab->tab[pos])
HNode* next = htab->tab[pos];
node->next = next;
htab->tab[pos] = node;
htab->size++;
}
// hashtable lookup
static HNode** h_lookup(HTab* htab, HNode* key, bool (*eq)(HNode*, HNode*)) {
if (!htab->tab) {
return NULL;
}
size_t pos = key->hcode & htab->mask;
HNode** from = &htab->tab[pos];
for (HNode* cur; (cur = *from) != NULL; from = &cur->next) {
// check if cur is the key node
if (cur->hcode == key->hcode && eq(cur, key)) {
// we return from and not cur, because can be useful for delete fn
return from;
}
}
return NULL;
}
// hashtable deletion
static HNode* h_detach(HTab* htab, HNode** from) {
HNode* node = *from;
*from = node->next;
htab->size--;
return node;
}
const size_t k_rehashing_work = 128; // constant work
static void hm_help_rehashing(HMap* hmap) {
size_t nwork = 0;
while (nwork < k_rehashing_work && hmap->older.size > 0) {
// start from migrate_pos
HNode** from = &hmap->older.tab[hmap->migrate_pos];
if (!*from) {
hmap->migrate_pos++;
continue;
}
// detach from old table and insert to new table
h_insert(&hmap->newer, h_detach(&hmap->older, from));
nwork++;
}
}
// hashmap rehashing
static void hm_trigger_rehashing(HMap* hmap) {
hmap->older = hmap->newer;
// double the previous size of newer
h_init(&hmap->newer, (hmap->newer.mask + 1) * 2);
hmap->migrate_pos = 0;
}
// hashmap lookup
HNode* hm_lookup(HMap* hmap, HNode* key, bool (*eq)(HNode*, HNode*)) {
// during rehashing lookup both tables
hm_help_rehashing(hmap);
HNode** from = h_lookup(&hmap->newer, key, eq);
if (!from) {
from = h_lookup(&hmap->older, key, eq);
}
return from ? *from : NULL;
}
// hashmap deletion
HNode* hm_delete(HMap* hmap, HNode* key, bool (*eq)(HNode*, HNode*)) {
hm_help_rehashing(hmap);
if (HNode** from = h_lookup(&hmap->newer, key, eq)) {
return h_detach(&hmap->newer, from);
}
if (HNode** from = h_lookup(&hmap->older, key, eq)) {
return h_detach(&hmap->older, from);
}
return NULL;
}
const size_t k_max_load_factor = 8;
// hashmap insertion
void hm_insert(HMap* hmap, HNode* node) {
if (!hmap->newer.tab) {
h_init(&hmap->newer, 4); // initialize if new table is empty
}
// always insert into new table
h_insert(&hmap->newer, node);
if (!hmap->older.tab) {
// check if we need to rehash
size_t shreshold = (hmap->newer.mask + 1) * k_max_load_factor;
if (hmap->newer.size >= shreshold) {
hm_trigger_rehashing(hmap);
}
}
hm_help_rehashing(hmap);
}