#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
//----------------------------------------------------------------
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) {
struct node16 *n16;
struct node48 *n48;
struct node256 *n256;
+ unsigned printable;
if (v.type == UNSET)
return;
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;
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++)
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++)
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++) {
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++)
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
+#include <ctype.h>
//----------------------------------------------------------------
// This implementation is based around nested binary trees. Very
struct radix_tree {
radix_value_dtr dtr;
void *dtr_context;
+ unsigned nr_entries;
struct node *root;
};
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)
if (!n || !n->has_value)
return false;
+ rt->nr_entries--;
+
if (rt->dtr)
rt->dtr(rt->dtr_context, n->value);
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);
}
//----------------------------------------------------------------