Skip to content

Commit

Permalink
Improve art_printf
Browse files Browse the repository at this point in the history
* Makes the representation more compact
* Uses 2-wide hex representation for key chunks

Before:
```
{
 type: Node4
 prefix_size: 2
 prefix: 3030
 key: 30
 {
  type: Node4
  prefix_size: 2
  prefix: 3030
  key: 31
  {
   type: Leaf
   key: 303030303031
  }
  key: 32
  {
   type: Leaf
   key: 303030303032
  }
  key: 33
  {
   type: Leaf
   key: 303030303033
  }
  key: 34
  {
   type: Leaf
   key: 303030303034
  }
 }
 key: 31
 {
  type: Leaf
  key: 303031303035
 }
}
```

After:
```
{
 type: Node4
 prefix_size: 2
 prefix: 3030
 key: 30 {
  type: Node4
  prefix_size: 2
  prefix: 3030
  key: 31 { type: Leaf, key: 303030303031 }
  key: 32 { type: Leaf, key: 303030303032 }
  key: 33 { type: Leaf, key: 303030303033 }
  key: 34 { type: Leaf, key: 303030303034 }
 }
 key: 31 { type: Leaf, key: 303031303035 }
}
```
  • Loading branch information
SLieve committed Jan 16, 2024
1 parent c7d1b5d commit 88ca6aa
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/art/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,15 @@ static void art_node_print_type(const art_node_t *node) {
}

void art_node_printf(const art_node_t *node, uint8_t depth) {
printf("%*s", depth, "");
if (art_is_leaf(node)) {
printf("{ type: Leaf, key: ");
art_leaf_t *leaf = CAST_LEAF(node);
for (size_t i = 0; i < ART_KEY_BYTES; ++i) {
printf("%02x", (char)(leaf->key[i]));
}
printf(" }\n");
return;
}
printf("{\n");
depth++;

Expand All @@ -1187,27 +1195,14 @@ void art_node_printf(const art_node_t *node, uint8_t depth) {
art_node_print_type(node);
printf("\n");

if (art_is_leaf(node)) {
art_leaf_t *leaf = CAST_LEAF(node);
printf("%*s", depth, "");
printf("key: ");
for (size_t i = 0; i < ART_KEY_BYTES; ++i) {
printf("%x", leaf->key[i]);
}
printf("\n");
depth--;
printf("%*s", depth, "");
printf("}\n");
return;
}
art_inner_node_t *inner_node = (art_inner_node_t *)node;
printf("%*s", depth, "");
printf("prefix_size: %d\n", inner_node->prefix_size);

printf("%*s", depth, "");
printf("prefix: ");
for (uint8_t i = 0; i < inner_node->prefix_size; ++i) {
printf("%x", (char)inner_node->prefix[i]);
printf("%02x", (char)inner_node->prefix[i]);
}
printf("\n");

Expand All @@ -1216,15 +1211,15 @@ void art_node_printf(const art_node_t *node, uint8_t depth) {
art_node4_t *node4 = (art_node4_t *)node;
for (uint8_t i = 0; i < node4->count; ++i) {
printf("%*s", depth, "");
printf("key: %x\n", node4->keys[i]);
printf("key: %02x ", node4->keys[i]);
art_node_printf(node4->children[i], depth);
}
} break;
case ART_NODE16_TYPE: {
art_node16_t *node16 = (art_node16_t *)node;
for (uint8_t i = 0; i < node16->count; ++i) {
printf("%*s", depth, "");
printf("key: %x\n", node16->keys[i]);
printf("key: %02x ", node16->keys[i]);
art_node_printf(node16->children[i], depth);
}
} break;
Expand All @@ -1233,7 +1228,7 @@ void art_node_printf(const art_node_t *node, uint8_t depth) {
for (int i = 0; i < 256; ++i) {
if (node48->keys[i] != ART_NODE48_EMPTY_VAL) {
printf("%*s", depth, "");
printf("key: %x\n", node48->keys[i]);
printf("key: %02x ", node48->keys[i]);
art_node_printf(node48->children[i], depth);
}
}
Expand All @@ -1243,7 +1238,7 @@ void art_node_printf(const art_node_t *node, uint8_t depth) {
for (int i = 0; i < 256; ++i) {
if (node256->children[i] != NULL) {
printf("%*s", depth, "");
printf("key: %x\n", i);
printf("key: %02x ", i);
art_node_printf(node256->children[i], depth);
}
}
Expand Down

0 comments on commit 88ca6aa

Please sign in to comment.