aboutsummaryrefslogtreecommitdiff
path: root/paludis/hashed_containers.hh
blob: 78f2a90071fff7d3eec1f86be99cd3140d46fc82 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_HASHED_CONTAINERS_HH
#define PALUDIS_GUARD_PALUDIS_HASHED_CONTAINERS_HH 1

/** \file
 * Declarations for various hashed container things.
 *
 * These are more complicated than they should be for two reasons:
 *
 * - There is no standard hashed container in C++. There is one in TR1, but not
 *   all compilers support TR1 yet. Most pre-TR1 standard library
 *   implementations do include hashed containers, but with various different
 *   class names and in different header files. For performance reasons, we need
 *   hashed containers if at all possible.
 *
 * - The current C++ standard doesn't have template typedefs.
 *
 * \ingroup g_data_structures
 *
 * \section Examples
 *
 * - None at this time.
 */

#include <paludis/util/validated.hh>
#include <paludis/util/tr1_type_traits.hh>
#include <paludis/name-fwd.hh>
#include <paludis/version_spec-fwd.hh>
#include <paludis/package_id-fwd.hh>

#ifdef PALUDIS_HASH_IS_STD_TR1_UNORDERED
#  include <tr1/unordered_set>
#  include <tr1/unordered_map>
#elif defined(PALUDIS_HASH_IS_GNU_CXX_HASH)
#  include <ext/hash_set>
#  include <ext/hash_map>
#elif defined(PALUDIS_HASH_IS_STD_HASH)
#  include <hash_set>
#  include <hash_map>
#else
#  include <set>
#  include <map>
#endif

#include <limits>
#include <string>
#include <functional>

namespace paludis
{
    /**
     * Hash function base template.
     *
     * \ingroup g_data_structures
     */
    template <typename T_>
    struct CRCHash;

    /**
     * Make a hashed map of some kind from Key_ to Value_.
     *
     * \ingroup g_data_structures
     */
    template <typename Key_, typename Value_>
    struct MakeHashedMap
    {
#ifdef PALUDIS_HASH_IS_STD_TR1_UNORDERED
        /// Our map type.
        typedef std::tr1::unordered_map<Key_, Value_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_GNU_CXX_HASH)
        /// Our map type.
        typedef __gnu_cxx::hash_map<Key_, Value_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_STD_HASH)
        /// Our map type.
        typedef std::hash_map<Key_, Value_, CRCHash<Key_> > Type;

#else
        /// Our map type.
        typedef std::map<Key_, Value_> Type;
#endif
    };

    /**
     * Make a hashed map of some kind from Key_ to Value_.
     *
     * \ingroup g_data_structures
     */
    template <typename Key_, typename Value_>
    struct MakeHashedMultiMap
    {
#ifdef PALUDIS_HASH_IS_STD_TR1_UNORDERED
        /// Our map type.
        typedef std::tr1::unordered_multimap<Key_, Value_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_GNU_CXX_HASH)
        /// Our map type.
        typedef __gnu_cxx::hash_multimap<Key_, Value_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_STD_HASH)
        /// Our map type.
        typedef std::hash_multimap<Key_, Value_, CRCHash<Key_> > Type;

#else
        /// Our map type.
        typedef std::multimap<Key_, Value_> Type;
#endif
    };

    /**
     * Make a hashed set of some kind of Key_.
     *
     * \ingroup g_data_structures
     */
    template <typename Key_>
    struct MakeHashedSet
    {
#ifdef PALUDIS_HASH_IS_STD_TR1_UNORDERED
        /// Our set type.
        typedef std::tr1::unordered_set<Key_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_GNU_CXX_HASH)
        /// Our set type.
        typedef __gnu_cxx::hash_set<Key_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_STD_HASH)
        /// Our set type.
        typedef std::hash_set<Key_, CRCHash<Key_> > Type;

#else
        /// Our set type.
        typedef std::set<Key_> Type;
#endif
    };

    /**
     * Make a hashed set of some kind of Key_.
     *
     * \ingroup g_data_structures
     */
    template <typename Key_>
    struct MakeHashedMultiSet
    {
#ifdef PALUDIS_HASH_IS_STD_TR1_UNORDERED
        /// Our set type.
        typedef std::tr1::unordered_multiset<Key_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_GNU_CXX_HASH)
        /// Our set type.
        typedef __gnu_cxx::hash_multiset<Key_, CRCHash<Key_> > Type;

#elif defined(PALUDIS_HASH_IS_STD_HASH)
        /// Our set type.
        typedef std::hash_multiset<Key_, CRCHash<Key_> > Type;

#else
        /// Our set type.
        typedef std::multiset<Key_> Type;
#endif
    };

