searchStep method

  1. @override
void searchStep(
  1. GraphDijkstraState state
)
override

Implementation

@override
void searchStep(GraphDijkstraState state) {
  // If there is no open node, we are stuck.
  if (state.open.isEmpty) {
    state.status = Status.failure;
    return;
  }

  ID pointId = state.open.removeFirst();
  GraphPoint point = state.getPoint(pointId);
  state.nodes[pointId] = GraphNode.closed;

  // If the target node is reached, we are done.
  if (state.nodes[state.targetId] == GraphNode.closed) {
    state.status = Status.success;
    return;
  }

  for (ID newId in state.edges[pointId] ?? []) {
    GraphNode newNode = state.getNode(newId);
    if (newNode == GraphNode.closed) continue;

    GraphPoint newPoint = state.getPoint(newId);
    double distToPrevious = newPoint.dist(point);
    double newGCost = state.gCost[pointId]! + distToPrevious;

    if (newGCost >= (state.gCost[newId] ?? double.infinity)) continue;

    state.parents[newId] = pointId;
    state.gCost[newId] = newGCost;

    if (newNode != GraphNode.open) {
      state.open.add(newId);
      state.nodes[newId] = GraphNode.open;
    }
  }
}