From cd977c17ee5cd4ef68d0021dc69c131364122376 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 12 Jul 2023 10:29:50 +0800 Subject: Fix the bug of testing prefixes and planting Previously the functions `is_prefix` and `plant` did not take the situation of packed nodes into considerations. That was because I only dealt with non-packed nodes in the past: the fragment to test for prefixes and for planting did not intersect the packed nodes in the forest, and the grammar is so simple that the fragments do not contain packed nodes. Then a test revealed this situation, so I have to fix this lack of considerations now. This commit attempts to fix this issue. From the newly added unit-tests, it seems that this fix works. :) --- chain/src/archive.txt | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) (limited to 'chain/src/archive.txt') diff --git a/chain/src/archive.txt b/chain/src/archive.txt index 7bd6f31..79fe6d8 100644 --- a/chain/src/archive.txt +++ b/chain/src/archive.txt @@ -540,3 +540,101 @@ // // Ok(None) // } + +// let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph); + +// // Just a dummy label for use in adding edges. +// // +// // REVIEW: I probably should refactor the API for builder_mut. +// let root_label = fragment +// .vertex_label(root)? +// .ok_or(Error::NodeNoLabel(root))?; + +// let nodes_len = fragment.nodes_len(); + +// /// If the fragment root has a duplicate label, the forest +// /// will not grow, so we use the label to find the adjoined +// /// node index. +// /// +// /// The nodes hava already been added to the forest, so it is +// /// safe to call unwrap. +// macro_rules! conversion ( +// ($node:expr) => { +// { +// builder +// .query_label( +// fragment +// .vertex_label($node)? +// .ok_or(Error::NodeNoLabel($node))? +// ).unwrap() +// } +// } +// ); + +// // If the fragment has been planted before, we just add an +// // edge. + +// if planted { +// builder.add_edge(node_id, conversion!(root), root_label)?; + +// return Ok(()); +// } + +// // First adjoin the relevant nodes and join the edges later. + +// let mut used_nodes: Vec = std::iter::repeat(false).take(nodes_len).collect(); + +// let mut stack = vec![root]; + +// while let Some(top) = stack.pop() { +// if used_nodes.get(top).copied() == Some(true) { +// continue; +// } + +// *used_nodes +// .get_mut(top) +// .ok_or(Error::IndexOutOfBounds(top, nodes_len))? = true; + +// stack.extend(fragment.children_of(top)?); +// } + +// let used_nodes = used_nodes; + +// for node in (0..nodes_len).filter(|node| used_nodes.get(*node).copied() == Some(true)) { +// let label = fragment +// .vertex_label(node)? +// .ok_or(Error::NodeNoLabel(node))?; + +// builder.add_vertex(label); +// } + +// // Don't forget to join the new sub-forest to the original +// // forest, at the specified position. + +// builder.add_edge(node_id, conversion!(root), root_label)?; + +// // We can try to calculate the depth of fragments, if we need +// // to lower the memory usage. But in our use cases, we +// // usually deal with fragments where each node has at most one +// // child, so the depth is supposed to be equal to the length +// // in this case. +// let mut stack = Vec::with_capacity(fragment.nodes_len()); + +// stack.push(root); + +// let mut seen_nodes = std::collections::HashSet::::new(); + +// while let Some(top) = stack.pop() { +// seen_nodes.insert(top); + +// for child in fragment.children_of(top)? { +// builder.add_edge(conversion!(top), conversion!(child), root_label)?; + +// if !seen_nodes.contains(&child) { +// seen_nodes.insert(child); +// stack.push(child); +// } +// } +// } + +// Ok(()) -- cgit v1.2.3-18-g5258