use crate::{Direction, Incoming}; use crate::visit::{ Data, EdgeRef, GraphBase, GraphProp, GraphRef, IntoEdgeReferences, IntoEdges, IntoEdgesDirected, IntoNeighbors, IntoNeighborsDirected, IntoNodeIdentifiers, IntoNodeReferences, NodeCompactIndexable, NodeCount, NodeIndexable, Visitable, }; /// An edge-reversing graph adaptor. /// /// All edges have the opposite direction with `Reversed`. #[derive(Copy, Clone, Debug)] pub struct Reversed(pub G); impl GraphBase for Reversed { type NodeId = G::NodeId; type EdgeId = G::EdgeId; } impl GraphRef for Reversed {} Data! {delegate_impl [[G], G, Reversed, access0]} impl IntoNeighbors for Reversed where G: IntoNeighborsDirected, { type Neighbors = G::NeighborsDirected; fn neighbors(self, n: G::NodeId) -> G::NeighborsDirected { self.0.neighbors_directed(n, Incoming) } } impl IntoNeighborsDirected for Reversed where G: IntoNeighborsDirected, { type NeighborsDirected = G::NeighborsDirected; fn neighbors_directed(self, n: G::NodeId, d: Direction) -> G::NeighborsDirected { self.0.neighbors_directed(n, d.opposite()) } } impl IntoEdges for Reversed where G: IntoEdgesDirected, { type Edges = ReversedEdges; fn edges(self, a: Self::NodeId) -> Self::Edges { ReversedEdges { iter: self.0.edges_directed(a, Incoming), } } } impl IntoEdgesDirected for Reversed where G: IntoEdgesDirected, { type EdgesDirected = ReversedEdges; fn edges_directed(self, a: Self::NodeId, dir: Direction) -> Self::Edges { ReversedEdges { iter: self.0.edges_directed(a, dir.opposite()), } } } impl Visitable for Reversed { type Map = G::Map; fn visit_map(&self) -> G::Map { self.0.visit_map() } fn reset_map(&self, map: &mut Self::Map) { self.0.reset_map(map); } } /// A reversed edges iterator. pub struct ReversedEdges { iter: I, } impl Iterator for ReversedEdges where I: Iterator, I::Item: EdgeRef, { type Item = ReversedEdgeReference; fn next(&mut self) -> Option { self.iter.next().map(ReversedEdgeReference) } } /// A reversed edge reference #[derive(Copy, Clone, Debug)] pub struct ReversedEdgeReference(R); impl ReversedEdgeReference { /// Return the original, unreversed edge reference. pub fn as_unreversed(&self) -> &R { &self.0 } /// Consume `self` and return the original, unreversed edge reference. pub fn into_unreversed(self) -> R { self.0 } } /// An edge reference impl EdgeRef for ReversedEdgeReference where R: EdgeRef, { type NodeId = R::NodeId; type EdgeId = R::EdgeId; type Weight = R::Weight; fn source(&self) -> Self::NodeId { self.0.target() } fn target(&self) -> Self::NodeId { self.0.source() } fn weight(&self) -> &Self::Weight { self.0.weight() } fn id(&self) -> Self::EdgeId { self.0.id() } } impl IntoEdgeReferences for Reversed where G: IntoEdgeReferences, { type EdgeRef = ReversedEdgeReference; type EdgeReferences = ReversedEdgeReferences; fn edge_references(self) -> Self::EdgeReferences { ReversedEdgeReferences { iter: self.0.edge_references(), } } } /// A reversed edge references iterator. pub struct ReversedEdgeReferences { iter: I, } impl Iterator for ReversedEdgeReferences where I: Iterator, I::Item: EdgeRef, { type Item = ReversedEdgeReference; fn next(&mut self) -> Option { self.iter.next().map(ReversedEdgeReference) } } macro_rules! access0 { ($e:expr) => { $e.0 }; } NodeIndexable! {delegate_impl [[G], G, Reversed, access0]} NodeCompactIndexable! {delegate_impl [[G], G, Reversed, access0]} IntoNodeIdentifiers! {delegate_impl [[G], G, Reversed, access0]} IntoNodeReferences! {delegate_impl [[G], G, Reversed, access0]} GraphProp! {delegate_impl [[G], G, Reversed, access0]} NodeCount! {delegate_impl [[G], G, Reversed, access0]}