250 lines
5.9 KiB
Plaintext
250 lines
5.9 KiB
Plaintext
|
// queue standard header
|
||
|
#pragma once
|
||
|
#ifndef _QUEUE_
|
||
|
#define _QUEUE_
|
||
|
#include <algorithm>
|
||
|
#include <deque>
|
||
|
#include <functional>
|
||
|
#include <vector>
|
||
|
|
||
|
#pragma pack(push,8)
|
||
|
#pragma warning(push,3)
|
||
|
_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;
|
||
|
|
||
|
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());
|
||
|
}
|
||
|
|
||
|
value_type& front()
|
||
|
{ // return first element of mutable queue
|
||
|
return (c.front());
|
||
|
}
|
||
|
|
||
|
const value_type& front() const
|
||
|
{ // return first element of nonmutable queue
|
||
|
return (c.front());
|
||
|
}
|
||
|
|
||
|
value_type& back()
|
||
|
{ // return last element of mutable queue
|
||
|
return (c.back());
|
||
|
}
|
||
|
|
||
|
const value_type& 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();
|
||
|
}
|
||
|
|
||
|
bool _Eq(const queue<_Ty, _Container>& _Right) const
|
||
|
{ // test for queue equality
|
||
|
return (c == _Right.c);
|
||
|
}
|
||
|
|
||
|
bool _Lt(const queue<_Ty, _Container>& _Right) const
|
||
|
{ // test if this < _Right for queues
|
||
|
return (c < _Right.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._Eq(_Right));
|
||
|
}
|
||
|
|
||
|
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._Lt(_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 (_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;
|
||
|
|
||
|
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 value_type& top() const
|
||
|
{ // return highest-priority element
|
||
|
return (c.front());
|
||
|
}
|
||
|
|
||
|
value_type& 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
|
||
|
#pragma warning(pop)
|
||
|
#pragma pack(pop)
|
||
|
|
||
|
#endif /* _QUEUE_ */
|
||
|
|
||
|
/*
|
||
|
* Copyright (c) 1992-2001 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.
|
||
|
V3.10:0009 */
|