Skip to content

Commit fa134b5

Browse files
committed
Add graph::depth_first_search_as_undirected
1 parent 7d2cb3d commit fa134b5

File tree

1 file changed

+26
-0
lines changed
  • compiler/rustc_data_structures/src/graph

1 file changed

+26
-0
lines changed

compiler/rustc_data_structures/src/graph/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,29 @@ where
5252
{
5353
iterate::DepthFirstSearch::new(graph).with_start_node(from)
5454
}
55+
56+
pub fn depth_first_search_as_undirected<G>(
57+
graph: G,
58+
from: G::Node,
59+
) -> iterate::DepthFirstSearch<impl Successors<Node = G::Node>>
60+
where
61+
G: Successors + Predecessors,
62+
{
63+
struct AsUndirected<G>(G);
64+
65+
impl<G: DirectedGraph> DirectedGraph for AsUndirected<G> {
66+
type Node = G::Node;
67+
68+
fn num_nodes(&self) -> usize {
69+
self.0.num_nodes()
70+
}
71+
}
72+
73+
impl<G: Successors + Predecessors> Successors for AsUndirected<G> {
74+
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
75+
self.0.successors(node).chain(self.0.predecessors(node))
76+
}
77+
}
78+
79+
iterate::DepthFirstSearch::new(AsUndirected(graph)).with_start_node(from)
80+
}

0 commit comments

Comments
 (0)