From 44ee478d389c4d66df4ef62dc358c0a04d194028 Mon Sep 17 00:00:00 2001 From: nazeh Date: Mon, 18 Dec 2023 19:16:38 +0300 Subject: [PATCH] fix: update the node before returning it as the root in insert() --- mast/src/mermaid.rs | 16 +++++++++++++--- mast/src/treap.rs | 20 +++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/mast/src/mermaid.rs b/mast/src/mermaid.rs index ad2e28d..e5481f6 100644 --- a/mast/src/mermaid.rs +++ b/mast/src/mermaid.rs @@ -12,29 +12,39 @@ mod test { self.build_graph_string(&root, &mut graph); } + graph.push_str(&format!( + " classDef null fill:#1111,stroke-width:1px,color:#fff,stroke-dasharray: 5 5;\n" + )); + graph } fn build_graph_string(&self, node: &Node, graph: &mut String) { let key = bytes_to_string(node.key()); - let node_label = format!("{}({})", key, key); + let node_label = format!("{}(({}))", node.hash(), key); graph.push_str(&format!(" {};\n", node_label)); if let Some(child) = self.get_node(node.left()) { let key = bytes_to_string(child.key()); - let child_label = format!("{}({})", key, key); + let child_label = format!("{}(({}))", child.hash(), key); graph.push_str(&format!(" {} --> {};\n", node_label, child_label)); self.build_graph_string(&child, graph); + } else { + graph.push_str(&format!(" {} -.-> {}l((l));\n", node_label, node.hash())); + graph.push_str(&format!(" class {}l null;\n", node.hash())); } if let Some(child) = self.get_node(node.right()) { let key = bytes_to_string(child.key()); - let child_label = format!("{}({})", key, key); + let child_label = format!("{}(({}))", child.hash(), key); graph.push_str(&format!(" {} --> {};\n", node_label, child_label)); self.build_graph_string(&child, graph); + } else { + graph.push_str(&format!(" {} -.-> {}r((r));\n", node_label, node.hash())); + graph.push_str(&format!(" class {}r null;\n", node.hash())); } } } diff --git a/mast/src/treap.rs b/mast/src/treap.rs index 826e8c7..fe15786 100644 --- a/mast/src/treap.rs +++ b/mast/src/treap.rs @@ -25,12 +25,8 @@ impl<'a> HashTreap<'a> { let value = self.insert_blob(value); let mut node = Node::new(key, value); - println!( - "\n New insert {:?}", - String::from_utf8(key.to_vec()).unwrap() - ); - if self.root.is_none() { + node.update(self.storage); self.update_root(*node.hash()); return; } @@ -97,12 +93,6 @@ impl<'a> HashTreap<'a> { } }; } - dbg!(( - "Out of the first loop", - &top_path, - &left_unzip_path, - &right_unzip_path - )); // === Updating hashes bottom up === @@ -150,7 +140,8 @@ impl<'a> HashTreap<'a> { // I node.set_child(&Branch::Left, left_unzip_path.first().map(|n| *n.hash())); - node.set_child(&Branch::Right, left_unzip_path.first().map(|n| *n.hash())); + node.set_child(&Branch::Right, right_unzip_path.first().map(|n| *n.hash())); + // No more updates lower than the new node, save it to storage. node.update(self.storage); // Update the rest of the path upwards with the new hashes. @@ -248,11 +239,11 @@ mod test { let mut storage = MemoryStorage::new(); let mut treap = HashTreap::new(&mut storage); - // let mut keys = ["A", "C", "D", "F", "G", "H", "M", "P", "X", "Y"]; + let mut keys = ["A", "C", "D", "F", "G", "H", "M", "P", "X", "Y"]; let mut keys = [ "D", "N", "P", "X", "F", "Z", "Y", "A", "G", "C", "M", "H", "I", "J", ]; - // let mut keys = ["A", "B", "C"]; + let mut keys = ["A", "B", "C"]; // keys.reverse(); // keys.reverse(); // Overflowing stack! damn recursion. @@ -261,7 +252,6 @@ mod test { } assert!(treap.verify_ranks()); - // dbg!(&tree); println!("{}", treap.as_mermaid_graph()) } }