Add BfsImplResult and DfsImplResult as named return structs
This commit is contained in:
+36
-22
@@ -122,12 +122,12 @@ where
|
|||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
let mut predecessors = graph.vertex_map(None);
|
let mut predecessors = graph.vertex_map(None);
|
||||||
let (distances, _) = bfs_impl(graph, source, |neighbor, predecessor| {
|
let result = bfs_impl(graph, source, |neighbor, predecessor| {
|
||||||
predecessors[neighbor] = Some(predecessor);
|
predecessors[neighbor] = Some(predecessor);
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
BfsResult {
|
BfsResult {
|
||||||
distances,
|
distances: result.distances,
|
||||||
predecessors,
|
predecessors,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ pub fn bfs_distances<G>(graph: &G, source: G::Vertex) -> VertexMap<G::Vertex, Op
|
|||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
bfs_impl(graph, source, |_, _| true).0
|
bfs_impl(graph, source, |_, _| true).distances
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<u32>
|
pub fn bfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<u32>
|
||||||
@@ -154,14 +154,15 @@ where
|
|||||||
if predicate(source) {
|
if predicate(source) {
|
||||||
return Some((source, 0));
|
return Some((source, 0));
|
||||||
}
|
}
|
||||||
bfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).1
|
bfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).found
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bfs_impl<G, F>(
|
struct BfsImplResult<V: Copy> {
|
||||||
graph: &G,
|
distances: VertexMap<V, Option<u32>>,
|
||||||
source: G::Vertex,
|
found: Option<(V, u32)>,
|
||||||
mut on_discover: F,
|
}
|
||||||
) -> (VertexMap<G::Vertex, Option<u32>>, Option<(G::Vertex, u32)>)
|
|
||||||
|
fn bfs_impl<G, F>(graph: &G, source: G::Vertex, mut on_discover: F) -> BfsImplResult<G::Vertex>
|
||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
||||||
@@ -178,13 +179,19 @@ where
|
|||||||
let distance = distances[v].unwrap() + 1;
|
let distance = distances[v].unwrap() + 1;
|
||||||
distances[neighbor] = Some(distance);
|
distances[neighbor] = Some(distance);
|
||||||
if !on_discover(neighbor, v) {
|
if !on_discover(neighbor, v) {
|
||||||
return (distances, Some((neighbor, distance)));
|
return BfsImplResult {
|
||||||
|
distances,
|
||||||
|
found: Some((neighbor, distance)),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
queue.push_back(neighbor);
|
queue.push_back(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(distances, None)
|
BfsImplResult {
|
||||||
|
distances,
|
||||||
|
found: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DfsResult<V: Copy> {
|
pub struct DfsResult<V: Copy> {
|
||||||
@@ -197,12 +204,12 @@ where
|
|||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
let mut predecessors = graph.vertex_map(None);
|
let mut predecessors = graph.vertex_map(None);
|
||||||
let (visited, _) = dfs_impl(graph, source, |neighbor, predecessor| {
|
let result = dfs_impl(graph, source, |neighbor, predecessor| {
|
||||||
predecessors[neighbor] = Some(predecessor);
|
predecessors[neighbor] = Some(predecessor);
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
DfsResult {
|
DfsResult {
|
||||||
visited,
|
visited: result.visited,
|
||||||
predecessors,
|
predecessors,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,7 +218,7 @@ pub fn dfs_visited<G>(graph: &G, source: G::Vertex) -> VertexMap<G::Vertex, bool
|
|||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
{
|
{
|
||||||
dfs_impl(graph, source, |_, _| true).0
|
dfs_impl(graph, source, |_, _| true).visited
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> bool
|
pub fn dfs_find<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> bool
|
||||||
@@ -229,14 +236,15 @@ where
|
|||||||
if predicate(source) {
|
if predicate(source) {
|
||||||
return Some(source);
|
return Some(source);
|
||||||
}
|
}
|
||||||
dfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).1
|
dfs_impl(graph, source, |neighbor, _| !predicate(neighbor)).found
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dfs_impl<G, F>(
|
struct DfsImplResult<V: Copy> {
|
||||||
graph: &G,
|
visited: VertexMap<V, bool>,
|
||||||
source: G::Vertex,
|
found: Option<V>,
|
||||||
mut on_discover: F,
|
}
|
||||||
) -> (VertexMap<G::Vertex, bool>, Option<G::Vertex>)
|
|
||||||
|
fn dfs_impl<G, F>(graph: &G, source: G::Vertex, mut on_discover: F) -> DfsImplResult<G::Vertex>
|
||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
F: FnMut(G::Vertex, G::Vertex) -> bool,
|
||||||
@@ -248,7 +256,10 @@ where
|
|||||||
while let Some((v, predecessor)) = stack.pop() {
|
while let Some((v, predecessor)) = stack.pop() {
|
||||||
if let Some(p) = predecessor {
|
if let Some(p) = predecessor {
|
||||||
if !on_discover(v, p) {
|
if !on_discover(v, p) {
|
||||||
return (visited, Some(v));
|
return DfsImplResult {
|
||||||
|
visited,
|
||||||
|
found: Some(v),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for neighbor in graph.adjacent_vertices(v) {
|
for neighbor in graph.adjacent_vertices(v) {
|
||||||
@@ -258,7 +269,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(visited, None)
|
DfsImplResult {
|
||||||
|
visited,
|
||||||
|
found: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dfs_find_path<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<Vec<G::Edge>>
|
pub fn dfs_find_path<G>(graph: &G, source: G::Vertex, target: G::Vertex) -> Option<Vec<G::Edge>>
|
||||||
|
|||||||
Reference in New Issue
Block a user