summaryrefslogtreecommitdiffstats
blob: 1c497c328a934959823c27146194dc38a7d712d7 (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

// Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
// Copyright (C) 2005-2009 Daniel James
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED
#define BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED

#include <boost/unordered/detail/table.hpp>
#include <boost/unordered/detail/extract_key.hpp>

namespace boost { namespace unordered_detail {

    template <class T>
    class hash_equivalent_table : public T::table
    {
    public:
        typedef BOOST_DEDUCED_TYPENAME T::hasher hasher;
        typedef BOOST_DEDUCED_TYPENAME T::key_equal key_equal;
        typedef BOOST_DEDUCED_TYPENAME T::value_allocator value_allocator;
        typedef BOOST_DEDUCED_TYPENAME T::key_type key_type;
        typedef BOOST_DEDUCED_TYPENAME T::value_type value_type;
        typedef BOOST_DEDUCED_TYPENAME T::table table;
        typedef BOOST_DEDUCED_TYPENAME T::node_constructor node_constructor;

        typedef BOOST_DEDUCED_TYPENAME T::node node;
        typedef BOOST_DEDUCED_TYPENAME T::node_ptr node_ptr;
        typedef BOOST_DEDUCED_TYPENAME T::bucket_ptr bucket_ptr;
        typedef BOOST_DEDUCED_TYPENAME T::iterator_base iterator_base;
        typedef BOOST_DEDUCED_TYPENAME T::extractor extractor;

        // Constructors

        hash_equivalent_table(std::size_t n,
            hasher const& hf, key_equal const& eq, value_allocator const& a)
          : table(n, hf, eq, a) {}
        hash_equivalent_table(hash_equivalent_table const& x)
          : table(x, x.node_alloc()) {}
        hash_equivalent_table(hash_equivalent_table const& x,
            value_allocator const& a)
          : table(x, a) {}
        hash_equivalent_table(hash_equivalent_table& x, move_tag m)
          : table(x, m) {}
        hash_equivalent_table(hash_equivalent_table& x,
            value_allocator const& a, move_tag m)
          : table(x, a, m) {}
        ~hash_equivalent_table() {}

        // Insert methods

        iterator_base emplace_impl(node_constructor& a);
        void emplace_impl_no_rehash(node_constructor& a);

        // equals

        bool equals(hash_equivalent_table const&) const;

        inline node_ptr add_node(node_constructor& a,
            bucket_ptr bucket, node_ptr pos);

#if defined(BOOST_UNORDERED_STD_FORWARD)

        template <class... Args>
        iterator_base emplace(Args&&... args);

#else

#define BOOST_UNORDERED_INSERT_IMPL(z, n, _)                                   \
        template <BOOST_UNORDERED_TEMPLATE_ARGS(z, n)>                         \
        iterator_base emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, n));

        BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT,
            BOOST_UNORDERED_INSERT_IMPL, _)

#undef BOOST_UNORDERED_INSERT_IMPL
#endif

        template <class I>
        void insert_for_range(I i, I j, forward_traversal_tag);
        template <class I>
        void insert_for_range(I i, I j, boost::incrementable_traversal_tag);
        template <class I>
        void insert_range(I i, I j);
    };

    template <class H, class P, class A>
    struct multiset : public types<
        BOOST_DEDUCED_TYPENAME A::value_type,
        BOOST_DEDUCED_TYPENAME A::value_type,
        H, P, A,
        set_extractor<BOOST_DEDUCED_TYPENAME A::value_type>,
        grouped>
    {
        typedef hash_equivalent_table<multiset<H, P, A> > impl;
        typedef hash_table<multiset<H, P, A> > table;
    };

    template <class K, class H, class P, class A>
    struct multimap : public types<
        K, BOOST_DEDUCED_TYPENAME A::value_type,
        H, P, A,
        map_extractor<K, BOOST_DEDUCED_TYPENAME A::value_type>,
        grouped>
    {
        typedef hash_equivalent_table<multimap<K, H, P, A> > impl;
        typedef hash_table<multimap<K, H, P, A> > table;
    };

    ////////////////////////////////////////////////////////////////////////////
    // Equality

    template <class T>
    bool hash_equivalent_table<T>
        ::equals(hash_equivalent_table<T> const& other) const
    {
        if(this->size_ != other.size_) return false;
        if(!this->size_) return true;

        bucket_ptr end = this->get_bucket(this->bucket_count_);
        for(bucket_ptr i = this->cached_begin_bucket_; i != end; ++i)
        {
            node_ptr it1 = i->next_;
            while(BOOST_UNORDERED_BORLAND_BOOL(it1))
            {
                node_ptr it2 = other.find_iterator(this->get_key_from_ptr(it1));
                if(!BOOST_UNORDERED_BORLAND_BOOL(it2)) return false;
                
                node_ptr end1 = node::next_group(it1);
                node_ptr end2 = node::next_group(it2);

                do {
                    if(!extractor::compare_mapped(
                        node::get_value(it1), node::get_value(it2)))
                        return false;
                    it1 = it1->next_;
                    it2 = it2->next_;
                } while(it1 != end1 && it2 != end2);
                if(it1 != end1 || it2 != end2) return false;
            }
        }

        return true;
    }

    ////////////////////////////////////////////////////////////////////////////
    // A convenience method for adding nodes.

    template <class T>
    inline BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::node_ptr
        hash_equivalent_table<T>
            ::add_node(node_constructor& a, bucket_ptr bucket, node_ptr pos)
    {
        node_ptr n = a.release();
        if(BOOST_UNORDERED_BORLAND_BOOL(pos)) {
            node::add_after_node(n, pos);                
        }
        else {
            node::add_to_bucket(n, *bucket);
            if(bucket < this->cached_begin_bucket_)
                this->cached_begin_bucket_ = bucket;
        }
        ++this->size_;
        return n;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Insert methods

    template <class T>
    inline BOOST_DEDUCED_TYPENAME
        hash_equivalent_table<T>::iterator_base
        hash_equivalent_table<T>::emplace_impl(node_constructor& a)
    {
        key_type const& k = this->get_key(a.value());
        std::size_t hash_value = this->hash_function()(k);
        
        if(!this->size_) {
            return this->emplace_empty_impl_with_node(a, 1);
        }
        else {
            bucket_ptr bucket = this->bucket_ptr_from_hash(hash_value);
            node_ptr position = this->find_iterator(bucket, k);

            // reserve has basic exception safety if the hash function
            // throws, strong otherwise.
            if(this->reserve_for_insert(this->size_ + 1))
                bucket = this->bucket_ptr_from_hash(hash_value);

            return iterator_base(bucket, add_node(a, bucket, position));
        }
    }
    
    template <class T>
    inline void hash_equivalent_table<T>
            ::emplace_impl_no_rehash(node_constructor& a)
    {
        key_type const& k = this->get_key(a.value());
        bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
        add_node(a, bucket, this->find_iterator(bucket, k));
    }

#if defined(BOOST_UNORDERED_STD_FORWARD)

    // Emplace (equivalent key containers)
    // (I'm using an overloaded emplace for both 'insert' and 'emplace')

    // if hash function throws, basic exception safety
    // strong otherwise
    template <class T>
    template <class... Args>
    BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::iterator_base
        hash_equivalent_table<T>
            ::emplace(Args&&... args)
    {
        // Create the node before rehashing in case it throws an
        // exception (need strong safety in such a case).
        node_constructor a(*this);
        a.construct(std::forward<Args>(args)...);

        return emplace_impl(a);
    }

#else

#define BOOST_UNORDERED_INSERT_IMPL(z, num_params, _)                       \
    template <class T>                                                      \
    template <BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params)>                 \
    BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::iterator_base          \
        hash_equivalent_table<T>                                            \
            ::emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params))       \
    {                                                                       \
        node_constructor a(*this);                                          \
        a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params));            \
        return emplace_impl(a);                                             \
    }

    BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT,
        BOOST_UNORDERED_INSERT_IMPL, _)

