93ee15ca78899e416502d95ab812512e25865db6
[libcds.git] / tests / unit / pqueue / skiplist_pqueue.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSUNIT_SKIPLIST_PQUEUE_H
32 #define CDSUNIT_SKIPLIST_PQUEUE_H
33
34 #include <cds/container/skip_list_set_hp.h>
35 #include <cds/container/skip_list_set_dhp.h>
36 #include <cds/urcu/general_instant.h>
37 #include <cds/urcu/general_buffered.h>
38 #include <cds/urcu/general_threaded.h>
39 #include <cds/urcu/signal_buffered.h>
40 #include <cds/urcu/signal_threaded.h>
41 #include <cds/container/skip_list_set_rcu.h>
42
43 namespace pqueue {
44
45     template <typename GC>
46     struct SkipListPQueue_pop_max
47     {
48         template <typename T, typename Set>
49         bool operator()( T& dest, Set& container ) const
50         {
51             typename Set::guarded_ptr gp( container.extract_max());
52             if ( gp )
53                 dest = *gp;
54             return !gp.empty();
55         }
56     };
57
58     template <typename RCU>
59     struct SkipListPQueue_pop_max< cds::urcu::gc<RCU> >
60     {
61         template <typename T, typename Set>
62         bool operator()( T& dest, Set& container ) const
63         {
64             typename Set::exempt_ptr ep( container.extract_max());
65             if ( ep )
66                 dest = *ep;
67             return !ep.empty();
68         }
69     };
70
71     template <typename GC>
72     struct SkipListPQueue_pop_min
73     {
74         template <typename T, typename Set>
75         bool operator()( T& dest, Set& container ) const
76         {
77             typename Set::guarded_ptr gp( container.extract_min());
78             if ( gp )
79                 dest = *gp;
80             return !gp.empty();
81         }
82     };
83
84     template <typename RCU>
85     struct SkipListPQueue_pop_min< cds::urcu::gc<RCU> >
86     {
87         template <typename T, typename Set>
88         bool operator()( T& dest, Set& container ) const
89         {
90             typename Set::exempt_ptr ep( container.extract_min());
91             if ( ep )
92                 dest = *ep;
93             return !ep.empty();
94         }
95     };
96
97     template <typename GC, typename T, typename Traits, bool Max=true>
98     class SkipListPQueue: protected cds::container::SkipListSet< GC, T, Traits >
99     {
100         typedef cds::container::SkipListSet< GC, T, Traits > base_class;
101         typedef T value_type;
102         template <typename GC2> friend struct SkipListPQueue_pop_max;
103         template <typename GC2> friend struct SkipListPQueue_pop_min;
104
105     public:
106         bool push( value_type const& val )
107         {
108             return base_class::insert( val );
109         }
110
111         bool pop( value_type& dest )
112         {
113             return Max ? SkipListPQueue_pop_max< typename base_class::gc >()( dest, *this )
114                        : SkipListPQueue_pop_min< typename base_class::gc >()( dest, *this );
115         }
116
117         void clear()
118         {
119             base_class::clear();
120         }
121
122         bool empty() const
123         {
124             return base_class::empty();
125         }
126
127         size_t size() const
128         {
129             return base_class::size();
130         }
131
132         typename base_class::stat const& statistics() const
133         {
134             return base_class::statistics();
135         }
136     };
137
138 } // namespace pqueue
139
140 #endif // #ifndef CDSUNIT_SKIPLIST_PQUEUE_H