/*
   File:        pheap.h
   Author(s):   Andrew Moore and Ting Liu
   Created:     Wed Nov 13 08:17:59 EST 2002
   Description: New Faster Heap of Pointers
   Copyright (c) Carnegie Mellon University
*/

/* Note: this is a FASTER REIMPLEMENTATION of struct sheap.

   struct sheap had too much unnecessary overhead, so should no longer
   be used. */

#ifndef pheap_H

#include "amut.h"

typedef struct ph_node
{
  void *data;
  double priority;
} ph_node;

typedef struct pheap
{
  int size;
  int array_size;
  ph_node **array;
} pheap;


/* Makes a pheap (Pointer HEAP) structure with initially nothing
   in the queue */
pheap *mk_pheap();

void fprintf_pheap(FILE *s,char *m1,pheap *x,char *m2);

void ppheap(pheap *x);

pheap *mk_copy_pheap(pheap *old);

/* Adds the given pointer to the heap with the given priority.
   Notice that it directly stores the pointer...it does not
   make a copy of the thing in "data". This means that the caller
   must not deallocate (or otherwise hurt) whatever it is that "data"
   points to until after we're done with using the pheap, or until
   "data" has been removed from the pheap. */
void add_to_pheap(pheap *ph,void *data,double priority);

/* The following three operations all refer to the "next_to_pop"
   item, which is the item with the smallest value of "priority". Ties are
   broken by some arbitrary rule that may depend on order of insertion. */

/* The priority of the item "next_to_pop" item on the heap (i.e. the
   item that has minimum priority). Note that this "next_to_pop" item is
   NOT popped by this operation. */
double pheap_minimum_priority(pheap *ph);

/* The data pointer of the "next_to_pop" item on the heap (i.e. the
   item that has minimum priority). Note that this "next_to_pop" item is
   NOT popped by this operation. */
void *pheap_data_with_minimum_priority(pheap *ph);

int pheap_size(pheap *ph);

int pheap_linear_search(pheap *ph, void* data);
void pheap_remove_element(pheap *ph, int hindex);
/* This deletes the lowest priority item. The data memory is not freed. */
void pheap_remove_minimum_priority(pheap *ph);

/* Frees the pheap and all memory that it used, except does not
   do anything with the defunct data items that had been stored within it.
   Thus if a data item had been stored in the heap, it is the callers
   responsibility to eventually clean up the memory for that data item. */
void free_pheap(pheap *ph);

#endif /* ifndef pheap_H */
