#ifndef _PQUEUE_H
#define _PQUEUE_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

typedef struct _pqelem_t pqelem_t;

typedef struct {
  int size,len;
  pqelem_t *q;     // q==NULL is error state!
  int (*compa)(const void *,const void *);
} PQUEUE;

// compare modes (i.e. max-heap or min-heap)
enum { PQ_MODE_HIGHEST, PQ_MODE_LOWEST, PQ_MODE_LOWEST_ALSO_DATAPTR };

// reservesize is only a hint, add_pq will realloc when required
PQUEUE *new_pq(int reservesize,int mode);
// or: for caller-allocated (stack-based) PQUEUE pq;
// and to be used with free_pq_data(&pq):
void init_pq(PQUEUE *pqueue,int reservesize,int mode);

// does not free user-data (from add_pq)!
void free_pq(PQUEUE *pqueue);
void free_pq_data(PQUEUE *pqueue);

void clear_pq(PQUEUE *pqueue);

void add_pq(PQUEUE *pqueue,int priority,intptr_t data);

// returns next priority  or INT_MIN [irrespective of mode!]
int peek_pq(PQUEUE *pqueue);

intptr_t get_pq(PQUEUE *pqueue); // or (intptr_t)0

static inline int haserror_pq(PQUEUE *pqueue) { return ( (!pqueue)||(!pqueue->q) ); }

#ifdef __cplusplus
}
#endif

#endif
