issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / tests / unit / pqueue / std_pqueue.h
1 //$$CDS-header$$
2
3 #ifndef CDSUNIT_STD_PQUEUE_H
4 #define CDSUNIT_STD_PQUEUE_H
5
6 #include <queue>
7 #include <mutex>    //unique_lock
8
9 namespace pqueue {
10
11     struct dummy_stat {};
12
13     template <typename T, typename Container, typename Lock, typename Less=std::less<typename Container::value_type> >
14     class StdPQueue
15     {
16         typedef T value_type;
17         typedef std::priority_queue<value_type, Container, Less> pqueue_type;
18
19         pqueue_type     m_PQueue;
20         mutable Lock    m_Lock;
21
22         typedef std::unique_lock<Lock> scoped_lock;
23
24     public:
25         bool push( value_type const& val )
26         {
27             scoped_lock l( m_Lock );
28             m_PQueue.push( val );
29             return true;
30         }
31
32         bool pop( value_type& dest )
33         {
34             scoped_lock l( m_Lock );
35             if ( !m_PQueue.empty() ) {
36                 dest = m_PQueue.top();
37                 m_PQueue.pop();
38                 return true;
39             }
40             return false;
41         }
42
43         template <typename Q, typename MoveFunc>
44         bool pop_with( Q& dest, MoveFunc f )
45         {
46             scoped_lock l( m_Lock );
47             if ( !m_PQueue.empty() ) {
48                 f( dest, m_PQueue.top());
49                 m_PQueue.pop();
50                 return true;
51             }
52             return false;
53         }
54
55         void clear()
56         {
57             scoped_lock l( m_Lock );
58             while ( !m_PQueue.empty() )
59                 m_PQueue.pop();
60         }
61
62         template <typename Func>
63         void clear_with( Func f )
64         {
65             scoped_lock l( m_Lock );
66             while ( !m_PQueue.empty() ) {
67                 f( m_PQueue.top() );
68                 m_PQueue.pop();
69             }
70         }
71
72         bool empty() const
73         {
74             return m_PQueue.empty();
75         }
76
77         size_t size() const
78         {
79             return m_PQueue.size();
80         }
81
82         dummy_stat statistics() const
83         {
84             return dummy_stat();
85         }
86     };
87
88 } // namespace pqueue
89
90 namespace std {
91     static inline ostream& operator <<( ostream& o, pqueue::dummy_stat )
92     {
93         return o;
94     }
95 }
96
97 #endif // #ifndef CDSUNIT_STD_PQUEUE_H