summaryrefslogtreecommitdiff
path: root/nfa/src/default
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-06-02 15:00:48 +0800
committerJSDurand <mmemmew@gmail.com>2023-06-02 15:00:48 +0800
commit8486474f377faf2d800d79166a7abe6b975e3e50 (patch)
treef06baa22bafebf9393869664067e9cf308c92634 /nfa/src/default
parent1455da10f943e2aa1bdf26fb2697dafccc61e073 (diff)
Fix a bug of duplication from planting after sploing
I should have staged and committed these changes separately, but I am too lazy to deal with that. The main changes in this commit are that I added the derive macro that automates the delegation of the Graph trait. This saves a lot of boiler-plate codes. The second main change, perhaps the most important one, is that I found and tried to fix a bug that caused duplication of nodes. The bug arises from splitting or cloning a node multiple times, and immediately planting the same fragment under the new "sploned" node. That is, when we try to splone the node again, we found that we need to splone, because the node that was created by the same sploning process now has a different label because of the planting of the fragment. Then after the sploning, we plant the fragment again. This makes the newly sploned node have the same label (except for the clone index) and the same children as the node that was sploned and planted in the previous rounds. The fix is to check for the existence of a node that has the same set of children as the about-to-be-sploned node, except for the last one, which contains the about-to-be-planted fragment as a prefix. If that is the case, treat it as an already existing node, so that we do not have to splone the node again. This is consistent with the principle to not create what we do not need.
Diffstat (limited to 'nfa/src/default')
-rw-r--r--nfa/src/default/nfa.rs56
-rw-r--r--nfa/src/default/regex.rs60
2 files changed, 18 insertions, 98 deletions
diff --git a/nfa/src/default/nfa.rs b/nfa/src/default/nfa.rs
index 6b1e56f..d2fe88e 100644
--- a/nfa/src/default/nfa.rs
+++ b/nfa/src/default/nfa.rs
@@ -12,8 +12,10 @@ use crate::{
use core::fmt::{Debug, Display};
+use graph_macro::Graph;
+
/// Default NFA implementation.
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Graph)]
pub struct DefaultNFA<T: GraphLabel> {
graph: DLGraph<T>,
}
@@ -31,51 +33,7 @@ impl<T: GraphLabel + Display> Default for DefaultNFA<T> {
// I deliberately avoid using [`Deref`][std::ops::Deref] here, as I do
// not want to dereference an NFA to a Graph.
-impl<T: GraphLabel + Display> Graph for DefaultNFA<T> {
- type Iter<'a> = <DLGraph<T> as Graph>::Iter<'a> where T: 'a;
-
- #[inline]
- fn is_empty(&self) -> bool {
- self.graph.is_empty()
- }
-
- #[inline]
- fn nodes_len(&self) -> usize {
- self.graph.nodes_len()
- }
-
- #[inline]
- fn children_of(&self, node_id: usize) -> Result<Self::Iter<'_>, GError> {
- self.graph.children_of(node_id)
- }
-
- #[inline]
- fn degree(&self, node_id: usize) -> Result<usize, GError> {
- self.graph.degree(node_id)
- }
-
- #[inline]
- fn is_empty_node(&self, node_id: usize) -> Result<bool, GError> {
- self.graph.is_empty_node(node_id)
- }
-
- #[inline]
- fn has_edge(&self, source: usize, target: usize) -> Result<bool, GError> {
- self.graph.has_edge(source, target)
- }
-
- #[inline]
- fn print_viz(&self, filename: &str) -> Result<(), std::io::Error> {
- self.graph.print_viz(filename)
- }
-
- #[inline]
- fn replace_by_builder(&mut self, _builder: impl Builder<Result = Self>) {
- unimplemented!()
- }
-}
-
-impl<T: GraphLabel + Display> LabelGraph<T> for DefaultNFA<T> {
+impl<T: GraphLabel> LabelGraph<T> for DefaultNFA<T> {
type Iter<'a> = <DLGraph<T> as LabelGraph<T>>::Iter<'a> where T: 'a;
type LabelIter<'a> = <DLGraph<T> as LabelGraph<T>>::LabelIter<'a> where T: 'a;
@@ -119,14 +77,14 @@ impl<T: GraphLabel + Display> LabelGraph<T> for DefaultNFA<T> {
}
}
-impl<T: GraphLabel + Display> LabelExtGraph<T> for DefaultNFA<T> {
+impl<T: GraphLabel> LabelExtGraph<T> for DefaultNFA<T> {
#[inline]
fn extend(&mut self, edges: impl IntoIterator<Item = (T, usize)>) -> Result<usize, GError> {
self.graph.extend(edges)
}
}
-impl<T: GraphLabel + Display> Nfa<T> for DefaultNFA<T> {
+impl<T: GraphLabel> Nfa<T> for DefaultNFA<T> {
type FromRegex<S: GraphLabel + Display + Default> = DefaultNFA<S>;
// Reminder: This is an adopted version of Thompson's
@@ -484,7 +442,7 @@ impl<T: GraphLabel + Display> Nfa<T> for DefaultNFA<T> {
}
}
-impl<T: GraphLabel + Display + Debug> Display for DefaultNFA<T> {
+impl<T: GraphLabel> Display for DefaultNFA<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(self, f)
}
diff --git a/nfa/src/default/regex.rs b/nfa/src/default/regex.rs
index 1c22687..1b1b325 100644
--- a/nfa/src/default/regex.rs
+++ b/nfa/src/default/regex.rs
@@ -4,6 +4,8 @@ use graph::{error::Error as GError, ALGraph, ExtGraph, Graph, GraphLabel};
use crate::{desrec::DesRec, error::Error, Regex};
+use graph_macro::Graph;
+
#[cfg(feature = "recursion")]
use receme::{algebra::Algebra, catana::Cata};
@@ -64,9 +66,10 @@ impl<T: GraphLabel> Display for RegexType<T> {
}
/// A default implementation of regular expressions.
-#[derive(Debug, Clone)]
-pub struct DefaultRegex<T: GraphLabel + Display> {
+#[derive(Debug, Clone, Graph)]
+pub struct DefaultRegex<T: GraphLabel> {
/// The underlying graph is stored using adjacency lists.
+ #[graph]
graph: ALGraph,
/// The types of the underlying nodes.
types: Vec<RegexType<T>>,
@@ -76,7 +79,7 @@ pub struct DefaultRegex<T: GraphLabel + Display> {
root: Option<usize>,
}
-impl<T: GraphLabel + Display> Default for DefaultRegex<T> {
+impl<T: GraphLabel> Default for DefaultRegex<T> {
fn default() -> Self {
Self {
graph: Default::default(),
@@ -503,54 +506,13 @@ impl<T: GraphLabel> DefaultRegex<T> {
}
}
-impl<T: GraphLabel + Display + Debug> Display for DefaultRegex<T> {
+impl<T: GraphLabel> Display for DefaultRegex<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_string_with(|t| format!("{t}"))?)
}
}
-impl<T: GraphLabel + Display> Graph for DefaultRegex<T> {
- type Iter<'a> = <ALGraph as Graph>::Iter<'a>
- where
- Self: 'a;
-
- #[inline]
- fn is_empty(&self) -> bool {
- self.graph.is_empty()
- }
-
- #[inline]
- fn nodes_len(&self) -> usize {
- self.graph.nodes_len()
- }
-
- #[inline]
- fn children_of(&self, node_id: usize) -> Result<Self::Iter<'_>, GError> {
- self.graph.children_of(node_id)
- }
-
- #[inline]
- fn degree(&self, node_id: usize) -> Result<usize, GError> {
- self.graph.degree(node_id)
- }
-
- #[inline]
- fn is_empty_node(&self, node_id: usize) -> Result<bool, GError> {
- self.graph.is_empty_node(node_id)
- }
-
- #[inline]
- fn has_edge(&self, source: usize, target: usize) -> Result<bool, GError> {
- self.graph.has_edge(source, target)
- }
-
- #[inline]
- fn replace_by_builder(&mut self, _builder: impl graph::Builder<Result = Self>) {
- unimplemented!()
- }
-}
-
-impl<T: GraphLabel + Display + Debug> Regex<RegexType<T>> for DefaultRegex<T> {
+impl<T: GraphLabel> Regex<RegexType<T>> for DefaultRegex<T> {
/// Return the root of the regular expression.
#[inline]
fn root(&self) -> Option<usize> {
@@ -649,7 +611,7 @@ pub struct DefaultRegParser<T: GraphLabel + Display> {
_phantom: PhantomData<T>,
}
-impl<T: GraphLabel + Display> DefaultRegParser<T> {
+impl<T: GraphLabel> DefaultRegParser<T> {
/// Query if a terminal or a non-terminal is already found.
///
/// If found, return the associated index of the terminal or
@@ -676,7 +638,7 @@ impl<T: GraphLabel + Display> DefaultRegParser<T> {
}
}
-impl<T: GraphLabel + Display> Default for DefaultRegParser<T> {
+impl<T: GraphLabel> Default for DefaultRegParser<T> {
fn default() -> Self {
Self {
ter_map: Default::default(),
@@ -686,7 +648,7 @@ impl<T: GraphLabel + Display> Default for DefaultRegParser<T> {
}
}
-impl<T: GraphLabel + Display + Debug> DesRec for DefaultRegParser<T> {
+impl<T: GraphLabel> DesRec for DefaultRegParser<T> {
type Label = RegexType<T>;
type Regex = DefaultRegex<T>;