-
Notifications
You must be signed in to change notification settings - Fork 9
/
cb-debug.c
63 lines (57 loc) · 1.55 KB
/
cb-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
// cb-debug.c: crit-bit trie debug support
//
// Written by Tony Finch <dot@dotat.at>
// 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 "cb.h"
static void
dump_rec(Trie *t, int d) {
if(isbranch(t)) {
printf("Tdump%*s branch %p %zu\n", d, "",
t, (size_t)t->branch.index);
assert(t->branch.index >= d);
printf("Tdump%*s twig 0\n", d, "");
dump_rec(twig(t, 0), t->branch.index + 1);
printf("Tdump%*s twig 1\n", d, "");
dump_rec(twig(t, 1), t->branch.index + 1);
} else {
printf("Tdump%*s leaf %p\n", d, "", t);
printf("Tdump%*s leaf key %p %s\n", d, "",
t->leaf.key, t->leaf.key);
printf("Tdump%*s leaf val %p\n", d, "",
t->leaf.val);
}
}
void
Tdump(Tbl *tbl) {
printf("Tdump root %p\n", tbl);
if(tbl != NULL)
dump_rec(&tbl->root, 0);
}
static void
size_rec(Trie *t, uint d,
size_t *rsize, size_t *rdepth, size_t *rbranches, size_t *rleaves) {
*rsize += sizeof(*t);
if(isbranch(t)) {
*rbranches += 1;
size_rec(twig(t, 0), d+1, rsize, rdepth, rbranches, rleaves);
size_rec(twig(t, 1), 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 = "cb";
*rsize = *rdepth = *rbranches = *rleaves = 0;
if(tbl != NULL)
size_rec(&tbl->root, 0, rsize, rdepth, rbranches, rleaves);
}