clean up dijkstra
This commit is contained in:
+10
-8
@@ -96,18 +96,18 @@ where
|
||||
while !(self.unvisited_without_distance.is_empty()
|
||||
&& self.unvisited_with_distance.is_empty())
|
||||
{
|
||||
let coord = self.get_unvisited_coord_with_smallest_distance_from_start();
|
||||
let neighbours = self.get_neighbours(&coord);
|
||||
let previous_distance = *self.matrix.get(coord).unwrap().get_distance().unwrap();
|
||||
let to_visit = self.get_unvisited_coord_with_smallest_distance_from_start();
|
||||
self.visit(to_visit);
|
||||
let neighbours = self.get_neighbours(&to_visit);
|
||||
let previous_distance = *self.matrix.get(to_visit).unwrap().get_distance().unwrap();
|
||||
for neighbour in neighbours {
|
||||
self.update_neighbour(neighbour, coord, previous_distance);
|
||||
self.update_neighbour(neighbour, to_visit, previous_distance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_unvisited_coord_with_smallest_distance_from_start(&mut self) -> Coordinate {
|
||||
let coord = self
|
||||
.unvisited_with_distance
|
||||
self.unvisited_with_distance
|
||||
.iter()
|
||||
.filter_map(|c| {
|
||||
let node = self.matrix.get(*c).unwrap();
|
||||
@@ -115,9 +115,11 @@ where
|
||||
})
|
||||
.reduce(|(c0, v0), (c1, v1)| if v0 < v1 { (c0, v0) } else { (c1, v1) })
|
||||
.map(|(c, _)| *c)
|
||||
.unwrap_or(self.start);
|
||||
.unwrap_or(self.start)
|
||||
}
|
||||
|
||||
fn visit(&mut self, coord: (usize, usize)) {
|
||||
self.unvisited_with_distance.remove(&coord);
|
||||
coord
|
||||
}
|
||||
|
||||
fn get_neighbours(&self, coordinate: &Coordinate) -> Vec<Coordinate> {
|
||||
|
||||
Reference in New Issue
Block a user