Added copyright and license
[libcds.git] / tests / unit / set2 / set_type.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_SET_TYPE_H
32 #define CDSUNIT_SET_TYPE_H
33
34 #include <cds/urcu/general_instant.h>
35 #include <cds/urcu/general_buffered.h>
36 #include <cds/urcu/general_threaded.h>
37 #include <cds/urcu/signal_buffered.h>
38 #include <cds/urcu/signal_threaded.h>
39
40 #include <cds/opt/hash.h>
41 #include <cds/sync/spinlock.h>
42 #include <boost/functional/hash/hash.hpp>
43
44 #include "cppunit/cppunit_mini.h"
45 #include "lock/nolock.h"
46 #include "michael_alloc.h"
47
48 namespace set2 {
49     namespace cc = cds::container;
50     namespace co = cds::opt;
51
52     typedef cds::urcu::gc< cds::urcu::general_instant<> >   rcu_gpi;
53     typedef cds::urcu::gc< cds::urcu::general_buffered<> >  rcu_gpb;
54     typedef cds::urcu::gc< cds::urcu::general_threaded<> >  rcu_gpt;
55 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
56     typedef cds::urcu::gc< cds::urcu::signal_buffered<> >  rcu_shb;
57     typedef cds::urcu::gc< cds::urcu::signal_threaded<> >  rcu_sht;
58 #endif
59
60     template <typename Key>
61     struct cmp {
62         int operator ()(Key const& k1, Key const& k2) const
63         {
64             if ( std::less<Key>( k1, k2 ) )
65                 return -1;
66             return std::less<Key>( k2, k1 ) ? 1 : 0;
67         }
68     };
69
70 #define CDSUNIT_INT_COMPARE(t)  template <> struct cmp<t> { int operator()( t k1, t k2 ){ return (int)(k1 - k2); } }
71     CDSUNIT_INT_COMPARE(char);
72     CDSUNIT_INT_COMPARE(unsigned char);
73     CDSUNIT_INT_COMPARE(int);
74     CDSUNIT_INT_COMPARE(unsigned int);
75     CDSUNIT_INT_COMPARE(long);
76     CDSUNIT_INT_COMPARE(unsigned long);
77     CDSUNIT_INT_COMPARE(long long);
78     CDSUNIT_INT_COMPARE(unsigned long long);
79 #undef CDSUNIT_INT_COMPARE
80
81     template <>
82     struct cmp<std::string>
83     {
84         int operator()(std::string const& s1, std::string const& s2)
85         {
86             return s1.compare( s2 );
87         }
88         int operator()(std::string const& s1, char const * s2)
89         {
90             return s1.compare( s2 );
91         }
92         int operator()(char const * s1, std::string const& s2)
93         {
94             return -s2.compare( s1 );
95         }
96     };
97
98     // forward
99     template <typename ImplSelector, typename Key, typename Value>
100     struct set_type;
101
102     template <typename Key, typename Value>
103     struct set_type_base
104     {
105         typedef Key     key_type;
106         typedef Value   value_type;
107
108         struct key_val {
109             key_type    key;
110             value_type  val;
111
112             /*explicit*/ key_val( key_type const& k ): key(k), val() {}
113             key_val( key_type const& k, value_type const& v ): key(k), val(v) {}
114
115             template <typename K>
116             /*explicit*/ key_val( K const& k ): key(k) {}
117
118             template <typename K, typename T>
119             key_val( K const& k, T const& v ): key(k), val(v) {}
120         };
121
122         typedef co::v::hash<key_type>   key_hash;
123         typedef std::less<key_type>     key_less;
124         typedef cmp<key_type>           key_compare;
125
126         struct less {
127             bool operator()( key_val const& k1, key_val const& k2 ) const
128             {
129                 return key_less()( k1.key, k2.key );
130             }
131             bool operator()( key_type const& k1, key_val const& k2 ) const
132             {
133                 return key_less()( k1, k2.key );
134             }
135             bool operator()( key_val const& k1, key_type const& k2 ) const
136             {
137                 return key_less()( k1.key, k2 );
138             }
139         };
140
141         struct compare {
142             int operator()( key_val const& k1, key_val const& k2 ) const
143             {
144                 return key_compare()( k1.key, k2.key );
145             }
146             int operator()( key_type const& k1, key_val const& k2 ) const
147             {
148                 return key_compare()( k1, k2.key );
149             }
150             int operator()( key_val const& k1, key_type const& k2 ) const
151             {
152                 return key_compare()( k1.key, k2 );
153             }
154         };
155
156         struct equal_to {
157             bool operator()( key_val const& k1, key_val const& k2 ) const
158             {
159                 return key_compare()( k1.key, k2.key ) == 0;
160             }
161             bool operator()( key_type const& k1, key_val const& k2 ) const
162             {
163                 return key_compare()( k1, k2.key ) == 0;
164             }
165             bool operator()( key_val const& k1, key_type const& k2 ) const
166             {
167                 return key_compare()( k1.key, k2 ) == 0;
168             }
169         };
170
171
172         struct hash: public key_hash
173         {
174             size_t operator()( key_val const& v ) const
175             {
176                 return key_hash::operator()( v.key );
177             }
178             size_t operator()( key_type const& key ) const
179             {
180                 return key_hash::operator()( key );
181             }
182             template <typename Q>
183             size_t operator()( Q const& k ) const
184             {
185                 return key_hash::operator()( k );
186             }
187         };
188
189         struct hash2: public hash
190         {
191             size_t operator()( key_val const& k ) const
192             {
193                 size_t seed = ~hash::operator ()( k );
194                 boost::hash_combine( seed, k.key );
195                 return seed;
196             }
197             size_t operator()( key_type const& k ) const
198             {
199                 size_t seed = ~hash::operator ()( k );
200                 boost::hash_combine( seed, k );
201                 return seed;
202             }
203             template <typename Q>
204             size_t operator()( Q const& k ) const
205             {
206                 return key_hash::operator()( k );
207             }
208         };
209     };
210
211
212     // *************************************************
213     // print_stat
214     // *************************************************
215
216     template <typename Set>
217     static inline void print_stat( Set const& /*s*/ )
218     {}
219
220
221     //*******************************************************
222     // additional_check
223     //*******************************************************
224
225     template <typename Set>
226     static inline void additional_check( Set& /*set*/ )
227     {}
228
229     template <typename Set>
230     static inline void additional_cleanup( Set& /*set*/ )
231     {}
232
233     //*******************************************************
234     // check_before_clear
235     //*******************************************************
236
237     template <typename Set>
238     static inline void check_before_clear( Set& /*s*/ )
239     {}
240
241 }   // namespace set2
242
243 #endif // ifndef CDSUNIT_SET_TYPE_H