aboutsummaryrefslogtreecommitdiff
path: root/graph/src/lib.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2026-10-16 14:01:01 +0800
committerJSDurand <mmemmew@gmail.com>2026-10-16 14:02:10 +0800
commitfba9a90cfc6aca2a26cce72f55fb261789139781 (patch)
treeb4b121791ad639e745df689c0d5a8ed7a9e5dcc4 /graph/src/lib.rs
parent3ae0cf9898485a2ec35df7d23cd274e7dea5796d (diff)
new startHEADmaster
Now I tested and discussed with claude, and had a working version of this project. The rest is to port the claude version to my own writings. So this serves as the starting point before that attempt. In the new attempt the design is better, and a number of bugs were found and supposedly fixed, so let us see how this goes at the end.
Diffstat (limited to 'graph/src/lib.rs')
-rw-r--r--graph/src/lib.rs52
1 files changed, 49 insertions, 3 deletions
diff --git a/graph/src/lib.rs b/graph/src/lib.rs
index 058d62c..36fc545 100644
--- a/graph/src/lib.rs
+++ b/graph/src/lib.rs
@@ -22,6 +22,7 @@ pub use adlist::ALGraph;
pub mod labelled;
+pub use labelled::simple::{SLGBuilder, SLGraph};
pub use labelled::DLGraph;
pub use labelled::PLGraph;
@@ -29,6 +30,8 @@ pub mod builder;
pub use builder::Builder;
+pub mod dfs;
+
use error::Error;
/// The expected behaviour of an immutable graph.
@@ -138,6 +141,7 @@ pub trait Graph: Default {
let mut file = std::fs::File::options()
.write(true)
.create(true)
+ .truncate(true)
.open(filename)?;
use std::io::Write;
@@ -290,8 +294,8 @@ pub trait RedirectGraph: Graph {
/// This trait defines some convenient methods for accessing the
/// labels of vertices.
pub trait VLabelGraph<T: GraphLabel>: Graph {
- /// A type that iterates through tuples of labels and targets of
- /// edges.
+ /// A type that iterates through tuples of target labels and
+ /// target indices of edges.
type TupleIter<'a>: Iterator<Item = (T, usize)> + 'a
where
Self: 'a,
@@ -321,13 +325,55 @@ pub trait VLabelGraph<T: GraphLabel>: Graph {
}
/// Return an iterator of tuples of vertex labels and indices of
- /// targets edges out of a node.
+ /// targets for edges out of a node.
///
/// The name is a weird new word created to stand as a
/// simplification of the phrase "vertex label and children of".
fn verlabren_of(&self, node_id: usize) -> Result<Self::TupleIter<'_>, Error>;
}
+/// A graph whose edges are associated with labels.
+///
+/// This trait defines some convenient methods for accessing the
+/// labels of edges.
+pub trait ELabelGraph<T: GraphLabel>: Graph {
+ /// A type that iterates through tuples of edge labels and target
+ /// indices of edges.
+ type TupleIter<'a>: Iterator<Item = (T, usize)> + 'a
+ where
+ Self: 'a,
+ T: 'a;
+
+ #[inline]
+ /// Return the label of an edge or an error if the node is
+ /// invalid.
+ ///
+ /// The default implementation always returns `None` after
+ /// checking the validity of the edge.
+ fn edge_label(&self, from: usize, to: usize) -> Result<Option<T>, Error> {
+ self.has_edge(from, to).map(|_| None)
+ }
+
+ /// Return an iterator of tuples of edge labels and indices of
+ /// targets for edges out of a node.
+ ///
+ /// The name is a weird new word created to stand as a
+ /// simplification of the phrase "edge label and children of".
+ fn edlabren_of(&self, node_id: usize) -> Result<Self::TupleIter<'_>, Error>;
+}
+
+impl<T: GraphLabel, G: LabelGraph<T>> ELabelGraph<T> for G {
+ type TupleIter<'a> = <G as LabelGraph<T>>::TupleIter<'a>
+ where
+ Self: 'a,
+ T: 'a;
+
+ #[inline]
+ fn edlabren_of(&self, node_id: usize) -> Result<Self::TupleIter<'_>, Error> {
+ LabelGraph::tuples_of(self, node_id)
+ }
+}
+
/// A labelled graph is just a graph with labels associated to
/// vertices and / or edges.
///