8be18c7ea713cfd1787a84ce67d8c3744f8058de
[libcds.git] / tests / unit / pqueue / ellen_bintree_pqueue.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_ELLEN_BINTREE_PQUEUE_H
4 #define __CDSUNIT_STD_ELLEN_BINTREE_PQUEUE_H
5
6 #include <cds/container/ellen_bintree_set_hp.h>
7 #include <cds/container/ellen_bintree_set_dhp.h>
8 #include <cds/urcu/general_instant.h>
9 #include <cds/urcu/general_buffered.h>
10 #include <cds/urcu/general_threaded.h>
11 #include <cds/urcu/signal_buffered.h>
12 #include <cds/urcu/signal_threaded.h>
13 #include <cds/container/ellen_bintree_set_rcu.h>
14
15 namespace pqueue {
16
17     template <typename GC>
18     struct EllenBinTreePQueue_pop_max
19     {
20         template <typename T, typename Tree>
21         bool operator()( T& dest, Tree& container ) const
22         {
23             typename Tree::guarded_ptr gp;
24             bool bRet = container.extract_max( gp );
25             if ( bRet )
26                 dest = *gp;
27             return bRet;
28         }
29     };
30
31     template <typename RCU>
32     struct EllenBinTreePQueue_pop_max< cds::urcu::gc<RCU> >
33     {
34         template <typename T, typename Tree>
35         bool operator()( T& dest, Tree& container ) const
36         {
37             typename Tree::exempt_ptr ep;
38             bool bRet = container.extract_max( ep );
39             if ( bRet )
40                 dest = *ep;
41             return bRet;
42         }
43     };
44
45     template <typename GC>
46     struct EllenBinTreePQueue_pop_min
47     {
48         template <typename T, typename Tree>
49         bool operator()( T& dest, Tree& container ) const
50         {
51             typename Tree::guarded_ptr gp;
52             bool bRet = container.extract_min( gp );
53             if ( bRet )
54                 dest = *gp;
55             return bRet;
56         }
57     };
58
59     template <typename RCU>
60     struct EllenBinTreePQueue_pop_min< cds::urcu::gc<RCU> >
61     {
62         template <typename T, typename Tree>
63         bool operator()( T& dest, Tree& container ) const
64         {
65             typename Tree::exempt_ptr ep;
66             bool bRet = container.extract_min( ep );
67             if ( bRet )
68                 dest = *ep;
69             return bRet;
70         }
71     };
72
73     template <typename GC, typename Key, typename T, typename Traits, bool Max=true>
74     class EllenBinTreePQueue: protected cds::container::EllenBinTreeSet< GC, Key, T, Traits >
75     {
76         typedef cds::container::EllenBinTreeSet< GC, Key, T, Traits > base_class;
77         typedef T value_type;
78         template <typename GC2> friend struct EllenBinTreePQueue_pop_max;
79         template <typename GC2> friend struct EllenBinTreePQueue_pop_min;
80
81     public:
82         bool push( value_type const& val )
83         {
84             return base_class::insert( val );
85         }
86
87         bool pop( value_type& dest )
88         {
89             return Max ? EllenBinTreePQueue_pop_max< typename base_class::gc >()( dest, *this )
90                        : EllenBinTreePQueue_pop_min< typename base_class::gc >()( dest, *this );
91         }
92
93         void clear()
94         {
95             base_class::clear();
96         }
97
98         bool empty() const
99         {
100             return base_class::empty();
101         }
102
103         size_t size() const
104         {
105             return base_class::size();
106         }
107
108         typename base_class::stat const& statistics() const
109         {
110             return base_class::statistics();
111         }
112     };
113
114 } // namespace pqueue
115
116
117 #endif // #ifndef __CDSUNIT_STD_ELLEN_BINTREE_PQUEUE_H