File indexing completed on 2025-01-18 09:43:31
0001
0002
0003
0004
0005
0006 #ifndef BOOST_FENCED_PRIORITY_QUEUE_HPP
0007 #define BOOST_FENCED_PRIORITY_QUEUE_HPP
0008
0009 #include <vector>
0010 #include <queue>
0011 #include <functional>
0012 #include <boost/pending/queue.hpp>
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 namespace boost
0034 {
0035
0036 template < class T, class Compare = std::less< T >, bool implicit_fence = true,
0037 class Buffer = boost::queue< T > >
0038 class fenced_priority_queue
0039 {
0040 public:
0041 typedef T value_type;
0042 typedef typename Buffer::size_type size_type;
0043
0044 fenced_priority_queue(const Compare _comp = Compare()) : PQ(_comp) {}
0045
0046 void push(const T& data);
0047 void pop(void);
0048 T& top(void);
0049 const T& top(void) const;
0050 size_type size(void) const;
0051 bool empty(void) const;
0052 void fence(void);
0053
0054 private:
0055 void fence(void) const;
0056
0057
0058
0059 mutable std::priority_queue< T, std::vector< T >, Compare > PQ;
0060 mutable Buffer Q;
0061 };
0062
0063 template < class T, class Compare, bool implicit_fence, class Buffer >
0064 inline void fenced_priority_queue< T, Compare, implicit_fence, Buffer >::push(
0065 const T& t)
0066 {
0067
0068
0069
0070 PQ.push(t);
0071 }
0072
0073 template < class T, class Compare, bool implicit_fence, class Buffer >
0074 inline void fenced_priority_queue< T, Compare, implicit_fence, Buffer >::pop(
0075 void)
0076 {
0077
0078
0079
0080
0081
0082 if (implicit_fence)
0083 fence();
0084 if (!Q.empty())
0085 Q.pop();
0086 else
0087 PQ.pop();
0088 }
0089
0090 template < class T, class Compare, bool implicit_fence, class Buffer >
0091 inline T& fenced_priority_queue< T, Compare, implicit_fence, Buffer >::top(void)
0092 {
0093
0094
0095
0096 if (implicit_fence)
0097 fence();
0098 if (!Q.empty())
0099 return Q.top();
0100 else
0101
0102 return const_cast< T& >(PQ.top());
0103 }
0104
0105 template < class T, class Compare, bool implicit_fence, class Buffer >
0106 inline const T&
0107 fenced_priority_queue< T, Compare, implicit_fence, Buffer >::top(void) const
0108 {
0109 if (implicit_fence)
0110 fence();
0111 if (!Q.empty())
0112 return Q.top();
0113 else
0114 return PQ.top();
0115 }
0116
0117 template < class T, class Compare, bool implicit_fence, class Buffer >
0118 inline typename fenced_priority_queue< T, Compare, implicit_fence,
0119 Buffer >::size_type
0120 fenced_priority_queue< T, Compare, implicit_fence, Buffer >::size(void) const
0121 {
0122
0123 return Q.size() + PQ.size();
0124 }
0125
0126 template < class T, class Compare, bool implicit_fence, class Buffer >
0127 inline bool fenced_priority_queue< T, Compare, implicit_fence, Buffer >::empty(
0128 void) const
0129 {
0130
0131 return Q.empty() && PQ.empty();
0132 }
0133
0134 template < class T, class Compare, bool implicit_fence, class Buffer >
0135 inline void fenced_priority_queue< T, Compare, implicit_fence, Buffer >::fence(
0136 void)
0137 {
0138
0139
0140 while (!PQ.empty())
0141 {
0142 Q.push(PQ.top());
0143 PQ.pop();
0144 }
0145 }
0146 template < class T, class Compare, bool implicit_fence, class Buffer >
0147 inline void fenced_priority_queue< T, Compare, implicit_fence, Buffer >::fence(
0148 void) const
0149 {
0150
0151
0152 while (!PQ.empty())
0153 {
0154 Q.push(PQ.top());
0155 PQ.pop();
0156 }
0157 }
0158
0159 }
0160 #endif