From e01e9000b9c9ee514aeac23f30340ea1a34de4b9 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 18 Mar 2023 03:26:36 +0100 Subject: [PATCH] seems to work innit --- src/main.zig | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index 5f736e5..ebc0184 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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(); }