Fixed MultiLevelHashSet MT-test
[libcds.git] / tests / unit / set2 / set_type_multilevel_hashset.h
1 //$$CDS-header$$
2
3 #ifndef CDSUNIT_SET_TYPE_MICHAEL_H
4 #define CDSUNIT_SET_TYPE_MICHAEL_H
5
6 #include <cds/container/multilevel_hashset_hp.h>
7 #include <cds/container/multilevel_hashset_dhp.h>
8
9 #include "print_multilevel_hashset_stat.h"
10 #include "hashing/hash_func.h"
11
12 namespace set2 {
13
14     template <class GC, typename T, typename Traits = cc::multilevel_hashset::traits>
15     class MultiLevelHashSet : public cc::MultiLevelHashSet< GC, T, Traits >
16     {
17         typedef cc::MultiLevelHashSet< GC, T, Traits > base_class;
18     public:
19         typedef typename T::hasher hasher ;
20         typedef typename base_class::guarded_ptr guarded_ptr;
21
22         template <class Config>
23         MultiLevelHashSet( Config const& cfg )
24             : base_class( cfg.c_nMultiLevelSet_HeadBits, cfg.c_nMultiLevelSet_ArrayBits )
25         {}
26
27         template <typename Q>
28         bool erase( Q const& key )
29         {
30             return base_class::erase( hasher()( key ));
31         }
32
33         template <typename Q, typename Func>
34         bool erase( Q const& key, Func f )
35         {
36             return base_class::erase( hasher()( key ), f );
37         }
38
39         template <typename Q>
40         guarded_ptr extract(Q const& key)
41         {
42             return base_class::extract( hasher()(key) );
43         }
44
45         template <typename Q>
46         bool contains( Q const& key )
47         {
48             return base_class::contains( hasher()(key) );
49         }
50
51         // for testing
52         static CDS_CONSTEXPR bool const c_bExtractSupported = true;
53         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
54     };
55
56     struct tag_MultiLevelHashSet;
57
58     template <typename Key, typename Val>
59     struct set_type< tag_MultiLevelHashSet, Key, Val >: public set_type_base< Key, Val >
60     {
61         typedef set_type_base< Key, Val > base_class;
62         typedef typename base_class::compare compare;
63         typedef typename base_class::less less;
64         typedef typename base_class::hash hash;
65         typedef typename base_class::key_type   key_type;
66         typedef typename base_class::value_type value_type;
67
68         template <typename Hasher>
69         struct hash_type
70         {
71             typedef Hasher hasher;
72             typedef typename hasher::hash_type type;
73         };
74
75         template <typename TH>
76         struct hash_type<std::hash<TH>>
77         {
78             typedef std::hash<TH> hasher;
79             typedef size_t type;
80         };
81
82         template <typename Hasher>
83         struct key_val: base_class::key_val
84         {
85             typedef typename base_class::key_val base;
86             typedef Hasher hasher;
87             typedef typename hash_type<hasher>::type hash_type;
88
89             hash_type hash;
90
91             /*explicit*/ key_val( key_type const& k ): base(k), hash( hasher()( k )) {}
92             key_val( key_type const& k, value_type const& v ): base(k, v), hash( hasher()( k )) {}
93
94             template <typename K>
95             /*explicit*/ key_val( K const& k ): base(k), hash( hasher()( k )) {}
96
97             template <typename K, typename T>
98             key_val( K const& k, T const& v ): base(k, v), hash( hasher()( k )) {}
99         };
100
101         struct default_traits : public cc::multilevel_hashset::traits
102         {
103             struct hash_accessor {
104                 template <typename Hasher>
105                 typename key_val<Hasher>::hash_type const& operator()( key_val<Hasher> const& kv )
106                 {
107                     return kv.hash;
108                 }
109             };
110         };
111
112         typedef MultiLevelHashSet< cds::gc::HP,  key_val<std::hash<key_type>>, default_traits >    MultiLevelHashSet_hp_stdhash;
113         typedef MultiLevelHashSet< cds::gc::DHP, key_val<std::hash<key_type>>, default_traits >    MultiLevelHashSet_dhp_stdhash;
114
115         struct traits_MultiLevelHashSet_stat: public cc::multilevel_hashset::make_traits<
116                 co::type_traits< default_traits >,
117                 co::stat< cc::multilevel_hashset::stat<>>
118             >::type
119         {};
120
121         typedef MultiLevelHashSet< cds::gc::HP,  key_val<std::hash<key_type>>, traits_MultiLevelHashSet_stat >    MultiLevelHashSet_hp_stdhash_stat;
122         typedef MultiLevelHashSet< cds::gc::DHP, key_val<std::hash<key_type>>, traits_MultiLevelHashSet_stat >    MultiLevelHashSet_dhp_stdhash_stat;
123
124         // SHA256
125         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::sha256>, default_traits >    MultiLevelHashSet_hp_sha256;
126         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::sha256>, default_traits >    MultiLevelHashSet_dhp_sha256;
127
128         struct traits_MultiLevelHashSet_sha256_stat : public default_traits
129         {
130             typedef cc::multilevel_hashset::stat<> stat;
131         };
132         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::sha256>, traits_MultiLevelHashSet_sha256_stat >    MultiLevelHashSet_hp_sha256_stat;
133         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::sha256>, traits_MultiLevelHashSet_sha256_stat >    MultiLevelHashSet_dhp_sha256_stat;
134
135         //MD5
136         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::md5>, default_traits >    MultiLevelHashSet_hp_md5;
137         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::md5>, default_traits >    MultiLevelHashSet_dhp_md5;
138
139         struct traits_MultiLevelHashSet_md5_stat : public default_traits
140         {
141             typedef cc::multilevel_hashset::stat<> stat;
142         };
143         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::md5>, traits_MultiLevelHashSet_md5_stat >    MultiLevelHashSet_hp_md5_stat;
144         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::md5>, traits_MultiLevelHashSet_md5_stat >    MultiLevelHashSet_dhp_md5_stat;
145
146         // CityHash
147         struct traits_MultiLevelHashSet_city64 : public default_traits
148         {
149             typedef ::hashing::city64::less less;
150         };
151         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::city64>, traits_MultiLevelHashSet_city64 >    MultiLevelHashSet_hp_city64;
152         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::city64>, traits_MultiLevelHashSet_city64 >    MultiLevelHashSet_dhp_city64;
153
154         struct traits_MultiLevelHashSet_city64_stat : public traits_MultiLevelHashSet_city64
155         {
156             typedef cc::multilevel_hashset::stat<> stat;
157         };
158         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::city64>, traits_MultiLevelHashSet_city64_stat >    MultiLevelHashSet_hp_city64_stat;
159         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::city64>, traits_MultiLevelHashSet_city64_stat >    MultiLevelHashSet_dhp_city64_stat;
160
161         struct traits_MultiLevelHashSet_city128 : public default_traits
162         {
163             typedef ::hashing::city128::less less;
164         };
165         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::city128>, traits_MultiLevelHashSet_city128 >    MultiLevelHashSet_hp_city128;
166         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::city128>, traits_MultiLevelHashSet_city128 >    MultiLevelHashSet_dhp_city128;
167
168         struct traits_MultiLevelHashSet_city128_stat : public traits_MultiLevelHashSet_city128
169         {
170             typedef cc::multilevel_hashset::stat<> stat;
171         };
172         typedef MultiLevelHashSet< cds::gc::HP,  key_val<::hashing::city128>, traits_MultiLevelHashSet_city128_stat >    MultiLevelHashSet_hp_city128_stat;
173         typedef MultiLevelHashSet< cds::gc::DHP, key_val<::hashing::city128>, traits_MultiLevelHashSet_city128_stat >    MultiLevelHashSet_dhp_city128_stat;
174     };
175
176     template <typename GC, typename T, typename Traits >
177     static inline void print_stat( cc::MultiLevelHashSet< GC, T, Traits > const& s )
178     {
179         CPPUNIT_MSG( s.statistics() );
180     }
181
182 } // namespace set2
183
184 #endif // #ifndef CDSUNIT_SET_TYPE_MICHAEL_H