Decent A* improvement by changing the order of success testing.
@@ -104,12 +104,8 @@ impl AStar {
}
/// Adds a successor; if we're at the end, marks success.
- fn add_successor(&mut self, q: Node, idx: usize, cost: f32, map: &dyn BaseMap) -> bool {
+ fn add_successor(&mut self, q: Node, idx: usize, cost: f32, map: &dyn BaseMap) {
// Did we reach our goal?
- if idx == self.end {
- self.parents.insert(idx, q.idx);
- true
- } else {
let distance = self.distance_to_end(idx, map);
let s = Node {
idx,
@@ -134,9 +130,6 @@ impl AStar {
self.open_list.push(s);
self.parents.insert(idx, q.idx);
}
-
- false
- }
}
/// Helper function to unwrap a path once we've found the end-point.
@@ -164,16 +157,16 @@ impl AStar {
// Pop Q off of the list
let q = self.open_list.pop().unwrap();
-
- // Generate successors
- let successors = map.get_available_exits(q.idx);
-
- for s in successors {
- if q.idx != s.0 && self.add_successor(q, s.0, s.1 + q.f, map) {
+ if q.idx == self.end {
let success = self.found_it();
return success;
}
- }
+
+ // Generate successors
+ map
+ .get_available_exits(q.idx)
+ .iter()
+ .for_each(|s| self.add_successor(q, s.0, s.1 + q.f, map));
if self.closed_list.contains_key(&q.idx) {
self.closed_list.remove(&q.idx);
