diff options
Diffstat (limited to 'graph/src/lib.rs')
| -rw-r--r-- | graph/src/lib.rs | 52 |
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. /// |
