Skip to content

Commit

Permalink
Add graph::depth_first_search_as_undirected
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Apr 15, 2024
1 parent 7d2cb3d commit fa134b5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions compiler/rustc_data_structures/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,29 @@ where
{
iterate::DepthFirstSearch::new(graph).with_start_node(from)
}

pub fn depth_first_search_as_undirected<G>(
graph: G,
from: G::Node,
) -> iterate::DepthFirstSearch<impl Successors<Node = G::Node>>
where
G: Successors + Predecessors,
{
struct AsUndirected<G>(G);

impl<G: DirectedGraph> DirectedGraph for AsUndirected<G> {
type Node = G::Node;

fn num_nodes(&self) -> usize {
self.0.num_nodes()
}
}

impl<G: Successors + Predecessors> Successors for AsUndirected<G> {
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
self.0.successors(node).chain(self.0.predecessors(node))
}
}

iterate::DepthFirstSearch::new(AsUndirected(graph)).with_start_node(from)
}

0 comments on commit fa134b5

Please sign in to comment.