// $Id: PriorityQueue.h,v 1.8 2002/10/11 02:20:15 leres Exp $
//
// Copyright (c) 1996, 1997, 1998, 1999, 2001, 2002
//      The Regents of the University of California.  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that: (1) source code distributions
// retain the above copyright notice and this paragraph in its entirety, (2)
// distributions including binary code include the above copyright notice and
// this paragraph in its entirety in the documentation or other materials
// provided with the distribution, and (3) all advertising materials mentioning
// features or use of this software display the following acknowledgement:
// ``This product includes software developed by the University of California,
// Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
// the University nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#ifndef __PriorityQueue__
#define __PriorityQueue__

#include <math.h>

class PriorityQueue;

class PQ_Element {
public:
	PQ_Element(double t)		{ time = t; }
	virtual ~PQ_Element()		{ }

	double Time() const		{ return time; }

protected:
	double time;
};

class PriorityQueue {
public:
	PriorityQueue(int initial_size = 16);
	~PriorityQueue();

	// Returns the top of queue, or nil if the queue is empty.
	PQ_Element* Top() const
		{
		if ( heap_size == 0 )
			return 0;
		else
			return heap[0];
		}

	PQ_Element* Remove();	// Removes (and returns) top of queue.  Returns
				// nil if the queue is empty.

	// Add a new element to the queue.  Returns 0 on failure (not enough
	// memory to add the element), 1 on success.
	int Add(PQ_Element* e);

	int Size() const	{ return heap_size; }
	int PeakSize() const	{ return peak_heap_size; }

protected:
	int Resize(int new_size);

	void BubbleUp(int bin);
	void BubbleDown(int bin);

	int Parent(int bin) const
		{
		return bin >> 1;
		}

	int LeftChild(int bin) const
		{
		return bin << 1;
		}

	int RightChild(int bin) const
		{
		return LeftChild(bin) + 1;
		}

	void Swap(int bin1, int bin2)
		{
		PQ_Element* t = heap[bin1];
		heap[bin1] = heap[bin2];
		heap[bin2] = t;
		}

	PQ_Element** heap;
	int heap_size;
	int peak_heap_size;
	int max_heap_size;
};

#endif
