-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.c
246 lines (205 loc) · 6.5 KB
/
table.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "memory.h"
#include "table.h"
#include "vm.h"
void initTable(Table* table) {
table->count = 0;
table->capacity = 0;
table->entries = NULL;
}
void freeTable(Table* table) {
FREE_ARRAY(Entry, table->entries, table->capacity);
initTable(table);
}
static Entry* findEntry(Entry* entries, int capacity, Value key) {
uint32_t index = hashValue(key) & (capacity - 1);
Entry* tombstone = NULL;
Entry* entry;
for (;;) {
entry = &entries[index];
if (IS_EMPTY(entry->key)) {
if (IS_NIL(entry->value))
return tombstone != NULL ? tombstone : entry; // Empty entry
else {
// We found a tombstone
if (tombstone == NULL)
tombstone = entry;
}
} else if (valuesEqual(key, entry->key))
return entry; //We found the key
index = (index + 1) & (capacity - 1);
}
return NULL; // not reached
}
bool tableGet(Table* table, Value key, Value* value) {
Entry* entry;
if (table->count == 0)
return false;
entry = findEntry(table->entries, table->capacity, key);
if (IS_EMPTY(entry->key))
return false;
*value = entry->value;
return true;
}
static void adjustCapacity(Table* table, int capacity) {
Entry* entries = ALLOCATE(Entry, capacity);
Entry* entry;
Entry* dest;
int16_t i;
for (i = 0; i < capacity; i++) {
entries[i].key = EMPTY_VAL;
entries[i].value = NIL_VAL;
}
table->count = 0;
for (i = 0; i < table->capacity; i++) {
entry = &table->entries[i];
if (IS_EMPTY(entry->key))
continue;
dest = findEntry(entries, capacity, entry->key);
dest->key = entry->key;
dest->value = entry->value;
table->count++;
}
FREE_ARRAY(Entry, table->entries, table->capacity);
table->entries = entries;
table->capacity = capacity;
}
bool tableSet(Table* table, Value key, Value value) {
Entry* entry;
bool isNewKey;
int16_t capacity = table->capacity;
// Grow when load factor exceeds 0.75
if (table->count + 1 > ((capacity + capacity + capacity) >> 2)) {
capacity = GROW_CAPACITY(table->capacity);
adjustCapacity(table, capacity);
}
entry = findEntry(table->entries, table->capacity, key);
isNewKey = IS_EMPTY(entry->key);
if (isNewKey && IS_NIL(entry->value))
table->count++;
entry->key = key;
entry->value = value;
return isNewKey;
}
bool tableDelete(Table* table, Value key) {
Entry* entry;
if (table->count == 0)
return false;
entry = findEntry(table->entries, table->capacity, key);
if (IS_EMPTY(entry->key))
return false;
// Place a tombstone in the entry.
entry->key = EMPTY_VAL;
entry->value = BOOL_VAL(true);
return true;
}
void tableAddAll(Table* from, Table* to) {
int16_t i;
Entry* entry;
for (i = 0; i < from->capacity; i++) {
entry = &from->entries[i];
if (!IS_EMPTY(entry->key))
tableSet(to, entry->key, entry->value);
}
}
void tableShrink(Table* table) {
int16_t num_entries = 0;
int16_t i, capacity;
for (i = 0; i < table->capacity; i++)
if (!IS_EMPTY(table->entries[i].key))
num_entries++;
// Find optimal capacity for load factor 0.75
for (capacity = 8; num_entries > ((capacity + capacity + capacity) >> 2); capacity <<= 1)
;
if (capacity < table->capacity) {
if (vm.debug_log_gc & DBG_GC_STRINGS)
printf("GC shrink strings from %d to %d\n", table->capacity, capacity);
adjustCapacity(table, capacity);
}
}
ObjString* tableFindString(Table* table, const char* chars, int length, uint32_t hash) {
uint32_t index;
Entry* entry;
ObjString* string;
if (table->count == 0)
return NULL;
index = hash & (table->capacity - 1);
for (;;) {
entry = &table->entries[index];
if (IS_EMPTY(entry->key)) {
// Stop if we find an empty non-tombstone entry.
if (IS_NIL(entry->value))
return NULL;
} else {
string = AS_STRING(entry->key); // keys will always be strings here!
if (string->length == length &&
string->hash == hash &&
fix_memcmp(string->chars, chars, length) == 0) {
// we found duplicate string
return string;
}
}
index = (index + 1) & (table->capacity - 1);
}
return NULL; // not reached
}
void tableRemoveWhite(Table* table) {
int16_t i;
Entry* entry;
for (i = 0; i < table->capacity; i++) {
entry = &table->entries[i];
// keys either empty or ObjString*
if (!IS_EMPTY(entry->key) && !(AS_OBJ(entry->key))->isMarked)
tableDelete(table, entry->key);
}
}
void markTable(Table* table) {
int16_t i;
Entry* entry;
for (i = 0; i < table->capacity; i++) {
entry = &table->entries[i];
markValue(entry->key);
markValue(entry->value);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Hashtable iterators
////////////////////////////////////////////////////////////////////////////////////////////////////
int firstIterator(Table* table) {
int i;
if (table->count > 0)
for (i = 0; i < table->capacity; i++)
if (!IS_EMPTY(table->entries[i].key))
return i;
return -1;
}
void nextIterator(ObjIterator* iter) {
int16_t i;
Table* table = iter->table;
if (iter->position >= 0) {
for (i = iter->position + 1; i < table->capacity; i++)
if (!IS_EMPTY(table->entries[i].key)) {
iter->position = i;
return;
}
}
iter->position = -1;
}
bool isValidIterator(ObjIterator* iter) {
bool valid = iter->position >= 0 &&
iter->position < iter->table->capacity &&
!IS_EMPTY(iter->table->entries[iter->position].key);
return valid;
}
Value getIterator(ObjIterator* iter, bool wantKey) {
// Valid iterator has already been checked
Entry* entry = &iter->table->entries[iter->position];
return wantKey ? entry->key : entry->value;
}
void setIterator(ObjIterator* iter, Value value) {
// Valid iterator has already been checked
Entry* entry = &iter->table->entries[iter->position];
entry->value = value;
}