-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
133 lines (106 loc) · 3.1 KB
/
Copy pathhashtable.cpp
File metadata and controls
133 lines (106 loc) · 3.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "hashtable.h"
#include <stdlib.h>
#include <assert.h>
static const double max_lf = 0.6;
void create_table(HTab* table, size_t size) {
table->tab = (HNode**)calloc(size, sizeof(HNode*));
table->mask = size - 1;
table->size = 0;
}//use multiple of 2
struct HMap* map() {
HMap* map = new HMap();
return map;
}
struct HNode* h_lookup(HTab* table, HNode* key, bool(*eq)(HNode*, HNode*)) {
if(!table->tab) return NULL;
size_t idx = key->hashed_key & table->mask;
struct HNode* head = table->tab[idx];
while(head) {
if(key->hashed_key != head->hashed_key) {
head=head->next;
continue;
}
if(eq(key, head)) return head;
head = head->next;
}
return NULL;
}
struct HNode* h_delete(HTab* table, HNode* key, bool(*eq)(HNode*, HNode*)) {
if(!table->tab) return 0;
size_t idx = key->hashed_key & table->mask;
struct HNode** curr = &table->tab[idx];
while(*curr) {
HNode* node = *curr;
if(node->hashed_key == key->hashed_key && eq(key, node)) {
*curr = node->next;
table->size--;
return node;
}
curr = &node->next;
}
return NULL;
}
void h_insert(HTab* table, HNode* node) {
size_t pos = node->hashed_key & table->mask;
struct HNode *next = table->tab[pos];
node->next = next;
table->tab[pos] = node;
table->size++;
}
size_t work_per_cycle = 128;
void resize(HMap* map) {
if(!map->old_table.tab) {
return;
}
size_t work_done = 0;
HNode **from = map->old_table.tab + map->migration_idx;
while(work_done < work_per_cycle && map->old_table.size > 0) {
if(!*from) {
map->migration_idx++;
from = map->old_table.tab + map->migration_idx;
continue;
}
HNode* node = *from;
*from = node->next;
map->old_table.size--;
h_insert(&map->curr_table, node);
work_done++;
}
if (map->old_table.tab && map->old_table.size == 0) {
free(map->old_table.tab);
map->old_table = HTab{}; // reset to empty
map->migration_idx = 0;
}
}
void trigger_resize(HMap* map, size_t size) {
assert(map->old_table.tab == NULL);
map->old_table = map->curr_table;
create_table(&map->curr_table, size);
map->migration_idx = 0;
}
struct HNode* hm_lookup(HMap* map, HNode* node, bool (*eq)(HNode*, HNode*)) {
resize(map);
if(HNode* fetched_node = h_lookup(&map->curr_table, node, eq)) return fetched_node;
return h_lookup(&map->old_table, node, eq);
}
struct HNode* hm_delete(HMap* map, HNode* node, bool (*eq)(HNode*, HNode*)) {
if(HNode* del_node = h_delete(&map->curr_table, node, eq)) return del_node;
return h_delete(&map->old_table, node, eq);
}
struct HNode* hm_insert(HMap* map, HNode* node, bool (*eq)(HNode*, HNode*)) {
if(!map->curr_table.tab) {
create_table(&map->curr_table, 16);
}
HNode* replaced = h_delete(&map->curr_table, node, eq);
if (!replaced) {
replaced = h_delete(&map->old_table, node, eq);
}
h_insert(&map->curr_table, node);
if(!map->old_table.tab) {
size_t size = map->curr_table.size;
size_t threshold = max_lf*(map->curr_table.mask+1);
if(size >= threshold) trigger_resize(map, 2*size);
}
resize(map);
return replaced;
}