Updated copyright
[libcds.git] / test / stress / set / 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-2017
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 "set/set_type.h"
35
36 #include <cds/container/cuckoo_set.h>
37 #include <cds_test/stat_cuckoo_out.h>
38
39 namespace set {
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                 cfg.s_nCuckooInitialSize,
52                 static_cast<unsigned int>( cfg.s_nCuckooProbesetSize ),
53                 static_cast<unsigned int>( cfg.s_nCuckooProbesetThreshold )
54             )
55         {}
56
57         template <typename Q, typename Pred>
58         bool erase_with( Q const& key, Pred /*pred*/ )
59         {
60             return cuckoo_base_class::erase_with( key, typename std::conditional< cuckoo_base_class::c_isSorted, Pred, typename Pred::equal_to>::type());
61         }
62
63         // for testing
64         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
65         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
66         static CDS_CONSTEXPR bool const c_bEraseExactKey = false;
67     };
68
69     struct tag_CuckooSet;
70
71     template <typename Key, typename Val>
72     struct set_type< tag_CuckooSet, Key, Val >: public set_type_base< Key, Val >
73     {
74         typedef set_type_base< Key, Val > base_class;
75         typedef typename base_class::key_val key_val;
76         typedef typename base_class::compare compare;
77         typedef typename base_class::equal_to equal_to;
78         typedef typename base_class::less less;
79         typedef typename base_class::hash hash;
80         typedef typename base_class::hash2 hash2;
81
82         // ***************************************************************************
83         // CuckooSet
84
85         template <typename Traits>
86         struct traits_CuckooStripedSet : public Traits
87         {
88             typedef cc::cuckoo::striping<> mutex_policy;
89         };
90         template <typename Traits>
91         struct traits_CuckooRefinableSet : public Traits
92         {
93             typedef cc::cuckoo::refinable<> mutex_policy;
94         };
95
96         struct traits_CuckooSet_list_unord :
97             public cc::cuckoo::make_traits <
98                 cc::cuckoo::probeset_type< cc::cuckoo::list >
99                 , co::equal_to< equal_to >
100                 , co::hash< std::tuple< hash, hash2 > >
101             > ::type
102         {};
103         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord>> CuckooStripedSet_list_unord;
104         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord>> CuckooRefinableSet_list_unord;
105
106         struct traits_CuckooSet_list_unord_stat : public traits_CuckooSet_list_unord
107         {
108             typedef cc::cuckoo::stat stat;
109         };
110         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord_stat>> CuckooStripedSet_list_unord_stat;
111         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord_stat>> CuckooRefinableSet_list_unord_stat;
112
113         struct traits_CuckooSet_list_unord_storehash : public traits_CuckooSet_list_unord
114         {
115             static CDS_CONSTEXPR const bool store_hash = true;
116         };
117         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_unord_storehash>> CuckooStripedSet_list_unord_storehash;
118         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_unord_storehash>> CuckooRefinableSet_list_unord_storehash;
119
120         struct traits_CuckooSet_list_ord :
121             public cc::cuckoo::make_traits <
122                 cc::cuckoo::probeset_type< cc::cuckoo::list >
123                 , co::compare< compare >
124                 , co::hash< std::tuple< hash, hash2 > >
125             > ::type
126         {};
127         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord>> CuckooStripedSet_list_ord;
128         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord>> CuckooRefinableSet_list_ord;
129
130         struct traits_CuckooSet_list_ord_stat : public traits_CuckooSet_list_ord
131         {
132             typedef cc::cuckoo::stat stat;
133         };
134         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord_stat>> CuckooStripedSet_list_ord_stat;
135         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord_stat>> CuckooRefinableSet_list_ord_stat;
136
137         struct traits_CuckooSet_list_ord_storehash : public traits_CuckooSet_list_ord
138         {
139             static CDS_CONSTEXPR const bool store_hash = true;
140         };
141         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_list_ord_storehash>> CuckooStripedSet_list_ord_storehash;
142         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_list_ord_storehash>> CuckooRefinableSet_list_ord_storehash;
143
144
145         struct traits_CuckooSet_vector_unord :
146             public cc::cuckoo::make_traits <
147                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
148                 , co::equal_to< equal_to >
149                 , co::hash< std::tuple< hash, hash2 > >
150             > ::type
151         {};
152         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord>> CuckooStripedSet_vector_unord;
153         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord>> CuckooRefinableSet_vector_unord;
154
155         struct traits_CuckooSet_vector_unord_stat : public traits_CuckooSet_vector_unord
156         {
157             typedef cc::cuckoo::stat stat;
158         };
159         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord_stat>> CuckooStripedSet_vector_unord_stat;
160         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord_stat>> CuckooRefinableSet_vector_unord_stat;
161
162         struct traits_CuckooSet_vector_unord_storehash : public traits_CuckooSet_vector_unord
163         {
164             static CDS_CONSTEXPR const bool store_hash = true;
165         };
166         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_unord_storehash>> CuckooStripedSet_vector_unord_storehash;
167         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_unord_storehash>> CuckooRefinableSet_vector_unord_storehash;
168
169         struct traits_CuckooSet_vector_ord :
170             public cc::cuckoo::make_traits <
171                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
172                 , co::compare< compare >
173                 , co::hash< std::tuple< hash, hash2 > >
174             > ::type
175         {};
176         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord>> CuckooStripedSet_vector_ord;
177         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord>> CuckooRefinableSet_vector_ord;
178
179         struct traits_CuckooSet_vector_ord_stat : public traits_CuckooSet_vector_ord
180         {
181             typedef cc::cuckoo::stat stat;
182         };
183         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord_stat>> CuckooStripedSet_vector_ord_stat;
184         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord_stat>> CuckooRefinableSet_vector_ord_stat;
185
186         struct traits_CuckooSet_vector_ord_storehash : public traits_CuckooSet_vector_ord
187         {
188             static CDS_CONSTEXPR const bool store_hash = true;
189         };
190         typedef CuckooSet< key_val, traits_CuckooStripedSet<traits_CuckooSet_vector_ord_storehash>> CuckooStripedSet_vector_ord_storehash;
191         typedef CuckooSet< key_val, traits_CuckooRefinableSet<traits_CuckooSet_vector_ord_storehash>> CuckooRefinableSet_vector_ord_storehash;
192
193     };
194
195     template <typename T, typename Traits >
196     static inline void print_stat( cds_test::property_stream& o, cc::CuckooSet< T, Traits > const& s )
197     {
198         o << s.statistics() << s.mutex_policy_statistics();
199     }
200
201     template <typename V, typename Traits>
202     static inline void print_stat( cds_test::property_stream& o, CuckooSet< V, Traits > const& s )
203     {
204         typedef CuckooSet< V, Traits > set_type;
205         print_stat( o, static_cast<typename set_type::cuckoo_base_class const&>(s));
206     }
207
208 } // namespace set
209
210 #define CDSSTRESS_CuckooSet_case( fixture, test_case, cuckoo_set_type, key_type, value_type ) \
211     TEST_F( fixture, cuckoo_set_type ) \
212     { \
213         typedef set::set_type< tag_CuckooSet, key_type, value_type >::cuckoo_set_type set_type; \
214         test_case<set_type>(); \
215     }
216
217 #define CDSSTRESS_CuckooSet( fixture, test_case, key_type, value_type ) \
218     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_unord,                  key_type, value_type ) \
219     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_unord,                key_type, value_type ) \
220     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_unord_stat,             key_type, value_type ) \
221     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_unord_stat,           key_type, value_type ) \
222     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_unord_storehash,        key_type, value_type ) \
223     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_unord_storehash,      key_type, value_type ) \
224     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_ord,                    key_type, value_type ) \
225     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_ord,                  key_type, value_type ) \
226     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_ord_stat,               key_type, value_type ) \
227     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_ord_stat,             key_type, value_type ) \
228     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_list_ord_storehash,          key_type, value_type ) \
229     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_list_ord_storehash,        key_type, value_type ) \
230     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_unord,                key_type, value_type ) \
231     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_unord,              key_type, value_type ) \
232     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_unord_stat,           key_type, value_type ) \
233     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_unord_stat,         key_type, value_type ) \
234     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_unord_storehash,      key_type, value_type ) \
235     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_unord_storehash,    key_type, value_type ) \
236     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_ord,                  key_type, value_type ) \
237     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_ord,                key_type, value_type ) \
238     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_ord_stat,             key_type, value_type ) \
239     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_ord_stat,           key_type, value_type ) \
240     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooStripedSet_vector_ord_storehash,        key_type, value_type ) \
241     CDSSTRESS_CuckooSet_case( fixture, test_case, CuckooRefinableSet_vector_ord_storehash,      key_type, value_type ) \
242
243
244 #endif // #ifndef CDSUNIT_SET_TYPE_CUCKOO_H