Make graph::DepthFirstSearch accept G by value

It's required for the next commit.

Note that you can still have `G = &H`, since there are implementations of all
the graph traits for references.
This commit is contained in:
Maybe Waffle 2024-04-15 18:17:51 +00:00
parent 86a576528c
commit 7d2cb3dda7
2 changed files with 13 additions and 13 deletions

View File

@ -70,21 +70,21 @@ pub fn reverse_post_order<G: DirectedGraph + Successors>(
} }
/// A "depth-first search" iterator for a directed graph. /// A "depth-first search" iterator for a directed graph.
pub struct DepthFirstSearch<'graph, G> pub struct DepthFirstSearch<G>
where where
G: ?Sized + DirectedGraph + Successors, G: DirectedGraph + Successors,
{ {
graph: &'graph G, graph: G,
stack: Vec<G::Node>, stack: Vec<G::Node>,
visited: BitSet<G::Node>, visited: BitSet<G::Node>,
} }
impl<'graph, G> DepthFirstSearch<'graph, G> impl<G> DepthFirstSearch<G>
where where
G: ?Sized + DirectedGraph + Successors, G: DirectedGraph + Successors,
{ {
pub fn new(graph: &'graph G) -> Self { pub fn new(graph: G) -> Self {
Self { graph, stack: vec![], visited: BitSet::new_empty(graph.num_nodes()) } Self { stack: vec![], visited: BitSet::new_empty(graph.num_nodes()), graph }
} }
/// Version of `push_start_node` that is convenient for chained /// Version of `push_start_node` that is convenient for chained
@ -125,9 +125,9 @@ where
} }
} }
impl<G> std::fmt::Debug for DepthFirstSearch<'_, G> impl<G> std::fmt::Debug for DepthFirstSearch<G>
where where
G: ?Sized + DirectedGraph + Successors, G: DirectedGraph + Successors,
{ {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = fmt.debug_set(); let mut f = fmt.debug_set();
@ -138,9 +138,9 @@ where
} }
} }
impl<G> Iterator for DepthFirstSearch<'_, G> impl<G> Iterator for DepthFirstSearch<G>
where where
G: ?Sized + DirectedGraph + Successors, G: DirectedGraph + Successors,
{ {
type Item = G::Node; type Item = G::Node;

View File

@ -46,9 +46,9 @@ where
.is_some() .is_some()
} }
pub fn depth_first_search<G>(graph: &G, from: G::Node) -> iterate::DepthFirstSearch<'_, G> pub fn depth_first_search<G>(graph: G, from: G::Node) -> iterate::DepthFirstSearch<G>
where where
G: ?Sized + Successors, G: Successors,
{ {
iterate::DepthFirstSearch::new(graph).with_start_node(from) iterate::DepthFirstSearch::new(graph).with_start_node(from)
} }