Skip to content

Commit

Permalink
radix_tree: debug updates
Browse files Browse the repository at this point in the history
Some updates to _dump()  debugging function so the printed result
can be more easily examined by human.

Also print 'prefix' as string when all chars are printable.

Add 'simple' variant of _dump also to 'simple' version of radix tree
(which is however normally not compiled).
  • Loading branch information
Zdenek Kabelac committed Jun 3, 2024
1 parent 88681f0 commit 1e2a344
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
29 changes: 22 additions & 7 deletions base/data-struct/radix-tree-adaptive.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

//----------------------------------------------------------------

Expand Down Expand Up @@ -500,7 +501,7 @@ static struct lookup_result _lookup_prefix(struct value *v, const uint8_t *kb, c
struct node48 *n48;
struct node256 *n256;

if (kb == ke)
if (kb == ke || !kb) /* extra check for !kb for coverity */
return (struct lookup_result) {.v = v, .kb = kb};

switch (v->type) {
Expand Down Expand Up @@ -1218,6 +1219,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
struct node16 *n16;
struct node48 *n48;
struct node256 *n256;
unsigned printable;

if (v.type == UNSET)
return;
Expand All @@ -1242,9 +1244,22 @@ static void _dump(FILE *out, struct value v, unsigned indent)

case PREFIX_CHAIN:
pc = v.value.ptr;
fprintf(out, "<prefix: ");
fprintf(out, "<prefix(%u): ", pc->len);
printable = 1;
for (i = 0; i < pc->len; i++)
fprintf(out, "%x.", (unsigned) *(pc->prefix + i));
if (!isprint(pc->prefix[i])) {
printable = 0;
break;
}
if (printable)
fputc('"', out);
for (i = 0; i < pc->len; i++)
if (printable)
fprintf(out, "%c", pc->prefix[i]);
else
fprintf(out, "%02x.", (unsigned) *(pc->prefix + i));
if (printable)
fputc('"', out);
fprintf(out, ">\n");
_dump(out, pc->child, indent + 1);
break;
Expand All @@ -1253,7 +1268,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
n4 = v.value.ptr;
fprintf(out, "<n4: ");
for (i = 0; i < n4->nr_entries; i++)
fprintf(out, "%x ", (unsigned) n4->keys[i]);
fprintf(out, "%02x ", (unsigned) n4->keys[i]);
fprintf(out, ">\n");

for (i = 0; i < n4->nr_entries; i++)
Expand All @@ -1264,7 +1279,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
n16 = v.value.ptr;
fprintf(out, "<n16: ");
for (i = 0; i < n16->nr_entries; i++)
fprintf(out, "%x ", (unsigned) n16->keys[i]);
fprintf(out, "%02x ", (unsigned) n16->keys[i]);
fprintf(out, ">\n");

for (i = 0; i < n16->nr_entries; i++)
Expand All @@ -1276,7 +1291,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
fprintf(out, "<n48: ");
for (i = 0; i < 256; i++)
if (n48->keys[i] < 48)
fprintf(out, "%x ", i);
fprintf(out, "%02x ", i);
fprintf(out, ">\n");

for (i = 0; i < n48->nr_entries; i++) {
Expand All @@ -1290,7 +1305,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
fprintf(out, "<n256: ");
for (i = 0; i < 256; i++)
if (n256->values[i].type != UNSET)
fprintf(out, "%x ", i);
fprintf(out, "%02x ", i);
fprintf(out, ">\n");

for (i = 0; i < 256; i++)
Expand Down
34 changes: 33 additions & 1 deletion base/data-struct/radix-tree-simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

//----------------------------------------------------------------
// This implementation is based around nested binary trees. Very
Expand All @@ -37,6 +38,7 @@ struct node {
struct radix_tree {
radix_value_dtr dtr;
void *dtr_context;
unsigned nr_entries;

struct node *root;
};
Expand Down Expand Up @@ -156,7 +158,11 @@ bool radix_tree_insert(struct radix_tree *rt, const void *key, size_t keylen,
const uint8_t *kb = key;
const uint8_t *ke = kb + keylen;

return _insert(&rt->root, kb, ke, v);
if (!_insert(&rt->root, kb, ke, v))
return false;

rt->nr_entries++;
return true;
}

bool radix_tree_remove(struct radix_tree *rt, const void *key, size_t keylen)
Expand All @@ -169,6 +175,8 @@ bool radix_tree_remove(struct radix_tree *rt, const void *key, size_t keylen)
if (!n || !n->has_value)
return false;

rt->nr_entries--;

if (rt->dtr)
rt->dtr(rt->dtr_context, n->value);

Expand Down Expand Up @@ -259,8 +267,32 @@ bool radix_tree_is_well_formed(struct radix_tree *rt)
return true;
}

static void _dump(FILE *out, struct node *n, unsigned indent)
{
unsigned i;

if (!n)
return;

_dump(out, n->left, indent + 1);

for (i = 0; i < 2 * indent; i++)
fprintf(out, " ");

if (n->has_value) {
fprintf(out, "value: %llu\n", n->value.n);
} else {
fprintf(out, "key: '%c' [0x%02x] %u\n",
isprint(n->key) ? n->key : ' ', n->key, indent);
}

_dump(out, n->center, indent + 1);
_dump(out, n->right, indent + 1);
}

void radix_tree_dump(struct radix_tree *rt, FILE *out)
{
_dump(out, rt->root, 0);
}

//----------------------------------------------------------------
Expand Down

0 comments on commit 1e2a344

Please sign in to comment.