shadow_rs/network/graph/
petgraph_wrapper.rs

1use petgraph::graph::{EdgeIndex, Graph, IndexType, NodeIndex};
2use petgraph::{Directed, Undirected};
3
4#[derive(Debug)]
5pub enum GraphWrapper<N, E, Ix: IndexType> {
6    Directed(Graph<N, E, Directed, Ix>),
7    Undirected(Graph<N, E, Undirected, Ix>),
8}
9
10// for functions that are the same for both directed and undirected graphs, we can added
11// wrapper functions
12#[allow(dead_code)]
13impl<N, E, Ix: IndexType> GraphWrapper<N, E, Ix> {
14    enum_passthrough!(self, (weight), Directed, Undirected;
15        pub fn add_node(&mut self, weight: N) -> NodeIndex<Ix>
16    );
17    enum_passthrough!(self, (a, b, weight), Directed, Undirected;
18        pub fn add_edge(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>, weight: E) -> EdgeIndex<Ix>
19    );
20    enum_passthrough!(self, (node), Directed, Undirected;
21        pub fn node_weight(&self, node: NodeIndex<Ix>) -> Option<&N>
22    );
23    enum_passthrough!(self, (edge), Directed, Undirected;
24        pub fn edge_weight(&self, edge: EdgeIndex<Ix>) -> Option<&E>
25    );
26    enum_passthrough!(self, (a, b), Directed, Undirected;
27        pub fn find_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<EdgeIndex<Ix>>
28    );
29}