From 8756fa7dcc0163c0d29eeac281139fd13b3d9938 Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Wed, 2 Nov 2022 13:50:20 +0100 Subject: [PATCH] clean up dijkstra --- lib/dijkstra/src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/dijkstra/src/lib.rs b/lib/dijkstra/src/lib.rs index bc1eed1..8e87a8b 100644 --- a/lib/dijkstra/src/lib.rs +++ b/lib/dijkstra/src/lib.rs @@ -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 {