#ifndef _HEAP_H
#define _HEAP_H

/*
Heap routines: a smallest-at-top heap.  You also supply a comparison
function.  You allocate a heap using HeapAlloc, and HeapCmp is a pointer
to a function to compare your entries.  HeapAlloc would like a
reasonable guess as to the maximum size the heap will grow, but if you
get it wrong, don't sweat it, these routines will use realloc(3) to
increase the size if needed.  Entries are of type foint (union of int
and void*), see misc.h for details.
*/

#include "misc.h"   /* for foint */

/* this is returned if any errors occur */
#define HEAPERROR ABSTRACT_ERROR

typedef struct _heaptype
{
    int HEAPSIZE;
    pCmpFcn HeapCmp;
    pFointFreeFcn FointFree;
    foint *heap;
} HEAP;


HEAP    *HeapAlloc(int maxNumber, pCmpFcn);
void    HeapReset(HEAP*);   /* make heap empty */
int     HeapSize(HEAP*);    /* how many things currently in heap? */
void    HeapFree(HEAP*);    /* free the entire heap */
foint   HeapPeek(HEAP*);    /* what's the top element? */
foint   HeapNext(HEAP*);    /* pop and return top element */
foint   HeapInsert(HEAP*, foint);
/*
** delete is slow O(n); if more than one match, only one is deleted, but
** no guarantees on which gets deleted.
*/
foint   HeapDelete(HEAP*, foint);
/*int     HeapSort(foint *list, pCmpFcn);*/
int     HeapSanityCheck(HEAP*);   /* sanity check, for debugging */
void    HeapPrint(HEAP*);   /* dump an entire heap.  You must define a HeapTypePrint function */
#if 0
void    HeapTypePrint(/*not-void*/);
#endif

#endif  /* _HEAP_H */
