seems to work innit

This commit is contained in:
Janis 2023-03-18 03:26:36 +01:00
parent be8f926319
commit e01e9000b9

View file

@ -53,6 +53,19 @@ const BTree = struct {
}
}
fn find_key(self: *Self, key: u32) ?u32 {
std.debug.print("attempting to find {}\n", .{key});
switch (self.root.?.find_key(key)) {
.Leaf => |leaf| {
return leaf.leaf.get_values()[leaf.idx];
},
else => {
return null;
},
}
}
fn dbg(self: *Self) void {
if (self.root) |root| {
root.dbg();
@ -208,6 +221,9 @@ const BTree = struct {
.internal => |internal| {
if (internal.get_edges()[edge.idx]) |child| {
leaf = child.as_leaf();
// TODO: incredibly hacky I think..
// gotta figure out WHERE this would even happen..
leaf.parent = .{ .parent = internal, .idx = edge.idx };
continue;
}
},
@ -306,6 +322,7 @@ const BTree = struct {
std.debug.print("\n", .{});
NodeOrLeaf.from_leaf(leaf).push_value(value);
std.debug.print("insert_split_insert_node ", .{});
self.insert_node(NodeOrLeaf.from_leaf(split.right));
} else {
std.debug.print("splitting node ", .{});
@ -440,10 +457,12 @@ const BTree = struct {
// 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?
std.debug.print("concatinate ", .{});
@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 {
std.debug.print("concatinate {} {} ", .{ child.middle, parent.middle });
@ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right));
}
@ -606,15 +625,25 @@ test "btree rand insert" {
var tree = BTree.create(std.testing.allocator);
defer tree.destroy();
var buf = std.ArrayList(u32).init(std.testing.allocator);
defer buf.deinit();
var rng = std.rand.DefaultPrng.init(0);
for (0..100) |_| {
for (0..1000) |_| {
const i = rng.random().intRangeAtMost(u32, 0, 512);
try buf.append(i);
// const i = rng.random().int(u32);
tree.insert(i) catch {
std.debug.print("{} already present - ignoring\n", .{i});
};
}
for (buf.items) |i| {
if (tree.find_key(i)) |_| {} else {
std.debug.print("{} lost\n", .{i});
}
}
tree.dbg();
}