diff options
Diffstat (limited to 'chain/src/item/default')
-rw-r--r-- | chain/src/item/default/mod.rs | 35 | ||||
-rw-r--r-- | chain/src/item/default/splone.rs | 22 |
2 files changed, 32 insertions, 25 deletions
diff --git a/chain/src/item/default/mod.rs b/chain/src/item/default/mod.rs index 22069d2..3903162 100644 --- a/chain/src/item/default/mod.rs +++ b/chain/src/item/default/mod.rs @@ -628,7 +628,7 @@ impl<T: GraphLabel> DefaultForest<ForestLabel<T>> { /// case we clone it only when its degree is larger than one. /// /// Return either the original node or the cloned node at the end. - pub(crate) fn plant_if_needed( + pub(crate) fn plant_at_start( &mut self, node: usize, mut fragment: Self, @@ -691,6 +691,34 @@ impl<T: GraphLabel> DefaultForest<ForestLabel<T>> { Ok(node) } + + /// Extract the node from `pavi`. + /// + /// If pavi is a parent, this returns the child pointed to by the + /// represented edge. + /// + /// If pavi is a virtual node, this simply returns the virtual + /// node. + /// + /// If pavi is empty, this returns the root, unless the forest is + /// empty. + /// + /// # Guarantee + /// + /// The returned node is guaranteed to be a valid node. + pub(crate) fn node_from_pavi(&self, pavi: PaVi) -> Result<usize, Error> { + match pavi { + PaVi::Parent(node, edge) => Ok(self.nth_child(node, edge)?), + PaVi::Virtual(_, _, node) => { + if node >= self.nodes_len() { + return Err(Error::IndexOutOfBounds(node, self.nodes_len())); + } + + Ok(node) + } + PaVi::Empty => Ok(self.root().ok_or(Error::IndexOutOfBounds(0, 0))?), + } + } } pub mod splone; @@ -831,6 +859,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { /// `child` is the index of the child node in question, and /// `index` means that the child is the `index`-th child of the /// node. + #[allow(dead_code)] pub(crate) fn position_search( &self, node: usize, @@ -1052,7 +1081,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { // } /// Set the position of every node. - pub(crate) fn set_pos(&mut self, pos: usize) -> Result<(), Error> { + pub(crate) fn set_pos(&mut self, pos: usize, allp: bool) -> Result<(), Error> { let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph); for node in 0..builder.nodes_len() { @@ -1064,7 +1093,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { label_label.set_start(pos); - if matches!(label_label.label().tnt(), Some(TNT::Ter(_))) { + if allp || matches!(label_label.label().tnt(), Some(TNT::Ter(_))) { label_label.set_end(pos + 1); } else if builder.degree(node)? == 1 && label_label.label().rule().is_some() { let child = builder.children_of(node)?.next().unwrap(); diff --git a/chain/src/item/default/splone.rs b/chain/src/item/default/splone.rs index 237b92a..4cd11b9 100644 --- a/chain/src/item/default/splone.rs +++ b/chain/src/item/default/splone.rs @@ -142,7 +142,6 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { // replace the label directly - // if new_label.clone_index().is_none() { let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph); builder.set_label(node, new_label)?; @@ -176,27 +175,6 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { builder.set_label(parent, parent_label)?; } - // } else { - // // REVIEW: Call `split_node` in this situation as well? - - // // If we are here, the new label should have a packed - // // parent. - // let packed = self - // .query_label(ForestLabel::new(new_label.label(), ForestLabelType::Packed)) - // .unwrap(); - - // let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph); - - // builder.set_label(node, new_label)?; - - // let parents: Vec<_> = builder.parents_of(node)?.collect(); - - // for parent in parents.iter() { - // builder.redirect(parent.node(), parent.edge(), packed)?; - // } - - // builder.add_edge(packed, node, new_label)?; - // } Ok(node) } |