#undef BOOST_UNORDERED_INSERT_IMPL
#endif

    ////////////////////////////////////////////////////////////////////////////
    // Insert range methods

    // if hash function throws, or inserting > 1 element, basic exception safety
    // strong otherwise
    template <class T>
    template <class I>
    inline void hash_equivalent_table<T>
        ::insert_for_range(I i, I j, forward_traversal_tag)
    {
        if(i == j) return;
        std::size_t distance = unordered_detail::distance(i, j);
        if(distance == 1) {
            emplace(*i);
        }
        else {
            node_constructor a(*this);

            // Only require basic exception safety here
            if(this->size_) {
                this->reserve_for_insert(this->size_ + distance);
            }
            else {
                a.construct(*i++);
                this->emplace_empty_impl_with_node(a, distance);
            }

            for (; i != j; ++i) {
                a.construct(*i);
                emplace_impl_no_rehash(a);
            }
        }
    }

    // if hash function throws, or inserting > 1 element, basic exception safety
    // strong otherwise
    template <class T>
    template <class I>
    inline void hash_equivalent_table<T>
        ::insert_for_range(I i, I j, boost::incrementable_traversal_tag)
    {
        node_constructor a(*this);
        for (; i != j; ++i) {
            a.construct(*i);
            emplace_impl(a);
        }
    }

    // if hash function throws, or inserting > 1 element, basic exception safety
    // strong otherwise
    template <class T>
    template <class I>
    void hash_equivalent_table<T>::insert_range(I i, I j)
    {
        BOOST_DEDUCED_TYPENAME boost::iterator_traversal<I>::type
            iterator_traversal_tag;
        insert_for_range(i, j, iterator_traversal_tag);
    }
}}

#endif