-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaa_map.c
More file actions
733 lines (549 loc) · 16.7 KB
/
aa_map.c
File metadata and controls
733 lines (549 loc) · 16.7 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "aa_map.h"
#include <assert.h>
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#define safecall(v, f, ...) ((f) ? (f(__VA_ARGS__)) : (v))
#define unit {}
typedef enum { L = 0, R = 1 } lr;
struct aa_node;
struct aa_tree {
struct aa_node *root;
size_t itemsize;
int (*cmp)(void*,void*);
void (*cpy)(void*,void*);
void *allocator;
void *(*alloc)(void*,size_t);
void (*dealloc)(void*,void*);
};
const size_t aa_tree_size = sizeof(struct aa_tree);
struct aa_node {
size_t level;
struct aa_node *child[2];
uint8_t data[0];
};
static struct aa_node aa_node_bottom = { .level = 0
, .child = { &aa_node_bottom
, &aa_node_bottom
}
};
static struct aa_node *aa_node_null = &aa_node_bottom;
const size_t aa_node_size = sizeof(struct aa_node);
static inline void *aa_value( struct aa_node *n ) {
return (void*)n->data;
}
static inline struct aa_node* l(struct aa_node *n) {
return n->child[L];
}
static inline struct aa_node* r(struct aa_node *n) {
return n->child[R];
}
static inline bool aa_nil( struct aa_node *n ) {
return !n || n == aa_node_null;
}
static inline bool aa_leaf( struct aa_node *n ) {
return aa_nil(l(n)) && aa_nil(r(n));
}
struct aa_tree *aa_tree_create( size_t memsize
, void *mem
, size_t itemsize
, int (*cmp)(void*,void*)
, void (*cpy)(void*,void*)
, void *allocator
, void *(*alloc)(void*,size_t)
, void (*dealloc)(void*,void*) ) {
if( memsize < aa_tree_size ) {
return 0;
}
struct aa_tree *t = mem;
t->root = aa_node_null;
t->itemsize = itemsize;
t->cmp = cmp;
t->cpy = cpy;
t->allocator = allocator;
t->alloc = alloc;
t->dealloc = dealloc;
return t;
}
static inline size_t dir_of(int cmp) {
return cmp < 0 ? L : R;
}
static void aa_node_destroy( struct aa_tree *t
, struct aa_node *v
, void *cc
, void (*fn)(void*,void*) ) {
if( aa_nil(v) ) {
return;
}
aa_node_destroy(t, l(v), cc, fn);
aa_node_destroy(t, r(v), cc, fn);
safecall(unit, fn, cc, aa_value(v));
t->dealloc(t->allocator, v);
}
static inline struct aa_node* aa_skew(struct aa_node *n) {
if( aa_nil(n) ) {
return n;
}
if( n->child[L]->level == n->level && n->level ) {
struct aa_node *tmp = n->child[L];
n->child[L] = tmp->child[R];
tmp->child[R] = n;
n = tmp;
}
return n;
}
static inline struct aa_node* aa_split(struct aa_node *n)
{
if( aa_nil(n) ) {
return n;
}
if(n->child[R]->child[R]->level == n->level && n->level != 0) {
struct aa_node *tmp = n->child[R];
n->child[R] = tmp->child[L];
tmp->child[L] = n;
n = tmp;
n->level++;
}
return n;
}
static struct aa_node *aa_node_init( struct aa_tree *t
, struct aa_node *n
, void *v) {
if( !n ) {
return n;
}
*n = aa_node_bottom;
n->level = 1;
n->child[L] = n->child[R] = aa_node_null;
t->cpy(&n->data[0], v);
return n;
}
static struct aa_node *aa_node_create(struct aa_tree *t, void *v) {
struct aa_node *n = t->alloc(t->allocator, aa_node_size + t->itemsize);
if( !n ) {
return 0;
}
return aa_node_init(t, n, v);
}
static struct aa_node *aa_node_insert( struct aa_tree *t
, struct aa_node *to
, void *v
, void *cc
, void (*fn)(void*,void*,bool)
) {
if( to == aa_node_null ) {
struct aa_node *n = aa_node_create(t, v);
safecall(unit, fn, cc, aa_value(n), true);
return n;
}
int cmp = t->cmp(v, aa_value(to));
if( !cmp ) {
safecall(unit, fn, cc, aa_value(to), false);
return to;
}
const lr dir = dir_of(cmp);
to->child[dir] = aa_node_insert(t, to->child[dir], v, cc, fn);
return aa_split(aa_skew(to));
}
static struct aa_node* aa_node_remove( struct aa_tree *t
, struct aa_node *n
, void *v
, void *cc
, void (*fn)(void*,void*)
, struct aa_node **last
, struct aa_node **deleted
) {
if( aa_nil(n) ) {
return n;
}
*last = n;
int cmp = t->cmp(v, aa_value(n));
lr dir = dir_of(cmp);
if( dir == R ) {
*deleted = n;
}
n->child[dir] = aa_node_remove(t, n->child[dir], v, cc, fn, last, deleted);
if( n == *last && !aa_nil(*deleted) && !t->cmp(v, aa_value(*deleted) ) ) {
// we're about to delete the value??
t->cpy((*deleted)->data, n->data);
*deleted = aa_node_null;
n = r(n);
safecall(unit, fn, cc, aa_value(*last));
t->dealloc(t->allocator, (*last));
} else if( l(n)->level < n->level-1 || r(n)->level < n->level-1) {
//go up a level and rebalance?
n->level--;
if( r(n)->level > n->level ) {
r(n)->level = n->level;
}
n = aa_skew(n);
n->child[R] = aa_skew( r(n) );
n->child[R]->child[R] = aa_skew( r(r(n)) );
n = aa_split(n);
n->child[R] = aa_split(r(n));
}
return n;
}
static void aa_node_walk( struct aa_node *p
, struct aa_node *n
, void *cc
, void (*fn)( void*
, struct aa_node *
, struct aa_node *)) {
if( aa_nil(n) ) {
return;
}
aa_node_walk(n, n->child[L], cc, fn);
fn(cc, p, n);
aa_node_walk(n, n->child[R], cc, fn);
}
static struct aa_node *aa_node_find( struct aa_tree *t
, void *v
, struct aa_node *n
, struct aa_node **p ) {
if( aa_nil(n) ) {
return 0;
}
if( !t->cmp( aa_value(n), v ) ) {
return n;
}
*p = n;
return aa_node_find(t,v,n->child[dir_of(t->cmp(v,aa_value(n)))],p);
}
void *aa_tree_find( struct aa_tree *t, void *v ) {
struct aa_node *p = 0;
struct aa_node *n = aa_node_find(t, v, t->root, &p);
return n ? aa_value(n) : 0;
}
void aa_tree_remove_with( struct aa_tree *t
, void *v
, void *cc
, void (*fn)(void*,void*)) {
struct aa_node *l = 0, *d = 0;
t->root = aa_node_remove(t, t->root, v, cc, fn, &l, &d);
}
void aa_tree_remove(struct aa_tree *t, void *v) {
aa_tree_remove_with(t, v, 0, 0);
}
static bool aa_tree_alter (struct aa_tree *t
, void *v
, void *cc
, void (*fn)(void*,void*,bool)) {
struct aa_node *n = aa_node_insert(t, t->root, v, cc, fn);
if( !n ) {
return false;
}
t->root = n;
return true;
}
static void aa_tree_insert_fn(void *c, void *v, bool n) {
*(bool*)c = n;
}
bool aa_tree_insert(struct aa_tree *t, void *v) {
bool n = false;
aa_tree_alter(t,v,&n,aa_tree_insert_fn);
return n;
}
void aa_tree_destroy(struct aa_tree *t, void *cc, void (*fn)(void*,void*)) {
aa_node_destroy(t, t->root, cc, fn);
t->root = aa_node_null;
}
struct aa_tree_enum_cc {
void *cc;
void (*fn)(void*,void*);
};
static void __aa_tree_walk_fn0( void *cc_
, struct aa_node *p
, struct aa_node *n) {
struct aa_tree_enum_cc *cc = cc_;
cc->fn(cc->cc, aa_value(n));
}
void aa_tree_enum( struct aa_tree *t
, void *cc_
, void (*fn)(void*, void*)) {
struct aa_tree_enum_cc cc = { .cc = cc_
, .fn = fn
};
aa_node_walk(aa_node_null, t->root, &cc, __aa_tree_walk_fn0);
}
struct aa_tree_walk_debug_cc {
void *cc;
void (*fn)(void*,struct aa_node_info*,struct aa_node_info*);
};
static void __aa_tree_walk_debug_fn( void *cc_
, struct aa_node *p
, struct aa_node *n) {
struct aa_tree_walk_debug_cc *cc = cc_;
struct aa_node_info prev = { .level = !aa_nil(p) ? p->level : 0
, .value = !aa_nil(p) ? aa_value(p) : 0
};
struct aa_node_info curr = { .level = !aa_nil(n) ? n->level : 0
, .value = !aa_nil(n) ? aa_value(n) : 0
};
cc->fn(cc->cc, &prev, &curr);
}
void aa_tree_enum_debug( struct aa_tree *t
, void *cc_
, void (*fn)( void*
, struct aa_node_info *p
, struct aa_node_info *n ) ) {
struct aa_tree_walk_debug_cc cc = { .cc = cc_
, .fn = fn
};
aa_node_walk(aa_node_null, t->root, &cc, __aa_tree_walk_debug_fn);
}
// aa map
struct aa_map;
struct aa_map {
size_t keysize;
size_t valsize;
bool bigkey;
bool bigval;
int (*keycmp)(void*,void*);
void (*keycpy)(void*,void*);
void (*valcpy)(void*,void*);
void *allocator;
void *(*alloc)(void*,size_t);
void (*dealloc)(void*,void*);
struct aa_tree *t;
char aa_tree_mem[sizeof(struct aa_tree)];
};
const size_t aa_map_size = sizeof(struct aa_map);
struct cell {
struct aa_map *m;
void *k;
void *v;
};
static const size_t ptr_size = sizeof(void*);
static inline void __dealloc(struct aa_map *m, void *mem) {
if( mem ) {
m->dealloc(m->allocator, mem);
}
}
static inline void *cell_key(struct cell *c) {
if( !c->m->bigkey ) {
return &c->k;
} else {
return c->k;
}
return 0;
}
static inline void *cell_val(struct cell *c) {
if( !c->m->bigval ) {
return &c->v;
} else {
return c->v;
}
return 0;
}
static int cell_key_cmp(void *a, void *b) {
struct cell *ca = a;
return ca->m->keycmp(cell_key(a), cell_key(b));
}
static void cell_copy(void *a, void *b) {
*(struct cell*)a = *(struct cell*)b;
}
static void cell_cleanup(void *cc, void *v) {
struct aa_map *m = cc;
struct cell *cell = v;
if( m->bigkey ) {
__dealloc(m, cell->k);
}
if( m->bigval ) {
__dealloc(m, cell->v);
}
}
void aa_map_destroy(struct aa_map *m) {
aa_tree_destroy(m->t, m, cell_cleanup);
}
struct aa_map *aa_map_create( size_t memsize
, void *mem
, size_t keysize
, size_t valsize
, int cmp(void*,void*) // compare KEYS!
, void (*keycpy)(void*,void*)
, void (*valcpy)(void*,void*)
, void *allocator
, void *(*alloc)(void*,size_t)
, void (*dealloc)(void*,void*) ) {
if(memsize < aa_map_size) {
return 0;
}
struct aa_map *m = mem;
m->keysize = keysize;
m->valsize = valsize;
m->bigkey = m->keysize > ptr_size;
m->bigval = m->valsize > ptr_size;
m->keycmp = cmp;
m->keycpy = keycpy;
m->valcpy = valcpy;
m->allocator = allocator;
m->alloc = alloc;
m->dealloc = dealloc;
// FIXME
m->t = aa_tree_create( sizeof(m->aa_tree_mem)
, m->aa_tree_mem
, sizeof(struct cell)
, cell_key_cmp
, cell_copy
, allocator
, alloc
, dealloc );
if( !m->t ) {
return 0;
}
return m;
}
static const uint64_t aa_map_nocopy_value_mem = 0x0101010101010101;
static void *aa_map_nocopy_value = (void*)&aa_map_nocopy_value_mem;
static inline struct cell* cell_init( struct aa_map *m
, struct cell *cell
, void *k
, void *v) {
const struct cell dummy = { .m = m, .k = 0, .v = 0 };
*cell = dummy;
if( !v ) {
if( !m->bigkey ) {
m->keycpy(cell_key(cell), (void*)k);
} else {
cell->k = k;
}
return cell;
}
void *kmem = 0, *vmem = 0;
do {
if( m->bigkey ) {
kmem = cell->k = m->alloc(m->allocator, m->keysize);
if( !kmem ) {
break;
}
}
if( m->bigval ) {
vmem = cell->v = m->alloc(m->allocator, m->valsize);
if( !vmem ) {
break;
}
}
m->keycpy(cell_key(cell), k);
if( v != aa_map_nocopy_value && m->valcpy ) {
m->valcpy(cell_val(cell), (void*)v);
}
return cell;
} while(0);
__dealloc(m, kmem);
__dealloc(m, vmem);
return 0;
}
bool aa_map_add(struct aa_map *m, void *k, void *v) {
if( !m->valcpy ) {
return false;
}
struct cell cell;
struct cell *cp = cell_init(m, &cell, k, v);
if( !cp ) {
return false;
}
if( !aa_tree_insert(m->t, &cell) ) {
cell_cleanup(m, &cell);
return false;
}
return true;
}
void *aa_map_find(struct aa_map *m, void *k) {
struct cell cell;
struct cell *r = aa_tree_find(m->t, cell_init(m, &cell, k, 0));
return r ? cell_val(r) : 0;
}
void aa_map_del(struct aa_map *m, void *k) {
struct cell cell;
aa_tree_remove_with(m->t, cell_init(m, &cell, k, 0), m, cell_cleanup);
}
struct aa_enum_fn_cc {
void *cc;
void (*fn)(void*,void*,void*);
};
static void __aa_enum_fn(void *cc_, void *v) {
struct aa_enum_fn_cc *cc = cc_;
cc->fn(cc->cc, cell_key(v), cell_val(v));
}
void aa_map_enum(struct aa_map *m, void *cc, void (*fn)(void*,void*,void*)) {
// FIXME
struct aa_enum_fn_cc rcc = { .cc = cc, .fn = fn };
aa_tree_enum(m->t, &rcc, __aa_enum_fn);
}
struct aa_map_alter_cc {
bool n;
void *cc;
void (*fn)(void*,void*,void*,bool);
};
static void aa_map_alter_fn(void *cc, void *v, bool n) {
struct aa_map_alter_cc *acc = cc;
acc->n = n;
acc->fn(acc->cc, cell_key(v), cell_val(v), n);
}
bool aa_map_alter( struct aa_map *m
, bool create
, void *k
, void *cc
, void (*fn)( void* // cc
, void* // k
, void* // v
, bool)) {
struct aa_map_alter_cc acc = { .cc = cc, .fn = fn, .n = false };
struct cell cell;
if( create ) {
struct cell *cp = cell_init(m, &cell, k, aa_map_nocopy_value);
bool r = aa_tree_alter(m->t, cp, &acc, aa_map_alter_fn);
if( !acc.n ) {
cell_cleanup(m, cp);
}
return r;
} else {
struct cell *cp = cell_init(m, &cell, k, 0);
struct aa_node *p = 0;
struct aa_node *n = aa_node_find(m->t, cp, m->t->root, &p);
if( n ) {
struct cell *cp = aa_value(n);
fn(cc, cell_key(cp), cell_val(cp), false);
return true;
}
}
return false;
}
static void aa_node_unlink(struct aa_node *c, struct aa_node **ns) {
if( aa_nil(c) ) {
return;
}
struct aa_node *lp = l(c);
struct aa_node *rp = r(c);
c->child[L] = 0;
c->child[R] = *ns;
(*ns) = c;
aa_node_unlink(lp, ns);
aa_node_unlink(rp, ns);
}
bool aa_map_filter(struct aa_map *m, void *cc, bool (*fn)(void*,void*,void*)) {
struct aa_node *ns = 0;
aa_node_unlink(m->t->root, &ns);
struct aa_tree *t = m->t;
t->root = aa_node_null;
for(; ns; ) {
void *cell = aa_value(ns);
void *k = cell_key(cell);
void *v = cell_val(cell);
if( fn && fn(cc, k, v) ) {
if( !aa_tree_insert(m->t, cell) ) {
return false;
}
} else {
cell_cleanup(m, cell);
}
void *zombie = ns;
ns = r(ns);
t->dealloc(t->allocator, zombie);
}
return true;
}