jenerated

pqueue

About

Exactly what it says on the tin. Features some amusing hacks to approximate C++'s templates.

Why not just use C++, you say? I needed this piece of code for CS452 (Real-Time Operating Systems). You're more or less stuck with a very specific version of gcc, and it's kind of painful and not really worth the time to get a C++ compiler working the way you want.

Usage

To use, define BETTER(a, b), SET(a, b) and PQnode in pqueue.h and run make.

For a priority queue of ints sorted from low to high:

#define PQnode int
#define BETTER(a, b) (a < b)
#define SET(a, b) a = b
#define pqnode_destroy(a)

For a priority queue of struct pointers, you might want something along the lines of:

typedef struct node {
    float heur; // heuristic. Sort heap based on this value
    int data;
} node;
#define PQnode node*
#define BETTER(a, b) (a->heur > b->heur)
#define SET(a, b) a = b
#define pqnode_destroy(a) free(a)

For a priority queue of structs:

typedef struct node {
    float heur; // heuristic. Sort heap based on this value
    int data;
} node;
#define PQnode node
#define BETTER(a, b) (a.heur > b.heur)
#define SET(a, b) a.heur = b.heur; a.data = b.data
#define pqnode_destroy(a)

The library exposes the following functions:

void pq_push(priority_queue *PQ, PQnode new_node);
PQnode pq_pop(priority_queue *PQ);
priority_queue* pq_create(int initial_max_size);
priority_queue* pq_from_array(PQnode *array, int array_size);
void pq_destroy(priority_queue *PQ);

Downloads

pqueue.zip