dcdbb6a129b32b7ed17cd56e8143950c4b4b6ead
[libcds.git] / test / stress / map / map_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_MAP_TYPE_CUCKOO_H
32 #define CDSUNIT_MAP_TYPE_CUCKOO_H
33
34 #include "map_type.h"
35 #include <cds/container/cuckoo_map.h>
36 #include <cds_test/stat_cuckoo_out.h>
37
38 namespace map {
39
40     template <typename K, typename V, typename Traits>
41     class CuckooMap: public cc::CuckooMap< K, V, Traits >
42     {
43     public:
44         typedef cc::CuckooMap< K, V, Traits > base_class;
45
46     public:
47         template <typename Config>
48         CuckooMap( Config const& cfg )
49             : base_class(
50                 cfg.s_nCuckooInitialSize,
51                 static_cast<unsigned int>( cfg.s_nCuckooProbesetSize ),
52                 static_cast<unsigned int>( cfg.s_nCuckooProbesetThreshold )
53             )
54         {}
55
56         template <typename Q, typename Pred>
57         bool erase_with( Q const& key, Pred /*pred*/ )
58         {
59             return base_class::erase_with( key, typename std::conditional< base_class::c_isSorted, Pred, typename Pred::equal_to>::type() );
60         }
61
62         // for testing
63         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
64         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
65         static CDS_CONSTEXPR bool const c_bEraseExactKey = false;
66     };
67
68     struct tag_CuckooMap;
69
70     template <typename Key, typename Value>
71     struct map_type< tag_CuckooMap, Key, Value >: public map_type_base< Key, Value >
72     {
73         typedef map_type_base< Key, Value > base_class;
74         typedef typename base_class::key_compare compare;
75         typedef typename base_class::key_less    less;
76         typedef typename base_class::equal_to    equal_to;
77         typedef typename base_class::key_hash    hash;
78         typedef typename base_class::hash2       hash2;
79
80         template <typename Traits>
81         struct traits_CuckooStripedMap: public Traits
82         {
83             typedef cc::cuckoo::striping<> mutex_policy;
84         };
85         template <typename Traits>
86         struct traits_CuckooRefinableMap : public Traits
87         {
88             typedef cc::cuckoo::refinable<> mutex_policy;
89         };
90
91         struct traits_CuckooMap_list_unord :
92             public cc::cuckoo::make_traits <
93                 cc::cuckoo::probeset_type< cc::cuckoo::list >
94                 , co::equal_to< equal_to >
95                 , co::hash< std::tuple< hash, hash2 > >
96             > ::type
97         {};
98         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_unord>> CuckooStripedMap_list_unord;
99         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_unord>> CuckooRefinableMap_list_unord;
100
101         struct traits_CuckooMap_list_unord_stat : public traits_CuckooMap_list_unord
102         {
103             typedef cc::cuckoo::stat stat;
104         };
105         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_unord_stat>> CuckooStripedMap_list_unord_stat;
106         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_unord_stat>> CuckooRefinableMap_list_unord_stat;
107
108         struct traits_CuckooMap_list_unord_storehash : public traits_CuckooMap_list_unord
109         {
110             static CDS_CONSTEXPR const bool store_hash = true;
111         };
112         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_unord_storehash>> CuckooStripedMap_list_unord_storehash;
113         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_unord_storehash>> CuckooRefinableMap_list_unord_storehash;
114
115         struct traits_CuckooMap_list_ord :
116             public cc::cuckoo::make_traits <
117                 cc::cuckoo::probeset_type< cc::cuckoo::list >
118                 , co::compare< compare >
119                 , co::hash< std::tuple< hash, hash2 > >
120             >::type
121         {};
122         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_ord>> CuckooStripedMap_list_ord;
123         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_ord>> CuckooRefinableMap_list_ord;
124
125         struct traits_CuckooMap_list_ord_stat : public traits_CuckooMap_list_ord
126         {
127             typedef cc::cuckoo::stat stat;
128         };
129         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_ord_stat>> CuckooStripedMap_list_ord_stat;
130         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_ord_stat>> CuckooRefinableMap_list_ord_stat;
131
132         struct traits_CuckooMap_list_ord_storehash : public traits_CuckooMap_list_ord
133         {
134             static CDS_CONSTEXPR const bool store_hash = true;
135         };
136         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_list_ord_storehash>> CuckooStripedMap_list_ord_storehash;
137         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_list_ord_storehash>> CuckooRefinableMap_list_ord_storehash;
138
139         struct traits_CuckooMap_vector_unord :
140             public cc::cuckoo::make_traits <
141                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
142                 , co::equal_to< equal_to >
143                 , co::hash< std::tuple< hash, hash2 > >
144             >::type
145         {};
146         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_unord>> CuckooStripedMap_vector_unord;
147         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_unord>> CuckooRefinableMap_vector_unord;
148
149         struct traits_CuckooMap_vector_unord_stat : public traits_CuckooMap_vector_unord
150         {
151             typedef cc::cuckoo::stat stat;
152         };
153         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_unord_stat>> CuckooStripedMap_vector_unord_stat;
154         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_unord_stat>> CuckooRefinableMap_vector_unord_stat;
155
156         struct traits_CuckooMap_vector_unord_storehash : public traits_CuckooMap_vector_unord
157         {
158             static CDS_CONSTEXPR const bool store_hash = true;
159         };
160         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_unord_storehash>> CuckooStripedMap_vector_unord_storehash;
161         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_unord_storehash>> CuckooRefinableMap_vector_unord_storehash;
162
163         struct traits_CuckooMap_vector_ord :
164             public cc::cuckoo::make_traits <
165                 cc::cuckoo::probeset_type< cc::cuckoo::vector<4> >
166                 , co::compare< compare >
167                 , co::hash< std::tuple< hash, hash2 > >
168             >::type
169         {};
170         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_ord>> CuckooStripedMap_vector_ord;
171         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_ord>> CuckooRefinableMap_vector_ord;
172
173         struct traits_CuckooMap_vector_ord_stat : public traits_CuckooMap_vector_ord
174         {
175             typedef cc::cuckoo::stat stat;
176         };
177         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_ord_stat>> CuckooStripedMap_vector_ord_stat;
178         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_ord_stat>> CuckooRefinableMap_vector_ord_stat;
179
180         struct traits_CuckooMap_vector_ord_storehash : public traits_CuckooMap_vector_ord
181         {
182             static CDS_CONSTEXPR const bool store_hash = true;
183         };
184         typedef CuckooMap< Key, Value, traits_CuckooStripedMap<traits_CuckooMap_vector_ord_storehash>> CuckooStripedMap_vector_ord_storehash;
185         typedef CuckooMap< Key, Value, traits_CuckooRefinableMap<traits_CuckooMap_vector_ord_storehash>> CuckooRefinableMap_vector_ord_storehash;
186
187     };
188
189     template <typename Key, typename T, typename Traits >
190     static inline void print_stat( cds_test::property_stream& o, cc::CuckooMap< Key, T, Traits > const& s )
191     {
192         o << s.statistics() << s.mutex_policy_statistics();
193     }
194
195     template <typename Key, typename V, typename Traits>
196     static inline void print_stat( cds_test::property_stream& o, CuckooMap< Key, V, Traits > const& s )
197     {
198         typedef CuckooMap< Key, V, Traits > map_type;
199         print_stat( o, static_cast<typename map_type::base_class const&>(s) );
200     }
201
202 }   // namespace map
203
204
205 #define CDSSTRESS_CuckooMap_case( fixture, test_case, cuckoo_map_type, key_type, value_type ) \
206     TEST_F( fixture, cuckoo_map_type ) \
207     { \
208         typedef map::map_type< tag_CuckooMap, key_type, value_type >::cuckoo_map_type map_type; \
209         test_case<map_type>(); \
210     }
211
212 #define CDSSTRESS_CuckooMap( fixture, test_case, key_type, value_type ) \
213     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_unord,              key_type, value_type ) \
214     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_unord,            key_type, value_type ) \
215     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_unord_stat,         key_type, value_type ) \
216     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_unord_stat,       key_type, value_type ) \
217     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_unord_storehash,    key_type, value_type ) \
218     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_unord_storehash,  key_type, value_type ) \
219     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_ord,                key_type, value_type ) \
220     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_ord,              key_type, value_type ) \
221     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_ord_stat,           key_type, value_type ) \
222     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_ord_stat,         key_type, value_type ) \
223     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_list_ord_storehash,      key_type, value_type ) \
224     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_list_ord_storehash,    key_type, value_type ) \
225     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_unord,            key_type, value_type ) \
226     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_unord,          key_type, value_type ) \
227     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_unord_stat,       key_type, value_type ) \
228     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_unord_stat,     key_type, value_type ) \
229     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_unord_storehash,  key_type, value_type ) \
230     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_unord_storehash, key_type, value_type ) \
231     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_ord,              key_type, value_type ) \
232     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_ord,            key_type, value_type ) \
233     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_ord_stat,         key_type, value_type ) \
234     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_ord_stat,       key_type, value_type ) \
235     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooStripedMap_vector_ord_storehash,    key_type, value_type ) \
236     CDSSTRESS_CuckooMap_case( fixture, test_case, CuckooRefinableMap_vector_ord_storehash,  key_type, value_type )
237
238 #endif // ifndef CDSUNIT_MAP_TYPE_CUCKOO_H