diff --git a/src/main.zig b/src/main.zig index e01d81b..5f736e5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -32,6 +32,7 @@ const BTree = struct { .Edge => |edge| { const result = try edge.leaf.insert_value(value); if (result) |split| { + std.debug.print("reparenting root\n", .{}); // create new node which will replace self. const parent = try Node.create(self.ally); parent.leaf.level = split.left.level + 1; @@ -74,9 +75,13 @@ const BTree = struct { internal: *Node, leaf: *Leaf, + fn force(self: NodeOrLeaf) NodeOrLeaf { + return NodeOrLeaf.from_leaf(self.as_leaf()); + } + fn destroy(self: NodeOrLeaf) void { std.debug.print("destroying node\n", .{}); - switch (self) { + switch (self.force()) { .internal => |node| { node.destroy(); }, @@ -305,7 +310,7 @@ const BTree = struct { } else { std.debug.print("splitting node ", .{}); self.dbg(); - var parent_split = try leaf.split_at(value); + const parent_split = try leaf.split_at(value); std.debug.print(" into [ ", .{}); split.left.dbg(); std.debug.print(", {}, ", .{split.middle}); @@ -316,9 +321,10 @@ const BTree = struct { const next_split = Leaf.SplitResult.concat(parent_split, split); if (leaf.parent) |parent| { + std.debug.print("forwarding concat split\n", .{}); return parent.parent.insert_split(next_split); } else { - return split; + return next_split; } } @@ -334,6 +340,7 @@ const BTree = struct { const values = self.leaf.get_values(); const edges = self.get_edges()[0..values.len]; std.debug.print("{{ ", .{}); + std.debug.print("[{}] ", .{self.leaf.level}); for (values, edges) |v, e| { if (e) |edge| { edge.dbg(); @@ -398,20 +405,31 @@ const BTree = struct { // new, free floating leaf, must be attached right: *Leaf, + fn dbg(self: SplitResult) void { + std.debug.print("[ ", .{}); + self.left.dbg(); + std.debug.print(", {}, ", .{self.middle}); + self.right.dbg(); + std.debug.print(" ]", .{}); + } + + fn dbg_verbose(self: SplitResult) void { + std.debug.print("[ ", .{}); + NodeOrLeaf.from_leaf(self.left).dbg(); + std.debug.print(", {}, ", .{self.middle}); + NodeOrLeaf.from_leaf(self.right).dbg(); + std.debug.print(" ]", .{}); + } + fn concat(parent: SplitResult, child: SplitResult) SplitResult { // safety @ptrCast(): we know parent left and right are nodes because // they originated from childs parent - std.debug.print("concatinating [", .{}); - parent.left.dbg(); - std.debug.print(", {}, ", .{parent.middle}); - parent.right.dbg(); - std.debug.print(" ]", .{}); - std.debug.print(" and [ ", .{}); - child.left.dbg(); - std.debug.print(", {}, ", .{child.middle}); - child.right.dbg(); - std.debug.print(" ]", .{}); + std.debug.print("concatinating ", .{}); + parent.dbg(); + std.debug.print(" and ", .{}); + child.dbg(); + std.debug.print("\n", .{}); // we only care about the childs middle and left, and since they are ordered we // can learn about the right part from the middle part @@ -429,11 +447,9 @@ const BTree = struct { @ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right)); } - std.debug.print(" into [", .{}); - NodeOrLeaf.from_leaf(parent.left).dbg(); - std.debug.print(", {}, ", .{parent.middle}); - NodeOrLeaf.from_leaf(parent.right).dbg(); - std.debug.print(" ]\n", .{}); + std.debug.print("concatinating into ", .{}); + parent.dbg_verbose(); + std.debug.print("\n", .{}); return parent; } @@ -585,20 +601,20 @@ test "btree seq insert" { tree.dbg(); } -// test "btree rand insert" { -// std.debug.print("random insertions\n", .{}); +test "btree rand insert" { + std.debug.print("random insertions\n", .{}); -// var tree = BTree.create(std.testing.allocator); -// defer tree.destroy(); + var tree = BTree.create(std.testing.allocator); + defer tree.destroy(); -// var rng = std.rand.DefaultPrng.init(0); + var rng = std.rand.DefaultPrng.init(0); -// for (0..100) |_| { -// const i = rng.random().intRangeAtMost(u32, 0, 512); -// // const i = rng.random().int(u32); -// tree.insert(i) catch { -// std.debug.print("{} already present - ignoring\n", .{i}); -// }; -// } -// tree.dbg(); -// } + for (0..100) |_| { + const i = rng.random().intRangeAtMost(u32, 0, 512); + // const i = rng.random().int(u32); + tree.insert(i) catch { + std.debug.print("{} already present - ignoring\n", .{i}); + }; + } + tree.dbg(); +}