debug printing mostly

This commit is contained in:
Janis 2023-03-18 01:32:34 +01:00
parent 4335f77577
commit c2d0022cf8

View file

@ -161,6 +161,8 @@ const BTree = struct {
} }
} }
std.debug.print("placing {} in {}/{}th position\n", .{ value, n, leaf.len });
var tmp = value; var tmp = value;
for (leaf.get_values()[n..]) |*val| { for (leaf.get_values()[n..]) |*val| {
const t = val.*; const t = val.*;
@ -265,7 +267,10 @@ const BTree = struct {
} }
if (self.get_edges()[idx]) |edge| { if (self.get_edges()[idx]) |edge| {
std.debug.print("edge already present?:", .{}); std.debug.print("edge already present between {} and {}\nedge already present:", .{
self_leaf.get_values()[@max(idx - 1, 0)],
self_leaf.get_values()[@min(idx + 1, self_leaf.len - 1)],
});
child.dbg(); child.dbg();
std.debug.print(" - ", .{}); std.debug.print(" - ", .{});
edge.dbg(); edge.dbg();
@ -350,26 +355,42 @@ const BTree = struct {
right: *Leaf, right: *Leaf,
fn concat(parent: SplitResult, child: SplitResult) SplitResult { fn concat(parent: SplitResult, child: SplitResult) SplitResult {
const middle = parent.middle; // safety @ptrCast(): we know parent left and right are nodes because
// safety: we know parent left and right are nodes because
// they originated from childs parent // they originated from childs parent
if (child.left.get_values()[0] < middle) {
// the left node is already attached to the tree in the correct place
// check if the rightmost value of the right child is smaller than the parent middle std.debug.print("concatinating [", .{});
if (child.right.get_values()[child.right.len - 1] < middle) { parent.left.dbg();
@ptrCast(*Node, parent.left).insert_node(NodeOrLeaf.from_leaf(child.right)); std.debug.print(", {}, ", .{parent.middle});
} else if (child.right.get_values()[0] > middle) { parent.right.dbg();
@ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right)); std.debug.print(" ]", .{});
} else { std.debug.print(" and [ ", .{});
std.debug.print("AAAAAAAAAAA something bad", .{}); child.left.dbg();
} std.debug.print(", {}, ", .{child.middle});
} else { child.right.dbg();
// if the child left component is in the parent right, so must the child right std.debug.print(" ]", .{});
// 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
if (child.middle < parent.middle) {
// I'm not sure if checking the childs right part is actually needed?
// I don't think so but can't think of a solid enough reason why
// child is entirely between two values of the parent, so any relation between
// the childs mid point and any of the parents values is true for any of
// the childs values, right?
@ptrCast(*Node, parent.left).insert_node(NodeOrLeaf.from_leaf(child.right));
}
// since they cant be equal, this must mean child is bigger than parent
else {
@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 [", .{});
parent.left.dbg();
std.debug.print(", {}, ", .{parent.middle});
parent.right.dbg();
std.debug.print(" ]\n", .{});
return parent; return parent;
} }
}; };
@ -408,34 +429,36 @@ const BTree = struct {
if (leaf.len < CAPACITY) { if (leaf.len < CAPACITY) {
std.debug.print("pushing value {} into ", .{value}); std.debug.print("pushing value {} into ", .{value});
NodeOrLeaf.from_leaf(self).dbg(); self.dbg();
std.debug.print("\n", .{}); std.debug.print("\n", .{});
NodeOrLeaf.from_leaf(leaf).push_value(value); NodeOrLeaf.from_leaf(leaf).push_value(value);
} else { } else {
std.debug.print("splitting node ", .{}); std.debug.print("splitting node ", .{});
self.dbg(); self.dbg();
const split = try leaf.split_at(value); var 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});
split.right.dbg(); split.right.dbg();
std.debug.print("]\n", .{}); std.debug.print("]\n", .{});
if (leaf.parent) |parent| { // recursively solve splits
// parent can only throw split if it has no parent, so we just pass it back along to the top? var prnt = leaf.parent;
const result = try parent.parent.as_leaf().insert_value(split.middle); while (prnt) |parent| {
const maybe_parent_split = try parent.parent.as_leaf().insert_value(split.middle);
if (result) |parent_split| { if (maybe_parent_split) |parent_split| {
std.debug.print("double split\n", .{}); std.debug.print("concatinating splits\n", .{});
split = SplitResult.concat(parent_split, split);
return SplitResult.concat(parent_split, split); prnt = parent.parent.as_leaf().parent;
} else { } else {
parent.parent.insert_node(NodeOrLeaf.from_leaf(split.right)); parent.parent.insert_node(NodeOrLeaf.from_leaf(split.right));
return null;
} }
} else {
return split;
} }
return split;
} }
return null; return null;
@ -541,24 +564,38 @@ test "btree insert" {
tree.dbg(); tree.dbg();
} }
test "btree rand insert" { test "btree seq insert" {
std.debug.print("random insertions\n", .{}); std.debug.print("sequential 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); for (0..100) |i| {
tree.insert(@intCast(u32, i)) catch {
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}); std.debug.print("{} already present - ignoring\n", .{i});
}; };
} }
tree.dbg(); tree.dbg();
} }
// test "btree rand insert" {
// std.debug.print("random insertions\n", .{});
// var tree = BTree.create(std.testing.allocator);
// defer tree.destroy();
// 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();
// }
test "btree new" { test "btree new" {
std.debug.print("testing insertion\n", .{}); std.debug.print("testing insertion\n", .{});
var tree = BTree.create(std.testing.allocator); var tree = BTree.create(std.testing.allocator);