Skip to content

Commit

Permalink
remove unnecessary visited set
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Nov 9, 2024
1 parent 8583e27 commit a8baa76
Showing 1 changed file with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.onthegomap.planetiler.geo.GeoUtils;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -123,13 +122,11 @@ record HasLoop(Edge edge, double distance) {}

private double shortestDistance(Node start, Node end, Node exclude, double maxLength) {
Map<Integer, Double> bestDistance = new HashMap<>();
BitSet visitedNodes = new BitSet();
visitedNodes.set(exclude.id);
// BitSet visitedNodes = new BitSet();
record Candidate(Node node, double cost, double heuristic) {}
PriorityQueue<Candidate> frontier = new PriorityQueue<>(Comparator.comparingDouble(Candidate::heuristic));
if (!visitedNodes.get(start.id)) {
if (exclude != start) {
frontier.offer(new Candidate(start, 0, start.distance(end)));
visitedNodes.set(start.id);
}
while (!frontier.isEmpty()) {
Candidate candidate = frontier.poll();
Expand All @@ -140,15 +137,15 @@ record Candidate(Node node, double cost, double heuristic) {}

for (var edge : current.getEdges()) {
var neighbor = edge.to;
if (!visitedNodes.get(neighbor.id)) {
if (neighbor != exclude) {
double newDist = candidate.cost + edge.length;
double prev = bestDistance.getOrDefault(neighbor.id, Double.POSITIVE_INFINITY);
if (newDist < prev) {
bestDistance.put(neighbor.id, newDist);
double heuristic = newDist + neighbor.distance(end);
if (heuristic <= maxLength) {
frontier.offer(new Candidate(neighbor, newDist, heuristic));
visitedNodes.set(neighbor.id);
// visitedNodes.set(neighbor.id);
}
}
}
Expand Down

0 comments on commit a8baa76

Please sign in to comment.