-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.c
More file actions
337 lines (297 loc) · 8.79 KB
/
map.c
File metadata and controls
337 lines (297 loc) · 8.79 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifdef CEE_AMALGAMATION
#undef S
#define S(f) _cee_map_##f
#else
#define S(f) _##f
#include "cee.h"
#include "musl-search.h"
#include <stdlib.h>
#include <string.h>
#endif
#include "cee-header.h"
struct S(header){
struct cee_list *insertion_ordered_keys;
int (*cmp)(const void *l, const void *r);
uintptr_t size;
union {
struct {
enum cee_del_policy key;
enum cee_del_policy val;
} _;
enum cee_del_policy a[2];
} del_policies;
enum cee_trace_action ta;
struct cee_sect cs;
void * _[1];
};
#include "cee-resize.h"
static void S(free_pair_follow)(void * ctx, void * c) {
cee_del(c);
}
static void S(trace_pair) (void * ctx, const void *nodep, const VISIT which, const int depth) {
struct cee_tuple * p;
struct S(header) * h;
switch (which)
{
case preorder:
case leaf:
p = *(void **)nodep;
cee_trace(p, *(enum cee_trace_action *)ctx);
break;
default:
break;
}
}
static void S(trace)(void * p, enum cee_trace_action ta) {
struct S(header) * h = FIND_HEADER (p);
switch (ta) {
case CEE_TRACE_DEL_NO_FOLLOW:
//cee_del(h->insertion_ordered_keys);
musl_tdestroy(NULL, h->_[0], NULL);
S(de_chain)(h);
free(h);
break;
case CEE_TRACE_DEL_FOLLOW:
cee_del(h->insertion_ordered_keys);
musl_tdestroy(&ta, h->_[0], S(free_pair_follow));
S(de_chain)(h);
free(h);
break;
case CEE_TRACE_MARK:
default:
h->cs.gc_mark = ta - CEE_TRACE_MARK;
h->ta = ta;
musl_twalk(&ta, h->_[0], S(trace_pair));
break;
}
}
static int S(cmp) (void * ctx, const void * v1, const void * v2) {
struct S(header) * h = ctx;
struct cee_tuple * t1 = (void *)v1; /* to remove const */
struct cee_tuple * t2 = (void *)v2;
return h->cmp(t1->_[0], t2->_[0]);
}
struct cee_map * cee_map_mk_e (struct cee_state * st, enum cee_del_policy o[2],
int (*cmp)(const void *, const void *)) {
size_t mem_block_size = sizeof(struct S(header));
struct S(header) * m = malloc(mem_block_size);
m->insertion_ordered_keys = cee_list_mk(st, 16);
m->cmp = cmp;
m->size = 0;
ZERO_CEE_SECT(&m->cs);
S(chain)(m, st);
m->cs.trace = S(trace);
m->cs.resize_method = CEE_RESIZE_WITH_IDENTITY;
m->cs.mem_block_size = mem_block_size;
m->cs.cmp = 0;
m->cs.cmp_stop_at_null = 0;
m->cs.n_product = 2; /* key, value */
m->del_policies._.key = o[0];
m->del_policies._.val = o[1];
m->_[0] = 0;
return (void *)m->_;
}
struct cee_map * cee_map_mk(struct cee_state * st, int (*cmp) (const void *, const void *)) {
static enum cee_del_policy d[2] = { CEE_DP_DEL_RC, CEE_DP_DEL_RC };
return cee_map_mk_e(st, d, cmp);
}
uintptr_t cee_map_size(struct cee_map * m) {
if (!m) return 0;
struct S(header) * b = FIND_HEADER(m);
return b->size;
}
void* cee_map_add_e(struct cee_map * m, void * key, void * value,
void *ctx, void* (*merge)(void *ctx, void *old_value, void *new_value)) {
if( key == NULL ) return NULL;
struct S(header) *b = FIND_HEADER(m);
struct cee_tuple *t, *t1 = NULL, **oldp;
t = cee_tuple_mk_e(b->cs.state, b->del_policies.a, key, value);
oldp = musl_tsearch(b, t, b->_, S(cmp));
if (oldp == NULL)
cee_segfault(); /* run out of memory */
else if (*oldp != t) {
t1 = *oldp;
void *old_value = t1->_[1];
void *new_value = value;
if (merge)
new_value = merge(ctx, old_value, new_value);
/* detach old_value and capture new_value */
if (new_value != old_value) {
t1->_[1] = new_value;
cee_decr_indegree(b->del_policies._.val, old_value); /* decrease the rc of old value */
}
cee_tuple_update_del_policy(t, 1, CEE_DP_NOOP); /* do nothing for t[1] */
cee_del(t);
return old_value;
}else{
cee_list_append(b->insertion_ordered_keys, t->_[0]);
b->size ++;
}
return NULL;
}
void* cee_map_add(struct cee_map * m, void * key, void * value) {
return cee_map_add_e(m, key, value, NULL, NULL);
}
void* cee_map_find(struct cee_map * m, void * key){
if( key == NULL ) return NULL;
struct S(header) * b = FIND_HEADER(m);
struct cee_tuple t = { key, 0 };
struct cee_tuple **pp = musl_tfind(b, &t, b->_, S(cmp));
if( pp == NULL )
return NULL;
else{
struct cee_tuple *p = *pp;
return p->_[1];
}
}
static void cee_map_ordered_key_delete(struct S(header) *b, void *deleted_key){
int i, s = cee_list_size(b->insertion_ordered_keys);
for( i = 0; i < s; i++ ){
char *inserted_key = b->insertion_ordered_keys->a[i];
if( b->cmp(deleted_key, inserted_key) == 0 ){
cee_list_remove(b->insertion_ordered_keys, i);
return;
}
}
cee_segfault();
}
void* cee_map_remove(struct cee_map * m, void *key){
if( key == NULL ) return NULL;
struct S(header) *b = FIND_HEADER(m);
struct cee_tuple t = { key, 0 };
struct cee_tuple **pp = musl_tfind(b, &t, b->_, S(cmp));
if( pp == NULL )
return NULL;
else{
b->size --;
struct cee_tuple *old_t = *pp;
musl_tdelete(b, &t, b->_, S(cmp));
void *old_value = old_t->_[1];
cee_decr_indegree(b->del_policies._.key, old_t->_[0]); /* decrease the rc of key */
cee_decr_indegree(b->del_policies._.val, old_t->_[1]); /* decrease the rc of val */
cee_tuple_update_del_policy(old_t, 0, CEE_DP_NOOP); /* do nothing for t[0] */
cee_tuple_update_del_policy(old_t, 1, CEE_DP_NOOP); /* do nothing for t[1] */
cee_del(old_t);
cee_map_ordered_key_delete(b, key);
return old_value;
}
}
bool cee_map_rename(struct cee_map *m, void *old_key, void *new_key){
if( old_key == NULL || new_key == NULL ) return false;
void *v = cee_map_remove(m, old_key);
if( v ){
cee_map_add(m, new_key, v);
return true;
}else
return false;
}
static void S(get_key) (void * ctx, const void *nodep, const VISIT which, const int depth) {
struct cee_tuple * p;
switch (which){
case preorder:
case leaf:
p = *(void **)nodep;
cee_list_append(ctx, p->_[0]);
break;
default:
break;
}
}
struct cee_list * cee_map_keys(struct cee_map * m) {
uintptr_t n = cee_map_size(m);
struct S(header) * b = FIND_HEADER(m);
struct cee_list *keys = cee_list_mk(b->cs.state, n);
musl_twalk(keys, b->_[0], S(get_key));
return keys;
}
struct cee_list * cee_map_insertion_ordered_keys(struct cee_map * m){
struct S(header) * b = FIND_HEADER(m);
uintptr_t n = cee_list_size(b->insertion_ordered_keys);
struct cee_list *keys = cee_list_mk(b->cs.state, n);
for( int i = 0; i < n; i++ ){
cee_list_append(keys, b->insertion_ordered_keys->a[i]);
}
return keys;
}
static void S(get_value) (void * ctx, const void *nodep, const VISIT which, const int depth){
struct cee_tuple * p;
switch (which){
case preorder:
case leaf:
p = *(void **)nodep;
cee_list_append(ctx, p->_[1]);
break;
default:
break;
}
}
struct cee_list* cee_map_values(struct cee_map *m) {
uintptr_t s = cee_map_size(m);
struct S(header) *b = FIND_HEADER(m);
struct cee_list *values = cee_list_mk(b->cs.state, s);
musl_twalk(values, b->_[0], S(get_value));
return values;
}
/*
* internal structure for cee_map_iterate
*/
struct S(fn_ctx) {
void *ctx;
int (*f)(void *ctx, void *key, void *value);
int ret;
};
/*
* helper function for cee_map_iterate
*/
static void S(apply_each) (void *ctx, const void *nodep, const VISIT which, const int depth){
struct cee_tuple *p;
struct S(fn_ctx) * fn_ctx_p = ctx;
if (fn_ctx_p->ret)
/* the previous iteration has an error, skip the rest iterations */
return;
switch(which){
case preorder:
case leaf:
p = *(void **)nodep;
fn_ctx_p->ret = fn_ctx_p->f(fn_ctx_p->ctx, p->_[0], p->_[1]);
break;
default:
break;
}
}
/*
* iterate
*/
int cee_map_iterate(struct cee_map *m, void *ctx,
int (*f)(void *ctx, void *key, void *value)){
if (!m) return 0;
struct S(header) *b = FIND_HEADER(m);
struct S(fn_ctx) fn_ctx = { .ctx = ctx, .f = f, .ret = 0 };
musl_twalk(&fn_ctx, b->_[0], S(apply_each));
return fn_ctx.ret;
}
struct S(merge_ctx){
struct cee_map *dest_map;
void *merge_ctx;
void* (*merge)(void *ctx, void *old_value, void *new_value);
};
static int S(_add_kv)(void *ctx, void *key, void *value){
struct S(merge_ctx) *mctx = ctx;
cee_map_add_e(mctx->dest_map, key, value, mctx->merge_ctx, mctx->merge);
return 0;
}
/*
* add all key/value pairs of the src to the dest
* and keep the src intact.
*/
void cee_map_merge(struct cee_map *dest, struct cee_map *src,
void *ctx, void* (*merge)(void *ctx, void *old, void *new)){
struct S(merge_ctx) mctx = { .dest_map = dest, .merge_ctx = ctx, .merge = merge };
cee_map_iterate(src, &mctx, S(_add_kv));
}
struct cee_map* cee_map_clone(struct cee_map *src){
struct S(header) *m = FIND_HEADER(src);
struct cee_map *new_map = cee_map_mk_e(cee_get_state(src), m->del_policies.a, m->cmp);
cee_map_merge(new_map, src, NULL, NULL);
return new_map;
}