-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfn-debug.c
90 lines (83 loc) · 2.09 KB
/
fn-debug.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
// fn-debug.c: fn trie debug support
//
// Written by Tony Finch <[email protected]>
// You may do anything with this. It has no warranty.
// <http://creativecommons.org/publicdomain/zero/1.0/>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "Tbl.h"
#include "fn.h"
const char *
dump_bitmap(Tbitmap w) {
static char buf[32*3];
int size = (int)sizeof(buf), n = 0;
n += snprintf(buf+n, size-n, "(");
for(uint s = 0; s < 32; s++) {
Tbitmap b = 1 << s;
if(w & b)
n += snprintf(buf+n, size-n, "%u,", s);
}
if(n > 1)
buf[n-1] = ')';
return buf;
}
static void
dump_rec(Trie *t, uint d) {
Tindex i = t->index;
if(Tindex_branch(i)) {
printf("Tdump%*s branch %p %s %zu %d\n", d, "", (void*)t,
dump_bitmap(Tindex_bitmap(i)),
(size_t)Tindex_offset(i), Tindex_shift(i));
uint dd = 1 + Tindex_offset(i) * 8 + Tindex_shift(i);
assert(dd > d);
for(uint s = 0; s < 32; s++) {
Tbitmap b = 1 << s;
if(hastwig(i, b)) {
printf("Tdump%*s twig %d\n", d, "", s);
dump_rec(Tbranch_twigs(t) + twigoff(i, b), dd);
}
}
} else {
printf("Tdump%*s leaf %p\n", d, "",
(void *)t);
printf("Tdump%*s leaf key %p %s\n", d, "",
(const void *)Tleaf_key(t), Tleaf_key(t));
printf("Tdump%*s leaf val %p\n", d, "",
(void *)Tleaf_val(t));
}
}
void
Tdump(Tbl *tbl) {
printf("Tdump root %p\n", (void*)tbl);
if(tbl != NULL)
dump_rec(tbl, 0);
}
static void
size_rec(Trie *t, uint d,
size_t *rsize, size_t *rdepth, size_t *rbranches, size_t *rleaves) {
*rsize += sizeof(*t);
Tindex i = t->index;
if(Tindex_branch(i)) {
*rbranches += 1;
for(uint s = 0; s < 32; s++) {
Tbitmap b = 1U << s;
if(hastwig(i, b))
size_rec(Tbranch_twigs(t) + twigoff(i, b),
d+1, rsize, rdepth, rbranches, rleaves);
}
} else {
*rleaves += 1;
*rdepth += d;
}
}
void
Tsize(Tbl *tbl, const char **rtype,
size_t *rsize, size_t *rdepth, size_t *rbranches, size_t *rleaves) {
*rtype = "fn";
*rsize = *rdepth = *rbranches = *rleaves = 0;
if(tbl != NULL)
size_rec(tbl, 0, rsize, rdepth, rbranches, rleaves);
}