about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/graph
diff options
context:
space:
mode:
authorMaybe Waffle <waffle.lapkin@gmail.com>2024-04-15 18:20:30 +0000
committerMaybe Waffle <waffle.lapkin@gmail.com>2024-04-15 23:20:52 +0000
commitfa134b5e0f87c44074549af8bd6d7479b0954799 (patch)
treea51d2ff1f991f17206432176ee40b1091e7820ca /compiler/rustc_data_structures/src/graph
parent7d2cb3dda732028c7f8dfa8de5876da5be4bad9d (diff)
downloadrust-fa134b5e0f87c44074549af8bd6d7479b0954799.tar.gz
rust-fa134b5e0f87c44074549af8bd6d7479b0954799.zip
Add `graph::depth_first_search_as_undirected`
Diffstat (limited to 'compiler/rustc_data_structures/src/graph')
-rw-r--r--compiler/rustc_data_structures/src/graph/mod.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/graph/mod.rs b/compiler/rustc_data_structures/src/graph/mod.rs
index 0906c04716b..103ddd917bf 100644
--- a/compiler/rustc_data_structures/src/graph/mod.rs
+++ b/compiler/rustc_data_structures/src/graph/mod.rs
@@ -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)
+}