/*
* priorityqueue.h
*
* Priority queue library.
*
* Dependencies:
* - linked.{h,c}
* - stack.{h,c}
*/
#ifndef JB_PRIORITYQUEUE_H
#define JB_PRIORITYQUEUE_H
#include "linked.h"
/*
* structure - priorityQueue
*
* It's a queue... with priority!
*/
struct priorityQueue {
link* front;
int (*comparison)(link_value, link_value);
unsigned int size;
};
typedef struct priorityQueue priorityQueue;
/*
* function - newPriorityQueue
*
* Returns a pointer to a new priority queue using the specified comparison
* function. The function must return 1 if the first parameter has a higher
* priority than the second, -1 if vice-versa, and 0 if they are equal.
*/
priorityQueue* newPriorityQueue(int (*comparison)(link_value, link_value));
/*
* function - freePriorityQueue
*
* Releases the memory used by a priority queue.
*/
void freePriorityQueue(priorityQueue* q);
/*
* function - priorityQueuePush[_suffix]
*
* Pushes a value (specify the type by using the appropriate suffix) onto the
* priority queue, placing it in the correct location.
*/
void priorityQueuePush(priorityQueue* q, link_value value);
void priorityQueuePush_u(priorityQueue* q, link_u value);
void priorityQueuePush_i(priorityQueue* q, link_i value);
void priorityQueuePush_f(priorityQueue* q, link_f value);
void priorityQueuePush_p(priorityQueue* q, link_p value);
/*
* function - priorityQueuePop[_suffix]
*
*
* Pops a value (specify the type by using the appropriate suffix) off of the
* priority queue. Note that if a node's priority has changed since it was
* put on it will not be reevaulated automaticly. You must use
* priorityQueueReprioritize if your priorities are based on dynamic
* properties such as time.
*/
link_value priorityQueuePop(priorityQueue* q);
link_u priorityQueuePop_u(priorityQueue* q);
link_i priorityQueuePop_i(priorityQueue* q);
link_f priorityQueuePop_f(priorityQueue* q);
link_p priorityQueuePop_p(priorityQueue* q);
/*
* macro - priorityQueueFront[_suffix]
*
* Evaluates to the value (specify the type by using the appropriate suffix) at
* the front of the queue.
*/
#define priorityQueueFront(q) (q->front->value)
#define priorityQueueFront_u(q) (priorityQueueFront(q).u)
#define priorityQueueFront_i(q) (priorityQueueFront(q).i)
#define priorityQueueFront_f(q) (priorityQueueFront(q).f)
#define priorityQueueFront_p(q) (priorityQueueFront(q).p)
/*
* function - priorityQueueReprioritize
*
* Reevaluates the priority all nodes. This must be called before retriving any
* values if you include dynamic properties such as time in your priority
* evaluation.
*/
void priorityQueueReprioritize(priorityQueue* q);
#endif // #ifndef JB_PRIORITYQUEUE_H