use crate::graph::{DirectedGraph, Predecessors, Successors}; /// View that reverses the direction of edges in its underlying graph, so that /// successors become predecessors and vice-versa. /// /// Because of `impl Graph for &G`, the underlying graph can be /// wrapped by-reference instead of by-value if desired. #[derive(Clone, Copy, Debug)] pub struct ReversedGraph { pub inner: G, } impl ReversedGraph { pub fn new(inner: G) -> Self { Self { inner } } } impl DirectedGraph for ReversedGraph { type Node = G::Node; fn num_nodes(&self) -> usize { self.inner.num_nodes() } } // Implementing `StartNode` is not possible in general, because the start node // of an underlying graph is instead an _end_ node in the reversed graph. // But would be possible to define another wrapper type that adds an explicit // start node to its underlying graph, if desired. impl Successors for ReversedGraph { fn successors(&self, node: Self::Node) -> impl Iterator { self.inner.predecessors(node) } } impl Predecessors for ReversedGraph { fn predecessors(&self, node: Self::Node) -> impl Iterator { self.inner.successors(node) } }