From 065abf63fd79397552347c8f2d587cd99426a309 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Thu, 26 Mar 2026 13:50:12 +0300 Subject: Add ppq_insert() return codes + fix breadth_first_search_4dir() --- path.c | 7 ++++--- priority_queue.c | 10 +++++----- priority_queue.h | 3 +++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/path.c b/path.c index 6f00ee8..e32610b 100644 --- a/path.c +++ b/path.c @@ -49,8 +49,9 @@ Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Positi /* The Russian constitution doesn't allow walking on walls */ if (map[na[i].y][na[i].x] == WALL) continue; - ppq_insert(&frontier, na[i], 0); - path[na[i].y][na[i].x].parent = cur; + if (ppq_insert(&frontier, na[i], 0) != PPQ_INSERT_ALREADY) { + path[na[i].y][na[i].x].parent = cur; + } } if (should_anim) { @@ -91,7 +92,7 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi /* The Russian constitution doesn't allow walking on walls */ if (map[na[i].y][na[i].x] == WALL) continue; - if (ppq_insert(&frontier, na[i], 0) != 3) { + if (ppq_insert(&frontier, na[i], 0) != PPQ_INSERT_ALREADY) { path[na[i].y][na[i].x].parent = cur; } } diff --git a/priority_queue.c b/priority_queue.c index d4be6e2..6e74b22 100644 --- a/priority_queue.c +++ b/priority_queue.c @@ -20,7 +20,7 @@ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority) { if ((*ppq) == NULL) { (*ppq) = ppq_new(pos, priority); - return 1; + return PPQ_INSERT_NEW; } PositionPQ *n = ppq_new(pos, priority); @@ -28,7 +28,7 @@ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority) { if (start->priority > priority) { n->next = start; start = n; - return 2; + return PPQ_INSERT_SUCCESS; } PositionPQ *temp = *ppq; @@ -36,19 +36,19 @@ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority) { while(temp->next != NULL && temp->next->priority <= priority) { if (temp->pos.x == pos.x && temp->pos.y == pos.y && temp->priority <= priority) { free(n); - return 3; /* pos is already in ppq with a fine priority */ + return PPQ_INSERT_ALREADY; } temp = temp->next; } if (temp->pos.x == pos.x && temp->pos.y == pos.y && temp->priority <= priority) { free(n); - return 3; /* pos is already in ppq with a fine priority */ + return PPQ_INSERT_ALREADY; } n->next = temp->next; temp->next = n; - return 4; + return PPQ_INSERT_SUCCESS; } Position ppq_pop(PositionPQ **ppq) { diff --git a/priority_queue.h b/priority_queue.h index 63e9bbc..13eea1a 100644 --- a/priority_queue.h +++ b/priority_queue.h @@ -19,6 +19,9 @@ typedef struct PositionPQNode_s PositionPQ; PositionPQ *ppq_new(Position pos, size_t priority); /* Insert a pos with priority into a given PositionPQ */ +#define PPQ_INSERT_SUCCESS 0 +#define PPQ_INSERT_NEW 1 /* ppq was NULL, created a new one */ +#define PPQ_INSERT_ALREADY 2 /* pos is already in ppq */ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority); /* Remove and return the position with the lowest priority */ -- cgit v1.2.3