File: set_type.h

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (300 lines) | stat: -rw-r--r-- 8,609 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSUNIT_SET_TYPE_H
#define CDSUNIT_SET_TYPE_H

#include <cds/urcu/general_instant.h>
#include <cds/urcu/general_buffered.h>
#include <cds/urcu/general_threaded.h>
#include <cds/urcu/signal_buffered.h>

#include <cds/opt/hash.h>
#include <cds/sync/spinlock.h>

#include <cds_test/stress_test.h>

namespace set {
    namespace cc = cds::container;
    namespace co = cds::opt;

    typedef cds::urcu::gc< cds::urcu::general_instant_stripped >   rcu_gpi;
    typedef cds::urcu::gc< cds::urcu::general_buffered_stripped >  rcu_gpb;
    typedef cds::urcu::gc< cds::urcu::general_threaded_stripped >  rcu_gpt;
#ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
    typedef cds::urcu::gc< cds::urcu::signal_buffered_stripped >  rcu_shb;
#endif

    template <typename Key>
    struct less;

    template <typename Key>
    struct cmp
    {
        int operator ()(Key const& k1, Key const& k2) const
        {
            if ( less<Key>( k1, k2 ))
                return -1;
            return less<Key>( k2, k1 ) ? 1 : 0;
        }
    };

    template <typename Key>
    struct hash;

#define CDSUNIT_INT_COMPARE(t)  template <> struct cmp<t> { int operator()( t k1, t k2 ){ return (int)(k1 - k2); } }
    CDSUNIT_INT_COMPARE(char);
    CDSUNIT_INT_COMPARE(unsigned char);
    CDSUNIT_INT_COMPARE(int);
    CDSUNIT_INT_COMPARE(unsigned int);
    CDSUNIT_INT_COMPARE(long);
    CDSUNIT_INT_COMPARE(unsigned long);
    CDSUNIT_INT_COMPARE(long long);
    CDSUNIT_INT_COMPARE(unsigned long long);
#undef CDSUNIT_INT_COMPARE

#define CDSUNIT_INT_LESS(t)  template <> struct less<t> { bool operator()( t k1, t k2 ){ return k1 < k2; } }
    CDSUNIT_INT_LESS( char );
    CDSUNIT_INT_LESS( unsigned char );
    CDSUNIT_INT_LESS( int );
    CDSUNIT_INT_LESS( unsigned int );
    CDSUNIT_INT_LESS( long );
    CDSUNIT_INT_LESS( unsigned long );
    CDSUNIT_INT_LESS( long long );
    CDSUNIT_INT_LESS( unsigned long long );
#undef CDSUNIT_INT_LESS

    template <>
    struct cmp<std::string>
    {
        int operator()(std::string const& s1, std::string const& s2)
        {
            return s1.compare( s2 );
        }
        int operator()(std::string const& s1, char const * s2)
        {
            return s1.compare( s2 );
        }
        int operator()(char const * s1, std::string const& s2)
        {
            return -s2.compare( s1 );
        }
    };

    template <>
    struct less<std::string>
    {
        bool operator ()( std::string const& k1, std::string const& k2 ) const
        {
            return cmp<std::string>()( k1, k2 ) < 0;
        }
        bool operator ()( std::string const& k1, char const* k2 ) const
        {
            return cmp<std::string>()( k1, k2 ) < 0;
        }
        bool operator ()( char const* k1, std::string const& k2 ) const
        {
            return cmp<std::string>()( k1, k2 ) < 0;
        }
    };

    template <typename T>
    struct hash
    {
        typedef size_t result_type;
        typedef T      argument_type;

        size_t operator()( T const& k ) const
        {
            return std::hash<size_t>()(k.nKey);
        }

        size_t operator()( size_t k ) const
        {
            return std::hash<size_t>()(k);
        }
    };

    template <>
    struct hash<size_t>
    {
        typedef size_t result_type;
        typedef size_t argument_type;

        size_t operator()( size_t k ) const
        {
            return std::hash<size_t>()(k);
        }
    };

