From fba9a90cfc6aca2a26cce72f55fb261789139781 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 16 Oct 2026 14:01:01 +0800 Subject: new start 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. --- graph/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'graph/src/lib.rs') 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: 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 + 'a where Self: 'a, @@ -321,13 +325,55 @@ pub trait VLabelGraph: 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, Error>; } +/// A graph whose edges are associated with labels. +/// +/// This trait defines some convenient methods for accessing the +/// labels of edges. +pub trait ELabelGraph: Graph { + /// A type that iterates through tuples of edge labels and target + /// indices of edges. + type TupleIter<'a>: Iterator + '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, 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, Error>; +} + +impl> ELabelGraph for G { + type TupleIter<'a> = >::TupleIter<'a> + where + Self: 'a, + T: 'a; + + #[inline] + fn edlabren_of(&self, node_id: usize) -> Result, Error> { + LabelGraph::tuples_of(self, node_id) + } +} + /// A labelled graph is just a graph with labels associated to /// vertices and / or edges. /// -- cgit v1.2.3-18-g5258