removing debug printing

This commit is contained in:
Janis 2023-03-18 14:13:28 +01:00
parent e01e9000b9
commit c8864b7e02

View file

@ -18,21 +18,16 @@ const BTree = struct {
}
fn insert(self: *Self, value: u32) !void {
std.debug.print("attempting to insert {} into ", .{value});
self.dbg();
if (self.root) |*root| {
const search = root.find_key(value);
switch (search) {
.Leaf => |node| {
std.debug.print("key already present: {}", .{node});
.Leaf => |_| {
return error.Occupied;
},
.Edge => |edge| {
const result = try edge.leaf.insert_value(value);
if (result) |split| {
std.debug.print("reparenting root\n", .{});
// create new node which will replace self.
const parent = try Node.create(self.ally);
parent.leaf.level = split.left.level + 1;
@ -54,8 +49,6 @@ 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];
@ -93,7 +86,6 @@ const BTree = struct {
}
fn destroy(self: NodeOrLeaf) void {
std.debug.print("destroying node\n", .{});
switch (self.force()) {
.internal => |node| {
node.destroy();
@ -135,7 +127,6 @@ const BTree = struct {
}
std.debug.assert(leaf.len == CAPACITY);
//std.debug.assert(idx > 0 and idx < CAPACITY - 1);
var new: *Leaf = undefined;
switch (self) {
@ -180,8 +171,6 @@ const BTree = struct {
}
}
std.debug.print("placing {} in {}/{}th position\n", .{ value, n, leaf.len });
var tmp = value;
for (leaf.get_values()[n..]) |*val| {
const t = val.*;
@ -311,34 +300,17 @@ const BTree = struct {
};
fn insert_split(self: *Node, split: Leaf.SplitResult) !?Leaf.SplitResult {
std.debug.print("inserting split\n", .{});
const leaf = self.as_leaf();
const value = split.middle;
if (leaf.len < CAPACITY) {
std.debug.print("pushing value {} into ", .{value});
self.dbg();
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 ", .{});
self.dbg();
const parent_split = try leaf.split_at(value);
std.debug.print(" into [ ", .{});
split.left.dbg();
std.debug.print(", {}, ", .{split.middle});
split.right.dbg();
std.debug.print("]\n", .{});
std.debug.print("concatinating splits\n", .{});
const next_split = Leaf.SplitResult.concat(parent_split, split);
if (leaf.parent) |parent| {
std.debug.print("forwarding concat split\n", .{});
return parent.parent.insert_split(next_split);
} else {
return next_split;
@ -442,12 +414,6 @@ const BTree = struct {
// safety @ptrCast(): we know parent left and right are nodes because
// they originated from childs parent
std.debug.print("concatinating ", .{});
parent.dbg();
std.debug.print(" and ", .{});
child.dbg();
std.debug.print("\n", .{});
// we only care about the childs middle and left, and since they are ordered we
// can learn about the right part from the middle part
if (child.middle < parent.middle) {
@ -457,19 +423,13 @@ 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));
}
std.debug.print("concatinating into ", .{});
parent.dbg_verbose();
std.debug.print("\n", .{});
return parent;
}
};
@ -486,18 +446,17 @@ const BTree = struct {
};
fn find_key(self: *Leaf, key: u32) SearchResult {
std.debug.print("looking for {} in {any}\n", .{ key, self.get_values() });
for (self.get_values(), 0..) |v, i| {
if (key < v) {
std.debug.print("decending left of {}\n", .{v});
// decending left of v
return .{ .Edge = .{ .leaf = self, .idx = @intCast(u16, i) } };
} else if (key == v) {
std.debug.print("located {} at {}\n", .{ key, v });
// located key at v
return .{ .Leaf = .{ .leaf = self, .idx = @intCast(u16, i) } };
}
}
std.debug.print("decending right of {}\n", .{self.get_values()[self.len - 1]});
// decending right-most
return .{ .Edge = .{ .leaf = self, .idx = self.len } };
}
@ -507,20 +466,9 @@ const BTree = struct {
const leaf = self;
if (leaf.len < CAPACITY) {
std.debug.print("pushing value {} into ", .{value});
self.dbg();
std.debug.print("\n", .{});
NodeOrLeaf.from_leaf(leaf).push_value(value);
} else {
std.debug.print("splitting node ", .{});
self.dbg();
var split = try leaf.split_at(value);
std.debug.print(" into [ ", .{});
split.left.dbg();
std.debug.print(", {}, ", .{split.middle});
split.right.dbg();
std.debug.print("]\n", .{});
if (leaf.parent) |parent| {
return parent.parent.insert_split(split);
@ -630,10 +578,10 @@ test "btree rand insert" {
var rng = std.rand.DefaultPrng.init(0);
for (0..1000) |_| {
const i = rng.random().intRangeAtMost(u32, 0, 512);
for (0..10000) |_| {
// const i = rng.random().intRangeAtMost(u32, 0, 512);
const i = rng.random().int(u32);
try buf.append(i);
// const i = rng.random().int(u32);
tree.insert(i) catch {
std.debug.print("{} already present - ignoring\n", .{i});
};