// queue standard header
#pragma once
#ifndef _QUEUE_
#define _QUEUE_
#ifndef RC_INVOKED
#include "drod_xutility.hpp"
#include "drod_algorithm.hpp"
#include <deque>
#include <functional>
#include <vector>

#ifdef _MSC_VER
 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
#endif  /* _MSC_VER */
_STD_BEGIN

		// TEMPLATE CLASS queue
template<class _Ty,
	class _Container = deque<_Ty> >
	class queue
	{	// FIFO queue implemented with a container
public:
	typedef _Container container_type;
	typedef typename _Container::value_type value_type;
	typedef typename _Container::size_type size_type;
	typedef typename _Container::reference reference;
	typedef typename _Container::const_reference const_reference;

	queue()
		: c()
		{	// construct with empty container
		}

	explicit queue(const _Container& _Cont)
		: c(_Cont)
		{	// construct by copying specified container
		}

	bool empty() const
		{	// test if queue is empty
		return (c.empty());
		}

	size_type size() const
		{	// return length of queue
		return (c.size());
		}

	reference front()
		{	// return first element of mutable queue
		return (c.front());
		}

	const_reference front() const
		{	// return first element of nonmutable queue
		return (c.front());
		}

	reference back()
		{	// return last element of mutable queue
		return (c.back());
		}

	const_reference back() const
		{	// return last element of nonmutable queue
		return (c.back());
		}

	void push(const value_type& _Val)
		{	// insert element at beginning
		c.push_back(_Val);
		}

	void pop()
		{	// erase element at end
		c.pop_front();
		}

	const _Container& _Get_container() const
		{	// get reference to container
		return (c);
		}

protected:
	_Container c;	// the underlying container
	};

		// queue TEMPLATE FUNCTIONS
template<class _Ty,
	class _Container> inline
	bool operator==(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test for queue equality
	return (_Left._Get_container() == _Right._Get_container());
	}

template<class _Ty,
	class _Container> inline
	bool operator!=(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test for queue inequality
	return (!(_Left == _Right));
	}

template<class _Ty,
	class _Container> inline
	bool operator<(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test if _Left < _Right for queues
	return (_Left._Get_container() < _Right._Get_container());
	}

template<class _Ty,
	class _Container> inline
	bool operator>(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test if _Left > _Right for queues
	return (_Right < _Left);
	}

template<class _Ty,
	class _Container> inline
	bool operator<=(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test if _Left <= _Right for queues
	return (!(_Right < _Left));
	}

template<class _Ty,
	class _Container> inline
	bool operator>=(const queue<_Ty, _Container>& _Left,
		const queue<_Ty, _Container>& _Right)
	{	// test if _Left >= _Right for queues
	return (!(_Left < _Right));
	}

		// TEMPLATE CLASS priority_queue
template<class _Ty,
	class _Container = vector<_Ty>,
	class _Pr = less<typename _Container::value_type> >
	class priority_queue
	{	// priority queue implemented with a _Container
public:
	typedef _Container container_type;
	typedef typename _Container::value_type value_type;
	typedef typename _Container::size_type size_type;
	typedef typename _Container::reference reference;
	typedef typename _Container::const_reference const_reference;

	priority_queue()
		: c(), comp()
		{	// construct with empty container, default comparator
		}

	explicit priority_queue(const _Pr& _Pred)
		: c(), comp(_Pred)
		{	// construct with empty container, specified comparator
		}

	priority_queue(const _Pr& _Pred, const _Container& _Cont)
		: c(_Cont), comp(_Pred)
		{	// construct by copying specified container, comparator
		make_heap(c.begin(), c.end(), comp);
		}

	template<class _Iter>
		priority_queue(_Iter _First, _Iter _Last)
		: c(_First, _Last), comp()
		{	// construct by copying [_First, _Last), default comparator
		make_heap(c.begin(), c.end(), comp);
		}

	template<class _Iter>
		priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred)
		: c(_First, _Last), comp(_Pred)
		{	// construct by copying [_First, _Last), specified comparator
		make_heap(c.begin(), c.end(), comp);
		}

	template<class _Iter>
		priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred,
			const _Container& _Cont)
		: c(_Cont), comp(_Pred)
		{	// construct by copying [_First, _Last), container, and comparator
		c.insert(c.end(), _First, _Last);
		make_heap(c.begin(), c.end(), comp);
		}

	bool empty() const
		{	// test if queue is empty
		return (c.empty());
		}

	size_type size() const
		{	// return length of queue
		return (c.size());
		}

	const_reference top() const
		{	// return highest-priority element
		return (c.front());
		}

	reference top()
		{	// return mutable highest-priority element (retained)
		return (c.front());
		}

	void push(const value_type& _Pred)
		{	// insert value in priority order
		c.push_back(_Pred);
		push_heap(c.begin(), c.end(), comp);
		}

	void pop()
		{	// erase highest-priority element
		pop_heap(c.begin(), c.end(), comp);
		c.pop_back();
		}

protected:
	_Container c;	// the underlying container
	_Pr comp;	// the comparator functor
	};
_STD_END
#ifdef _MSC_VER
 #pragma warning(pop)
 #pragma pack(pop)
#endif  /* _MSC_VER */

#endif /* RC_INVOKED */
#endif /* _queue_ */

/*
 * Copyright (c) 1992-2007 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 */

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright notice
 * appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 V5.03:0009 */