#if defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED) || defined(PALUDIS_HASH_IS_GNU_CXX_HASH) || defined(PALUDIS_HASH_IS_STD_HASH)
    namespace hashed_containers_internals
    {
        /**
         * Base definitions for our CRCHash.
         *
         * \ingroup g_data_structures
         */
        struct CRCHashBase
        {
            /// Shift value.
            static const std::size_t h_shift = std::numeric_limits<std::size_t>::digits - 5;

            /// Mask value.
            static const std::size_t h_mask = static_cast<std::size_t>(0x1f) << h_shift;
        };
    }

    /**
     * Hash, for QualifiedPackageName.
     *
     * \ingroup g_data_structures
     */
    template <>
    class PALUDIS_VISIBLE CRCHash<QualifiedPackageName> :
        public std::unary_function<const QualifiedPackageName, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const QualifiedPackageName & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const QualifiedPackageName & i1, const QualifiedPackageName & i2) const
            {
                return i1 < i2;
            }
#endif
    };

    /**
     * Hash, for PackageID.
     *
     * \ingroup g_data_structures
     */
    template <>
    class PALUDIS_VISIBLE CRCHash<PackageID> :
        public std::unary_function<const PackageID, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const PackageID & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const PackageID & i1, const PackageID & i2) const;
#endif
    };

    /**
     * Hash, for a validated string type.
     *
     * \ingroup g_data_structures
     */
    template <typename Validated_, bool b_>
    class CRCHash<Validated<std::string, Validated_, b_> > :
        public std::unary_function<const Validated<std::string, Validated_, b_>, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const Validated<std::string, Validated_, b_> & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const Validated<std::string, Validated_, b_> i1,
                    const Validated<std::string, Validated_> & i2, b_) const
            {
                return i1.data() < i2.data();
            }
#endif
    };


    /**
     * Hash, for a string.
     *
     * \ingroup g_data_structures
     */
    template<>
    class PALUDIS_VISIBLE CRCHash<std::string> :
        public std::unary_function<const std::string, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const std::string & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const std::string & i1, const std::string & i2) const
            {
                return i1 < i2;
            }
#endif
    };

    /**
     * Hash, for a QualifiedPackageName + VersionSpec pair.
     *
     * \ingroup g_data_structures
     */
    template <>
    class PALUDIS_VISIBLE CRCHash<std::pair<QualifiedPackageName, VersionSpec> > :
        public std::unary_function<const std::pair<const QualifiedPackageName, VersionSpec>, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const std::pair<QualifiedPackageName, VersionSpec> & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const std::pair<QualifiedPackageName, VersionSpec> & i1,
                    const std::pair<QualifiedPackageName, VersionSpec> & i2) const
            {
                return i1 < i2;
            }
#endif
    };

    /**
     * Hash, for a dev_t + ino_t pair.
     *
     * \ingroup g_data_structures
     */
    template <>
    class PALUDIS_VISIBLE CRCHash<std::pair<dev_t, ino_t> > :
        public std::unary_function<const std::pair<dev_t, ino_t>, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function
            std::size_t operator() (const std::pair<dev_t, ino_t> & val) const;

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const std::pair<dev_t, ino_t> & i1, const std::pair<dev_t, ino_t> & i2) const
            {
                return i1 < i2;
            }
#endif
    };

    /**
     * Hash, for a shared pointer.
     *
     * \ingroup g_data_structures
     */
    template <typename T_>
    class PALUDIS_VISIBLE CRCHash<tr1::shared_ptr<T_> > :
        public std::unary_function<tr1::shared_ptr<T_>, std::size_t>,
        protected hashed_containers_internals::CRCHashBase
    {
        public:
            /// Hash function.
            std::size_t operator() (const tr1::shared_ptr<T_> & val) const
            {
                return CRCHash<typename tr1::remove_const<T_>::type>()(*val);
            }

#if (! defined(PALUDIS_HASH_IS_STD_TR1_UNORDERED)) && (! defined(PALUDIS_HASH_IS_GNU_CXX_HASH))
            enum
            {
                min_buckets = 32,
                bucket_size = 4
            };

            bool operator() (const tr1::shared_ptr<T_> & i1, const tr1::shared_ptr<T_> & i2) const
            {
                return CRCHash<typename tr1::remove_const<T_>::type>()(*i1, *i2);
            }
#endif
    };

    template <typename Validated_, bool b_>
    std::size_t
    CRCHash<Validated<std::string, Validated_, b_> >::operator() (const Validated<std::string, Validated_, b_> & val) const
    {
        const std::string & s1(val.data());
        std::size_t h(0);

        for (std::string::size_type t(0) ; t < s1.length() ; ++t)
        {
            std::size_t hh(h & h_mask);
            h <<= 5;
            h ^= (hh >> h_shift);
            h ^= s1[t];
        }

        return h;
    }

#endif

}

#endif