03c06869d01cefe6376b573f6ec3466e543b9b53
[libcds.git] / test / stress / set / set_type_feldman_hashset.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_FELDMAN_HASHSET_H
32 #define CDSUNIT_SET_TYPE_FELDMAN_HASHSET_H
33
34 #include "set/set_type.h"
35
36 #include <cds/container/feldman_hashset_hp.h>
37 #include <cds/container/feldman_hashset_dhp.h>
38 #include <cds/container/feldman_hashset_rcu.h>
39
40 #include <cds_test/stat_feldman_hashset_out.h>
41 #include <cds_test/hash_func.h>
42
43 namespace set {
44
45     template <class GC, typename T, typename Traits = cc::feldman_hashset::traits>
46     class FeldmanHashSet : public cc::FeldmanHashSet< GC, T, Traits >
47     {
48         typedef cc::FeldmanHashSet< GC, T, Traits > base_class;
49
50         template <typename G>
51         struct get_extracted_ptr
52         {
53             typedef typename base_class::guarded_ptr extracted_ptr;
54         };
55
56         template <typename RCU>
57         struct get_extracted_ptr<cds::urcu::gc<RCU>>
58         {
59             typedef typename base_class::exempt_ptr extracted_ptr;
60         };
61
62     public:
63         typedef typename T::hasher hasher;
64         typedef typename get_extracted_ptr<GC>::extracted_ptr extracted_ptr;
65
66         template <class Config>
67         FeldmanHashSet( Config const& cfg )
68             : base_class( cfg.s_nFeldmanSet_HeadBits, cfg.s_nFeldmanSet_ArrayBits )
69         {}
70
71         template <typename Q>
72         bool erase( Q const& key )
73         {
74             return base_class::erase( hasher()( key ));
75         }
76
77         template <typename Q, typename Func>
78         bool erase( Q const& key, Func f )
79         {
80             return base_class::erase( hasher()( key ), f );
81         }
82
83         template <typename Q>
84         extracted_ptr extract(Q const& key)
85         {
86             return base_class::extract( hasher()(key) );
87         }
88
89         template <typename Q>
90         bool contains( Q const& key )
91         {
92             return base_class::contains( hasher()(key) );
93         }
94
95         // for testing
96         static CDS_CONSTEXPR bool const c_bExtractSupported = true;
97         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
98         static CDS_CONSTEXPR bool const c_bEraseExactKey = true;
99     };
100
101     struct tag_FeldmanHashSet;
102
103     template <typename Key, typename Val>
104     struct set_type< tag_FeldmanHashSet, Key, Val >: public set_type_base< Key, Val >
105     {
106         typedef set_type_base< Key, Val > base_class;
107         typedef typename base_class::compare compare;
108         typedef typename base_class::less less;
109         typedef typename base_class::hash hash;
110         typedef typename base_class::key_type   key_type;
111         typedef typename base_class::value_type value_type;
112
113         template <typename Hasher>
114         struct hash_type
115         {
116             typedef Hasher hasher;
117             typedef typename hasher::hash_type type;
118         };
119
120         template <typename TH>
121         struct hash_type<std::hash<TH>>
122         {
123             typedef std::hash<TH> hasher;
124             typedef size_t type;
125         };
126
127         template <typename Hasher>
128         struct key_val: base_class::key_val
129         {
130             typedef typename base_class::key_val base;
131             typedef Hasher hasher;
132             typedef typename hash_type<hasher>::type hash_type;
133
134             hash_type hash;
135
136             /*explicit*/ key_val( key_type const& k ): base(k), hash( hasher()( k )) {}
137             key_val( key_type const& k, value_type const& v ): base(k, v), hash( hasher()( k )) {}
138
139             template <typename K>
140             /*explicit*/ key_val( K const& k ): base(k), hash( hasher()( k )) {}
141
142             template <typename K, typename T>
143             key_val( K const& k, T const& v ): base(k, v), hash( hasher()( k )) {}
144         };
145
146         struct default_traits : public cc::feldman_hashset::traits
147         {
148             struct hash_accessor {
149                 template <typename Hasher>
150                 typename key_val<Hasher>::hash_type const& operator()( key_val<Hasher> const& kv )
151                 {
152                     return kv.hash;
153                 }
154             };
155         };
156
157         typedef FeldmanHashSet< cds::gc::HP,  key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_hp_stdhash;
158         typedef FeldmanHashSet< cds::gc::DHP, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_dhp_stdhash;
159         typedef FeldmanHashSet< rcu_gpi, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_rcu_gpi_stdhash;
160         typedef FeldmanHashSet< rcu_gpb, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_rcu_gpb_stdhash;
161         typedef FeldmanHashSet< rcu_gpt, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_rcu_gpt_stdhash;
162 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
163         typedef FeldmanHashSet< rcu_shb, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_rcu_shb_stdhash;
164         typedef FeldmanHashSet< rcu_sht, key_val<std::hash<key_type>>, default_traits >    FeldmanHashSet_rcu_sht_stdhash;
165 #endif
166
167         struct traits_FeldmanHashSet_stat: public cc::feldman_hashset::make_traits<
168                 co::type_traits< default_traits >,
169                 co::stat< cc::feldman_hashset::stat<>>
170             >::type
171         {};
172
173         typedef FeldmanHashSet< cds::gc::HP,  key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_hp_stdhash_stat;
174         typedef FeldmanHashSet< cds::gc::DHP, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_dhp_stdhash_stat;
175         typedef FeldmanHashSet< rcu_gpi, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_rcu_gpi_stdhash_stat;
176         typedef FeldmanHashSet< rcu_gpb, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_rcu_gpb_stdhash_stat;
177         typedef FeldmanHashSet< rcu_gpt, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_rcu_gpt_stdhash_stat;
178 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
179         typedef FeldmanHashSet< rcu_shb, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_rcu_shb_stdhash_stat;
180         typedef FeldmanHashSet< rcu_sht, key_val<std::hash<key_type>>, traits_FeldmanHashSet_stat >    FeldmanHashSet_rcu_sht_stdhash_stat;
181 #endif
182
183         // CityHash
184 #if CDS_BUILD_BITS == 64
185         struct traits_FeldmanHashSet_city64 : public default_traits
186         {
187             typedef ::cds_test::city64::less less;
188         };
189         typedef FeldmanHashSet< cds::gc::HP,  key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_hp_city64;
190         typedef FeldmanHashSet< cds::gc::DHP, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_dhp_city64;
191         typedef FeldmanHashSet< rcu_gpi, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_rcu_gpi_city64;
192         typedef FeldmanHashSet< rcu_gpb, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_rcu_gpb_city64;
193         typedef FeldmanHashSet< rcu_gpt, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_rcu_gpt_city64;
194 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
195         typedef FeldmanHashSet< rcu_shb, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_rcu_shb_city64;
196         typedef FeldmanHashSet< rcu_sht, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64 >    FeldmanHashSet_rcu_sht_city64;
197 #endif
198
199         struct traits_FeldmanHashSet_city64_stat : public traits_FeldmanHashSet_city64
200         {
201             typedef cc::feldman_hashset::stat<> stat;
202         };
203         typedef FeldmanHashSet< cds::gc::HP,  key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_hp_city64_stat;
204         typedef FeldmanHashSet< cds::gc::DHP, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_dhp_city64_stat;
205         typedef FeldmanHashSet< rcu_gpi, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_rcu_gpi_city64_stat;
206         typedef FeldmanHashSet< rcu_gpb, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_rcu_gpb_city64_stat;
207         typedef FeldmanHashSet< rcu_gpt, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_rcu_gpt_city64_stat;
208 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
209         typedef FeldmanHashSet< rcu_shb, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_rcu_shb_city64_stat;
210         typedef FeldmanHashSet< rcu_sht, key_val<::cds_test::city64>, traits_FeldmanHashSet_city64_stat >    FeldmanHashSet_rcu_sht_city64_stat;
211 #endif
212
213         struct traits_FeldmanHashSet_city128 : public default_traits
214         {
215             typedef ::cds_test::city128::less less;
216         };
217         typedef FeldmanHashSet< cds::gc::HP,  key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_hp_city128;
218         typedef FeldmanHashSet< cds::gc::DHP, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_dhp_city128;
219         typedef FeldmanHashSet< rcu_gpi, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_rcu_gpi_city128;
220         typedef FeldmanHashSet< rcu_gpb, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_rcu_gpb_city128;
221         typedef FeldmanHashSet< rcu_gpt, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_rcu_gpt_city128;
222 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
223         typedef FeldmanHashSet< rcu_shb, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_rcu_shb_city128;
224         typedef FeldmanHashSet< rcu_sht, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128 >    FeldmanHashSet_rcu_sht_city128;
225 #endif
226
227         struct traits_FeldmanHashSet_city128_stat : public traits_FeldmanHashSet_city128
228         {
229             typedef cc::feldman_hashset::stat<> stat;
230         };
231         typedef FeldmanHashSet< cds::gc::HP,  key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_hp_city128_stat;
232         typedef FeldmanHashSet< cds::gc::DHP, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_dhp_city128_stat;
233         typedef FeldmanHashSet< rcu_gpi, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_rcu_gpi_city128_stat;
234         typedef FeldmanHashSet< rcu_gpb, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_rcu_gpb_city128_stat;
235         typedef FeldmanHashSet< rcu_gpt, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_rcu_gpt_city128_stat;
236 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
237         typedef FeldmanHashSet< rcu_shb, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_rcu_shb_city128_stat;
238         typedef FeldmanHashSet< rcu_sht, key_val<::cds_test::city128>, traits_FeldmanHashSet_city128_stat >    FeldmanHashSet_rcu_sht_city128_stat;
239 #endif
240
241 #endif // #if CDS_BUILD_BITS == 64
242
243
244         // for fixed-sized key
245         // No hash function is necessary
246
247         struct fixed_sized_key
248         {
249             typedef typename set_type_base< Key, Val >::key_type key_type;
250             struct key_val : public set_type_base< Key, Val >::key_val
251             {
252                 typedef typename set_type_base< Key, Val >::key_val base_class;
253
254                 /*explicit*/ key_val(key_type const& k) : base_class(k) {}
255                 key_val(key_type const& k, value_type const& v) : base_class(k, v) {}
256
257                 template <typename K>
258                 /*explicit*/ key_val(K const& k) : base_class(k) {}
259
260                 template <typename K, typename T>
261                 key_val(K const& k, T const& v) : base_class(k, v) {}
262
263                 // mock hasher
264                 struct hasher {
265                 template <typename Q>
266                     key_type operator()( Q const& k ) const
267                     {
268                         return key_type( k );
269                     }
270                 };
271             };
272
273             struct traits : public cc::feldman_hashset::traits
274             {
275                 struct hash_accessor {
276                     key_type operator()(key_val const& kv)
277                     {
278                         return kv.key;
279                     }
280                 };
281             };
282
283             struct traits_stat : public traits
284             {
285                 typedef cc::feldman_hashset::stat<> stat;
286             };
287         };
288
289         typedef FeldmanHashSet< cds::gc::HP, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >  FeldmanHashSet_hp_fixed;
290         typedef FeldmanHashSet< cds::gc::DHP, typename fixed_sized_key::key_val, typename fixed_sized_key::traits > FeldmanHashSet_dhp_fixed;
291         typedef FeldmanHashSet< rcu_gpi, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >    FeldmanHashSet_rcu_gpi_fixed;
292         typedef FeldmanHashSet< rcu_gpb, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >    FeldmanHashSet_rcu_gpb_fixed;
293         typedef FeldmanHashSet< rcu_gpt, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >    FeldmanHashSet_rcu_gpt_fixed;
294 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
295         typedef FeldmanHashSet< rcu_shb, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >    FeldmanHashSet_rcu_shb_fixed;
296         typedef FeldmanHashSet< rcu_sht, typename fixed_sized_key::key_val, typename fixed_sized_key::traits >    FeldmanHashSet_rcu_sht_fixed;
297 #endif
298
299         typedef FeldmanHashSet< cds::gc::HP, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_hp_fixed_stat;
300         typedef FeldmanHashSet< cds::gc::DHP, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_dhp_fixed_stat;
301         typedef FeldmanHashSet< rcu_gpi, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_rcu_gpi_fixed_stat;
302         typedef FeldmanHashSet< rcu_gpb, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_rcu_gpb_fixed_stat;
303         typedef FeldmanHashSet< rcu_gpt, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_rcu_gpt_fixed_stat;
304 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
305         typedef FeldmanHashSet< rcu_shb, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_rcu_shb_fixed_stat;
306         typedef FeldmanHashSet< rcu_sht, typename fixed_sized_key::key_val, typename fixed_sized_key::traits_stat >    FeldmanHashSet_rcu_sht_fixed_stat;
307 #endif
308
309     };
310
311     template <typename GC, typename T, typename Traits >
312     static inline void print_stat( cds_test::property_stream& o, FeldmanHashSet< GC, T, Traits > const& s )
313     {
314         std::vector< cds::intrusive::feldman_hashset::level_statistics > level_stat;
315         s.get_level_statistics( level_stat );
316
317         o << s.statistics() << level_stat;
318     }
319 } // namespace set
320
321 #define CDSSTRESS_FeldmanHashSet_case( fixture, test_case, feldman_set_type, key_type, value_type ) \
322     TEST_F( fixture, feldman_set_type ) \
323     { \
324         typedef set::set_type< tag_FeldmanHashSet, key_type, value_type >::feldman_set_type set_type; \
325         test_case<set_type>(); \
326     }
327
328 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
329 #   define CDSSTRESS_FeldmanHashSet_SHRCU( fixture, test_case, key_type, value_type ) \
330         CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_shb_fixed,      key_type, value_type ) \
331         CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_sht_fixed,      key_type, value_type ) \
332         CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_shb_fixed_stat, key_type, value_type ) \
333         CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_sht_fixed_stat, key_type, value_type )
334 #else
335 #   define CDSSTRESS_FeldmanHashSet_SHRCU( fixture, test_case, key_type, value_type )
336 #endif
337
338
339 #define CDSSTRESS_FeldmanHashSet( fixture, test_case, key_type, value_type ) \
340     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_hp_fixed,             key_type, value_type ) \
341     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_dhp_fixed,            key_type, value_type ) \
342     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpi_fixed,        key_type, value_type ) \
343     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpb_fixed,        key_type, value_type ) \
344     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpt_fixed,        key_type, value_type ) \
345     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_hp_fixed_stat,        key_type, value_type ) \
346     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_dhp_fixed_stat,       key_type, value_type ) \
347     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpi_fixed_stat,   key_type, value_type ) \
348     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpb_fixed_stat,   key_type, value_type ) \
349     CDSSTRESS_FeldmanHashSet_case( fixture, test_case, FeldmanHashSet_rcu_gpt_fixed_stat,   key_type, value_type ) \
350     CDSSTRESS_FeldmanHashSet_SHRCU( fixture, test_case, key_type, value_type )
351
352 #endif // #ifndef CDSUNIT_SET_TYPE_FELDMAN_HASHSET_H