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