1 | |
---|
2 | // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard. |
---|
3 | // Copyright (C) 2005-2009 Daniel James |
---|
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #ifndef BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED |
---|
8 | #define BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED |
---|
9 | |
---|
10 | #include <boost/unordered/detail/table.hpp> |
---|
11 | #include <boost/unordered/detail/extract_key.hpp> |
---|
12 | |
---|
13 | namespace boost { namespace unordered_detail { |
---|
14 | |
---|
15 | template <class T> |
---|
16 | class hash_equivalent_table : public T::table |
---|
17 | { |
---|
18 | public: |
---|
19 | typedef BOOST_DEDUCED_TYPENAME T::hasher hasher; |
---|
20 | typedef BOOST_DEDUCED_TYPENAME T::key_equal key_equal; |
---|
21 | typedef BOOST_DEDUCED_TYPENAME T::value_allocator value_allocator; |
---|
22 | typedef BOOST_DEDUCED_TYPENAME T::key_type key_type; |
---|
23 | typedef BOOST_DEDUCED_TYPENAME T::value_type value_type; |
---|
24 | typedef BOOST_DEDUCED_TYPENAME T::table table; |
---|
25 | typedef BOOST_DEDUCED_TYPENAME T::node_constructor node_constructor; |
---|
26 | |
---|
27 | typedef BOOST_DEDUCED_TYPENAME T::node node; |
---|
28 | typedef BOOST_DEDUCED_TYPENAME T::node_ptr node_ptr; |
---|
29 | typedef BOOST_DEDUCED_TYPENAME T::bucket_ptr bucket_ptr; |
---|
30 | typedef BOOST_DEDUCED_TYPENAME T::iterator_base iterator_base; |
---|
31 | typedef BOOST_DEDUCED_TYPENAME T::extractor extractor; |
---|
32 | |
---|
33 | // Constructors |
---|
34 | |
---|
35 | hash_equivalent_table(std::size_t n, |
---|
36 | hasher const& hf, key_equal const& eq, value_allocator const& a) |
---|
37 | : table(n, hf, eq, a) {} |
---|
38 | hash_equivalent_table(hash_equivalent_table const& x) |
---|
39 | : table(x, x.node_alloc()) {} |
---|
40 | hash_equivalent_table(hash_equivalent_table const& x, |
---|
41 | value_allocator const& a) |
---|
42 | : table(x, a) {} |
---|
43 | hash_equivalent_table(hash_equivalent_table& x, move_tag m) |
---|
44 | : table(x, m) {} |
---|
45 | hash_equivalent_table(hash_equivalent_table& x, |
---|
46 | value_allocator const& a, move_tag m) |
---|
47 | : table(x, a, m) {} |
---|
48 | ~hash_equivalent_table() {} |
---|
49 | |
---|
50 | // Insert methods |
---|
51 | |
---|
52 | iterator_base emplace_impl(node_constructor& a); |
---|
53 | void emplace_impl_no_rehash(node_constructor& a); |
---|
54 | |
---|
55 | // equals |
---|
56 | |
---|
57 | bool equals(hash_equivalent_table const&) const; |
---|
58 | |
---|
59 | inline node_ptr add_node(node_constructor& a, |
---|
60 | bucket_ptr bucket, node_ptr pos); |
---|
61 | |
---|
62 | #if defined(BOOST_UNORDERED_STD_FORWARD) |
---|
63 | |
---|
64 | template <class... Args> |
---|
65 | iterator_base emplace(Args&&... args); |
---|
66 | |
---|
67 | #else |
---|
68 | |
---|
69 | #define BOOST_UNORDERED_INSERT_IMPL(z, n, _) \ |
---|
70 | template <BOOST_UNORDERED_TEMPLATE_ARGS(z, n)> \ |
---|
71 | iterator_base emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, n)); |
---|
72 | |
---|
73 | BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT, |
---|
74 | BOOST_UNORDERED_INSERT_IMPL, _) |
---|
75 | |
---|
76 | #undef BOOST_UNORDERED_INSERT_IMPL |
---|
77 | #endif |
---|
78 | |
---|
79 | template <class I> |
---|
80 | void insert_for_range(I i, I j, forward_traversal_tag); |
---|
81 | template <class I> |
---|
82 | void insert_for_range(I i, I j, boost::incrementable_traversal_tag); |
---|
83 | template <class I> |
---|
84 | void insert_range(I i, I j); |
---|
85 | }; |
---|
86 | |
---|
87 | template <class H, class P, class A> |
---|
88 | struct multiset : public types< |
---|
89 | BOOST_DEDUCED_TYPENAME A::value_type, |
---|
90 | BOOST_DEDUCED_TYPENAME A::value_type, |
---|
91 | H, P, A, |
---|
92 | set_extractor<BOOST_DEDUCED_TYPENAME A::value_type>, |
---|
93 | grouped> |
---|
94 | { |
---|
95 | typedef hash_equivalent_table<multiset<H, P, A> > impl; |
---|
96 | typedef hash_table<multiset<H, P, A> > table; |
---|
97 | }; |
---|
98 | |
---|
99 | template <class K, class H, class P, class A> |
---|
100 | struct multimap : public types< |
---|
101 | K, BOOST_DEDUCED_TYPENAME A::value_type, |
---|
102 | H, P, A, |
---|
103 | map_extractor<K, BOOST_DEDUCED_TYPENAME A::value_type>, |
---|
104 | grouped> |
---|
105 | { |
---|
106 | typedef hash_equivalent_table<multimap<K, H, P, A> > impl; |
---|
107 | typedef hash_table<multimap<K, H, P, A> > table; |
---|
108 | }; |
---|
109 | |
---|
110 | //////////////////////////////////////////////////////////////////////////// |
---|
111 | // Equality |
---|
112 | |
---|
113 | template <class T> |
---|
114 | bool hash_equivalent_table<T> |
---|
115 | ::equals(hash_equivalent_table<T> const& other) const |
---|
116 | { |
---|
117 | if(this->size_ != other.size_) return false; |
---|
118 | if(!this->size_) return true; |
---|
119 | |
---|
120 | bucket_ptr end = this->get_bucket(this->bucket_count_); |
---|
121 | for(bucket_ptr i = this->cached_begin_bucket_; i != end; ++i) |
---|
122 | { |
---|
123 | node_ptr it1 = i->next_; |
---|
124 | while(BOOST_UNORDERED_BORLAND_BOOL(it1)) |
---|
125 | { |
---|
126 | node_ptr it2 = other.find_iterator(this->get_key_from_ptr(it1)); |
---|
127 | if(!BOOST_UNORDERED_BORLAND_BOOL(it2)) return false; |
---|
128 | |
---|
129 | node_ptr end1 = node::next_group(it1); |
---|
130 | node_ptr end2 = node::next_group(it2); |
---|
131 | |
---|
132 | do { |
---|
133 | if(!extractor::compare_mapped( |
---|
134 | node::get_value(it1), node::get_value(it2))) |
---|
135 | return false; |
---|
136 | it1 = it1->next_; |
---|
137 | it2 = it2->next_; |
---|
138 | } while(it1 != end1 && it2 != end2); |
---|
139 | if(it1 != end1 || it2 != end2) return false; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | return true; |
---|
144 | } |
---|
145 | |
---|
146 | //////////////////////////////////////////////////////////////////////////// |
---|
147 | // A convenience method for adding nodes. |
---|
148 | |
---|
149 | template <class T> |
---|
150 | inline BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::node_ptr |
---|
151 | hash_equivalent_table<T> |
---|
152 | ::add_node(node_constructor& a, bucket_ptr bucket, node_ptr pos) |
---|
153 | { |
---|
154 | node_ptr n = a.release(); |
---|
155 | if(BOOST_UNORDERED_BORLAND_BOOL(pos)) { |
---|
156 | node::add_after_node(n, pos); |
---|
157 | } |
---|
158 | else { |
---|
159 | node::add_to_bucket(n, *bucket); |
---|
160 | if(bucket < this->cached_begin_bucket_) |
---|
161 | this->cached_begin_bucket_ = bucket; |
---|
162 | } |
---|
163 | ++this->size_; |
---|
164 | return n; |
---|
165 | } |
---|
166 | |
---|
167 | //////////////////////////////////////////////////////////////////////////// |
---|
168 | // Insert methods |
---|
169 | |
---|
170 | template <class T> |
---|
171 | inline BOOST_DEDUCED_TYPENAME |
---|
172 | hash_equivalent_table<T>::iterator_base |
---|
173 | hash_equivalent_table<T>::emplace_impl(node_constructor& a) |
---|
174 | { |
---|
175 | key_type const& k = this->get_key(a.value()); |
---|
176 | std::size_t hash_value = this->hash_function()(k); |
---|
177 | |
---|
178 | if(!this->size_) { |
---|
179 | return this->emplace_empty_impl_with_node(a, 1); |
---|
180 | } |
---|
181 | else { |
---|
182 | bucket_ptr bucket = this->bucket_ptr_from_hash(hash_value); |
---|
183 | node_ptr position = this->find_iterator(bucket, k); |
---|
184 | |
---|
185 | // reserve has basic exception safety if the hash function |
---|
186 | // throws, strong otherwise. |
---|
187 | if(this->reserve_for_insert(this->size_ + 1)) |
---|
188 | bucket = this->bucket_ptr_from_hash(hash_value); |
---|
189 | |
---|
190 | return iterator_base(bucket, add_node(a, bucket, position)); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | template <class T> |
---|
195 | inline void hash_equivalent_table<T> |
---|
196 | ::emplace_impl_no_rehash(node_constructor& a) |
---|
197 | { |
---|
198 | key_type const& k = this->get_key(a.value()); |
---|
199 | bucket_ptr bucket = this->get_bucket(this->bucket_index(k)); |
---|
200 | add_node(a, bucket, this->find_iterator(bucket, k)); |
---|
201 | } |
---|
202 | |
---|
203 | #if defined(BOOST_UNORDERED_STD_FORWARD) |
---|
204 | |
---|
205 | // Emplace (equivalent key containers) |
---|
206 | // (I'm using an overloaded emplace for both 'insert' and 'emplace') |
---|
207 | |
---|
208 | // if hash function throws, basic exception safety |
---|
209 | // strong otherwise |
---|
210 | template <class T> |
---|
211 | template <class... Args> |
---|
212 | BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::iterator_base |
---|
213 | hash_equivalent_table<T> |
---|
214 | ::emplace(Args&&... args) |
---|
215 | { |
---|
216 | // Create the node before rehashing in case it throws an |
---|
217 | // exception (need strong safety in such a case). |
---|
218 | node_constructor a(*this); |
---|
219 | a.construct(std::forward<Args>(args)...); |
---|
220 | |
---|
221 | return emplace_impl(a); |
---|
222 | } |
---|
223 | |
---|
224 | #else |
---|
225 | |
---|
226 | #define BOOST_UNORDERED_INSERT_IMPL(z, num_params, _) \ |
---|
227 | template <class T> \ |
---|
228 | template <BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params)> \ |
---|
229 | BOOST_DEDUCED_TYPENAME hash_equivalent_table<T>::iterator_base \ |
---|
230 | hash_equivalent_table<T> \ |
---|
231 | ::emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ |
---|
232 | { \ |
---|
233 | node_constructor a(*this); \ |
---|
234 | a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ |
---|
235 | return emplace_impl(a); \ |
---|
236 | } |
---|
237 | |
---|
238 | BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT, |
---|
239 | BOOST_UNORDERED_INSERT_IMPL, _) |
---|
240 | |
---|
241 | #undef BOOST_UNORDERED_INSERT_IMPL |
---|
242 | #endif |
---|
243 | |
---|
244 | //////////////////////////////////////////////////////////////////////////// |
---|
245 | // Insert range methods |
---|
246 | |
---|
247 | // if hash function throws, or inserting > 1 element, basic exception safety |
---|
248 | // strong otherwise |
---|
249 | template <class T> |
---|
250 | template <class I> |
---|
251 | inline void hash_equivalent_table<T> |
---|
252 | ::insert_for_range(I i, I j, forward_traversal_tag) |
---|
253 | { |
---|
254 | if(i == j) return; |
---|
255 | std::size_t distance = unordered_detail::distance(i, j); |
---|
256 | if(distance == 1) { |
---|
257 | emplace(*i); |
---|
258 | } |
---|
259 | else { |
---|
260 | node_constructor a(*this); |
---|
261 | |
---|
262 | // Only require basic exception safety here |
---|
263 | if(this->size_) { |
---|
264 | this->reserve_for_insert(this->size_ + distance); |
---|
265 | } |
---|
266 | else { |
---|
267 | a.construct(*i++); |
---|
268 | this->emplace_empty_impl_with_node(a, distance); |
---|
269 | } |
---|
270 | |
---|
271 | for (; i != j; ++i) { |
---|
272 | a.construct(*i); |
---|
273 | emplace_impl_no_rehash(a); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | // if hash function throws, or inserting > 1 element, basic exception safety |
---|
279 | // strong otherwise |
---|
280 | template <class T> |
---|
281 | template <class I> |
---|
282 | inline void hash_equivalent_table<T> |
---|
283 | ::insert_for_range(I i, I j, boost::incrementable_traversal_tag) |
---|
284 | { |
---|
285 | node_constructor a(*this); |
---|
286 | for (; i != j; ++i) { |
---|
287 | a.construct(*i); |
---|
288 | emplace_impl(a); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | // if hash function throws, or inserting > 1 element, basic exception safety |
---|
293 | // strong otherwise |
---|
294 | template <class T> |
---|
295 | template <class I> |
---|
296 | void hash_equivalent_table<T>::insert_range(I i, I j) |
---|
297 | { |
---|
298 | BOOST_DEDUCED_TYPENAME boost::iterator_traversal<I>::type |
---|
299 | iterator_traversal_tag; |
---|
300 | insert_for_range(i, j, iterator_traversal_tag); |
---|
301 | } |
---|
302 | }} |
---|
303 | |
---|
304 | #endif |
---|