correctly reparent edges when splitting internal node

This commit is contained in:
Janis 2023-03-18 16:03:50 +01:00
parent c8864b7e02
commit b190719970

View file

@ -3,8 +3,13 @@ const std = @import("std");
const BTree = struct { const BTree = struct {
const Self = @This(); 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 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; const NUM_EDGES: usize = 2 * B;
ally: std.mem.Allocator = std.heap.c_allocator, ally: std.mem.Allocator = std.heap.c_allocator,
@ -134,6 +139,12 @@ const BTree = struct {
const node = try Node.create(leaf.ally); const node = try Node.create(leaf.ally);
std.mem.copy(?NodeOrLeaf, &node.edges, internal.edges[B..]); 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(); new = node.as_leaf();
}, },
.leaf => { .leaf => {
@ -212,7 +223,9 @@ const BTree = struct {
leaf = child.as_leaf(); leaf = child.as_leaf();
// TODO: incredibly hacky I think.. // TODO: incredibly hacky I think..
// gotta figure out WHERE this would even happen.. // 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; continue;
} }
}, },