44773db73dbddc810e59994df22e2034ed46d0cc
[libcds.git] / test / stress / map / map_type_striped.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_STRIPED_H
32 #define CDSUNIT_MAP_TYPE_STRIPED_H
33
34 #include "map_type.h"
35 #include <cds/container/striped_map/std_list.h>
36 #include <cds/container/striped_map/std_map.h>
37 #include <cds/container/striped_map/std_hash_map.h>
38
39 #include <boost/version.hpp>
40 #if BOOST_VERSION >= 104800
41 #   include <cds/container/striped_map/boost_list.h>
42 #   include <cds/container/striped_map/boost_slist.h>
43 #   include <cds/container/striped_map/boost_map.h>
44 #   include <cds/container/striped_map/boost_flat_map.h>
45 #endif
46 #include <cds/container/striped_map/boost_unordered_map.h>
47 #include <cds/container/striped_map.h>
48
49 namespace map {
50
51     struct tag_StripedMap;
52
53     template <typename Key, typename Value>
54     struct map_type< tag_StripedMap, Key, Value >: public map_type_base< Key, Value >
55     {
56         typedef map_type_base< Key, Value >      base_class;
57         typedef typename base_class::key_compare compare;
58         typedef typename base_class::key_less    less;
59         typedef typename base_class::equal_to    equal_to;
60         typedef typename base_class::key_hash    hash;
61         typedef typename base_class::hash2       hash2;
62
63         // for sequential containers
64         template <class BucketEntry, typename... Options>
65         class StripedHashMap_seq:
66             public cc::StripedMap< BucketEntry,
67                 co::mutex_policy< cc::striped_set::striping<> >
68                 ,co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
69                 , Options...
70             >
71         {
72             typedef cc::StripedMap< BucketEntry,
73                 co::mutex_policy< cc::striped_set::striping<> >
74                 ,co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
75                 , Options...
76             > base_class;
77             typedef typename base_class::resizing_policy resizing_policy_t;
78
79             resizing_policy_t   m_placeHolder;
80         public:
81             template <class Config>
82             StripedHashMap_seq( Config const& cfg )
83                 : base_class( cfg.s_nMapSize / cfg.s_nLoadFactor / 16, *(new(&m_placeHolder) resizing_policy_t( cfg.s_nLoadFactor )) )
84             {}
85
86             // for testing
87             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
88             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = true;
89         };
90
91         // for non-sequential ordered containers
92         template <class BucketEntry, typename... Options>
93         class StripedHashMap_ord:
94             public cc::StripedMap< BucketEntry,
95                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
96                 ,co::mutex_policy< cc::striped_set::striping<> >
97                 , Options...
98             >
99         {
100             typedef cc::StripedMap< BucketEntry,
101                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
102                 ,co::mutex_policy< cc::striped_set::striping<> >
103                 , Options...
104             > base_class;
105             typedef typename base_class::resizing_policy resizing_policy_t;
106
107             resizing_policy_t   m_placeHolder;
108         public:
109             template <class Config>
110             StripedHashMap_ord( Config const& cfg )
111                 : base_class( 0, *(new(&m_placeHolder) resizing_policy_t( cfg.s_nMaxLoadFactor * 1024 )) )
112             {}
113
114             // for testing
115             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
116             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
117         };
118
119
120         typedef StripedHashMap_seq<
121             std::list< std::pair< Key const, Value > >
122             , co::hash< hash2 >
123             , co::less< less >
124         > StripedMap_list;
125
126         typedef StripedHashMap_ord<
127             std::unordered_map< Key, Value, hash, equal_to >
128             , co::hash< hash2 >
129         > StripedMap_hashmap;
130
131         typedef StripedHashMap_ord<
132             std::map< Key, Value, less >
133             , co::hash< hash2 >
134         > StripedMap_map;
135
136         typedef StripedHashMap_ord<
137             boost::unordered_map< Key, Value, hash, equal_to >
138             , co::hash< hash2 >
139         > StripedMap_boost_unordered_map;
140
141 #   if BOOST_VERSION >= 104800
142         typedef StripedHashMap_seq<
143             boost::container::slist< std::pair< Key const, Value > >
144             , co::hash< hash2 >
145             , co::less< less >
146         > StripedMap_slist;
147
148         typedef StripedHashMap_seq<
149             boost::container::list< std::pair< Key const, Value > >
150             , co::hash< hash2 >
151             , co::less< less >
152         > StripedMap_boost_list;
153
154         typedef StripedHashMap_ord<
155             boost::container::map< Key, Value, less >
156             , co::hash< hash2 >
157         > StripedMap_boost_map;
158
159         typedef StripedHashMap_ord<
160             boost::container::flat_map< Key, Value, less >
161             , co::hash< hash2 >
162         > StripedMap_boost_flat_map;
163 #   endif  // BOOST_VERSION >= 104800
164
165
166         // ***************************************************************************
167         // RefinableHashMap
168
169         // for sequential containers
170         template <class BucketEntry, typename... Options>
171         class RefinableHashMap_seq:
172             public cc::StripedMap< BucketEntry,
173                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
174                 ,co::mutex_policy< cc::striped_set::refinable<> >
175                 , Options...
176             >
177         {
178             typedef cc::StripedMap< BucketEntry,
179                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
180                 ,co::mutex_policy< cc::striped_set::refinable<> >
181                 , Options...
182             > base_class;
183             typedef typename base_class::resizing_policy resizing_policy_t;
184
185             resizing_policy_t   m_placeHolder;
186         public:
187             template <class Config>
188             RefinableHashMap_seq( Config const& cfg )
189                 : base_class( cfg.s_nMapSize / cfg.s_nLoadFactor / 16, *(new(&m_placeHolder) resizing_policy_t( cfg.s_nLoadFactor )))
190             {}
191
192             // for testing
193             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
194             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = true;
195         };
196
197         // for non-sequential ordered containers
198         template <class BucketEntry, typename... Options>
199         class RefinableHashMap_ord:
200             public cc::StripedMap< BucketEntry,
201                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
202                 ,co::mutex_policy< cc::striped_set::refinable<> >
203                 , Options...
204             >
205         {
206             typedef cc::StripedMap< BucketEntry,
207                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
208                 ,co::mutex_policy< cc::striped_set::refinable<> >
209                 , Options...
210             > base_class;
211             typedef typename base_class::resizing_policy resizing_policy_t;
212
213             resizing_policy_t   m_placeHolder;
214         public:
215             template <class Config>
216             RefinableHashMap_ord( Config const& cfg )
217                 : base_class( 0, *(new(&m_placeHolder) resizing_policy_t( cfg.s_nMaxLoadFactor * 1024 )) )
218             {}
219
220             // for testing
221             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
222             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
223         };
224
225
226         typedef RefinableHashMap_seq<
227             std::list< std::pair< Key const, Value > >
228             , co::hash< hash2 >
229             , co::less< less >
230         > RefinableMap_list;
231
232 #   if BOOST_VERSION >= 104800
233         typedef RefinableHashMap_seq<
234             boost::container::slist< std::pair< Key const, Value > >
235             , co::hash< hash2 >
236             , co::less< less >
237         > RefinableMap_slist;
238 #   endif
239
240         typedef RefinableHashMap_ord<
241             std::map< Key, Value, less >
242             , co::hash< hash2 >
243         > RefinableMap_map;
244
245         typedef RefinableHashMap_ord<
246             std::unordered_map< Key, Value, hash, equal_to >
247             , co::hash< hash2 >
248         > RefinableMap_hashmap;
249
250         typedef RefinableHashMap_ord<
251             boost::unordered_map< Key, Value, hash, equal_to >
252             , co::hash< hash2 >
253         > RefinableMap_boost_unordered_map;
254
255 #   if BOOST_VERSION >= 104800
256         typedef RefinableHashMap_seq<
257             boost::container::list< std::pair< Key const, Value > >
258             , co::hash< hash2 >
259             , co::less< less >
260         > RefinableMap_boost_list;
261
262         typedef RefinableHashMap_ord<
263             boost::container::map< Key, Value, less >
264             , co::hash< hash2 >
265         > RefinableMap_boost_map;
266
267         typedef RefinableHashMap_ord<
268             boost::container::flat_map< Key, Value, less >
269             , co::hash< hash2 >
270         > RefinableMap_boost_flat_map;
271 #   endif // #if BOOST_VERSION >= 104800
272
273     };
274 }   // namespace map
275
276 #define CDSSTRESS_StripedMap_case( fixture, test_case, striped_map_type, key_type, value_type ) \
277     TEST_P( fixture, striped_map_type ) \
278     { \
279         typedef map::map_type< tag_StripedMap, key_type, value_type >::striped_map_type map_type; \
280         test_case<map_type>(); \
281     }
282
283 #define CDSSTRESS_StripedMap( fixture, test_case, key_type, value_type ) \
284     CDSSTRESS_StripedMap_case( fixture, test_case, StripedMap_list,         key_type, value_type ) \
285     CDSSTRESS_StripedMap_case( fixture, test_case, StripedMap_hashmap,      key_type, value_type ) \
286     CDSSTRESS_StripedMap_case( fixture, test_case, StripedMap_map,          key_type, value_type ) \
287     CDSSTRESS_StripedMap_case( fixture, test_case, RefinableMap_list,       key_type, value_type ) \
288     CDSSTRESS_StripedMap_case( fixture, test_case, RefinableMap_map,        key_type, value_type ) \
289     CDSSTRESS_StripedMap_case( fixture, test_case, RefinableMap_hashmap,    key_type, value_type ) \
290
291 #endif // ifndef CDSUNIT_MAP_TYPE_STRIPED_H