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