summaryrefslogtreecommitdiff
path: root/graph/src/builder.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-01-05 10:24:39 +0800
committerJSDurand <mmemmew@gmail.com>2023-01-05 10:24:39 +0800
commit7dd4935230e303aef8d295d992239d59d95b32d7 (patch)
tree486104820b5f3701518c1030a0393a5cef428cb9 /graph/src/builder.rs
parentbdbd4b4dc21af09711c97d3f903877443199af06 (diff)
singly labelled graphs
Now I have a new type of labelled graphs, which can index vertices by labels, but not index edges by labels. The biggest difference is that I do not have to keep a hashmap of edge targets by labels, and I do not have to guard against the duplication of nodes with the same set of edges. I guard against nodes with the same label, though. Also, in this graph, both vertices and edges have one label at a time, whereas in the previous labelled graph there can be a multitude of edges between the same source and target nodes, but with different labels. Now it remains to test this type of graphs, and to think through how we attach forest fragments to nondeterministic finite automata edges, and how to join forest fragments together while skipping nullable edges, in order to finish the "compilation" part.
Diffstat (limited to 'graph/src/builder.rs')
-rw-r--r--graph/src/builder.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/graph/src/builder.rs b/graph/src/builder.rs
index 9ab5895..4a480a5 100644
--- a/graph/src/builder.rs
+++ b/graph/src/builder.rs
@@ -54,3 +54,40 @@ pub trait Builder: Default {
/// still be used later on.
fn build_ref(&self) -> Self::Result;
}
+
+/// The type of builders that actually reference the underlying graph
+/// instead of owe it.
+///
+/// To finish the task of the builder, just do not use it anymore.
+/// The building happens right when the building methods are called.
+pub trait BuilderMut {
+ /// Some graphs are labelled. This type should be the type of the
+ /// labels.
+ type Label;
+
+ /// The type of the underlying graph.
+ type Graph: Graph;
+
+ /// The type of the builder from a borrow.
+ type ResultBuilder<'a>: BuilderMut
+ where
+ Self::Label: 'a;
+
+ /// Borrow a graph to create a builder without copying.
+ fn from_graph_mut(graph: &mut Self::Graph) -> Self::ResultBuilder<'_>;
+
+ /// Add a new vertex.
+ fn add_vertex(&mut self, label: Self::Label) -> Result<usize, Error>;
+
+ /// Add an edge from the source to the target.
+ fn add_edge(&mut self, source: usize, target: usize, label: Self::Label) -> Result<(), Error>;
+
+ /// Remove an edge from the source to the target.
+ ///
+ /// Since some graphs are labelled, the users are allowed to pass
+ /// a predicate to determine if an edge from the source to the
+ /// target should be removed.
+ fn remove_edge<F>(&mut self, source: usize, target: usize, predicate: F) -> Result<(), Error>
+ where
+ F: Fn(Self::Label) -> bool;
+}