seems to work innit
This commit is contained in:
parent
be8f926319
commit
e01e9000b9
31
src/main.zig
31
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 {
|
fn dbg(self: *Self) void {
|
||||||
if (self.root) |root| {
|
if (self.root) |root| {
|
||||||
root.dbg();
|
root.dbg();
|
||||||
|
@ -208,6 +221,9 @@ const BTree = struct {
|
||||||
.internal => |internal| {
|
.internal => |internal| {
|
||||||
if (internal.get_edges()[edge.idx]) |child| {
|
if (internal.get_edges()[edge.idx]) |child| {
|
||||||
leaf = child.as_leaf();
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -306,6 +322,7 @@ const BTree = struct {
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
|
|
||||||
NodeOrLeaf.from_leaf(leaf).push_value(value);
|
NodeOrLeaf.from_leaf(leaf).push_value(value);
|
||||||
|
std.debug.print("insert_split_insert_node ", .{});
|
||||||
self.insert_node(NodeOrLeaf.from_leaf(split.right));
|
self.insert_node(NodeOrLeaf.from_leaf(split.right));
|
||||||
} else {
|
} else {
|
||||||
std.debug.print("splitting node ", .{});
|
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
|
// 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 mid point and any of the parents values is true for any of
|
||||||
// the childs values, right?
|
// the childs values, right?
|
||||||
|
std.debug.print("concatinate ", .{});
|
||||||
@ptrCast(*Node, parent.left).insert_node(NodeOrLeaf.from_leaf(child.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
|
// since they cant be equal, this must mean child is bigger than parent
|
||||||
else {
|
else {
|
||||||
|
std.debug.print("concatinate {} {} ", .{ child.middle, parent.middle });
|
||||||
@ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right));
|
@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);
|
var tree = BTree.create(std.testing.allocator);
|
||||||
defer tree.destroy();
|
defer tree.destroy();
|
||||||
|
var buf = std.ArrayList(u32).init(std.testing.allocator);
|
||||||
|
defer buf.deinit();
|
||||||
|
|
||||||
var rng = std.rand.DefaultPrng.init(0);
|
var rng = std.rand.DefaultPrng.init(0);
|
||||||
|
|
||||||
for (0..100) |_| {
|
for (0..1000) |_| {
|
||||||
const i = rng.random().intRangeAtMost(u32, 0, 512);
|
const i = rng.random().intRangeAtMost(u32, 0, 512);
|
||||||
|
try buf.append(i);
|
||||||
// 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});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (buf.items) |i| {
|
||||||
|
if (tree.find_key(i)) |_| {} else {
|
||||||
|
std.debug.print("{} lost\n", .{i});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tree.dbg();
|
tree.dbg();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue