-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
220 lines (172 loc) · 6.23 KB
/
Copy pathhashtable.c
File metadata and controls
220 lines (172 loc) · 6.23 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "compiler.h"
#include "array.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_MAX_LOAD 0.75
#define table_capacity(t) ((struct HashTableHeader*)(t) - 1)->capacity
#define table_filled(t) ((struct HashTableHeader*)(t) - 1)->filled
static PreprocessorTable preproc_table_create_with_capacity(uint64_t capacity) {
PreprocessorTable t = (void*)(
(struct HashTableHeader*)malloc(sizeof(struct HashTableHeader)
+ capacity*sizeof(struct KeyValueStrView))
+ 1);
memset(t, 0, capacity*sizeof(struct KeyValueStrView));
table_capacity(t) = capacity;
table_filled(t) = 0;
return t;
}
static MacroTable macro_table_create_with_capacity(uint64_t capacity) {
MacroTable t = (void*)(
(struct HashTableHeader*)malloc(sizeof(struct HashTableHeader)
+ capacity*sizeof(struct KeyValueMacro))
+ 1);
memset(t, 0, capacity*sizeof(struct KeyValueMacro));
table_capacity(t) = capacity;
table_filled(t) = 0;
return t;
}
PreprocessorTable preproc_table_create() {
return preproc_table_create_with_capacity(16);
}
MacroTable macro_table_create() {
return macro_table_create_with_capacity(16);
}
void preproc_table_destroy(PreprocessorTable t) {
free((struct HashTableHeader*)t - 1);
}
void macro_table_destroy(MacroTable t) {
for(uint64_t i = 0; i < table_capacity(t); i++) {
struct KeyValueMacro* entry = &t[i];
if(entry->value.arg_names == NULL) continue;
array_free(entry->value.arg_names);
}
free((struct HashTableHeader*)t - 1);
}
static struct KeyValueStrView*
find_entry(PreprocessorTable t, struct string_view key)
{
uint64_t index = strview_hash(key) & (table_capacity(t)-1);
struct KeyValueStrView* tombstone = NULL;
while(true) {
struct KeyValueStrView* entry = &t[index];
if(entry->key.begin == NULL) {
if(entry->key.length == 0) { // Empty entry
return tombstone != NULL ? tombstone : entry;
} else { // Found tombstone
if(tombstone == NULL) tombstone = entry;
}
} else if(!strviewcmp(entry->key, key)) {
return entry;
}
index = (index + 1) & (table_capacity(t) - 1);
}
}
static struct KeyValueMacro*
macro_table_find_entry(MacroTable t, struct string_view key)
{
uint64_t index = strview_hash(key) & (table_capacity(t)-1);
struct KeyValueMacro* tombstone = NULL;
while(true) {
struct KeyValueMacro* entry = &t[index];
if(entry->key.begin == NULL) {
if(entry->key.length == 0) { // Empty entry
return tombstone != NULL ? tombstone : entry;
} else { // Found tombstone
if(tombstone == NULL) tombstone = entry;
}
} else if(!strviewcmp(entry->key, key)) {
return entry;
}
index = (index + 1) & (table_capacity(t) - 1);
}
}
struct string_view preproc_table_get(PreprocessorTable t,
struct string_view key) {
if(table_filled(t) == 0) return (struct string_view){.begin=NULL, .length=0};
struct KeyValueStrView *entry = find_entry(t, key);
if(entry == NULL) return (struct string_view){.begin=NULL, .length=0};
return entry->value;
}
struct Macro macro_table_get(MacroTable t, struct string_view key) {
struct Macro null_macro = { .text = {0}, .arg_names = NULL };
if(table_filled(t) == 0) return null_macro;
struct KeyValueMacro *entry = macro_table_find_entry(t, key);
if(entry == NULL) return null_macro;
return entry->value;
}
static void adjust_capacity(PreprocessorTable* t, uint64_t capacity) {
PreprocessorTable newT = preproc_table_create_with_capacity(capacity);
for(uint64_t i = 0; i < table_capacity(*t); i++) {
struct KeyValueStrView* entry = &(*t)[i];
if(entry->key.begin == NULL) continue;
struct KeyValueStrView* dst = find_entry(newT, entry->key);
dst->key = entry->key;
dst->value = entry->value;
table_filled(newT)++;
}
preproc_table_destroy(*t);
*t = newT;
}
static void macro_table_adjust_capacity(MacroTable* t, uint64_t capacity) {
MacroTable newT = macro_table_create_with_capacity(capacity);
for(uint64_t i = 0; i < table_capacity(*t); i++) {
struct KeyValueMacro* entry = &(*t)[i];
if(entry->key.begin == NULL) continue;
struct KeyValueMacro* dst = macro_table_find_entry(newT, entry->key);
dst->key = entry->key;
dst->value = macro_copy(entry->value);
table_filled(newT)++;
}
macro_table_destroy(*t);
*t = newT;
}
_Bool preproc_table_set(PreprocessorTable* t,
struct string_view key,
struct string_view value) {
if(table_filled(*t) + 1 > (double)table_capacity(*t) * TABLE_MAX_LOAD) {
uint64_t capacity = 2 * table_capacity(*t);
adjust_capacity(t, capacity);
}
struct KeyValueStrView *entry = find_entry(*t, key);
bool isNewKey = entry->key.begin == NULL;
if(isNewKey) table_filled(*t)++;
entry->key = key;
entry->value = value;
return isNewKey;
}
bool macro_table_set(MacroTable* t, struct string_view key,
struct Macro value) {
if(table_filled(*t) + 1 > (double)table_capacity(*t) * TABLE_MAX_LOAD) {
uint64_t capacity = 2 * table_capacity(*t);
macro_table_adjust_capacity(t, capacity);
}
struct KeyValueMacro *entry = macro_table_find_entry(*t, key);
bool isNewKey = entry->key.begin == NULL;
if(isNewKey) table_filled(*t)++;
if(!isNewKey && !entry->value.arg_names) array_free(entry->value.arg_names);
entry->key = key;
entry->value = macro_copy(value);
return isNewKey;
}
_Bool preproc_table_delete(PreprocessorTable* t,
struct string_view key) {
if(table_filled(*t) == 0) return false;
struct KeyValueStrView* entry = find_entry(*t, key);
if(entry->key.begin == NULL) return false;
entry->key.begin = NULL;
entry->key.length = 1; // Tombstone
entry->value = (struct string_view){ .begin = NULL, .length = 0 };
return true;
}
bool macro_table_delete(MacroTable* t, struct string_view key) {
if(table_filled(*t) == 0) return false;
struct KeyValueMacro* entry = macro_table_find_entry(*t, key);
if(entry->key.begin == NULL) return false;
entry->key.begin = NULL;
entry->key.length = 1; // Tombstone
array_free(entry->value.arg_names);
entry->value = (struct Macro){0};
return true;
}