feat: print table using recursive walk

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-04-11 01:53:00 -05:00
parent 3975ee9b02
commit acd7068b00
1 changed files with 24 additions and 7 deletions

View File

@ -154,14 +154,31 @@ pub fn insert(self: *HashArrayMappedTrie, comptime key: []const u8, value: void)
} }
} }
fn walk(node: *const Node, indent: u8) void { pub fn walk(hamt: *const HashArrayMappedTrie) void {
switch (node.*) { for (hamt.root, 0..) |maybe_node, i| {
.kv => |pair| std.debug.print("{}: {any}\n", .{ indent, pair }), std.debug.print("{:0>2}: ", .{i});
.table => |table| {
const len = @popCount(table.map);
for (0..len) |i| { if (maybe_node == null) {
walk(&table.base[i], indent + 1); std.debug.print("null\n", .{});
} else {
recurseNodes(maybe_node.?, 1);
}
}
}
fn recurseNodes(node: *Node, depth: u16) void {
switch (node.*) {
.kv => |pair| {
std.debug.print(".{{ .key = \"{s}\", .value = {} }}\n", .{ pair.key, pair.value });
},
.table => |table| {
std.debug.print(".{{ .map = 0x{X:0>8}, .ptr = {*} }}\n", .{ table.map, table.base });
for (0..@popCount(table.map)) |i| {
for (0..depth) |_| std.debug.print(" ", .{});
std.debug.print("{:0>2}: ", .{i});
recurseNodes(&table.base[i], depth + 1);
} }
}, },
} }