    template <>
    struct hash<std::string>
    {
        typedef size_t result_type;
        typedef std::string argument_type;

        size_t operator()( std::string const& k ) const
        {
            return std::hash<std::string>()(k);
        }
    };

    // forward
    template <typename ImplSelector, typename Key, typename Value>
    struct set_type;

    template <typename Key, typename Value>
    struct set_type_base
    {
        typedef Key     key_type;
        typedef Value   value_type;

        struct key_val {
            key_type    key;
            value_type  val;

            explicit key_val( key_type const& k ): key(k), val() {}
            key_val( key_type const& k, value_type const& v ): key(k), val(v) {}

            template <typename K>
            explicit key_val( K const& k ): key(k) {}

            template <typename K, typename T>
            key_val( K const& k, T const& v ): key(k), val(v) {}
        };

        typedef set::hash<key_type>   key_hash;
        typedef set::less<key_type>   key_less;
        typedef set::cmp<key_type>    key_compare;

        struct less {
            bool operator()( key_val const& k1, key_val const& k2 ) const
            {
                return key_less()( k1.key, k2.key );
            }
            bool operator()( key_type const& k1, key_val const& k2 ) const
            {
                return key_less()( k1, k2.key );
            }
            bool operator()( key_val const& k1, key_type const& k2 ) const
            {
                return key_less()( k1.key, k2 );
            }
        };

        struct compare {
            int operator()( key_val const& k1, key_val const& k2 ) const
            {
                return key_compare()( k1.key, k2.key );
            }
            int operator()( key_type const& k1, key_val const& k2 ) const
            {
                return key_compare()( k1, k2.key );
            }
            int operator()( key_val const& k1, key_type const& k2 ) const
            {
                return key_compare()( k1.key, k2 );
            }
        };

        struct equal_to {
            bool operator()( key_val const& k1, key_val const& k2 ) const
            {
                return key_compare()( k1.key, k2.key ) == 0;
            }
            bool operator()( key_type const& k1, key_val const& k2 ) const
            {
                return key_compare()( k1, k2.key ) == 0;
            }
            bool operator()( key_val const& k1, key_type const& k2 ) const
            {
                return key_compare()( k1.key, k2 ) == 0;
            }
        };


        struct hash: public key_hash
        {
            size_t operator()( key_val const& v ) const
            {
                return key_hash::operator()( v.key );
            }
            size_t operator()( key_type const& key ) const
            {
                return key_hash::operator()( key );
            }
            template <typename Q>
            size_t operator()( Q const& k ) const
            {
                return key_hash::operator()( k );
            }
        };

        struct hash2: public hash
        {
            size_t operator()( key_val const& k ) const
            {
                size_t h = hash::operator ()( k.key );
                size_t seed = ~h;
                seed ^= h + 0x9e3779b9 + (seed << 6) + (seed >> 2);
                return seed;
            }
            size_t operator()( key_type const& k ) const
            {
                size_t h = hash::operator ()( k );
                size_t seed = ~h;
                seed ^= h + 0x9e3779b9 + (seed << 6) + (seed >> 2);
                return seed;
            }
            template <typename Q>
            size_t operator()( Q const& k ) const
            {
                return key_hash::operator()( k );
            }
        };
    };


    // *************************************************
    // print_stat
    // *************************************************

    struct empty_stat {};
    static inline cds_test::property_stream& operator <<( cds_test::property_stream& o, empty_stat const& )
    {
        return o;
    }

    template <typename Set>
    static inline void print_stat( cds_test::property_stream& o, Set const& s )
    {
        o << s.statistics();
    }


    //*******************************************************
    // additional_check
    //*******************************************************

    template <typename Set>
    static inline void additional_check( Set& /*set*/ )
    {}

    template <typename Set>
    static inline void additional_cleanup( Set& /*set*/ )
    {}

    //*******************************************************
    // check_before_clear
    //*******************************************************

    template <typename Set>
    static inline void check_before_clear( Set& /*s*/ )
    {}

} // namespace set


#endif // ifndef CDSUNIT_SET_TYPE_H