a392cb3cc4b0c1c80f3d4286eb440c6e027cce56
[libcds.git] / tests / unit / set2 / set_type_cuckoo.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_SET_TYPE_CUCKOO_H
32 #define CDSUNIT_SET_TYPE_CUCKOO_H
33
34 #include "set2/set_type.h"
35
36 #include <cds/container/cuckoo_set.h>
37 #include "print_cuckoo_stat.h"
38
39 namespace set2 {
40
41     template <typename V, typename Traits>
42     class CuckooSet : public cc::CuckooSet< V, Traits >
43     {
44     public:
45         typedef cc::CuckooSet< V, Traits > cuckoo_base_class;
46
47     public:
48         template <typename Config>
49         CuckooSet( Config const& cfg )
50             : cuckoo_base_class(
51
52                 cfg.c_nCuckooInitialSize,
53                 static_cast<unsigned int>( cfg.c_nCuckooProbesetSize ),
54                 static_cast<unsigned int>( cfg.c_nCuckooProbesetThreshold )
55             )
56         {}
57
58         template <typename Q, typename Pred>
59         bool erase_with( Q const& key, Pred /*pred*/ )
60         {
61             return cuckoo_base_class::erase_with( key, typename std::conditional< cuckoo_base_class::c_isSorted, Pred, typename Pred::equal_to>::type() );
62         }
63
64         // for testing
65         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
66         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
67         static CDS_CONSTEXPR bool const c_bEraseExactKey = false;
68     };
69
70     struct tag_CuckooSet;
71
72     template <typename Key, typename Val>
73     struct set_type< tag_CuckooSet, Key, Val >: public set_type_base< Key, Val >
74     {
75         typedef set_type_base< Key, Val > base_class;
76         typedef typename base_class::key_val key_val;
77         typedef typename base_class::compare compare;
78         typedef typename base_class::equal_to equal_to;
79         typedef typename base_class::less less;
80         typedef typename base_class::hash hash;
81         typedef typename base_class::hash2 hash2;
82
83         // ***************************************************************************
84         // CuckooSet
85
86         template <typename Traits>
87         struct traits_CuckooStripedSet : public Traits
88         {
89             typedef cc::cuckoo::striping<> mutex_policy;
90         };
91         template <typename Traits>
92         struct traits_CuckooRefinableSet : public Traits
93         {
94             typedef cc::cuckoo::refinable<> mutex_policy;
95         };
96
97         struct traits_CuckooSet_list_unord :
98             public cc::cuckoo::make_traits <
99                 cc::cuckoo::probeset_type< cc::cuckoo::list >
100                 , co::equal_to< equal_to >
101                 , co::hash< std::tuple< hash, hash2 > >
102             > ::type
103         {};
104         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord>> CuckooStripedSet_list_unord;
105         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord>> CuckooRefinableSet_list_unord;
106
107         struct traits_CuckooSet_list_unord_stat : public traits_CuckooSet_list_unord
108         {
109             typedef cc::cuckoo::stat stat;
110         };
111         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord_stat>> CuckooStripedSet_list_unord_stat;
112         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord_stat>> CuckooRefinableSet_list_unord_stat;
113
114         struct traits_CuckooSet_list_unord_storehash : public traits_CuckooSet_list_unord
115         {
116             static CDS_CONSTEXPR const bool store_hash = true;
117         };
118         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord_storehash>> CuckooStripedSet_list_unord_storehash;
119         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord_storehash>> CuckooRefinableSet_list_unord_storehash;
120
121         struct traits_CuckooSet_list_ord :
122             public cc::cuckoo::make_traits <
123                 cc::cuckoo::probeset_type< cc::cuckoo::list >
124                 , co::compare< compare >
125                 , co::hash< std::tuple< hash, hash2 > >
126             > ::type
127         {};
128         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord>> CuckooStripedSet_list_ord;
129         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord>> CuckooRefinableSet_list_ord;
130
131         struct traits_CuckooSet_list_ord_stat : public traits_CuckooSet_list_ord
132         {
133             typedef cc::cuckoo::stat stat;
134         };
135         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord_stat>> CuckooStripedSet_list_ord_stat;
136         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord_stat>> CuckooRefinableSet_list_ord_stat;
137
138         struct traits_CuckooSet_list_ord_storehash : public traits_CuckooSet_list_ord
139         {
140             static CDS_CONSTEXPR const bool store_hash = true;
141         };
142         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord_storehash>> CuckooStripedSet_list_ord_storehash;
143         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord_storehash>> CuckooRefinableSet_list_ord_storehash;
144
145
146         struct traits_CuckooSet_vector_unord :
147             public cc::cuckoo::make_traits <
148                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
149                 , co::equal_to< equal_to >
150                 , co::hash< std::tuple< hash, hash2 > >
151             > ::type
152         {};
153         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord>> CuckooStripedSet_vector_unord;
154         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord>> CuckooRefinableSet_vector_unord;
155
156         struct traits_CuckooSet_vector_unord_stat : public traits_CuckooSet_vector_unord
157         {
158             typedef cc::cuckoo::stat stat;
159         };
160         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord_stat>> CuckooStripedSet_vector_unord_stat;
161         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord_stat>> CuckooRefinableSet_vector_unord_stat;
162
163         struct traits_CuckooSet_vector_unord_storehash : public traits_CuckooSet_vector_unord
164         {
165             static CDS_CONSTEXPR const bool store_hash = true;
166         };
167         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord_storehash>> CuckooStripedSet_vector_unord_storehash;
168         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord_storehash>> CuckooRefinableSet_vector_unord_storehash;
169
170         struct traits_CuckooSet_vector_ord :
171             public cc::cuckoo::make_traits <
172                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
173                 , co::compare< compare >
174                 , co::hash< std::tuple< hash, hash2 > >
175             > ::type
176         {};
177         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord>> CuckooStripedSet_vector_ord;
178         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord>> CuckooRefinableSet_vector_ord;
179
180         struct traits_CuckooSet_vector_ord_stat : public traits_CuckooSet_vector_ord
181         {
182             typedef cc::cuckoo::stat stat;
183         };
184         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord_stat>> CuckooStripedSet_vector_ord_stat;
185         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord_stat>> CuckooRefinableSet_vector_ord_stat;
186
187         struct traits_CuckooSet_vector_ord_storehash : public traits_CuckooSet_vector_ord
188         {
189             static CDS_CONSTEXPR const bool store_hash = true;
190         };
191         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord_storehash>> CuckooStripedSet_vector_ord_storehash;
192         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord_storehash>> CuckooRefinableSet_vector_ord_storehash;
193
194     };
195
196     template <typename T, typename Traits >
197     static inline void print_stat( cc::CuckooSet< T, Traits > const& s )
198     {
199         CPPUNIT_MSG( s.statistics() << s.mutex_policy_statistics() );
200     }
201
202     template <typename V, typename Traits>
203     static inline void print_stat( CuckooSet< V, Traits > const& s )
204     {
205         typedef CuckooSet< V, Traits > set_type;
206         print_stat( static_cast<typename set_type::cuckoo_base_class const&>(s) );
207     }
208
209 } // namespace set2
210
211 #endif // #ifndef CDSUNIT_SET_TYPE_CUCKOO_H