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