Added copyright and license
[libcds.git] / tests / unit / map2 / 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_MICHAEL_H
32 #define CDSUNIT_MAP_TYPE_MICHAEL_H
33
34 #include "map2/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 map2 {
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::compare    compare;
58         typedef typename base_class::less       less;
59         typedef typename base_class::equal_to   equal_to;
60         typedef typename base_class::key_hash   key_hash;
61         typedef typename base_class::hash       hash;
62         typedef typename base_class::hash2      hash2;
63
64         // for sequential containers
65         template <class BucketEntry, typename... Options>
66         class StripedHashMap_seq:
67             public cc::StripedMap< BucketEntry,
68                 co::mutex_policy< cc::striped_set::striping<> >
69                 ,co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
70                 , Options...
71             >
72         {
73             typedef cc::StripedMap< BucketEntry,
74                 co::mutex_policy< cc::striped_set::striping<> >
75                 ,co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
76                 , Options...
77             > base_class;
78             typedef typename base_class::resizing_policy resizing_policy_t;
79
80             resizing_policy_t   m_placeHolder;
81         public:
82             template <class Config>
83             StripedHashMap_seq( Config const& cfg )
84                 : base_class( cfg.c_nMapSize / cfg.c_nLoadFactor / 16, *(new(&m_placeHolder) resizing_policy_t( cfg.c_nLoadFactor )) )
85             {}
86
87             // for testing
88             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
89             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = true;
90         };
91
92         // for non-sequential ordered containers
93         template <class BucketEntry, typename... Options>
94         class StripedHashMap_ord:
95             public cc::StripedMap< BucketEntry,
96                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
97                 ,co::mutex_policy< cc::striped_set::striping<> >
98                 , Options...
99             >
100         {
101             typedef cc::StripedMap< BucketEntry,
102                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
103                 ,co::mutex_policy< cc::striped_set::striping<> >
104                 , Options...
105             > base_class;
106             typedef typename base_class::resizing_policy resizing_policy_t;
107
108             resizing_policy_t   m_placeHolder;
109         public:
110             template <class Config>
111             StripedHashMap_ord( Config const& cfg )
112                 : base_class( 0, *(new(&m_placeHolder) resizing_policy_t( cfg.c_nMaxLoadFactor * 1024 )) )
113             {}
114
115             // for testing
116             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
117             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
118         };
119
120
121         typedef StripedHashMap_seq<
122             std::list< std::pair< Key const, Value > >
123             , co::hash< hash2 >
124             , co::less< less >
125         > StripedMap_list;
126
127 #   if (CDS_COMPILER == CDS_COMPILER_MSVC || (CDS_COMPILER == CDS_COMPILER_INTEL && CDS_OS_INTERFACE == CDS_OSI_WINDOWS)) && _MSC_VER < 1600
128         typedef StripedHashMap_ord<
129             stdext::hash_map< Key, Value, stdext::hash_compare<Key, less > >
130             , co::hash< hash2 >
131         > StripedMap_hashmap;
132 #   else
133         typedef StripedHashMap_ord<
134             std::unordered_map< Key, Value, hash, equal_to >
135             , co::hash< hash2 >
136         > StripedMap_hashmap;
137 #   endif
138
139         typedef StripedHashMap_ord<
140             std::map< Key, Value, less >
141             , co::hash< hash2 >
142         > StripedMap_map;
143
144         typedef StripedHashMap_ord<
145             boost::unordered_map< Key, Value, hash, equal_to >
146             , co::hash< hash2 >
147         > StripedMap_boost_unordered_map;
148
149 #   if BOOST_VERSION >= 104800
150         typedef StripedHashMap_seq<
151             boost::container::slist< std::pair< Key const, Value > >
152             , co::hash< hash2 >
153             , co::less< less >
154         > StripedMap_slist;
155
156         typedef StripedHashMap_seq<
157             boost::container::list< std::pair< Key const, Value > >
158             , co::hash< hash2 >
159             , co::less< less >
160         > StripedMap_boost_list;
161
162         typedef StripedHashMap_ord<
163             boost::container::map< Key, Value, less >
164             , co::hash< hash2 >
165         > StripedMap_boost_map;
166
167         typedef StripedHashMap_ord<
168             boost::container::flat_map< Key, Value, less >
169             , co::hash< hash2 >
170         > StripedMap_boost_flat_map;
171 #   endif  // BOOST_VERSION >= 104800
172
173
174         // ***************************************************************************
175         // RefinableHashMap
176
177         // for sequential containers
178         template <class BucketEntry, typename... Options>
179         class RefinableHashMap_seq:
180             public cc::StripedMap< BucketEntry,
181                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
182                 ,co::mutex_policy< cc::striped_set::refinable<> >
183                 , Options...
184             >
185         {
186             typedef cc::StripedMap< BucketEntry,
187                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
188                 ,co::mutex_policy< cc::striped_set::refinable<> >
189                 , Options...
190             > base_class;
191             typedef typename base_class::resizing_policy resizing_policy_t;
192
193             resizing_policy_t   m_placeHolder;
194         public:
195             template <class Config>
196             RefinableHashMap_seq( Config const& cfg )
197                 : base_class( cfg.c_nMapSize / cfg.c_nLoadFactor / 16, *(new(&m_placeHolder) resizing_policy_t( cfg.c_nLoadFactor )))
198             {}
199
200             // for testing
201             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
202             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = true;
203         };
204
205         // for non-sequential ordered containers
206         template <class BucketEntry, typename... Options>
207         class RefinableHashMap_ord:
208             public cc::StripedMap< BucketEntry,
209                 co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
210                 ,co::mutex_policy< cc::striped_set::refinable<> >
211                 , Options...
212             >
213         {
214             typedef cc::StripedMap< BucketEntry,
215                co::resizing_policy<cc::striped_set::load_factor_resizing<0> >
216                 ,co::mutex_policy< cc::striped_set::refinable<> >
217                 , Options...
218             > base_class;
219             typedef typename base_class::resizing_policy resizing_policy_t;
220
221             resizing_policy_t   m_placeHolder;
222         public:
223             template <class Config>
224             RefinableHashMap_ord( Config const& cfg )
225                 : base_class( 0, *(new(&m_placeHolder) resizing_policy_t( cfg.c_nMaxLoadFactor * 1024 )) )
226             {}
227
228             // for testing
229             static CDS_CONSTEXPR bool const c_bExtractSupported = false;
230             static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
231         };
232
233
234         typedef RefinableHashMap_seq<
235             std::list< std::pair< Key const, Value > >
236             , co::hash< hash2 >
237             , co::less< less >
238         > RefinableMap_list;
239
240 #   if BOOST_VERSION >= 104800
241         typedef RefinableHashMap_seq<
242             boost::container::slist< std::pair< Key const, Value > >
243             , co::hash< hash2 >
244             , co::less< less >
245         > RefinableMap_slist;
246 #   endif
247
248         typedef RefinableHashMap_ord<
249             std::map< Key, Value, less >
250             , co::hash< hash2 >
251         > RefinableMap_map;
252
253         typedef RefinableHashMap_ord<
254             std::unordered_map< Key, Value, hash, equal_to >
255             , co::hash< hash2 >
256         > RefinableMap_hashmap;
257
258         typedef RefinableHashMap_ord<
259             boost::unordered_map< Key, Value, hash, equal_to >
260             , co::hash< hash2 >
261         > RefinableMap_boost_unordered_map;
262
263 #   if BOOST_VERSION >= 104800
264         typedef RefinableHashMap_seq<
265             boost::container::list< std::pair< Key const, Value > >
266             , co::hash< hash2 >
267             , co::less< less >
268         > RefinableMap_boost_list;
269
270         typedef RefinableHashMap_ord<
271             boost::container::map< Key, Value, less >
272             , co::hash< hash2 >
273         > RefinableMap_boost_map;
274
275         typedef RefinableHashMap_ord<
276             boost::container::flat_map< Key, Value, less >
277             , co::hash< hash2 >
278         > RefinableMap_boost_flat_map;
279 #   endif // #if BOOST_VERSION >= 104800
280
281     };
282 }   // namespace map2
283
284 #endif // ifndef CDSUNIT_MAP_TYPE_MICHAEL_H