#include "PQ.h"
#include "node.h"
#include "parameters.h"
#include "depq.h"

//----------------------------------- internal
int PQ_size;
//----------------------------------- 


// initialize the priority queue
void PQ_init(void)
{
 PQ_size = 0;
 InitSplay();
}


// free all remaining nodes in the priority queue
void PQ_free_all(void)
{
 DisposeSplay();
}


// boolean
int PQ_empty(void)
{return PQ_size==0;}

// boolean
int PQ_queue_overflow(void)
{return PQ_size>MAX_QUEUE_SIZE;}



// add an item to the priority queue
void PQ_push(AstarNode *node)
{element data;

 #ifdef ASTAR_DEBUG
 if (PQ_size >= MAX_QUEUE_SIZE+Astar_doclen)
 	{perror("PQ_queue overflow!");
	 exit(EXIT_FAILURE);
	}
 #endif

 // major key: f=g+h (bigger is better)
 // what about minor key: partial_length (bigger is better)?
 data.key = node->g + node->h;
 data.node = node;
 InsertSplay(data);
 PQ_size++;

}


// pop out (i.e. remove) the item with the best (highest) f score
AstarNode *PQ_pop_best(void)
{
 element data;
 if (PQ_size<=0)
 	{perror("PQ_queue underflow!");
	 exit(EXIT_FAILURE);
	}
 DeleteMaxSplay(&data);
 PQ_size--;
 return data.node;
}


// pop out the item with the worst (smallest) f score
AstarNode *PQ_pop_worst(void)
{
 element data;
 if (PQ_size<=0)
 	{perror("PQ_queue underflow!");
	 exit(EXIT_FAILURE);
	}
 DeleteMinSplay(&data);
 PQ_size--;
 return data.node;
}

