concatination, split handling works i think

This commit is contained in:
Janis 2023-03-18 02:40:26 +01:00
parent c7c71d0c3f
commit be8f926319

View file

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