Take the shortest edge when constructing the path

Previously we assumed that there was a single edge between nodes. This
is true of the current implementation, but not in all experiments.

This is also a tiny performance win (0.2% instruction count reduction).
pull/305/head^2
Wilfred Hughes 2022-07-03 11:14:52 +07:00
parent 5c19ac600d
commit 186a2f98f1
1 changed files with 16 additions and 1 deletions

@ -113,14 +113,29 @@ fn edge_between<'a>(before: &Vertex<'a>, after: &Vertex<'a>) -> Edge {
let vertex_arena = Bump::new();
neighbours(before, &mut neighbour_buf, &vertex_arena);
let mut shortest_edge: Option<Edge> = None;
for neighbour in &mut neighbour_buf {
if let Some((edge, next)) = neighbour.take() {
// If there are multiple edges that can take us to `next`,
// prefer the shortest.
if next == after {
return edge;
let is_shorter = match shortest_edge {
Some(prev_edge) => edge.cost() < prev_edge.cost(),
None => true,
};
if is_shorter {
shortest_edge = Some(edge);
}
}
}
}
if let Some(edge) = shortest_edge {
return edge;
}
panic!(
"Expected a route between the two vertices {:#?} and {:#?}",
before, after