Skip to content

Commit 128d35b

Browse files
committed
Messed with some data types and added goalNode as a parameter to the earlyExit callback.
1 parent a583238 commit 128d35b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

AStar.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,21 @@ static inline Node GetNode(VisitedNodes nodes, void *nodeKey)
225225
}
226226

227227
// looks it up in the index, if it's not found it inserts a new record in the sorted index and the nodeRecords array and returns a reference to it
228-
int first = 0;
228+
size_t first = 0;
229229

230230
if (nodes->nodeRecordsCount > 0) {
231-
int last = nodes->nodeRecordsCount-1;
231+
size_t last = nodes->nodeRecordsCount-1;
232232

233233
while (first <= last) {
234-
const int mid = (first + last) / 2;
234+
const size_t mid = (first + last) / 2;
235235
const int comp = NodeKeyCompare(NodeMake(nodes, nodes->nodeRecordsIndex[mid]), nodeKey);
236236

237237
if (comp < 0) {
238238
first = mid + 1;
239-
} else if (comp > 0) {
239+
} else if (comp > 0 && mid > 0) {
240240
last = mid - 1;
241+
} else if (comp > 0) {
242+
break;
241243
} else {
242244
return NodeMake(nodes, nodes->nodeRecordsIndex[mid]);
243245
}
@@ -427,7 +429,7 @@ ASPath ASPathCreate(const ASPathNodeSource *source, void *context, void *startNo
427429
// perform the A* algorithm
428430
while (HasOpenNode(visitedNodes) && !NodeIsGoal((current = GetOpenNode(visitedNodes)))) {
429431
if (source->earlyExit) {
430-
const int shouldExit = source->earlyExit(visitedNodes->nodeRecordsCount, GetNodeKey(current), context);
432+
const int shouldExit = source->earlyExit(visitedNodes->nodeRecordsCount, GetNodeKey(current), goalNodeKey, context);
431433

432434
if (shouldExit > 0) {
433435
SetNodeIsGoal(current);

AStar.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ typedef struct __ASNeighborList *ASNeighborList;
3636
typedef struct __ASPath *ASPath;
3737

3838
typedef struct {
39-
size_t nodeSize; // the size of the structure being used for the nodes - important since nodes are copied into the resulting path
40-
void (*nodeNeighbors)(ASNeighborList neighbors, void *node, void *context); // add nodes to the neighbor list if they are connected to this node
41-
float (*pathCostHeuristic)(void *fromNode, void *toNode, void *context); // estimated cost to transition from the first node to the second node -- optional, uses 0 if not specified
42-
int (*earlyExit)(size_t visitedCount, void *visitingNode, void *context); // early termination, return 1 for success, -1 for failure, 0 to continue searching -- optional
43-
int (*nodeComparator)(void *node1, void *node2, void *context); // must return a sort order for the nodes (-1, 0, 1) -- optional, uses memcmp if not specified
39+
size_t nodeSize; // the size of the structure being used for the nodes - important since nodes are copied into the resulting path
40+
void (*nodeNeighbors)(ASNeighborList neighbors, void *node, void *context); // add nodes to the neighbor list if they are connected to this node
41+
float (*pathCostHeuristic)(void *fromNode, void *toNode, void *context); // estimated cost to transition from the first node to the second node -- optional, uses 0 if not specified
42+
int (*earlyExit)(size_t visitedCount, void *visitingNode, void *goalNode, void *context); // early termination, return 1 for success, -1 for failure, 0 to continue searching -- optional
43+
int (*nodeComparator)(void *node1, void *node2, void *context); // must return a sort order for the nodes (-1, 0, 1) -- optional, uses memcmp if not specified
4444
} ASPathNodeSource;
4545

4646
// use in the nodeNeighbors callback to return neighbors

0 commit comments

Comments
 (0)