From b1907199702267e7866bacc3f99e74b6bc066537 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 18 Mar 2023 16:03:50 +0100 Subject: [PATCH] correctly reparent edges when splitting internal node --- src/main.zig | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2a0b433..93defb2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,8 +3,13 @@ const std = @import("std"); const BTree = struct { const Self = @This(); - const B: usize = 3; + const B: usize = 4; + // guaranteed to be odd, so can't insert into the middle of a full node + // which would complicate splitting const CAPACITY: usize = 2 * B - 1; + const MIN_AFTER_SPLIT: usize = B - 1; + const E_LEFT_OF_CENTER: usize = B - 1; + const E_RIGHT_OF_CENTER: usize = B; const NUM_EDGES: usize = 2 * B; ally: std.mem.Allocator = std.heap.c_allocator, @@ -134,6 +139,12 @@ const BTree = struct { const node = try Node.create(leaf.ally); std.mem.copy(?NodeOrLeaf, &node.edges, internal.edges[B..]); + for (node.edges, 0..) |edge, i| { + if (edge) |edge_node| { + edge_node.as_leaf().parent = .{ .parent = node, .idx = @intCast(u16, i) }; + } + } + new = node.as_leaf(); }, .leaf => { @@ -212,7 +223,9 @@ const BTree = struct { leaf = child.as_leaf(); // TODO: incredibly hacky I think.. // gotta figure out WHERE this would even happen.. - leaf.parent = .{ .parent = internal, .idx = edge.idx }; + // leaf.parent = .{ .parent = internal, .idx = edge.idx }; + // it happened when splitting nodes, and not reparenting + // the split-off edges :| continue; } },