/* Priority queue as heap code V1.21 23/3/05
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _PQUEUE_H
#define _PQUEUE_H

typedef struct {
	int size,maxsize;
	int (*cmp)(void *,void *);
	void *queue[0];
} pqueue;

extern pqueue *pqueue_new(int size,int (*cmp)(void *,void *));
/* Create new priority queue
   size is how many elements it can hold
   cmp is a function which should return >0 if the 1st arg has a higher priority than the 2nd (and <0 if lower, =0 if equal); highest priority items are at the start of the queue.
*/

extern void pqueue_free(pqueue *);
/* Destroy priority queue
   Doesn't attempt to delete any of the ptr's contained in the queue
*/

extern void *pqueue_head(pqueue *p);
/* Return head of queue, or 0 if empty
*/

extern void pqueue_pop(pqueue *p);
/* Pop head off of queue
   Does nothing if the queue is empty
*/

extern int pqueue_add(pqueue *p,void *i);
/* Add item i to the queue
   Returns 0 on success, !0 if the queue was full
*/

extern int pqueue_remove(pqueue *p,void *i);
/* Remove item i from the queue, wherever it may be
   Performs a linear search to find the item, so may be slow
   Returns 0 on success, !0 if the item wasn't found
*/

extern int pqueue_numitems(pqueue *p);
/* Return how many items are in the queue
*/

#endif
