forked from JoshParnell/libphx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrMap.cpp
242 lines (208 loc) · 5.01 KB
/
StrMap.cpp
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
#include "Hash.h"
#include "PhxMemory.h"
#include "StrMap.h"
#include "PhxString.h"
/* TODO : More rigorous tests for this structure, which has been the victim
* of some pretty serious implementation bugs in the past. */
struct Node {
cstr key;
Node* next;
void* value;
};
struct StrMap {
uint32 capacity;
uint32 size;
Node* data;
};
struct StrMapIter {
StrMap* map;
Node* node;
uint32 slot;
};
inline static uint64 Hash (cstr key) {
return Hash_XX64(key, StrLen(key), 0x0ULL);
}
inline static Node* StrMap_GetBucket (StrMap* self, cstr key) {
return self->data + (Hash(key) % self->capacity);
}
static void StrMap_Grow (StrMap* self) {
StrMap newMap = { self->capacity * 2, 0, 0 };
newMap.data = MemNewArrayZero(Node, newMap.capacity);
for (uint32 i = 0; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key)
continue;
StrMap_Set(&newMap, node->key, node->value);
StrFree(node->key);
node = node->next;
while (node) {
Node* next = node->next;
StrMap_Set(&newMap, node->key, node->value);
StrFree(node->key);
MemFree(node);
node = next;
}
}
MemFree(self->data);
*self = newMap;
}
StrMap* StrMap_Create (uint32 capacity) {
StrMap* self = MemNewZero(StrMap);
self->capacity = capacity;
self->data = MemNewArrayZero(Node, capacity);
return self;
}
void StrMap_Free (StrMap* self) {
for (uint32 i = 0; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key) continue;
StrFree(node->key);
node = node->next;
while (node) {
Node* next = node->next;
StrFree(node->key);
MemFree(node);
node = next;
}
}
MemFree(self->data);
MemFree(self);
}
void StrMap_FreeEx (StrMap* self, void (*freeFn)(cstr, void*)) {
for (uint32 i = 0; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key) continue;
freeFn(node->key, node->value);
StrFree(node->key);
node = node->next;
while (node) {
Node* next = node->next;
freeFn(node->key, node->value);
StrFree(node->key);
MemFree(node);
node = next;
}
}
MemFree(self->data);
MemFree(self);
}
void* StrMap_Get (StrMap* self, cstr key) {
Node* node = StrMap_GetBucket(self, key);
if (!node->key)
return 0;
while (node) {
if (StrEqual(node->key, key))
return node->value;
node = node->next;
}
return 0;
}
uint32 StrMap_GetSize (StrMap* self) {
return self->size;
}
void StrMap_Remove (StrMap* self, cstr key) {
Node** prev = 0;
Node* node = StrMap_GetBucket(self, key);
while (node && node->key) {
if (StrEqual(node->key, key)) {
StrFree(node->key);
Node* next = node->next;
if (next) {
node->key = next->key;
node->next = next->next;
node->value = next->value;
MemFree(next);
} else {
node->key = 0;
node->value = 0;
}
if (prev)
*prev = next;
return;
}
prev = &node->next;
node = node->next;
}
Fatal("StrMap_Remove: Map does not contain key <%s>", key);
}
void StrMap_Set (StrMap* self, cstr key, void* value) {
self->size++;
if (3 * self->capacity < 4 * self->size)
StrMap_Grow(self);
Node* node = StrMap_GetBucket(self, key);
if (!node->key) {
node->key = StrDup(key);
node->value = value;
return;
}
Node** prev = 0;
while (node) {
if (StrEqual(node->key, key)) {
node->value = value;
return;
}
prev = &node->next;
node = node->next;
}
node = MemNew(Node);
node->key = StrDup(key);
node->value = value;
node->next = 0;
*prev = node;
}
#include <stdio.h>
void StrMap_Dump (StrMap* self) {
printf("StrMap @ %p:\n", self);
printf(" size: %d\n", self->size);
printf(" capacity: %d\n", self->capacity);
printf(" load: %f\n", (float)self->size / (float)self->capacity);
puts("");
for (uint32 i = 0; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key) continue;
printf(" [%03i]:\n", i);
while (node) {
printf(" (%lx) %s -> %p\n", Hash(node->key), node->key, node->value);
node = node->next;
}
}
}
StrMapIter* StrMap_Iterate (StrMap* self) {
StrMapIter* it = MemNew(StrMapIter);
it->map = self;
it->slot = 0;
it->node = 0;
for (uint32 i = 0; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key) continue;
it->slot = i;
it->node = node;
break;
}
return it;
}
void StrMapIter_Free (StrMapIter* self) {
MemFree(self);
}
void StrMapIter_Advance (StrMapIter* it) {
StrMap* self = it->map;
it->node = it->node->next;
if (it->node) return;
it->slot++;
for (uint32 i = it->slot; i < self->capacity; ++i) {
Node* node = self->data + i;
if (!node->key) continue;
it->slot = i;
it->node = node;
break;
}
}
bool StrMapIter_HasMore (StrMapIter* it) {
return it->node != 0;
}
cstr StrMapIter_GetKey (StrMapIter* it) {
return it->node->key;
}
void* StrMapIter_GetValue (StrMapIter* it) {
return it->node->value;
}