changes hopefully nothing bad

This commit is contained in:
Janis 2023-03-18 00:26:44 +01:00
parent a627d4b1f7
commit 4335f77577

View file

@ -340,21 +340,6 @@ const BTree = struct {
self.ally.destroy(self); self.ally.destroy(self);
} }
fn push_value(self: *Leaf, value: u32) void {
std.debug.assert(self.len < CAPACITY);
var tmp = value;
for (self.get_values()) |*val| {
if (val.* < value) {
continue;
}
const t = val.*;
val.* = tmp;
tmp = t;
}
self.values[self.len] = tmp;
self.len = self.len + 1;
}
const SplitResult = struct { const SplitResult = struct {
// attached, old node that may be modified // attached, old node that may be modified
left: *Leaf, left: *Leaf,
@ -372,10 +357,13 @@ const BTree = struct {
if (child.left.get_values()[0] < middle) { if (child.left.get_values()[0] < middle) {
// the left node is already attached to the tree in the correct place // the left node is already attached to the tree in the correct place
// check if the rightmost value of the right child is smaller than the parent middle
if (child.right.get_values()[child.right.len - 1] < middle) { if (child.right.get_values()[child.right.len - 1] < middle) {
@ptrCast(*Node, parent.left).insert_node(NodeOrLeaf.from_leaf(child.right)); @ptrCast(*Node, parent.left).insert_node(NodeOrLeaf.from_leaf(child.right));
} else { } else if (child.right.get_values()[0] > middle) {
@ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right)); @ptrCast(*Node, parent.right).insert_node(NodeOrLeaf.from_leaf(child.right));
} else {
std.debug.print("AAAAAAAAAAA something bad", .{});
} }
} else { } else {
// if the child left component is in the parent right, so must the child right // if the child left component is in the parent right, so must the child right
@ -419,8 +407,8 @@ const BTree = struct {
const leaf = self; const leaf = self;
if (leaf.len < CAPACITY) { if (leaf.len < CAPACITY) {
std.debug.print("pushing value {} into leaf ", .{value}); std.debug.print("pushing value {} into ", .{value});
self.dbg(); NodeOrLeaf.from_leaf(self).dbg();
std.debug.print("\n", .{}); std.debug.print("\n", .{});
NodeOrLeaf.from_leaf(leaf).push_value(value); NodeOrLeaf.from_leaf(leaf).push_value(value);
@ -438,13 +426,13 @@ const BTree = struct {
// parent can only throw split if it has no parent, so we just pass it back along to the top? // parent can only throw split if it has no parent, so we just pass it back along to the top?
const result = try parent.parent.as_leaf().insert_value(split.middle); const result = try parent.parent.as_leaf().insert_value(split.middle);
if (result) |for_root| { if (result) |parent_split| {
std.debug.print("double split\n", .{}); std.debug.print("double split\n", .{});
return SplitResult.concat(for_root, split); return SplitResult.concat(parent_split, split);
} } else {
parent.parent.insert_node(NodeOrLeaf.from_leaf(split.right)); parent.parent.insert_node(NodeOrLeaf.from_leaf(split.right));
}
} else { } else {
return split; return split;
} }
@ -553,6 +541,24 @@ test "btree insert" {
tree.dbg(); tree.dbg();
} }
test "btree rand insert" {
std.debug.print("random insertions\n", .{});
var tree = BTree.create(std.testing.allocator);
defer tree.destroy();
var rng = std.rand.DefaultPrng.init(0);
for (0..100) |_| {
// const i = rng.random().intRangeAtMost(u32, 0, 512);
const i = rng.random().int(u32);
tree.insert(i) catch {
std.debug.print("{} already present - ignoring\n", .{i});
};
}
tree.dbg();
}
test "btree new" { test "btree new" {
std.debug.print("testing insertion\n", .{}); std.debug.print("testing insertion\n", .{});
var tree = BTree.create(std.testing.allocator); var tree = BTree.create(std.testing.allocator);