From 987c84f3454c687cca0efe0d471fcf00e052ecab Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 12 Feb 2023 12:07:34 +0800 Subject: Added the functionality of split or clone. I need more than the ability to clone nodes: I also need to split the nodes. Now this seems to be correctly added. --- chain/src/atom/default.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'chain/src/atom') diff --git a/chain/src/atom/default.rs b/chain/src/atom/default.rs index 9c91296..ec53596 100644 --- a/chain/src/atom/default.rs +++ b/chain/src/atom/default.rs @@ -415,7 +415,7 @@ impl DefaultAtom { .map(|(label, target_iter)| (*label, target_iter)) .collect(); - type TerminalsValue = (HashSet<(LabelType, usize)>, bool); + type TerminalsValue = (HashSet<(LabelType, usize, Option>)>, bool); let mut terminals_map: HashMap = HashMap::new(); @@ -454,14 +454,25 @@ impl DefaultAtom { // We checked this is safe above. let (set, _) = terminals_map.get_mut(&t).unwrap(); - set.extend(child_children_iter.map(|target| (*child_label, target))); + set.extend(child_children_iter.map(|target| { + ( + *child_label, + target, + grammar + .query_reduction(child, target) + .unwrap() + .map(|slice| slice.to_vec()), + ) + })); } } } } for (t, (set, accepting)) in terminals_map.into_iter() { - let new_index = nfa.extend(set).map_err(index_out_of_bounds_conversion)?; + let new_index = nfa + .extend(set.iter().map(|(label, target, _)| (*label, *target))) + .map_err(index_out_of_bounds_conversion)?; if accepting_vec.get(new_index).is_none() { #[cfg(debug_assertions)] @@ -473,6 +484,20 @@ impl DefaultAtom { let virtual_node = VirtualNode::new(nt, t); virtual_nodes.insert(virtual_node, new_index); + + // update the reduction information + for (_, target, info) in set.into_iter() { + if let Some(info) = info { + if !matches!( + grammar.query_reduction(new_index, target)?, + Some(original_reduction) + if original_reduction.len() + >= info.len()) + { + grammar.set_reduction(new_index, target, info); + } + } + } } } -- cgit v1.2.3-18-g5258