From 05d5cf3d7957d9345985ece508a619a61faaaf2e Mon Sep 17 00:00:00 2001 From: David Baer Date: Mon, 17 Jan 2022 14:11:52 -0500 Subject: [PATCH] Use generic PQ --- lib/pq.c | 110 ----------------------------------------------------- lib/pq.h | 102 +++++++++++++++++++++++++++++++++++++++++++++---- lib/prim.c | 57 +++++++++++++++++---------- 3 files changed, 130 insertions(+), 139 deletions(-) delete mode 100644 lib/pq.c diff --git a/lib/pq.c b/lib/pq.c deleted file mode 100644 index b221aca..0000000 --- a/lib/pq.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include "pq.h" - -typedef struct { - unsigned int priority; - size_t x, y; - mazeedge_dir_t dir; -} _point_t; - -struct pq { - size_t heap_size, next; - _point_t* heap; -}; - -struct pq* pq_new(size_t heap_size) { - struct pq* result = malloc(sizeof(struct pq)); - result->heap_size = heap_size; - result->next = 0; - result->heap = calloc(heap_size, sizeof(_point_t)); - return result; -} - -static void _swap(struct pq* q, size_t idx1, size_t idx2) { - unsigned int priority = q->heap[idx1].priority; - size_t x = q->heap[idx1].x; - size_t y = q->heap[idx1].y; - mazeedge_dir_t dir = q->heap[idx1].dir; - - q->heap[idx1].priority = q->heap[idx2].priority; - q->heap[idx1].x = q->heap[idx2].x; - q->heap[idx1].y = q->heap[idx2].y; - q->heap[idx1].dir = q->heap[idx2].dir; - - q->heap[idx2].priority = priority; - q->heap[idx2].x = x; - q->heap[idx2].y = y; - q->heap[idx2].dir = dir; -} - -static void _heapify_up(struct pq* q) { - size_t i = q->next - 1; - while (i > 0) { - size_t parent = i / 2; - if (q->heap[parent].priority > q->heap[i].priority) { - _swap(q, parent, i); - i = parent; - } else { - // done - return; - } - } -} - -void pq_add(struct pq* q, unsigned int priority, size_t x, size_t y, mazeedge_dir_t dir) { - q->heap[q->next].x = x; - q->heap[q->next].y = y; - q->heap[q->next].priority = priority; - q->heap[q->next].dir = dir; - q->next++; - _heapify_up(q); -} - -bool pq_empty(struct pq const* q) { - return q->next == 0; -} - -static void _heapify_down(struct pq* q) { - size_t i = 0; - while (i < q->next) { - size_t ch1 = i * 2, ch2 = i * 2 + 1; - if (ch1 >= q->next) return; - if (ch2 >= q->next) { - if (q->heap[ch1].priority < q->heap[i].priority) { - _swap(q, i, ch1); - } - return; - } - - // three-way compare - if ((q->heap[i].priority < q->heap[ch1].priority) && - (q->heap[i].priority < q->heap[ch2].priority)) { - return; // all done - partial order ensured - } - if (q->heap[ch1].priority < q->heap[ch2].priority) { - _swap(q, i, ch1); - i = ch1; - } else { - _swap(q, i, ch2); - i = ch2; - } - } -} - -int pq_pop(struct pq* q, size_t* x, size_t* y, mazeedge_dir_t* dir) { - if (pq_empty(q)) return 0; - *x = q->heap[0].x; - *y = q->heap[0].y; - *dir = q->heap[0].dir; - _swap(q, 0, q->next-1); - q->next--; - _heapify_down(q); - return 1; -} - -void pq_free(struct pq* q) { - free(q->heap); q->heap = NULL; - q->heap_size = 0; - q->next = 0; - free(q); -} diff --git a/lib/pq.h b/lib/pq.h index 12c7d41..05e3371 100644 --- a/lib/pq.h +++ b/lib/pq.h @@ -1,15 +1,101 @@ #ifndef _PQ_H #define _PQ_H 1 -#include -#include "grid.h" +#include +#include -struct pq; +#define DEFAULT_HEAP_SIZE 16 -struct pq* pq_new(size_t heap_size); -void pq_add(struct pq* q, unsigned int priority, size_t x, size_t y, mazeedge_dir_t dir); -int pq_pop(struct pq* q, size_t* x, size_t* y, mazeedge_dir_t *dir); -bool pq_empty(struct pq const* q); -void pq_free(struct pq* q); +#define DEFINE_PQ(T, N, P) \ +struct _##N##HeapNode { \ + P priority; \ + T data; \ +}; \ +typedef struct { \ + size_t heap_size, next; \ + struct _##N##HeapNode* heap; \ +} N; \ +static void _##N##_swap(N* pq, size_t i, size_t j) { \ + struct _##N##HeapNode tmp = pq->heap[i]; \ + pq->heap[i] = pq->heap[j]; \ + pq->heap[j] = tmp; \ +} \ +static void _##N##_heapify_up(N* pq) { \ + size_t i = pq->next - 1; \ + while (i > 0) { \ + size_t parent = i / 2; \ + if (pq->heap[parent].priority > pq->heap[i].priority) { \ + _##N##_swap(pq, parent, i); \ + i = parent; \ + } else return; \ + } \ +} \ +static void _##N##_heapify_down(N* pq) { \ + size_t i = 0; \ + while (i < pq->next) { \ + size_t ch1 = i * 2, ch2 = i * 2 + 1; \ + if (ch1 >= pq->next) return; \ + if (ch2 >= pq->next) { \ + if (pq->heap[ch1].priority < pq->heap[i].priority) { \ + _##N##_swap(pq, i, ch1); \ + } \ + return; \ + } \ + if ((pq->heap[i].priority < pq->heap[ch1].priority) && \ + (pq->heap[i].priority < pq->heap[ch2].priority)) { \ + return; \ + } \ + if (pq->heap[ch1].priority < pq->heap[ch2].priority) { \ + _##N##_swap(pq, i, ch1); \ + i = ch1; \ + } else { \ + _##N##_swap(pq, i, ch2); \ + i = ch2; \ + } \ + } \ +} + +#define NEW_PQ(T, N) \ + T N = (T) { .heap_size = DEFAULT_HEAP_SIZE, .next = 0, .heap = calloc(DEFAULT_HEAP_SIZE, sizeof(struct _##T##HeapNode)) } + +// WARNING: S cannot be an expression that has side effects! +#define NEW_PQ_SZ(T, N, S) \ + T N = (T) { .heap_size = (S), .next = 0, .heap = calloc((S), sizeof(struct _##T##HeapNode)) } + +#define PUSH_PQ(Q, N, I, P) { \ + if ((Q).next >= (Q).heap_size) { \ + (Q).heap = realloc((Q).heap, (Q).heap_size * 2 * sizeof(struct _##N##HeapNode)); \ + (Q).heap_size *= 2; \ + } \ + (Q).heap[(Q).next].data = (I); \ + (Q).heap[(Q).next].priority = (P); \ + (Q).next++; \ + _##N##_heapify_up(&(Q)); \ +} + +#define EMPTY_PQ(Q) ((Q).next == 0) + +// check for emptiness first! +#define POP_PQ(Q, N, DST_I, DST_P) { \ + DST_I = (Q).heap[0].data; \ + DST_P = (Q).heap[0].priority; \ + _##N##_swap(&(Q), 0, (Q).next-1); \ + (Q).next--; \ + _##N##_heapify_down(&(Q)); \ +} + +#define FOREACH_PQ(Q, DST_DATA, DST_P) \ + for (size_t idx = 0; idx < (Q).next; idx++) { \ + DST_DATA = (Q).heap[idx].data; \ + DST_P = (Q).heap[idx].priority; + +#define FOREACH_PQ_END } + +// warning: be sure to clean up your data to avoid leaks! +#define FREE_PQ(Q) { \ + free((Q).heap); \ + (Q).heap_size = 0; \ + (Q).next = 0; \ +} #endif // !def(_PQ_H) diff --git a/lib/prim.c b/lib/prim.c index 148db39..e231728 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -2,42 +2,57 @@ #include "prim.h" #include "set.h" +typedef struct { + size_t x, y; + mazeedge_dir_t dir; +} _point_t; + +DEFINE_PQ(_point_t, PointPQ, unsigned int); + void prim(mazegrid_t const* g, mazegrid_t* result) { - struct pq* q = pq_new(g->width*g->height*4); + NEW_PQ_SZ(PointPQ, q, g->width*g->height*4); struct node_set* s = node_set_new(g->width, g->height); *result = mazegrid_new(g->width, g->height); node_set_add(s, 0, 0); - if (g->height > 1) pq_add(q, mazegrid_get_edge(g, 0, 0, EDGE_UP), 0, 1, EDGE_DOWN); - if (g->width > 1) pq_add(q, mazegrid_get_edge(g, 0, 0, EDGE_RIGHT), 1, 0, EDGE_LEFT); - - while (!pq_empty(q) && (node_set_size(s) < g->width * g->height)) { - size_t x, y; - mazeedge_dir_t dir; - pq_pop(q, &x, &y, &dir); - - if (node_set_contains(s, x, y)) continue; + if (g->height > 1) { + _point_t e = { 0, 1, EDGE_DOWN }; + PUSH_PQ(q, PointPQ, e, mazegrid_get_edge(g, 0, 0, EDGE_UP)); + } + if (g->width > 1) { + _point_t e = { 1, 0, EDGE_LEFT }; + PUSH_PQ(q, PointPQ, e, mazegrid_get_edge(g, 0, 0, EDGE_RIGHT)); + } + while (!EMPTY_PQ(q) && (node_set_size(s) < g->width * g->height)) { + _point_t e; + unsigned int p; + POP_PQ(q, PointPQ, e, p); + if (node_set_contains(s, e.x, e.y)) continue; // add edge to tree - mazegrid_set_edge(result, x, y, dir, 1); + mazegrid_set_edge(result, e.x, e.y, e.dir, 1); // add neighbors - if ((x > 0) && !node_set_contains(s, x - 1, y)) { - pq_add(q, mazegrid_get_edge(g, x, y, EDGE_LEFT), x - 1, y, EDGE_RIGHT); + if ((e.x > 0) && !node_set_contains(s, e.x - 1, e.y)) { + _point_t pt = { e.x - 1, e.y, EDGE_RIGHT }; + PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_LEFT)); } - if ((x < g->width - 1) && !node_set_contains(s, x + 1, y)) { - pq_add(q, mazegrid_get_edge(g, x, y, EDGE_RIGHT), x + 1, y, EDGE_LEFT); + if ((e.x < g->width - 1) && !node_set_contains(s, e.x + 1, e.y)) { + _point_t pt = { e.x + 1, e.y, EDGE_LEFT }; + PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_LEFT)); } - if ((y > 0) && !node_set_contains(s, x, y - 1)) { - pq_add(q, mazegrid_get_edge(g, x, y, EDGE_DOWN), x, y - 1, EDGE_UP); + if ((e.y > 0) && !node_set_contains(s, e.x, e.y - 1)) { + _point_t pt = { e.x, e.y - 1, EDGE_UP }; + PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_DOWN)); } - if ((y < g->height - 1) && !node_set_contains(s, x, y + 1)) { - pq_add(q, mazegrid_get_edge(g, x, y, EDGE_UP), x, y + 1, EDGE_DOWN); + if ((e.y < g->height - 1) && !node_set_contains(s, e.x, e.y + 1)) { + _point_t pt = { e.x, e.y + 1, EDGE_DOWN }; + PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_UP)); } - node_set_add(s, x, y); + node_set_add(s, e.x, e.y); } node_set_free(s); - pq_free(q); + FREE_PQ(q); }