1
#ifndef BOOST_FENCED_PRIORITY_QUEUE_HPP
2
#define BOOST_FENCED_PRIORITY_QUEUE_HPP
7
#include <boost/pending/queue.hpp>
9
// Fenced priority queue
12
// This class implements a fenced priority queue. This is similar to
13
// a normal priority queue (sorts its members, and only returns the
14
// first), except that members cannot be sorted around a "fence" that
15
// can be placed into the buffer. This fence is inserted using the
16
// fence() member function or (possibly) implicitly by the top() and
17
// pop() methods, and is removed automatically when the elements
18
// around it are popped.
20
// The implementation is as follows: Q is an unsorted queue that
21
// contains the already-sorted list data, and PQ is a priority queue
22
// that contains new elements (since the last fence) that have yet to
23
// be sorted. New elements are inserted into PQ, and a fence moves
24
// all elements in PQ into the back of Q in sorted order. Elements
25
// are then popped from the front of Q, and if that is empty the front
30
template<class T, class Compare = std::less<T>, bool implicit_fence = true,
31
class Buffer = boost::queue<T> >
32
class fenced_priority_queue {
35
typedef typename Buffer::size_type size_type;
37
fenced_priority_queue(const Compare _comp = Compare() )
40
void push(const T& data);
43
const T& top(void) const;
44
size_type size(void) const;
45
bool empty(void) const;
49
void fence(void) const;
51
//let them mutable to allow const version of top and the same
52
//semantics with non-constant version. Rich Lee
53
mutable std::priority_queue<T, std::vector<T>, Compare> PQ;
57
template<class T, class Compare, bool implicit_fence, class Buffer>
59
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
61
// Push a new element after the last fence. This puts it into the
62
// priority queue to be sorted with all other elements in its
67
template<class T, class Compare, bool implicit_fence, class Buffer>
68
inline void fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
70
// Pop one element from the front of the queue. Removes from the
71
// already-sorted part of the queue if it is non-empty, otherwise
72
// removes from the new-element priority queue. Runs an implicit
73
// "fence" operation if the implicit_fence template argument is
75
if (implicit_fence) fence();
82
template<class T, class Compare, bool implicit_fence, class Buffer>
83
inline T& fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
85
// Get the top element from the queue. This element comes from Q if
86
// possible, otherwise from PQ. Causes an implicit "fence"
87
// operation if the implicit_fence template argument is true.
88
if (implicit_fence) fence();
92
//std::priority_queue only have const version of top. Rich Lee
93
return const_cast<T&>(PQ.top());
96
template<class T, class Compare, bool implicit_fence, class Buffer>
98
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
100
if (implicit_fence) fence();
107
template<class T, class Compare, bool implicit_fence, class Buffer>
108
inline typename fenced_priority_queue<T, Compare, implicit_fence, Buffer>::size_type
109
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
111
// Returns the size of the queue (both parts together).
112
return Q.size() + PQ.size();
115
template<class T, class Compare, bool implicit_fence, class Buffer>
117
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
119
// Returns if the queue is empty, i.e. both parts are empty.
120
return Q.empty() && PQ.empty();
123
template<class T, class Compare, bool implicit_fence, class Buffer>
125
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
127
// Perform a fence operation. Remove elements from PQ in sorted
128
// order and insert them in the back of Q.
129
while ( !PQ.empty() ) {
134
template<class T, class Compare, bool implicit_fence, class Buffer>
136
fenced_priority_queue<T, Compare, implicit_fence, Buffer>::
138
// Perform a fence operation. Remove elements from PQ in sorted
139
// order and insert them in the back of Q.
140
while ( !PQ.empty() ) {
147
#endif /* BOOST_FENCED_PRIORITY_QUEUE_HPP */