issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / tests / unit / pqueue / skiplist_pqueue.h
1 //$$CDS-header$$
2
3 #ifndef CDSUNIT_SKIPLIST_PQUEUE_H
4 #define CDSUNIT_SKIPLIST_PQUEUE_H
5
6 #include <cds/container/skip_list_set_hp.h>
7 #include <cds/container/skip_list_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/skip_list_set_rcu.h>
14
15 namespace pqueue {
16
17     template <typename GC>
18     struct SkipListPQueue_pop_max
19     {
20         template <typename T, typename Set>
21         bool operator()( T& dest, Set& container ) const
22         {
23             typename Set::guarded_ptr gp( container.extract_max());
24             if ( gp )
25                 dest = *gp;
26             return !gp.empty();
27         }
28     };
29
30     template <typename RCU>
31     struct SkipListPQueue_pop_max< cds::urcu::gc<RCU> >
32     {
33         template <typename T, typename Set>
34         bool operator()( T& dest, Set& container ) const
35         {
36             typename Set::exempt_ptr ep( container.extract_max());
37             if ( ep )
38                 dest = *ep;
39             return !ep.empty();
40         }
41     };
42
43     template <typename GC>
44     struct SkipListPQueue_pop_min
45     {
46         template <typename T, typename Set>
47         bool operator()( T& dest, Set& container ) const
48         {
49             typename Set::guarded_ptr gp( container.extract_min());
50             if ( gp )
51                 dest = *gp;
52             return !gp.empty();
53         }
54     };
55
56     template <typename RCU>
57     struct SkipListPQueue_pop_min< cds::urcu::gc<RCU> >
58     {
59         template <typename T, typename Set>
60         bool operator()( T& dest, Set& container ) const
61         {
62             typename Set::exempt_ptr ep( container.extract_min());
63             if ( ep )
64                 dest = *ep;
65             return !ep.empty();
66         }
67     };
68
69     template <typename GC, typename T, typename Traits, bool Max=true>
70     class SkipListPQueue: protected cds::container::SkipListSet< GC, T, Traits >
71     {
72         typedef cds::container::SkipListSet< GC, T, Traits > base_class;
73         typedef T value_type;
74         template <typename GC2> friend struct SkipListPQueue_pop_max;
75         template <typename GC2> friend struct SkipListPQueue_pop_min;
76
77     public:
78         bool push( value_type const& val )
79         {
80             return base_class::insert( val );
81         }
82
83         bool pop( value_type& dest )
84         {
85             return Max ? SkipListPQueue_pop_max< typename base_class::gc >()( dest, *this )
86                        : SkipListPQueue_pop_min< typename base_class::gc >()( dest, *this );
87         }
88
89         void clear()
90         {
91             base_class::clear();
92         }
93
94         bool empty() const
95         {
96             return base_class::empty();
97         }
98
99         size_t size() const
100         {
101             return base_class::size();
102         }
103
104         typename base_class::stat const& statistics() const
105         {
106             return base_class::statistics();
107         }
108     };
109
110 } // namespace pqueue
111
112 #endif // #ifndef CDSUNIT_SKIPLIST_PQUEUE_H