]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/intrusive/hashtable.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / intrusive / hashtable.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2015
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
13 #define BOOST_INTRUSIVE_HASHTABLE_HPP
14
15 #include <boost/intrusive/detail/config_begin.hpp>
16 #include <boost/intrusive/intrusive_fwd.hpp>
17
18 //General intrusive utilities
19 #include <boost/intrusive/detail/hashtable_node.hpp>
20 #include <boost/intrusive/detail/transform_iterator.hpp>
21 #include <boost/intrusive/link_mode.hpp>
22 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
23 #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
24 #include <boost/intrusive/detail/node_to_value.hpp>
25 #include <boost/intrusive/detail/exception_disposer.hpp>
26 #include <boost/intrusive/detail/node_cloner_disposer.hpp>
27 #include <boost/intrusive/detail/simple_disposers.hpp>
28 #include <boost/intrusive/detail/size_holder.hpp>
29 #include <boost/intrusive/detail/iterator.hpp>
30
31 //Implementation utilities
32 #include <boost/intrusive/unordered_set_hook.hpp>
33 #include <boost/intrusive/slist.hpp>
34 #include <boost/intrusive/pointer_traits.hpp>
35 #include <boost/intrusive/detail/mpl.hpp>
36
37 //boost
38 #include <boost/functional/hash.hpp>
39 #include <boost/intrusive/detail/assert.hpp>
40 #include <boost/static_assert.hpp>
41 #include <boost/move/utility_core.hpp>
42 #include <boost/move/adl_move_swap.hpp>
43
44 //std C++
45 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::equal_to
46 #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
47 #include <algorithm> //std::lower_bound, std::upper_bound
48 #include <cstddef> //std::size_t
49
50 #if defined(BOOST_HAS_PRAGMA_ONCE)
51 # pragma once
52 #endif
53
54 namespace boost {
55 namespace intrusive {
56
57 /// @cond
58
59 template<class InputIt, class T>
60 InputIt priv_algo_find(InputIt first, InputIt last, const T& value)
61 {
62 for (; first != last; ++first) {
63 if (*first == value) {
64 return first;
65 }
66 }
67 return last;
68 }
69
70 template<class InputIt, class T>
71 typename boost::intrusive::iterator_traits<InputIt>::difference_type
72 priv_algo_count(InputIt first, InputIt last, const T& value)
73 {
74 typename boost::intrusive::iterator_traits<InputIt>::difference_type ret = 0;
75 for (; first != last; ++first) {
76 if (*first == value) {
77 ret++;
78 }
79 }
80 return ret;
81 }
82
83 template <class ForwardIterator1, class ForwardIterator2>
84 bool priv_algo_is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
85 {
86 typedef typename
87 boost::intrusive::iterator_traits<ForwardIterator2>::difference_type
88 distance_type;
89 //Efficiently compare identical prefixes: O(N) if sequences
90 //have the same elements in the same order.
91 for ( ; first1 != last1; ++first1, ++first2){
92 if (! (*first1 == *first2))
93 break;
94 }
95 if (first1 == last1){
96 return true;
97 }
98
99 //Establish last2 assuming equal ranges by iterating over the
100 //rest of the list.
101 ForwardIterator2 last2 = first2;
102 boost::intrusive::iterator_advance(last2, boost::intrusive::iterator_distance(first1, last1));
103 for(ForwardIterator1 scan = first1; scan != last1; ++scan){
104 if (scan != (priv_algo_find)(first1, scan, *scan)){
105 continue; //We've seen this one before.
106 }
107 distance_type matches = (priv_algo_count)(first2, last2, *scan);
108 if (0 == matches || (priv_algo_count)(scan, last1, *scan != matches)){
109 return false;
110 }
111 }
112 return true;
113 }
114
115 template<int Dummy = 0>
116 struct prime_list_holder
117 {
118 private:
119
120 template <class SizeType> // sizeof(SizeType) < sizeof(std::size_t)
121 static BOOST_INTRUSIVE_FORCEINLINE SizeType truncate_size_type(std::size_t n, detail::true_)
122 {
123 return n < std::size_t(SizeType(-1)) ? static_cast<SizeType>(n) : SizeType(-1);
124 }
125
126 template <class SizeType> // sizeof(SizeType) == sizeof(std::size_t)
127 static BOOST_INTRUSIVE_FORCEINLINE SizeType truncate_size_type(std::size_t n, detail::false_)
128 {
129 return static_cast<SizeType>(n);
130 }
131
132 template <class SizeType> //sizeof(SizeType) > sizeof(std::size_t)
133 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_upper_bucket_count_dispatch(SizeType n, detail::true_)
134 {
135 std::size_t const c = n > std::size_t(-1)
136 ? std::size_t(-1)
137 : suggested_upper_bucket_count_impl(static_cast<std::size_t>(n));
138 return static_cast<SizeType>(c);
139 }
140
141 template <class SizeType> //sizeof(SizeType) > sizeof(std::size_t)
142 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_lower_bucket_count_dispatch(SizeType n, detail::true_)
143 {
144 std::size_t const c = n > std::size_t(-1)
145 ? std::size_t(-1)
146 : suggested_lower_bucket_count_impl(static_cast<std::size_t>(n));
147 return static_cast<SizeType>(c);
148 }
149
150 template <class SizeType>
151 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_upper_bucket_count_dispatch(SizeType n, detail::false_)
152 {
153 std::size_t const c = suggested_upper_bucket_count_impl(static_cast<std::size_t>(n));
154 return truncate_size_type<SizeType>(c, detail::bool_<(sizeof(SizeType) < sizeof(std::size_t))>());
155
156 }
157
158 template <class SizeType>
159 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_lower_bucket_count_dispatch(SizeType n, detail::false_)
160 {
161 std::size_t const c = suggested_lower_bucket_count_impl(static_cast<std::size_t>(n));
162 return truncate_size_type<SizeType>(c, detail::bool_<(sizeof(SizeType) < sizeof(std::size_t))>());
163 }
164
165 static const std::size_t prime_list[];
166 static const std::size_t prime_list_size;
167
168 static std::size_t suggested_lower_bucket_count_impl(std::size_t n)
169 {
170 const std::size_t *primes = &prime_list_holder<0>::prime_list[0];
171 const std::size_t *primes_end = primes + prime_list_holder<0>::prime_list_size;
172 std::size_t const* bound = std::lower_bound(primes, primes_end, n);
173 //Tables have upper SIZE_MAX, so we must always found an entry
174 BOOST_INTRUSIVE_INVARIANT_ASSERT(bound != primes_end);
175 bound -= std::size_t(bound != primes);
176 return *bound;
177 }
178
179 static std::size_t suggested_upper_bucket_count_impl(std::size_t n)
180 {
181 const std::size_t *primes = &prime_list_holder<0>::prime_list[0];
182 const std::size_t *primes_end = primes + prime_list_holder<0>::prime_list_size;
183 std::size_t const* bound = std::upper_bound(primes, primes_end, n);
184 bound -= std::size_t(bound == primes_end);
185 return *bound;
186 }
187
188 public:
189
190 template <class SizeType>
191 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_upper_bucket_count(SizeType n)
192 {
193 return (suggested_upper_bucket_count_dispatch)(n, detail::bool_<(sizeof(SizeType) > sizeof(std::size_t))>());
194 }
195
196 template <class SizeType>
197 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_lower_bucket_count(SizeType n)
198 {
199 return (suggested_lower_bucket_count_dispatch)(n, detail::bool_<(sizeof(SizeType) > sizeof(std::size_t))>());
200 }
201 };
202
203 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
204
205 //We only support LLP64(Win64) or LP64(most Unix) data models
206 #ifdef _WIN64 //In 64 bit windows sizeof(size_t) == sizeof(unsigned long long)
207 #define BOOST_INTRUSIVE_PRIME_C(NUMBER) NUMBER##ULL
208 #define BOOST_INTRUSIVE_64_BIT_SIZE_T 1
209 #else //In 32 bit windows and 32/64 bit unixes sizeof(size_t) == sizeof(unsigned long)
210 #define BOOST_INTRUSIVE_PRIME_C(NUMBER) NUMBER##UL
211 #define BOOST_INTRUSIVE_64_BIT_SIZE_T (((((ULONG_MAX>>16)>>16)>>16)>>15) != 0)
212 #endif
213
214 template<int Dummy>
215 const std::size_t prime_list_holder<Dummy>::prime_list[] = {
216 BOOST_INTRUSIVE_PRIME_C(3), BOOST_INTRUSIVE_PRIME_C(7),
217 BOOST_INTRUSIVE_PRIME_C(11), BOOST_INTRUSIVE_PRIME_C(17),
218 BOOST_INTRUSIVE_PRIME_C(29), BOOST_INTRUSIVE_PRIME_C(53),
219 BOOST_INTRUSIVE_PRIME_C(97), BOOST_INTRUSIVE_PRIME_C(193),
220 BOOST_INTRUSIVE_PRIME_C(389), BOOST_INTRUSIVE_PRIME_C(769),
221 BOOST_INTRUSIVE_PRIME_C(1543), BOOST_INTRUSIVE_PRIME_C(3079),
222 BOOST_INTRUSIVE_PRIME_C(6151), BOOST_INTRUSIVE_PRIME_C(12289),
223 BOOST_INTRUSIVE_PRIME_C(24593), BOOST_INTRUSIVE_PRIME_C(49157),
224 BOOST_INTRUSIVE_PRIME_C(98317), BOOST_INTRUSIVE_PRIME_C(196613),
225 BOOST_INTRUSIVE_PRIME_C(393241), BOOST_INTRUSIVE_PRIME_C(786433),
226 BOOST_INTRUSIVE_PRIME_C(1572869), BOOST_INTRUSIVE_PRIME_C(3145739),
227 BOOST_INTRUSIVE_PRIME_C(6291469), BOOST_INTRUSIVE_PRIME_C(12582917),
228 BOOST_INTRUSIVE_PRIME_C(25165843), BOOST_INTRUSIVE_PRIME_C(50331653),
229 BOOST_INTRUSIVE_PRIME_C(100663319), BOOST_INTRUSIVE_PRIME_C(201326611),
230 BOOST_INTRUSIVE_PRIME_C(402653189), BOOST_INTRUSIVE_PRIME_C(805306457),
231 BOOST_INTRUSIVE_PRIME_C(1610612741), BOOST_INTRUSIVE_PRIME_C(3221225473),
232 #if BOOST_INTRUSIVE_64_BIT_SIZE_T
233 //Taken from Boost.MultiIndex code, thanks to Joaquin M Lopez Munoz.
234 BOOST_INTRUSIVE_PRIME_C(6442450939), BOOST_INTRUSIVE_PRIME_C(12884901893),
235 BOOST_INTRUSIVE_PRIME_C(25769803751), BOOST_INTRUSIVE_PRIME_C(51539607551),
236 BOOST_INTRUSIVE_PRIME_C(103079215111), BOOST_INTRUSIVE_PRIME_C(206158430209),
237 BOOST_INTRUSIVE_PRIME_C(412316860441), BOOST_INTRUSIVE_PRIME_C(824633720831),
238 BOOST_INTRUSIVE_PRIME_C(1649267441651), BOOST_INTRUSIVE_PRIME_C(3298534883309),
239 BOOST_INTRUSIVE_PRIME_C(6597069766657), BOOST_INTRUSIVE_PRIME_C(13194139533299),
240 BOOST_INTRUSIVE_PRIME_C(26388279066623), BOOST_INTRUSIVE_PRIME_C(52776558133303),
241 BOOST_INTRUSIVE_PRIME_C(105553116266489), BOOST_INTRUSIVE_PRIME_C(211106232532969),
242 BOOST_INTRUSIVE_PRIME_C(422212465066001), BOOST_INTRUSIVE_PRIME_C(844424930131963),
243 BOOST_INTRUSIVE_PRIME_C(1688849860263953), BOOST_INTRUSIVE_PRIME_C(3377699720527861),
244 BOOST_INTRUSIVE_PRIME_C(6755399441055731), BOOST_INTRUSIVE_PRIME_C(13510798882111483),
245 BOOST_INTRUSIVE_PRIME_C(27021597764222939), BOOST_INTRUSIVE_PRIME_C(54043195528445957),
246 BOOST_INTRUSIVE_PRIME_C(108086391056891903), BOOST_INTRUSIVE_PRIME_C(216172782113783843),
247 BOOST_INTRUSIVE_PRIME_C(432345564227567621), BOOST_INTRUSIVE_PRIME_C(864691128455135207),
248 BOOST_INTRUSIVE_PRIME_C(1729382256910270481), BOOST_INTRUSIVE_PRIME_C(3458764513820540933),
249 BOOST_INTRUSIVE_PRIME_C(6917529027641081903), BOOST_INTRUSIVE_PRIME_C(13835058055282163729),
250 BOOST_INTRUSIVE_PRIME_C(18446744073709551557), BOOST_INTRUSIVE_PRIME_C(18446744073709551615) //Upper limit, just in case
251 #else
252 BOOST_INTRUSIVE_PRIME_C(4294967291), BOOST_INTRUSIVE_PRIME_C(4294967295) //Upper limit, just in case
253 #endif
254 };
255
256 #undef BOOST_INTRUSIVE_PRIME_C
257 #undef BOOST_INTRUSIVE_64_BIT_SIZE_T
258
259 #endif //#if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
260
261 template<int Dummy>
262 const std::size_t prime_list_holder<Dummy>::prime_list_size
263 = sizeof(prime_list)/sizeof(std::size_t);
264
265 struct hash_bool_flags
266 {
267 static const std::size_t unique_keys_pos = 1u;
268 static const std::size_t constant_time_size_pos = 2u;
269 static const std::size_t power_2_buckets_pos = 4u;
270 static const std::size_t cache_begin_pos = 8u;
271 static const std::size_t compare_hash_pos = 16u;
272 static const std::size_t incremental_pos = 32u;
273 };
274
275 namespace detail {
276
277 template<class SupposedValueTraits>
278 struct get_slist_impl_from_supposed_value_traits
279 {
280 typedef SupposedValueTraits value_traits;
281 typedef typename detail::get_node_traits
282 <value_traits>::type node_traits;
283 typedef typename get_slist_impl
284 <typename reduced_slist_node_traits
285 <node_traits>::type
286 >::type type;
287 };
288
289 template<class SupposedValueTraits>
290 struct unordered_bucket_impl
291 {
292 typedef typename
293 get_slist_impl_from_supposed_value_traits
294 <SupposedValueTraits>::type slist_impl;
295 typedef bucket_impl<slist_impl> implementation_defined;
296 typedef implementation_defined type;
297 };
298
299 template<class SupposedValueTraits>
300 struct unordered_bucket_ptr_impl
301 {
302 typedef typename detail::get_node_traits
303 <SupposedValueTraits>::type::node_ptr node_ptr;
304 typedef typename unordered_bucket_impl
305 <SupposedValueTraits>::type bucket_type;
306
307 typedef typename pointer_traits
308 <node_ptr>::template rebind_pointer
309 < bucket_type >::type implementation_defined;
310 typedef implementation_defined type;
311 };
312
313 template <class T>
314 struct store_hash_is_true
315 {
316 template<bool Add>
317 struct two_or_three {yes_type _[2 + Add];};
318 template <class U> static yes_type test(...);
319 template <class U> static two_or_three<U::store_hash> test (int);
320 static const bool value = sizeof(test<T>(0)) > sizeof(yes_type)*2;
321 };
322
323 template <class T>
324 struct optimize_multikey_is_true
325 {
326 template<bool Add>
327 struct two_or_three {yes_type _[2 + Add];};
328 template <class U> static yes_type test(...);
329 template <class U> static two_or_three<U::optimize_multikey> test (int);
330 static const bool value = sizeof(test<T>(0)) > sizeof(yes_type)*2;
331 };
332
333 struct insert_commit_data_impl
334 {
335 std::size_t hash;
336 };
337
338 template<class Node, class SlistNodePtr>
339 BOOST_INTRUSIVE_FORCEINLINE typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type
340 dcast_bucket_ptr(const SlistNodePtr &p)
341 {
342 typedef typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type node_ptr;
343 return pointer_traits<node_ptr>::pointer_to(static_cast<Node&>(*p));
344 }
345
346 template<class NodeTraits>
347 struct group_functions
348 {
349 // A group is reverse-linked
350 //
351 // A is "first in group"
352 // C is "last in group"
353 // __________________
354 // | _____ _____ |
355 // | | | | | | <- Group links
356 // ^ V ^ V ^ V
357 // _ _ _ _
358 // A|_| B|_| C|_| D|_|
359 //
360 // ^ | ^ | ^ | ^ V <- Bucket links
361 // _ _____| |_____| |______| |____| |
362 // |B| |
363 // ^________________________________|
364 //
365
366 typedef NodeTraits node_traits;
367 typedef unordered_group_adapter<node_traits> group_traits;
368 typedef typename node_traits::node_ptr node_ptr;
369 typedef typename node_traits::node node;
370 typedef typename reduced_slist_node_traits
371 <node_traits>::type reduced_node_traits;
372 typedef typename reduced_node_traits::node_ptr slist_node_ptr;
373 typedef typename reduced_node_traits::node slist_node;
374 typedef circular_slist_algorithms<group_traits> group_algorithms;
375 typedef circular_slist_algorithms<node_traits> node_algorithms;
376
377 static slist_node_ptr get_bucket_before_begin
378 (slist_node_ptr bucket_beg, slist_node_ptr bucket_end, node_ptr p)
379 {
380 //First find the last node of p's group.
381 //This requires checking the first node of the next group or
382 //the bucket node.
383 node_ptr prev_node = p;
384 node_ptr nxt(node_traits::get_next(p));
385 while(!(bucket_beg <= nxt && nxt <= bucket_end) &&
386 (group_traits::get_next(nxt) == prev_node)){
387 prev_node = nxt;
388 nxt = node_traits::get_next(nxt);
389 }
390
391 //If we've reached the bucket node just return it.
392 if(bucket_beg <= nxt && nxt <= bucket_end){
393 return nxt;
394 }
395
396 //Otherwise, iterate using group links until the bucket node
397 node_ptr first_node_of_group = nxt;
398 node_ptr last_node_group = group_traits::get_next(first_node_of_group);
399 slist_node_ptr possible_end = node_traits::get_next(last_node_group);
400
401 while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){
402 first_node_of_group = detail::dcast_bucket_ptr<node>(possible_end);
403 last_node_group = group_traits::get_next(first_node_of_group);
404 possible_end = node_traits::get_next(last_node_group);
405 }
406 return possible_end;
407 }
408
409 static node_ptr get_prev_to_first_in_group(slist_node_ptr bucket_node, node_ptr first_in_group)
410 {
411 node_ptr nb = detail::dcast_bucket_ptr<node>(bucket_node);
412 node_ptr n;
413 while((n = node_traits::get_next(nb)) != first_in_group){
414 nb = group_traits::get_next(n); //go to last in group
415 }
416 return nb;
417 }
418
419 static void erase_from_group(slist_node_ptr end_ptr, node_ptr to_erase_ptr, detail::true_)
420 {
421 node_ptr const nxt_ptr(node_traits::get_next(to_erase_ptr));
422 //Check if the next node is in the group (not end node) and reverse linked to
423 //'to_erase_ptr'. Erase if that's the case.
424 if(nxt_ptr != end_ptr && to_erase_ptr == group_traits::get_next(nxt_ptr)){
425 group_algorithms::unlink_after(nxt_ptr);
426 }
427 }
428
429 BOOST_INTRUSIVE_FORCEINLINE static void erase_from_group(const slist_node_ptr&, const node_ptr&, detail::false_)
430 {}
431
432 BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_last_in_group(node_ptr first_in_group, detail::true_)
433 { return group_traits::get_next(first_in_group); }
434
435 BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_last_in_group(node_ptr n, detail::false_)
436 { return n; }
437
438 static node_ptr get_first_in_group(node_ptr n, detail::true_)
439 {
440 node_ptr ng;
441 while(n == node_traits::get_next((ng = group_traits::get_next(n)))){
442 n = ng;
443 }
444 return n;
445 }
446
447 BOOST_INTRUSIVE_FORCEINLINE static node_ptr next_group_if_first_in_group(node_ptr ptr)
448 {
449 return node_traits::get_next(group_traits::get_next(ptr));
450 }
451
452 BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_first_in_group(node_ptr n, detail::false_)
453 { return n; }
454
455 BOOST_INTRUSIVE_FORCEINLINE static void insert_in_group(node_ptr first_in_group, node_ptr n, true_)
456 { group_algorithms::link_after(first_in_group, n); }
457
458 static void insert_in_group(const node_ptr&, const node_ptr&, false_)
459 {}
460
461 BOOST_INTRUSIVE_FORCEINLINE static node_ptr split_group(node_ptr const new_first_in_group)
462 {
463 node_ptr const first((get_first_in_group)(new_first_in_group, detail::true_()));
464 if(first != new_first_in_group){
465 node_ptr const last = group_traits::get_next(first);
466 group_traits::set_next(first, group_traits::get_next(new_first_in_group));
467 group_traits::set_next(new_first_in_group, last);
468 }
469 return first;
470 }
471 };
472
473 template<class BucketType, class SplitTraits>
474 class incremental_rehash_rollback
475 {
476 private:
477 typedef BucketType bucket_type;
478 typedef SplitTraits split_traits;
479
480 incremental_rehash_rollback();
481 incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
482 incremental_rehash_rollback (const incremental_rehash_rollback &);
483
484 public:
485 incremental_rehash_rollback
486 (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_traits)
487 : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
488 , split_traits_(split_traits), released_(false)
489 {}
490
491 BOOST_INTRUSIVE_FORCEINLINE void release()
492 { released_ = true; }
493
494 ~incremental_rehash_rollback()
495 {
496 if(!released_){
497 //If an exception is thrown, just put all moved nodes back in the old bucket
498 //and move back the split mark.
499 destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_);
500 split_traits_.decrement();
501 }
502 }
503
504 private:
505 bucket_type &source_bucket_;
506 bucket_type &destiny_bucket_;
507 split_traits &split_traits_;
508 bool released_;
509 };
510
511 template<class NodeTraits>
512 struct node_functions
513 {
514 BOOST_INTRUSIVE_FORCEINLINE static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_)
515 { return NodeTraits::set_hash(p, h); }
516
517 BOOST_INTRUSIVE_FORCEINLINE static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_)
518 {}
519 };
520
521 BOOST_INTRUSIVE_FORCEINLINE std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
522 { return hash_value % bucket_cnt; }
523
524 BOOST_INTRUSIVE_FORCEINLINE std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
525 { return hash_value & (bucket_cnt - 1); }
526
527 template<bool Power2Buckets, bool Incremental>
528 BOOST_INTRUSIVE_FORCEINLINE std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split)
529 {
530 std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
531 if(Incremental)
532 bucket_number -= static_cast<std::size_t>(bucket_number >= split)*(bucket_cnt/2);
533 return bucket_number;
534 }
535
536 } //namespace detail {
537
538 //!This metafunction will obtain the type of a bucket
539 //!from the value_traits or hook option to be used with
540 //!a hash container.
541 template<class ValueTraitsOrHookOption>
542 struct unordered_bucket
543 : public detail::unordered_bucket_impl
544 <typename ValueTraitsOrHookOption::
545 template pack<empty>::proto_value_traits
546 >
547 {};
548
549 //!This metafunction will obtain the type of a bucket pointer
550 //!from the value_traits or hook option to be used with
551 //!a hash container.
552 template<class ValueTraitsOrHookOption>
553 struct unordered_bucket_ptr
554 : public detail::unordered_bucket_ptr_impl
555 <typename ValueTraitsOrHookOption::
556 template pack<empty>::proto_value_traits
557 >
558 {};
559
560 //!This metafunction will obtain the type of the default bucket traits
561 //!(when the user does not specify the bucket_traits<> option) from the
562 //!value_traits or hook option to be used with
563 //!a hash container.
564 template<class ValueTraitsOrHookOption>
565 struct unordered_default_bucket_traits
566 {
567 typedef typename ValueTraitsOrHookOption::
568 template pack<empty>::proto_value_traits supposed_value_traits;
569 typedef typename detail::
570 get_slist_impl_from_supposed_value_traits
571 <supposed_value_traits>::type slist_impl;
572 typedef bucket_traits_impl
573 <slist_impl> implementation_defined;
574 typedef implementation_defined type;
575 };
576
577 struct default_bucket_traits;
578
579 //hashtable default hook traits
580 struct default_hashtable_hook_applier
581 { template <class T> struct apply{ typedef typename T::default_hashtable_hook type; }; };
582
583 template<>
584 struct is_default_hook_tag<default_hashtable_hook_applier>
585 { static const bool value = true; };
586
587 struct hashtable_defaults
588 {
589 typedef default_hashtable_hook_applier proto_value_traits;
590 typedef std::size_t size_type;
591 typedef void key_of_value;
592 typedef void equal;
593 typedef void hash;
594 typedef default_bucket_traits bucket_traits;
595 static const bool constant_time_size = true;
596 static const bool power_2_buckets = false;
597 static const bool cache_begin = false;
598 static const bool compare_hash = false;
599 static const bool incremental = false;
600 };
601
602 template<class ValueTraits, bool IsConst>
603 struct downcast_node_to_value_t
604 : public detail::node_to_value<ValueTraits, IsConst>
605 {
606 typedef detail::node_to_value<ValueTraits, IsConst> base_t;
607 typedef typename base_t::result_type result_type;
608 typedef ValueTraits value_traits;
609 typedef typename get_slist_impl
610 <typename reduced_slist_node_traits
611 <typename value_traits::node_traits>::type
612 >::type slist_impl;
613 typedef typename detail::add_const_if_c
614 <typename slist_impl::node, IsConst>::type & first_argument_type;
615 typedef typename detail::add_const_if_c
616 < typename ValueTraits::node_traits::node
617 , IsConst>::type & intermediate_argument_type;
618 typedef typename pointer_traits
619 <typename ValueTraits::pointer>::
620 template rebind_pointer
621 <const ValueTraits>::type const_value_traits_ptr;
622
623 BOOST_INTRUSIVE_FORCEINLINE downcast_node_to_value_t(const const_value_traits_ptr &ptr)
624 : base_t(ptr)
625 {}
626
627 BOOST_INTRUSIVE_FORCEINLINE result_type operator()(first_argument_type arg) const
628 { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
629 };
630
631 template<class F, class SlistNodePtr, class NodePtr>
632 struct node_cast_adaptor
633 //Use public inheritance to avoid MSVC bugs with closures
634 : public detail::ebo_functor_holder<F>
635 {
636 typedef detail::ebo_functor_holder<F> base_t;
637
638 typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
639 typedef typename pointer_traits<NodePtr>::element_type node;
640
641 template<class ConvertibleToF, class RealValuTraits>
642 BOOST_INTRUSIVE_FORCEINLINE node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
643 : base_t(base_t(c2f, traits))
644 {}
645
646 BOOST_INTRUSIVE_FORCEINLINE typename base_t::node_ptr operator()(const slist_node &to_clone)
647 { return base_t::operator()(static_cast<const node &>(to_clone)); }
648
649 BOOST_INTRUSIVE_FORCEINLINE void operator()(SlistNodePtr to_clone)
650 {
651 base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
652 }
653 };
654
655 //bucket_plus_vtraits stores ValueTraits + BucketTraits
656 //this data is needed by iterators to obtain the
657 //value from the iterator and detect the bucket
658 template<class ValueTraits, class BucketTraits>
659 struct bucket_plus_vtraits
660 {
661 typedef BucketTraits bucket_traits;
662 typedef ValueTraits value_traits;
663
664 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
665
666 typedef typename
667 detail::get_slist_impl_from_supposed_value_traits
668 <value_traits>::type slist_impl;
669 typedef typename value_traits::node_traits node_traits;
670 typedef unordered_group_adapter<node_traits> group_traits;
671 typedef typename slist_impl::iterator siterator;
672 typedef bucket_impl<slist_impl> bucket_type;
673 typedef detail::group_functions<node_traits> group_functions_t;
674 typedef typename slist_impl::node_algorithms node_algorithms;
675 typedef typename slist_impl::node_ptr slist_node_ptr;
676 typedef typename node_traits::node_ptr node_ptr;
677 typedef typename node_traits::node node;
678 typedef typename value_traits::value_type value_type;
679 typedef typename value_traits::pointer pointer;
680 typedef typename value_traits::const_pointer const_pointer;
681 typedef typename pointer_traits<pointer>::reference reference;
682 typedef typename pointer_traits
683 <const_pointer>::reference const_reference;
684 typedef circular_slist_algorithms<group_traits> group_algorithms;
685 typedef typename pointer_traits
686 <typename value_traits::pointer>::
687 template rebind_pointer
688 <const value_traits>::type const_value_traits_ptr;
689 typedef typename pointer_traits
690 <typename value_traits::pointer>::
691 template rebind_pointer
692 <const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
693 typedef typename detail::unordered_bucket_ptr_impl
694 <value_traits>::type bucket_ptr;
695
696 template<class BucketTraitsType>
697 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
698 : data(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
699 {}
700
701 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x)
702 { data.bucket_traits_ = x.data.bucket_traits_; return *this; }
703
704 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr priv_value_traits_ptr() const
705 { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
706
707 //bucket_value_traits
708 //
709 BOOST_INTRUSIVE_FORCEINLINE const bucket_plus_vtraits &get_bucket_value_traits() const
710 { return *this; }
711
712 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits &get_bucket_value_traits()
713 { return *this; }
714
715 BOOST_INTRUSIVE_FORCEINLINE const_bucket_value_traits_ptr bucket_value_traits_ptr() const
716 { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
717
718 //value traits
719 //
720 BOOST_INTRUSIVE_FORCEINLINE const value_traits &priv_value_traits() const
721 { return this->data; }
722
723 BOOST_INTRUSIVE_FORCEINLINE value_traits &priv_value_traits()
724 { return this->data; }
725
726 //bucket_traits
727 //
728 BOOST_INTRUSIVE_FORCEINLINE const bucket_traits &priv_bucket_traits() const
729 { return this->data.bucket_traits_; }
730
731 BOOST_INTRUSIVE_FORCEINLINE bucket_traits &priv_bucket_traits()
732 { return this->data.bucket_traits_; }
733
734 //bucket operations
735 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_bucket_pointer() const
736 { return this->priv_bucket_traits().bucket_begin(); }
737
738 std::size_t priv_bucket_count() const
739 { return this->priv_bucket_traits().bucket_count(); }
740
741 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_invalid_bucket() const
742 {
743 const bucket_traits &rbt = this->priv_bucket_traits();
744 return rbt.bucket_begin() + rbt.bucket_count();
745 }
746
747 BOOST_INTRUSIVE_FORCEINLINE siterator priv_invalid_local_it() const
748 { return this->priv_bucket_traits().bucket_begin()->before_begin(); }
749
750 template<class NodeDisposer>
751 static std::size_t priv_erase_from_single_bucket(bucket_type &b, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::true_) //optimize multikey
752 {
753 std::size_t n = 0;
754 siterator const sfirst(++siterator(sbefore_first));
755 if(sfirst != slast){
756 node_ptr const nf = detail::dcast_bucket_ptr<node>(sfirst.pointed_node());
757 node_ptr const nl = detail::dcast_bucket_ptr<node>(slast.pointed_node());
758 node_ptr const ne = detail::dcast_bucket_ptr<node>(b.end().pointed_node());
759
760 if(group_functions_t::next_group_if_first_in_group(nf) != nf) {
761 // The node is at the beginning of a group.
762 if(nl != ne){
763 group_functions_t::split_group(nl);
764 }
765 }
766 else {
767 node_ptr const group1 = group_functions_t::split_group(nf);
768 if(nl != ne) {
769 node_ptr const group2 = group_functions_t::split_group(ne);
770 if(nf == group2) { //Both first and last in the same group
771 //so join group1 and group2
772 node_ptr const end1 = group_traits::get_next(group1);
773 node_ptr const end2 = group_traits::get_next(group2);
774 group_traits::set_next(group1, end2);
775 group_traits::set_next(group2, end1);
776 }
777 }
778 }
779
780 siterator it(++siterator(sbefore_first));
781 while(it != slast){
782 node_disposer((it++).pointed_node());
783 ++n;
784 }
785 b.erase_after(sbefore_first, slast);
786 }
787 return n;
788 }
789
790 template<class NodeDisposer>
791 static std::size_t priv_erase_from_single_bucket(bucket_type &b, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::false_) //optimize multikey
792 {
793 std::size_t n = 0;
794 siterator it(++siterator(sbefore_first));
795 while(it != slast){
796 node_disposer((it++).pointed_node());
797 ++n;
798 }
799 b.erase_after(sbefore_first, slast);
800 return n;
801 }
802
803 template<class NodeDisposer>
804 static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::true_) //optimize multikey
805 {
806 node_ptr const ne(detail::dcast_bucket_ptr<node>(b.end().pointed_node()));
807 node_ptr n(detail::dcast_bucket_ptr<node>(i.pointed_node()));
808 node_ptr pos = node_traits::get_next(group_traits::get_next(n));
809 node_ptr bn;
810 node_ptr nn(node_traits::get_next(n));
811
812 if(pos != n) {
813 //Node is the first of the group
814 bn = group_functions_t::get_prev_to_first_in_group(ne, n);
815
816 //Unlink the rest of the group if it's not the last node of its group
817 if(nn != ne && group_traits::get_next(nn) == n){
818 group_algorithms::unlink_after(nn);
819 }
820 }
821 else if(nn != ne && group_traits::get_next(nn) == n){
822 //Node is not the end of the group
823 bn = group_traits::get_next(n);
824 group_algorithms::unlink_after(nn);
825 }
826 else{
827 //Node is the end of the group
828 bn = group_traits::get_next(n);
829 node_ptr const x(group_algorithms::get_previous_node(n));
830 group_algorithms::unlink_after(x);
831 }
832 b.erase_after_and_dispose(bucket_type::s_iterator_to(*bn), node_disposer);
833 }
834
835 template<class NodeDisposer>
836 BOOST_INTRUSIVE_FORCEINLINE static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::false_) //optimize multikey
837 { b.erase_after_and_dispose(b.previous(i), node_disposer); }
838
839 template<class NodeDisposer, bool OptimizeMultikey>
840 std::size_t priv_erase_node_range( siterator const &before_first_it, std::size_t const first_bucket
841 , siterator const &last_it, std::size_t const last_bucket
842 , NodeDisposer node_disposer, detail::bool_<OptimizeMultikey> optimize_multikey_tag)
843 {
844 std::size_t num_erased(0);
845 siterator last_step_before_it;
846 if(first_bucket != last_bucket){
847 bucket_type *b = (&this->priv_bucket_pointer()[0]);
848 num_erased += this->priv_erase_from_single_bucket
849 (b[first_bucket], before_first_it, b[first_bucket].end(), node_disposer, optimize_multikey_tag);
850 for(std::size_t i = 0, n = (last_bucket - first_bucket - 1); i != n; ++i){
851 num_erased += this->priv_erase_whole_bucket(b[first_bucket+i+1], node_disposer);
852 }
853 last_step_before_it = b[last_bucket].before_begin();
854 }
855 else{
856 last_step_before_it = before_first_it;
857 }
858 num_erased += this->priv_erase_from_single_bucket
859 (this->priv_bucket_pointer()[last_bucket], last_step_before_it, last_it, node_disposer, optimize_multikey_tag);
860 return num_erased;
861 }
862
863 static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
864 {
865 //First find the last node of p's group.
866 //This requires checking the first node of the next group or
867 //the bucket node.
868 slist_node_ptr end_ptr(b.end().pointed_node());
869 node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr<node>(end_ptr)));
870 node_ptr last_node_group(possible_end);
871
872 while(end_ptr != possible_end){
873 last_node_group = group_traits::get_next(detail::dcast_bucket_ptr<node>(possible_end));
874 possible_end = node_traits::get_next(last_node_group);
875 }
876 return bucket_type::s_iterator_to(*last_node_group);
877 }
878
879 template<class NodeDisposer>
880 std::size_t priv_erase_whole_bucket(bucket_type &b, NodeDisposer node_disposer)
881 {
882 std::size_t num_erased = 0;
883 siterator b_begin(b.before_begin());
884 siterator nxt(b_begin);
885 ++nxt;
886 siterator const end_sit(b.end());
887 while(nxt != end_sit){
888 //No need to init group links as we'll delete all bucket nodes
889 nxt = bucket_type::s_erase_after_and_dispose(b_begin, node_disposer);
890 ++num_erased;
891 }
892 return num_erased;
893 }
894
895 BOOST_INTRUSIVE_FORCEINLINE static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
896 { return b.previous(b.end()); }
897
898 static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
899 {
900 node_ptr const elem(detail::dcast_bucket_ptr<node>(i.pointed_node()));
901 node_ptr const prev_in_group(group_traits::get_next(elem));
902 bool const first_in_group = node_traits::get_next(prev_in_group) != elem;
903 typename bucket_type::node &n = first_in_group
904 ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem)
905 : *group_traits::get_next(elem)
906 ;
907 return bucket_type::s_iterator_to(n);
908 }
909
910 BOOST_INTRUSIVE_FORCEINLINE static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
911 { return b.previous(i); }
912
913 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey
914 {
915 const bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
916 slist_node_ptr bb = group_functions_t::get_bucket_before_begin
917 ( f->end().pointed_node()
918 , l->end().pointed_node()
919 , detail::dcast_bucket_ptr<node>(it.pointed_node()));
920 //Now get the bucket_impl from the iterator
921 const bucket_type &b = static_cast<const bucket_type&>
922 (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb)));
923 //Now just calculate the index b has in the bucket array
924 return static_cast<std::size_t>(&b - &*f);
925 }
926
927 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey
928 {
929 bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
930 slist_node_ptr first_ptr(f->cend().pointed_node())
931 , last_ptr(l->cend().pointed_node());
932
933 //The end node is embedded in the singly linked list:
934 //iterate until we reach it.
935 while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){
936 ++it;
937 }
938 //Now get the bucket_impl from the iterator
939 const bucket_type &b = static_cast<const bucket_type&>
940 (bucket_type::container_from_end_iterator(it));
941
942 //Now just calculate the index b has in the bucket array
943 return static_cast<std::size_t>(&b - &*f);
944 }
945
946 BOOST_INTRUSIVE_FORCEINLINE static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash
947 { return node_traits::get_hash(detail::dcast_bucket_ptr<node>(n)); }
948
949 BOOST_INTRUSIVE_FORCEINLINE static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash
950 { return std::size_t(-1); }
951
952 BOOST_INTRUSIVE_FORCEINLINE node &priv_value_to_node(reference v)
953 { return *this->priv_value_traits().to_node_ptr(v); }
954
955 BOOST_INTRUSIVE_FORCEINLINE const node &priv_value_to_node(const_reference v) const
956 { return *this->priv_value_traits().to_node_ptr(v); }
957
958 BOOST_INTRUSIVE_FORCEINLINE reference priv_value_from_slist_node(slist_node_ptr n)
959 { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
960
961 BOOST_INTRUSIVE_FORCEINLINE const_reference priv_value_from_slist_node(slist_node_ptr n) const
962 { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
963
964 void priv_clear_buckets(const bucket_ptr buckets_ptr, const std::size_t bucket_cnt)
965 {
966 bucket_ptr buckets_it = buckets_ptr;
967 for(std::size_t bucket_i = 0; bucket_i != bucket_cnt; ++buckets_it, ++bucket_i){
968 if(safemode_or_autounlink){
969 buckets_it->clear_and_dispose(detail::init_disposer<node_algorithms>());
970 }
971 else{
972 buckets_it->clear();
973 }
974 }
975 }
976
977 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
978 { return node_traits::get_hash(this->priv_value_traits().to_node_ptr(v)); }
979
980 typedef hashtable_iterator<bucket_plus_vtraits, false> iterator;
981 typedef hashtable_iterator<bucket_plus_vtraits, true> const_iterator;
982
983 BOOST_INTRUSIVE_FORCEINLINE iterator end()
984 { return iterator(this->priv_invalid_local_it(), 0); }
985
986 BOOST_INTRUSIVE_FORCEINLINE const_iterator end() const
987 { return this->cend(); }
988
989 BOOST_INTRUSIVE_FORCEINLINE const_iterator cend() const
990 { return const_iterator(this->priv_invalid_local_it(), 0); }
991
992 //Public functions:
993 struct data_type : public ValueTraits
994 {
995 template<class BucketTraitsType>
996 BOOST_INTRUSIVE_FORCEINLINE data_type(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
997 : ValueTraits(val_traits), bucket_traits_(::boost::forward<BucketTraitsType>(b_traits))
998 {}
999
1000 bucket_traits bucket_traits_;
1001 } data;
1002 };
1003
1004 template<class Hash, class>
1005 struct get_hash
1006 {
1007 typedef Hash type;
1008 };
1009
1010 template<class T>
1011 struct get_hash<void, T>
1012 {
1013 typedef ::boost::hash<T> type;
1014 };
1015
1016 template<class EqualTo, class>
1017 struct get_equal_to
1018 {
1019 typedef EqualTo type;
1020 };
1021
1022 template<class T>
1023 struct get_equal_to<void, T>
1024 {
1025 typedef std::equal_to<T> type;
1026 };
1027
1028 template<class KeyOfValue, class T>
1029 struct get_hash_key_of_value
1030 {
1031 typedef KeyOfValue type;
1032 };
1033
1034 template<class T>
1035 struct get_hash_key_of_value<void, T>
1036 {
1037 typedef ::boost::intrusive::detail::identity<T> type;
1038 };
1039
1040 template<class T, class VoidOrKeyOfValue>
1041 struct hash_key_types_base
1042 {
1043 typedef typename get_hash_key_of_value
1044 < VoidOrKeyOfValue, T>::type key_of_value;
1045 typedef typename key_of_value::type key_type;
1046 };
1047
1048 template<class T, class VoidOrKeyOfValue, class VoidOrKeyHash>
1049 struct hash_key_hash
1050 : get_hash
1051 < VoidOrKeyHash
1052 , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
1053 >
1054 {};
1055
1056 template<class T, class VoidOrKeyOfValue, class VoidOrKeyEqual>
1057 struct hash_key_equal
1058 : get_equal_to
1059 < VoidOrKeyEqual
1060 , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
1061 >
1062
1063 {};
1064
1065 //bucket_hash_t
1066 //Stores bucket_plus_vtraits plust the hash function
1067 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class BucketTraits>
1068 struct bucket_hash_t
1069 //Use public inheritance to avoid MSVC bugs with closures
1070 : public detail::ebo_functor_holder
1071 <typename hash_key_hash < typename bucket_plus_vtraits<ValueTraits,BucketTraits>::value_traits::value_type
1072 , VoidOrKeyOfValue
1073 , VoidOrKeyHash
1074 >::type
1075 >
1076 , bucket_plus_vtraits<ValueTraits, BucketTraits> //4
1077 {
1078 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::value_traits value_traits;
1079 typedef typename value_traits::value_type value_type;
1080 typedef typename value_traits::node_traits node_traits;
1081 typedef hash_key_hash
1082 < value_type, VoidOrKeyOfValue, VoidOrKeyHash> hash_key_hash_t;
1083 typedef typename hash_key_hash_t::type hasher;
1084 typedef typename hash_key_types_base<value_type, VoidOrKeyOfValue>::key_of_value key_of_value;
1085
1086 typedef BucketTraits bucket_traits;
1087 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
1088 typedef detail::ebo_functor_holder<hasher> base_t;
1089
1090 template<class BucketTraitsType>
1091 BOOST_INTRUSIVE_FORCEINLINE bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h)
1092 : detail::ebo_functor_holder<hasher>(h), bucket_plus_vtraits_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
1093 {}
1094
1095 BOOST_INTRUSIVE_FORCEINLINE const hasher &priv_hasher() const
1096 { return this->base_t::get(); }
1097
1098 hasher &priv_hasher()
1099 { return this->base_t::get(); }
1100
1101 using bucket_plus_vtraits_t::priv_stored_or_compute_hash; //For store_hash == true
1102
1103 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
1104 { return this->priv_hasher()(key_of_value()(v)); }
1105 };
1106
1107 template<class ValueTraits, class BucketTraits, class VoidOrKeyOfValue, class VoidOrKeyEqual>
1108 struct hashtable_equal_holder
1109 {
1110 typedef detail::ebo_functor_holder
1111 < typename hash_key_equal < typename bucket_plus_vtraits<ValueTraits, BucketTraits>::value_traits::value_type
1112 , VoidOrKeyOfValue
1113 , VoidOrKeyEqual
1114 >::type
1115 > type;
1116 };
1117
1118
1119 //bucket_hash_equal_t
1120 //Stores bucket_hash_t and the equality function when the first
1121 //non-empty bucket shall not be cached.
1122 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, bool>
1123 struct bucket_hash_equal_t
1124 //Use public inheritance to avoid MSVC bugs with closures
1125 : public bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> //3
1126 , public hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type //equal
1127 {
1128 typedef typename hashtable_equal_holder
1129 <ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type equal_holder_t;
1130 typedef bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> bucket_hash_type;
1131 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1132 typedef ValueTraits value_traits;
1133 typedef typename equal_holder_t::functor_type key_equal;
1134 typedef typename bucket_hash_type::hasher hasher;
1135 typedef BucketTraits bucket_traits;
1136 typedef typename bucket_plus_vtraits_t::slist_impl slist_impl;
1137 typedef typename slist_impl::iterator siterator;
1138 typedef bucket_impl<slist_impl> bucket_type;
1139 typedef typename detail::unordered_bucket_ptr_impl<value_traits>::type bucket_ptr;
1140
1141 template<class BucketTraitsType>
1142 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const key_equal &e)
1143 : bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
1144 , equal_holder_t(e)
1145 {}
1146
1147 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_get_cache()
1148 { return this->bucket_hash_type::priv_bucket_pointer(); }
1149
1150 BOOST_INTRUSIVE_FORCEINLINE void priv_set_cache(const bucket_ptr &)
1151 {}
1152
1153 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_get_cache_bucket_num()
1154 { return 0u; }
1155
1156 BOOST_INTRUSIVE_FORCEINLINE void priv_initialize_cache()
1157 {}
1158
1159 BOOST_INTRUSIVE_FORCEINLINE void priv_swap_cache(bucket_hash_equal_t &)
1160 {}
1161
1162 siterator priv_begin() const
1163 {
1164 std::size_t n = 0;
1165 std::size_t bucket_cnt = this->bucket_hash_type::priv_bucket_count();
1166 for (n = 0; n < bucket_cnt; ++n){
1167 bucket_type &b = this->bucket_hash_type::priv_bucket_pointer()[n];
1168 if(!b.empty()){
1169 return b.begin();
1170 }
1171 }
1172 return this->bucket_hash_type::priv_invalid_local_it();
1173 }
1174
1175 BOOST_INTRUSIVE_FORCEINLINE void priv_insertion_update_cache(std::size_t)
1176 {}
1177
1178 BOOST_INTRUSIVE_FORCEINLINE void priv_erasure_update_cache_range(std::size_t, std::size_t)
1179 {}
1180
1181 BOOST_INTRUSIVE_FORCEINLINE void priv_erasure_update_cache()
1182 {}
1183
1184 BOOST_INTRUSIVE_FORCEINLINE const key_equal &priv_equal() const
1185 { return this->equal_holder_t::get(); }
1186
1187 BOOST_INTRUSIVE_FORCEINLINE key_equal &priv_equal()
1188 { return this->equal_holder_t::get(); }
1189 };
1190
1191 //bucket_hash_equal_t
1192 //Stores bucket_hash_t and the equality function when the first
1193 //non-empty bucket shall be cached.
1194 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits> //cache_begin == true version
1195 struct bucket_hash_equal_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, true>
1196 //Use public inheritance to avoid MSVC bugs with closures
1197 : bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> //2
1198 , hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type
1199 {
1200 typedef typename hashtable_equal_holder
1201 <ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type equal_holder_t;
1202
1203 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1204 typedef ValueTraits value_traits;
1205 typedef typename equal_holder_t::functor_type key_equal;
1206 typedef bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> bucket_hash_type;
1207 typedef typename bucket_hash_type::hasher hasher;
1208 typedef BucketTraits bucket_traits;
1209 typedef typename bucket_plus_vtraits_t::slist_impl::iterator siterator;
1210
1211 template<class BucketTraitsType>
1212 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const key_equal &e)
1213 : bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
1214 , equal_holder_t(e)
1215 {}
1216
1217 typedef typename detail::unordered_bucket_ptr_impl
1218 <typename bucket_hash_type::value_traits>::type bucket_ptr;
1219
1220 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr &priv_get_cache()
1221 { return cached_begin_; }
1222
1223 BOOST_INTRUSIVE_FORCEINLINE const bucket_ptr &priv_get_cache() const
1224 { return cached_begin_; }
1225
1226 BOOST_INTRUSIVE_FORCEINLINE void priv_set_cache(const bucket_ptr &p)
1227 { cached_begin_ = p; }
1228
1229 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_get_cache_bucket_num()
1230 { return this->cached_begin_ - this->bucket_hash_type::priv_bucket_pointer(); }
1231
1232 BOOST_INTRUSIVE_FORCEINLINE void priv_initialize_cache()
1233 { this->cached_begin_ = this->bucket_hash_type::priv_invalid_bucket(); }
1234
1235 BOOST_INTRUSIVE_FORCEINLINE void priv_swap_cache(bucket_hash_equal_t &other)
1236 {
1237 ::boost::adl_move_swap(this->cached_begin_, other.cached_begin_);
1238 }
1239
1240 siterator priv_begin() const
1241 {
1242 if(this->cached_begin_ == this->bucket_hash_type::priv_invalid_bucket()){
1243 return this->bucket_hash_type::priv_invalid_local_it();
1244 }
1245 else{
1246 return this->cached_begin_->begin();
1247 }
1248 }
1249
1250 void priv_insertion_update_cache(std::size_t insertion_bucket)
1251 {
1252 bucket_ptr p = this->bucket_hash_type::priv_bucket_pointer() + insertion_bucket;
1253 if(p < this->cached_begin_){
1254 this->cached_begin_ = p;
1255 }
1256 }
1257
1258 BOOST_INTRUSIVE_FORCEINLINE const key_equal &priv_equal() const
1259 { return this->equal_holder_t::get(); }
1260
1261 BOOST_INTRUSIVE_FORCEINLINE key_equal &priv_equal()
1262 { return this->equal_holder_t::get(); }
1263
1264 void priv_erasure_update_cache_range(std::size_t first_bucket_num, std::size_t last_bucket_num)
1265 {
1266 //If the last bucket is the end, the cache must be updated
1267 //to the last position if all
1268 if(this->priv_get_cache_bucket_num() == first_bucket_num &&
1269 this->bucket_hash_type::priv_bucket_pointer()[first_bucket_num].empty() ){
1270 this->priv_set_cache(this->bucket_hash_type::priv_bucket_pointer() + last_bucket_num);
1271 this->priv_erasure_update_cache();
1272 }
1273 }
1274
1275 void priv_erasure_update_cache()
1276 {
1277 if(this->cached_begin_ != this->bucket_hash_type::priv_invalid_bucket()){
1278 std::size_t current_n = this->priv_get_cache() - this->bucket_hash_type::priv_bucket_pointer();
1279 for( const std::size_t num_buckets = this->bucket_hash_type::priv_bucket_count()
1280 ; current_n < num_buckets
1281 ; ++current_n, ++this->priv_get_cache()){
1282 if(!this->priv_get_cache()->empty()){
1283 return;
1284 }
1285 }
1286 this->priv_initialize_cache();
1287 }
1288 }
1289
1290 bucket_ptr cached_begin_;
1291 };
1292
1293 //This wrapper around size_traits is used
1294 //to maintain minimal container size with compilers like MSVC
1295 //that have problems with EBO and multiple empty base classes
1296 template<class DeriveFrom, class SizeType, bool>
1297 struct hashtable_size_traits_wrapper
1298 : public DeriveFrom
1299 {
1300 template<class Base, class Arg0, class Arg1, class Arg2>
1301 hashtable_size_traits_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
1302 , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
1303 : DeriveFrom(::boost::forward<Base>(base)
1304 , ::boost::forward<Arg0>(arg0)
1305 , ::boost::forward<Arg1>(arg1)
1306 , ::boost::forward<Arg2>(arg2))
1307 {}
1308 typedef detail::size_holder < true, SizeType> size_traits;//size_traits
1309
1310 size_traits size_traits_;
1311
1312 typedef const size_traits & size_traits_const_t;
1313 typedef size_traits & size_traits_t;
1314
1315 BOOST_INTRUSIVE_FORCEINLINE size_traits_const_t priv_size_traits() const
1316 { return size_traits_; }
1317
1318 BOOST_INTRUSIVE_FORCEINLINE size_traits_t priv_size_traits()
1319 { return size_traits_; }
1320 };
1321
1322 template<class DeriveFrom, class SizeType>
1323 struct hashtable_size_traits_wrapper<DeriveFrom, SizeType, false>
1324 : public DeriveFrom
1325 {
1326 template<class Base, class Arg0, class Arg1, class Arg2>
1327 hashtable_size_traits_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
1328 , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
1329 : DeriveFrom(::boost::forward<Base>(base)
1330 , ::boost::forward<Arg0>(arg0)
1331 , ::boost::forward<Arg1>(arg1)
1332 , ::boost::forward<Arg2>(arg2))
1333 {}
1334
1335 typedef detail::size_holder< false, SizeType> size_traits;
1336
1337 typedef size_traits size_traits_const_t;
1338 typedef size_traits size_traits_t;
1339
1340 BOOST_INTRUSIVE_FORCEINLINE size_traits priv_size_traits() const
1341 { return size_traits(); }
1342 };
1343
1344 //hashdata_internal
1345 //Stores bucket_hash_equal_t and split_traits
1346 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
1347 struct hashdata_internal
1348 : public hashtable_size_traits_wrapper
1349 < bucket_hash_equal_t
1350 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1351 , BucketTraits
1352 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
1353 > //2
1354 , SizeType
1355 , (BoolFlags & hash_bool_flags::incremental_pos) != 0
1356 >
1357 {
1358 typedef hashtable_size_traits_wrapper
1359 < bucket_hash_equal_t
1360 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1361 , BucketTraits
1362 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
1363 > //2
1364 , SizeType
1365 , (BoolFlags & hash_bool_flags::incremental_pos) != 0
1366 > internal_type;
1367 typedef typename internal_type::key_equal key_equal;
1368 typedef typename internal_type::hasher hasher;
1369 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1370 typedef SizeType size_type;
1371 typedef typename internal_type::size_traits split_traits;
1372 typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
1373 typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
1374 typedef typename bucket_plus_vtraits_t::siterator siterator;
1375 typedef typename bucket_plus_vtraits_t::bucket_traits bucket_traits;
1376 typedef typename bucket_plus_vtraits_t::value_traits value_traits;
1377 typedef typename bucket_plus_vtraits_t::bucket_type bucket_type;
1378 typedef typename value_traits::value_type value_type;
1379 typedef typename value_traits::pointer pointer;
1380 typedef typename value_traits::const_pointer const_pointer;
1381 typedef typename pointer_traits<pointer>::reference reference;
1382 typedef typename pointer_traits
1383 <const_pointer>::reference const_reference;
1384 typedef typename value_traits::node_traits node_traits;
1385 typedef typename node_traits::node node;
1386 typedef typename node_traits::node_ptr node_ptr;
1387 typedef typename node_traits::const_node_ptr const_node_ptr;
1388 typedef detail::node_functions<node_traits> node_functions_t;
1389 typedef typename get_slist_impl
1390 <typename reduced_slist_node_traits
1391 <typename value_traits::node_traits>::type
1392 >::type slist_impl;
1393 typedef typename slist_impl::node_algorithms node_algorithms;
1394 typedef typename slist_impl::node_ptr slist_node_ptr;
1395
1396 typedef hash_key_types_base
1397 < typename ValueTraits::value_type
1398 , VoidOrKeyOfValue
1399 > hash_types_base;
1400 typedef typename hash_types_base::key_of_value key_of_value;
1401
1402 static const bool store_hash = detail::store_hash_is_true<node_traits>::value;
1403 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
1404 static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
1405
1406 typedef detail::bool_<store_hash> store_hash_t;
1407
1408 typedef detail::transform_iterator
1409 < typename slist_impl::iterator
1410 , downcast_node_to_value_t
1411 < value_traits
1412 , false> > local_iterator;
1413
1414 typedef detail::transform_iterator
1415 < typename slist_impl::iterator
1416 , downcast_node_to_value_t
1417 < value_traits
1418 , true> > const_local_iterator;
1419 //
1420
1421 template<class BucketTraitsType>
1422 hashdata_internal( const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits
1423 , const hasher & h, const key_equal &e)
1424 : internal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
1425 {}
1426
1427 BOOST_INTRUSIVE_FORCEINLINE typename internal_type::size_traits_t priv_split_traits()
1428 { return this->priv_size_traits(); }
1429
1430 BOOST_INTRUSIVE_FORCEINLINE typename internal_type::size_traits_const_t priv_split_traits() const
1431 { return this->priv_size_traits(); }
1432
1433 ~hashdata_internal()
1434 { this->priv_clear_buckets(); }
1435
1436 void priv_clear_buckets()
1437 {
1438 this->internal_type::priv_clear_buckets
1439 ( this->priv_get_cache()
1440 , this->internal_type::priv_bucket_count()
1441 - (this->priv_get_cache()
1442 - this->internal_type::priv_bucket_pointer()));
1443 }
1444
1445 void priv_clear_buckets_and_cache()
1446 {
1447 this->priv_clear_buckets();
1448 this->priv_initialize_cache();
1449 }
1450
1451 void priv_initialize_buckets_and_cache()
1452 {
1453 this->internal_type::priv_clear_buckets
1454 ( this->internal_type::priv_bucket_pointer()
1455 , this->internal_type::priv_bucket_count());
1456 this->priv_initialize_cache();
1457 }
1458
1459 typedef hashtable_iterator<bucket_plus_vtraits_t, false> iterator;
1460 typedef hashtable_iterator<bucket_plus_vtraits_t, true> const_iterator;
1461
1462 static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_ true_value)
1463 { return bucket_plus_vtraits<ValueTraits, BucketTraits>::priv_stored_hash(n, true_value); }
1464
1465 static std::size_t priv_stored_hash(slist_node_ptr n, detail::false_ false_value)
1466 { return bucket_plus_vtraits<ValueTraits, BucketTraits>::priv_stored_hash(n, false_value); }
1467
1468 //public functions
1469 BOOST_INTRUSIVE_FORCEINLINE SizeType split_count() const
1470 {
1471 return this->priv_split_traits().get_size();
1472 }
1473
1474 BOOST_INTRUSIVE_FORCEINLINE iterator iterator_to(reference value)
1475 {
1476 return iterator(bucket_type::s_iterator_to
1477 (this->priv_value_to_node(value)), &this->get_bucket_value_traits());
1478 }
1479
1480 const_iterator iterator_to(const_reference value) const
1481 {
1482 siterator const sit = bucket_type::s_iterator_to
1483 ( *pointer_traits<node_ptr>::const_cast_from
1484 (pointer_traits<const_node_ptr>::pointer_to(this->priv_value_to_node(value)))
1485 );
1486 return const_iterator(sit, &this->get_bucket_value_traits());
1487 }
1488
1489 static local_iterator s_local_iterator_to(reference value)
1490 {
1491 BOOST_STATIC_ASSERT((!stateful_value_traits));
1492 siterator sit = bucket_type::s_iterator_to(*value_traits::to_node_ptr(value));
1493 return local_iterator(sit, const_value_traits_ptr());
1494 }
1495
1496 static const_local_iterator s_local_iterator_to(const_reference value)
1497 {
1498 BOOST_STATIC_ASSERT((!stateful_value_traits));
1499 siterator const sit = bucket_type::s_iterator_to
1500 ( *pointer_traits<node_ptr>::const_cast_from
1501 (value_traits::to_node_ptr(value))
1502 );
1503 return const_local_iterator(sit, const_value_traits_ptr());
1504 }
1505
1506 local_iterator local_iterator_to(reference value)
1507 {
1508 siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value));
1509 return local_iterator(sit, this->priv_value_traits_ptr());
1510 }
1511
1512 const_local_iterator local_iterator_to(const_reference value) const
1513 {
1514 siterator sit = bucket_type::s_iterator_to
1515 ( *pointer_traits<node_ptr>::const_cast_from
1516 (pointer_traits<const_node_ptr>::pointer_to(this->priv_value_to_node(value)))
1517 );
1518 return const_local_iterator(sit, this->priv_value_traits_ptr());
1519 }
1520
1521 BOOST_INTRUSIVE_FORCEINLINE size_type bucket_count() const
1522 {
1523 const std::size_t bc = this->priv_bucket_count();
1524 BOOST_INTRUSIVE_INVARIANT_ASSERT(sizeof(size_type) >= sizeof(std::size_t) || bc <= size_type(-1));
1525 return static_cast<size_type>(bc);
1526 }
1527
1528 BOOST_INTRUSIVE_FORCEINLINE size_type bucket_size(size_type n) const
1529 { return this->priv_bucket_pointer()[n].size(); }
1530
1531 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr bucket_pointer() const
1532 { return this->priv_bucket_pointer(); }
1533
1534 BOOST_INTRUSIVE_FORCEINLINE local_iterator begin(size_type n)
1535 { return local_iterator(this->priv_bucket_pointer()[n].begin(), this->priv_value_traits_ptr()); }
1536
1537 BOOST_INTRUSIVE_FORCEINLINE const_local_iterator begin(size_type n) const
1538 { return this->cbegin(n); }
1539
1540 static BOOST_INTRUSIVE_FORCEINLINE size_type suggested_upper_bucket_count(size_type n)
1541 {
1542 return prime_list_holder<0>::suggested_upper_bucket_count(n);
1543 }
1544
1545 static BOOST_INTRUSIVE_FORCEINLINE size_type suggested_lower_bucket_count(size_type n)
1546 {
1547 return prime_list_holder<0>::suggested_lower_bucket_count(n);
1548 }
1549
1550 const_local_iterator cbegin(size_type n) const
1551 {
1552 return const_local_iterator
1553 ( pointer_traits<bucket_ptr>::const_cast_from(this->priv_bucket_pointer())[n].begin()
1554 , this->priv_value_traits_ptr());
1555 }
1556
1557 using internal_type::end;
1558 using internal_type::cend;
1559
1560 local_iterator end(size_type n)
1561 { return local_iterator(this->priv_bucket_pointer()[n].end(), this->priv_value_traits_ptr()); }
1562
1563 BOOST_INTRUSIVE_FORCEINLINE const_local_iterator end(size_type n) const
1564 { return this->cend(n); }
1565
1566 const_local_iterator cend(size_type n) const
1567 {
1568 return const_local_iterator
1569 ( pointer_traits<bucket_ptr>::const_cast_from(this->priv_bucket_pointer())[n].end()
1570 , this->priv_value_traits_ptr());
1571 }
1572
1573 //Public functions for hashtable_impl
1574
1575 BOOST_INTRUSIVE_FORCEINLINE iterator begin()
1576 { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1577
1578 BOOST_INTRUSIVE_FORCEINLINE const_iterator begin() const
1579 { return this->cbegin(); }
1580
1581 BOOST_INTRUSIVE_FORCEINLINE const_iterator cbegin() const
1582 { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1583
1584 BOOST_INTRUSIVE_FORCEINLINE hasher hash_function() const
1585 { return this->priv_hasher(); }
1586
1587 BOOST_INTRUSIVE_FORCEINLINE key_equal key_eq() const
1588 { return this->priv_equal(); }
1589 };
1590
1591 /// @endcond
1592
1593 //! The class template hashtable is an intrusive hash table container, that
1594 //! is used to construct intrusive unordered_set and unordered_multiset containers. The
1595 //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
1596 //!
1597 //! hashtable is a semi-intrusive container: each object to be stored in the
1598 //! container must contain a proper hook, but the container also needs
1599 //! additional auxiliary memory to work: hashtable needs a pointer to an array
1600 //! of type `bucket_type` to be passed in the constructor. This bucket array must
1601 //! have at least the same lifetime as the container. This makes the use of
1602 //! hashtable more complicated than purely intrusive containers.
1603 //! `bucket_type` is default-constructible, copyable and assignable
1604 //!
1605 //! The template parameter \c T is the type to be managed by the container.
1606 //! The user can specify additional options and if no options are provided
1607 //! default options are used.
1608 //!
1609 //! The container supports the following options:
1610 //! \c base_hook<>/member_hook<>/value_traits<>,
1611 //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
1612 //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
1613 //!
1614 //! hashtable only provides forward iterators but it provides 4 iterator types:
1615 //! iterator and const_iterator to navigate through the whole container and
1616 //! local_iterator and const_local_iterator to navigate through the values
1617 //! stored in a single bucket. Local iterators are faster and smaller.
1618 //!
1619 //! It's not recommended to use non constant-time size hashtables because several
1620 //! key functions, like "empty()", become non-constant time functions. Non
1621 //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
1622 //!
1623 //! hashtables, does not make automatic rehashings nor
1624 //! offers functions related to a load factor. Rehashing can be explicitly requested
1625 //! and the user must provide a new bucket array that will be used from that moment.
1626 //!
1627 //! Since no automatic rehashing is done, iterators are never invalidated when
1628 //! inserting or erasing elements. Iterators are only invalidated when rehashing.
1629 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1630 template<class T, class ...Options>
1631 #else
1632 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
1633 #endif
1634 class hashtable_impl
1635 : private hashtable_size_traits_wrapper
1636 < hashdata_internal
1637 < ValueTraits
1638 , VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1639 , BucketTraits, SizeType
1640 , BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos) //1
1641 >
1642 , SizeType
1643 , (BoolFlags & hash_bool_flags::constant_time_size_pos) != 0
1644 >
1645 {
1646 typedef hashtable_size_traits_wrapper
1647 < hashdata_internal
1648 < ValueTraits
1649 , VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1650 , BucketTraits, SizeType
1651 , BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos) //1
1652 >
1653 , SizeType
1654 , (BoolFlags & hash_bool_flags::constant_time_size_pos) != 0
1655 > internal_type;
1656 typedef typename internal_type::size_traits size_traits;
1657 typedef hash_key_types_base
1658 < typename ValueTraits::value_type
1659 , VoidOrKeyOfValue
1660 > hash_types_base;
1661 public:
1662 typedef ValueTraits value_traits;
1663
1664 /// @cond
1665 typedef BucketTraits bucket_traits;
1666
1667 typedef typename internal_type::slist_impl slist_impl;
1668 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
1669 typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
1670
1671 using internal_type::begin;
1672 using internal_type::cbegin;
1673 using internal_type::end;
1674 using internal_type::cend;
1675 using internal_type::hash_function;
1676 using internal_type::key_eq;
1677 using internal_type::bucket_size;
1678 using internal_type::bucket_count;
1679 using internal_type::local_iterator_to;
1680 using internal_type::s_local_iterator_to;
1681 using internal_type::iterator_to;
1682 using internal_type::bucket_pointer;
1683 using internal_type::suggested_upper_bucket_count;
1684 using internal_type::suggested_lower_bucket_count;
1685 using internal_type::split_count;
1686
1687 /// @endcond
1688
1689 typedef typename value_traits::pointer pointer;
1690 typedef typename value_traits::const_pointer const_pointer;
1691 typedef typename value_traits::value_type value_type;
1692 typedef typename hash_types_base::key_type key_type;
1693 typedef typename hash_types_base::key_of_value key_of_value;
1694 typedef typename pointer_traits<pointer>::reference reference;
1695 typedef typename pointer_traits<const_pointer>::reference const_reference;
1696 typedef typename pointer_traits<pointer>::difference_type difference_type;
1697 typedef SizeType size_type;
1698 typedef typename internal_type::key_equal key_equal;
1699 typedef typename internal_type::hasher hasher;
1700 typedef bucket_impl<slist_impl> bucket_type;
1701 typedef typename internal_type::bucket_ptr bucket_ptr;
1702 typedef typename slist_impl::iterator siterator;
1703 typedef typename slist_impl::const_iterator const_siterator;
1704 typedef typename internal_type::iterator iterator;
1705 typedef typename internal_type::const_iterator const_iterator;
1706 typedef typename internal_type::local_iterator local_iterator;
1707 typedef typename internal_type::const_local_iterator const_local_iterator;
1708 typedef typename value_traits::node_traits node_traits;
1709 typedef typename node_traits::node node;
1710 typedef typename pointer_traits
1711 <pointer>::template rebind_pointer
1712 < node >::type node_ptr;
1713 typedef typename pointer_traits
1714 <pointer>::template rebind_pointer
1715 < const node >::type const_node_ptr;
1716 typedef typename pointer_traits
1717 <node_ptr>::reference node_reference;
1718 typedef typename pointer_traits
1719 <const_node_ptr>::reference const_node_reference;
1720 typedef typename slist_impl::node_algorithms node_algorithms;
1721
1722 static const bool stateful_value_traits = internal_type::stateful_value_traits;
1723 static const bool store_hash = internal_type::store_hash;
1724
1725 static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
1726 static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
1727 static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
1728 static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
1729 static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
1730 static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
1731
1732 static const bool optimize_multikey
1733 = detail::optimize_multikey_is_true<node_traits>::value && !unique_keys;
1734
1735 /// @cond
1736 static const bool is_multikey = !unique_keys;
1737 private:
1738
1739 //Configuration error: compare_hash<> can't be specified without store_hash<>
1740 //See documentation for more explanations
1741 BOOST_STATIC_ASSERT((!compare_hash || store_hash));
1742
1743 typedef typename slist_impl::node_ptr slist_node_ptr;
1744 typedef typename pointer_traits
1745 <slist_node_ptr>::template rebind_pointer
1746 < void >::type void_pointer;
1747 //We'll define group traits, but these won't be instantiated if
1748 //optimize_multikey is not true
1749 typedef unordered_group_adapter<node_traits> group_traits;
1750 typedef circular_slist_algorithms<group_traits> group_algorithms;
1751 typedef typename internal_type::store_hash_t store_hash_t;
1752 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
1753 typedef detail::bool_<cache_begin> cache_begin_t;
1754 typedef detail::bool_<power_2_buckets> power_2_buckets_t;
1755 typedef typename internal_type::split_traits split_traits;
1756 typedef detail::group_functions<node_traits> group_functions_t;
1757 typedef detail::node_functions<node_traits> node_functions_t;
1758
1759 private:
1760 //noncopyable, movable
1761 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
1762
1763 static const bool safemode_or_autounlink = internal_type::safemode_or_autounlink;
1764
1765 //Constant-time size is incompatible with auto-unlink hooks!
1766 BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
1767 //Cache begin is incompatible with auto-unlink hooks!
1768 BOOST_STATIC_ASSERT(!(cache_begin && ((int)value_traits::link_mode == (int)auto_unlink)));
1769
1770 template<class Disposer>
1771 struct typeof_node_disposer
1772 {
1773 typedef node_cast_adaptor
1774 < detail::node_disposer< Disposer, value_traits, CircularSListAlgorithms>
1775 , slist_node_ptr, node_ptr > type;
1776 };
1777
1778 template<class Disposer>
1779 typename typeof_node_disposer<Disposer>::type
1780 make_node_disposer(const Disposer &disposer) const
1781 {
1782 typedef typename typeof_node_disposer<Disposer>::type return_t;
1783 return return_t(disposer, &this->priv_value_traits());
1784 }
1785
1786 /// @endcond
1787
1788 public:
1789 typedef detail::insert_commit_data_impl insert_commit_data;
1790
1791
1792 public:
1793
1794 //! <b>Requires</b>: buckets must not be being used by any other resource.
1795 //!
1796 //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
1797 //! to the bucket array and copies of the key_hasher and equal_func functors.
1798 //!
1799 //! <b>Complexity</b>: Constant.
1800 //!
1801 //! <b>Throws</b>: If value_traits::node_traits::node
1802 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1803 //! or the copy constructor or invocation of hash_func or equal_func throws.
1804 //!
1805 //! <b>Notes</b>: buckets array must be disposed only after
1806 //! *this is disposed.
1807 explicit hashtable_impl ( const bucket_traits &b_traits
1808 , const hasher & hash_func = hasher()
1809 , const key_equal &equal_func = key_equal()
1810 , const value_traits &v_traits = value_traits())
1811 : internal_type(v_traits, b_traits, hash_func, equal_func)
1812 {
1813 this->priv_initialize_buckets_and_cache();
1814 this->priv_size_traits().set_size(size_type(0));
1815 size_type bucket_sz = this->bucket_count();
1816 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
1817 //Check power of two bucket array if the option is activated
1818 BOOST_INTRUSIVE_INVARIANT_ASSERT
1819 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
1820 this->priv_split_traits().set_size(bucket_sz>>1);
1821 }
1822
1823 //! <b>Requires</b>: buckets must not be being used by any other resource
1824 //! and dereferencing iterator must yield an lvalue of type value_type.
1825 //!
1826 //! <b>Effects</b>: Constructs an empty container and inserts elements from
1827 //! [b, e).
1828 //!
1829 //! <b>Complexity</b>: If N is distance(b, e): Average case is O(N)
1830 //! (with a good hash function and with buckets_len >= N),worst case O(N^2).
1831 //!
1832 //! <b>Throws</b>: If value_traits::node_traits::node
1833 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1834 //! or the copy constructor or invocation of hasher or key_equal throws.
1835 //!
1836 //! <b>Notes</b>: buckets array must be disposed only after
1837 //! *this is disposed.
1838 template<class Iterator>
1839 hashtable_impl ( bool unique, Iterator b, Iterator e
1840 , const bucket_traits &b_traits
1841 , const hasher & hash_func = hasher()
1842 , const key_equal &equal_func = key_equal()
1843 , const value_traits &v_traits = value_traits())
1844 : internal_type(v_traits, b_traits, hash_func, equal_func)
1845 {
1846 this->priv_initialize_buckets_and_cache();
1847 this->priv_size_traits().set_size(size_type(0));
1848 size_type bucket_sz = this->bucket_count();
1849 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
1850 //Check power of two bucket array if the option is activated
1851 BOOST_INTRUSIVE_INVARIANT_ASSERT
1852 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
1853 this->priv_split_traits().set_size(bucket_sz>>1);
1854 //Now insert
1855 if(unique)
1856 this->insert_unique(b, e);
1857 else
1858 this->insert_equal(b, e);
1859 }
1860
1861 //! <b>Effects</b>: Constructs a container moving resources from another container.
1862 //! Internal value traits, bucket traits, hasher and comparison are move constructed and
1863 //! nodes belonging to x are linked to *this.
1864 //!
1865 //! <b>Complexity</b>: Constant.
1866 //!
1867 //! <b>Throws</b>: If value_traits::node_traits::node's
1868 //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1869 //! or the move constructor of value traits, bucket traits, hasher or comparison throws.
1870 hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
1871 : internal_type( ::boost::move(x.priv_value_traits())
1872 , ::boost::move(x.priv_bucket_traits())
1873 , ::boost::move(x.priv_hasher())
1874 , ::boost::move(x.priv_equal())
1875 )
1876 {
1877 this->priv_swap_cache(x);
1878 x.priv_initialize_cache();
1879 this->priv_size_traits().set_size(x.priv_size_traits().get_size());
1880 x.priv_size_traits().set_size(size_type(0));
1881 this->priv_split_traits().set_size(x.priv_split_traits().get_size());
1882 x.priv_split_traits().set_size(size_type(0));
1883 }
1884
1885 //! <b>Effects</b>: Equivalent to swap.
1886 //!
1887 hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
1888 { this->swap(x); return *this; }
1889
1890 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1891 //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
1892 //! are not deleted (i.e. no destructors are called).
1893 //!
1894 //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
1895 //! it's a safe-mode or auto-unlink value. Otherwise constant.
1896 //!
1897 //! <b>Throws</b>: Nothing.
1898 ~hashtable_impl();
1899
1900 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
1901 //!
1902 //! <b>Complexity</b>: Amortized constant time.
1903 //! Worst case (empty unordered_set): O(this->bucket_count())
1904 //!
1905 //! <b>Throws</b>: Nothing.
1906 iterator begin();
1907
1908 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1909 //! of the unordered_set.
1910 //!
1911 //! <b>Complexity</b>: Amortized constant time.
1912 //! Worst case (empty unordered_set): O(this->bucket_count())
1913 //!
1914 //! <b>Throws</b>: Nothing.
1915 const_iterator begin() const;
1916
1917 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1918 //! of the unordered_set.
1919 //!
1920 //! <b>Complexity</b>: Amortized constant time.
1921 //! Worst case (empty unordered_set): O(this->bucket_count())
1922 //!
1923 //! <b>Throws</b>: Nothing.
1924 const_iterator cbegin() const;
1925
1926 //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
1927 //!
1928 //! <b>Complexity</b>: Constant.
1929 //!
1930 //! <b>Throws</b>: Nothing.
1931 iterator end();
1932
1933 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1934 //!
1935 //! <b>Complexity</b>: Constant.
1936 //!
1937 //! <b>Throws</b>: Nothing.
1938 const_iterator end() const;
1939
1940 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1941 //!
1942 //! <b>Complexity</b>: Constant.
1943 //!
1944 //! <b>Throws</b>: Nothing.
1945 const_iterator cend() const;
1946
1947 //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
1948 //!
1949 //! <b>Complexity</b>: Constant.
1950 //!
1951 //! <b>Throws</b>: If hasher copy-constructor throws.
1952 hasher hash_function() const;
1953
1954 //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
1955 //!
1956 //! <b>Complexity</b>: Constant.
1957 //!
1958 //! <b>Throws</b>: If key_equal copy-constructor throws.
1959 key_equal key_eq() const;
1960
1961 #endif
1962
1963 //! <b>Effects</b>: Returns true if the container is empty.
1964 //!
1965 //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
1966 //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
1967 //! Otherwise constant.
1968 //!
1969 //! <b>Throws</b>: Nothing.
1970 bool empty() const
1971 {
1972 if(constant_time_size){
1973 return !this->size();
1974 }
1975 else if(cache_begin){
1976 return this->begin() == this->end();
1977 }
1978 else{
1979 size_type bucket_cnt = this->bucket_count();
1980 const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
1981 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
1982 if(!b->empty()){
1983 return false;
1984 }
1985 }
1986 return true;
1987 }
1988 }
1989
1990 //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
1991 //!
1992 //! <b>Complexity</b>: Linear to elements contained in *this if
1993 //! constant_time_size is false. Constant-time otherwise.
1994 //!
1995 //! <b>Throws</b>: Nothing.
1996 size_type size() const
1997 {
1998 if(constant_time_size)
1999 return this->priv_size_traits().get_size();
2000 else{
2001 size_type len = 0;
2002 size_type bucket_cnt = this->bucket_count();
2003 const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
2004 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
2005 len += b->size();
2006 }
2007 return len;
2008 }
2009 }
2010
2011 //! <b>Requires</b>: the hasher and the equality function unqualified swap
2012 //! call should not throw.
2013 //!
2014 //! <b>Effects</b>: Swaps the contents of two unordered_sets.
2015 //! Swaps also the contained bucket array and equality and hasher functors.
2016 //!
2017 //! <b>Complexity</b>: Constant.
2018 //!
2019 //! <b>Throws</b>: If the swap() call for the comparison or hash functors
2020 //! found using ADL throw. Basic guarantee.
2021 void swap(hashtable_impl& other)
2022 {
2023 //These can throw
2024 ::boost::adl_move_swap(this->priv_equal(), other.priv_equal());
2025 ::boost::adl_move_swap(this->priv_hasher(), other.priv_hasher());
2026 //These can't throw
2027 ::boost::adl_move_swap(this->priv_bucket_traits(), other.priv_bucket_traits());
2028 ::boost::adl_move_swap(this->priv_value_traits(), other.priv_value_traits());
2029 this->priv_swap_cache(other);
2030 this->priv_size_traits().swap(other.priv_size_traits());
2031 this->priv_split_traits().swap(other.priv_split_traits());
2032 }
2033
2034 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
2035 //! Cloner should yield to nodes that compare equal and produce the same
2036 //! hash than the original node.
2037 //!
2038 //! <b>Effects</b>: Erases all the elements from *this
2039 //! calling Disposer::operator()(pointer), clones all the
2040 //! elements from src calling Cloner::operator()(const_reference )
2041 //! and inserts them on *this. The hash function and the equality
2042 //! predicate are copied from the source.
2043 //!
2044 //! If store_hash option is true, this method does not use the hash function.
2045 //!
2046 //! If any operation throws, all cloned elements are unlinked and disposed
2047 //! calling Disposer::operator()(pointer).
2048 //!
2049 //! <b>Complexity</b>: Linear to erased plus inserted elements.
2050 //!
2051 //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
2052 //! throws. Basic guarantee.
2053 template <class Cloner, class Disposer>
2054 BOOST_INTRUSIVE_FORCEINLINE void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer)
2055 { this->priv_clone_from(src, cloner, disposer); }
2056
2057 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
2058 //! Cloner should yield to nodes that compare equal and produce the same
2059 //! hash than the original node.
2060 //!
2061 //! <b>Effects</b>: Erases all the elements from *this
2062 //! calling Disposer::operator()(pointer), clones all the
2063 //! elements from src calling Cloner::operator()(reference)
2064 //! and inserts them on *this. The hash function and the equality
2065 //! predicate are copied from the source.
2066 //!
2067 //! If store_hash option is true, this method does not use the hash function.
2068 //!
2069 //! If any operation throws, all cloned elements are unlinked and disposed
2070 //! calling Disposer::operator()(pointer).
2071 //!
2072 //! <b>Complexity</b>: Linear to erased plus inserted elements.
2073 //!
2074 //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
2075 //! throws. Basic guarantee.
2076 template <class Cloner, class Disposer>
2077 BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(hashtable_impl) src, Cloner cloner, Disposer disposer)
2078 { this->priv_clone_from(static_cast<hashtable_impl&>(src), cloner, disposer); }
2079
2080 //! <b>Requires</b>: value must be an lvalue
2081 //!
2082 //! <b>Effects</b>: Inserts the value into the unordered_set.
2083 //!
2084 //! <b>Returns</b>: An iterator to the inserted value.
2085 //!
2086 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2087 //!
2088 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
2089 //!
2090 //! <b>Note</b>: Does not affect the validity of iterators and references.
2091 //! No copy-constructors are called.
2092 iterator insert_equal(reference value)
2093 {
2094 size_type bucket_num;
2095 std::size_t hash_value;
2096 siterator prev;
2097 siterator const it = this->priv_find
2098 (key_of_value()(value), this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev);
2099 bool const next_is_in_group = optimize_multikey && it != this->priv_invalid_local_it();
2100 return this->priv_insert_equal_after_find(value, bucket_num, hash_value, prev, next_is_in_group);
2101 }
2102
2103 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
2104 //! of type value_type.
2105 //!
2106 //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
2107 //!
2108 //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
2109 //! Worst case O(N*this->size()).
2110 //!
2111 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
2112 //!
2113 //! <b>Note</b>: Does not affect the validity of iterators and references.
2114 //! No copy-constructors are called.
2115 template<class Iterator>
2116 void insert_equal(Iterator b, Iterator e)
2117 {
2118 for (; b != e; ++b)
2119 this->insert_equal(*b);
2120 }
2121
2122 //! <b>Requires</b>: value must be an lvalue
2123 //!
2124 //! <b>Effects</b>: Tries to inserts value into the unordered_set.
2125 //!
2126 //! <b>Returns</b>: If the value
2127 //! is not already present inserts it and returns a pair containing the
2128 //! iterator to the new value and true. If there is an equivalent value
2129 //! returns a pair containing an iterator to the already present value
2130 //! and false.
2131 //!
2132 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2133 //!
2134 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
2135 //!
2136 //! <b>Note</b>: Does not affect the validity of iterators and references.
2137 //! No copy-constructors are called.
2138 std::pair<iterator, bool> insert_unique(reference value)
2139 {
2140 insert_commit_data commit_data;
2141 std::pair<iterator, bool> ret = this->insert_unique_check(key_of_value()(value), commit_data);
2142 if(ret.second){
2143 ret.first = this->insert_unique_commit(value, commit_data);
2144 }
2145 return ret;
2146 }
2147
2148 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
2149 //! of type value_type.
2150 //!
2151 //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
2152 //!
2153 //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
2154 //! Worst case O(N*this->size()).
2155 //!
2156 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
2157 //!
2158 //! <b>Note</b>: Does not affect the validity of iterators and references.
2159 //! No copy-constructors are called.
2160 template<class Iterator>
2161 void insert_unique(Iterator b, Iterator e)
2162 {
2163 for (; b != e; ++b)
2164 this->insert_unique(*b);
2165 }
2166
2167 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2168 //! the same hash values as the stored hasher. The difference is that
2169 //! "hash_func" hashes the given key instead of the value_type.
2170 //!
2171 //! "equal_func" must be a equality function that induces
2172 //! the same equality as key_equal. The difference is that
2173 //! "equal_func" compares an arbitrary key with the contained values.
2174 //!
2175 //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
2176 //! a user provided key instead of the value itself.
2177 //!
2178 //! <b>Returns</b>: If there is an equivalent value
2179 //! returns a pair containing an iterator to the already present value
2180 //! and false. If the value can be inserted returns true in the returned
2181 //! pair boolean and fills "commit_data" that is meant to be used with
2182 //! the "insert_commit" function.
2183 //!
2184 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2185 //!
2186 //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
2187 //!
2188 //! <b>Notes</b>: This function is used to improve performance when constructing
2189 //! a value_type is expensive: if there is an equivalent value
2190 //! the constructed object must be discarded. Many times, the part of the
2191 //! node that is used to impose the hash or the equality is much cheaper to
2192 //! construct than the value_type and this function offers the possibility to
2193 //! use that the part to check if the insertion will be successful.
2194 //!
2195 //! If the check is successful, the user can construct the value_type and use
2196 //! "insert_commit" to insert the object in constant-time.
2197 //!
2198 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
2199 //! objects are inserted or erased from the unordered_set.
2200 //!
2201 //! After a successful rehashing insert_commit_data remains valid.
2202 template<class KeyType, class KeyHasher, class KeyEqual>
2203 std::pair<iterator, bool> insert_unique_check
2204 ( const KeyType &key
2205 , KeyHasher hash_func
2206 , KeyEqual equal_func
2207 , insert_commit_data &commit_data)
2208 {
2209 size_type bucket_num;
2210 siterator prev;
2211 siterator const pos = this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev);
2212 return std::pair<iterator, bool>
2213 ( iterator(pos, &this->get_bucket_value_traits())
2214 , pos == this->priv_invalid_local_it());
2215 }
2216
2217 //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
2218 //! a user provided key instead of the value itself.
2219 //!
2220 //! <b>Returns</b>: If there is an equivalent value
2221 //! returns a pair containing an iterator to the already present value
2222 //! and false. If the value can be inserted returns true in the returned
2223 //! pair boolean and fills "commit_data" that is meant to be used with
2224 //! the "insert_commit" function.
2225 //!
2226 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2227 //!
2228 //! <b>Throws</b>: If hasher or key_compare throw. Strong guarantee.
2229 //!
2230 //! <b>Notes</b>: This function is used to improve performance when constructing
2231 //! a value_type is expensive: if there is an equivalent value
2232 //! the constructed object must be discarded. Many times, the part of the
2233 //! node that is used to impose the hash or the equality is much cheaper to
2234 //! construct than the value_type and this function offers the possibility to
2235 //! use that the part to check if the insertion will be successful.
2236 //!
2237 //! If the check is successful, the user can construct the value_type and use
2238 //! "insert_commit" to insert the object in constant-time.
2239 //!
2240 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
2241 //! objects are inserted or erased from the unordered_set.
2242 //!
2243 //! After a successful rehashing insert_commit_data remains valid.
2244 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator, bool> insert_unique_check
2245 ( const key_type &key, insert_commit_data &commit_data)
2246 { return this->insert_unique_check(key, this->priv_hasher(), this->priv_equal(), commit_data); }
2247
2248 //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
2249 //! must have been obtained from a previous call to "insert_check".
2250 //! No objects should have been inserted or erased from the unordered_set between
2251 //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
2252 //!
2253 //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
2254 //! from the "commit_data" that a previous "insert_check" filled.
2255 //!
2256 //! <b>Returns</b>: An iterator to the newly inserted object.
2257 //!
2258 //! <b>Complexity</b>: Constant time.
2259 //!
2260 //! <b>Throws</b>: Nothing.
2261 //!
2262 //! <b>Notes</b>: This function has only sense if a "insert_check" has been
2263 //! previously executed to fill "commit_data". No value should be inserted or
2264 //! erased between the "insert_check" and "insert_commit" calls.
2265 //!
2266 //! After a successful rehashing insert_commit_data remains valid.
2267 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
2268 {
2269 size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash);
2270 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
2271 this->priv_size_traits().increment();
2272 node_ptr const n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
2273 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(n));
2274 node_functions_t::store_hash(n, commit_data.hash, store_hash_t());
2275 this->priv_insertion_update_cache(bucket_num);
2276 group_functions_t::insert_in_group(n, n, optimize_multikey_t());
2277 return iterator(b.insert_after(b.before_begin(), *n), &this->get_bucket_value_traits());
2278 }
2279
2280 //! <b>Effects</b>: Erases the element pointed to by i.
2281 //!
2282 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2283 //!
2284 //! <b>Throws</b>: Nothing.
2285 //!
2286 //! <b>Note</b>: Invalidates the iterators (but not the references)
2287 //! to the erased element. No destructors are called.
2288 BOOST_INTRUSIVE_FORCEINLINE void erase(const_iterator i)
2289 { this->erase_and_dispose(i, detail::null_disposer()); }
2290
2291 //! <b>Effects</b>: Erases the range pointed to by b end e.
2292 //!
2293 //! <b>Complexity</b>: Average case O(distance(b, e)),
2294 //! worst case O(this->size()).
2295 //!
2296 //! <b>Throws</b>: Nothing.
2297 //!
2298 //! <b>Note</b>: Invalidates the iterators (but not the references)
2299 //! to the erased elements. No destructors are called.
2300 BOOST_INTRUSIVE_FORCEINLINE void erase(const_iterator b, const_iterator e)
2301 { this->erase_and_dispose(b, e, detail::null_disposer()); }
2302
2303 //! <b>Effects</b>: Erases all the elements with the given value.
2304 //!
2305 //! <b>Returns</b>: The number of erased elements.
2306 //!
2307 //! <b>Complexity</b>: Average case O(this->count(value)).
2308 //! Worst case O(this->size()).
2309 //!
2310 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2311 //! Basic guarantee.
2312 //!
2313 //! <b>Note</b>: Invalidates the iterators (but not the references)
2314 //! to the erased elements. No destructors are called.
2315 BOOST_INTRUSIVE_FORCEINLINE size_type erase(const key_type &key)
2316 { return this->erase(key, this->priv_hasher(), this->priv_equal()); }
2317
2318 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2319 //! the same hash values as the stored hasher. The difference is that
2320 //! "hash_func" hashes the given key instead of the value_type.
2321 //!
2322 //! "equal_func" must be a equality function that induces
2323 //! the same equality as key_equal. The difference is that
2324 //! "equal_func" compares an arbitrary key with the contained values.
2325 //!
2326 //! <b>Effects</b>: Erases all the elements that have the same hash and
2327 //! compare equal with the given key.
2328 //!
2329 //! <b>Returns</b>: The number of erased elements.
2330 //!
2331 //! <b>Complexity</b>: Average case O(this->count(value)).
2332 //! Worst case O(this->size()).
2333 //!
2334 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
2335 //!
2336 //! <b>Note</b>: Invalidates the iterators (but not the references)
2337 //! to the erased elements. No destructors are called.
2338 template<class KeyType, class KeyHasher, class KeyEqual>
2339 BOOST_INTRUSIVE_FORCEINLINE size_type erase(const KeyType& key, KeyHasher hash_func, KeyEqual equal_func)
2340 { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); }
2341
2342 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2343 //!
2344 //! <b>Effects</b>: Erases the element pointed to by i.
2345 //! Disposer::operator()(pointer) is called for the removed element.
2346 //!
2347 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2348 //!
2349 //! <b>Throws</b>: Nothing.
2350 //!
2351 //! <b>Note</b>: Invalidates the iterators
2352 //! to the erased elements.
2353 template<class Disposer>
2354 BOOST_INTRUSIVE_DOC1ST(void
2355 , typename detail::disable_if_convertible<Disposer BOOST_INTRUSIVE_I const_iterator>::type)
2356 erase_and_dispose(const_iterator i, Disposer disposer)
2357 {
2358 //Get the bucket number and local iterator for both iterators
2359 siterator const first_local_it(i.slist_it());
2360 size_type const first_bucket_num = this->priv_get_bucket_num(first_local_it);
2361 this->priv_erase_node(this->priv_bucket_pointer()[first_bucket_num], first_local_it, make_node_disposer(disposer), optimize_multikey_t());
2362 this->priv_size_traits().decrement();
2363 this->priv_erasure_update_cache_range(first_bucket_num, first_bucket_num);
2364 }
2365
2366 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2367 //!
2368 //! <b>Effects</b>: Erases the range pointed to by b end e.
2369 //! Disposer::operator()(pointer) is called for the removed elements.
2370 //!
2371 //! <b>Complexity</b>: Average case O(distance(b, e)),
2372 //! worst case O(this->size()).
2373 //!
2374 //! <b>Throws</b>: Nothing.
2375 //!
2376 //! <b>Note</b>: Invalidates the iterators
2377 //! to the erased elements.
2378 template<class Disposer>
2379 void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
2380 {
2381 if(b != e){
2382 //Get the bucket number and local iterator for both iterators
2383 siterator first_local_it(b.slist_it());
2384 size_type first_bucket_num = this->priv_get_bucket_num(first_local_it);
2385
2386 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
2387 siterator before_first_local_it
2388 = this->priv_get_previous(buck_ptr[first_bucket_num], first_local_it);
2389 size_type last_bucket_num;
2390 siterator last_local_it;
2391
2392 //For the end iterator, we will assign the end iterator
2393 //of the last bucket
2394 if(e == this->end()){
2395 last_bucket_num = this->bucket_count() - 1;
2396 last_local_it = buck_ptr[last_bucket_num].end();
2397 }
2398 else{
2399 last_local_it = e.slist_it();
2400 last_bucket_num = this->priv_get_bucket_num(last_local_it);
2401 }
2402 size_type const num_erased = this->priv_erase_node_range
2403 ( before_first_local_it, first_bucket_num, last_local_it, last_bucket_num
2404 , make_node_disposer(disposer), optimize_multikey_t());
2405 this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased);
2406 this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
2407 }
2408 }
2409
2410 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2411 //!
2412 //! <b>Effects</b>: Erases all the elements with the given value.
2413 //! Disposer::operator()(pointer) is called for the removed elements.
2414 //!
2415 //! <b>Returns</b>: The number of erased elements.
2416 //!
2417 //! <b>Complexity</b>: Average case O(this->count(value)).
2418 //! Worst case O(this->size()).
2419 //!
2420 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2421 //! Basic guarantee.
2422 //!
2423 //! <b>Note</b>: Invalidates the iterators (but not the references)
2424 //! to the erased elements. No destructors are called.
2425 template<class Disposer>
2426 BOOST_INTRUSIVE_FORCEINLINE size_type erase_and_dispose(const key_type &key, Disposer disposer)
2427 { return this->erase_and_dispose(key, this->priv_hasher(), this->priv_equal(), disposer); }
2428
2429 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2430 //!
2431 //! <b>Effects</b>: Erases all the elements with the given key.
2432 //! according to the comparison functor "equal_func".
2433 //! Disposer::operator()(pointer) is called for the removed elements.
2434 //!
2435 //! <b>Returns</b>: The number of erased elements.
2436 //!
2437 //! <b>Complexity</b>: Average case O(this->count(value)).
2438 //! Worst case O(this->size()).
2439 //!
2440 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
2441 //!
2442 //! <b>Note</b>: Invalidates the iterators
2443 //! to the erased elements.
2444 template<class KeyType, class KeyHasher, class KeyEqual, class Disposer>
2445 size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
2446 ,KeyEqual equal_func, Disposer disposer)
2447 {
2448 size_type bucket_num;
2449 std::size_t h;
2450 siterator prev;
2451 siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
2452 bool const success = it != this->priv_invalid_local_it();
2453
2454 size_type cnt(0);
2455 if(success){
2456 if(optimize_multikey){
2457 cnt = this->priv_erase_from_single_bucket
2458 (this->priv_bucket_pointer()[bucket_num], prev, ++(priv_last_in_group)(it), make_node_disposer(disposer), optimize_multikey_t());
2459 }
2460 else{
2461 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
2462 siterator const end_sit = b.end();
2463 do{
2464 ++cnt;
2465 ++it;
2466 }while(it != end_sit &&
2467 this->priv_is_value_equal_to_key
2468 (this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func));
2469 bucket_type::s_erase_after_and_dispose(prev, it, make_node_disposer(disposer));
2470 }
2471 this->priv_size_traits().set_size(this->priv_size_traits().get_size()-cnt);
2472 this->priv_erasure_update_cache();
2473 }
2474
2475 return cnt;
2476 }
2477
2478 //! <b>Effects</b>: Erases all of the elements.
2479 //!
2480 //! <b>Complexity</b>: Linear to the number of elements on the container.
2481 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
2482 //!
2483 //! <b>Throws</b>: Nothing.
2484 //!
2485 //! <b>Note</b>: Invalidates the iterators (but not the references)
2486 //! to the erased elements. No destructors are called.
2487 void clear()
2488 {
2489 this->priv_clear_buckets_and_cache();
2490 this->priv_size_traits().set_size(size_type(0));
2491 }
2492
2493 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2494 //!
2495 //! <b>Effects</b>: Erases all of the elements.
2496 //!
2497 //! <b>Complexity</b>: Linear to the number of elements on the container.
2498 //! Disposer::operator()(pointer) is called for the removed elements.
2499 //!
2500 //! <b>Throws</b>: Nothing.
2501 //!
2502 //! <b>Note</b>: Invalidates the iterators (but not the references)
2503 //! to the erased elements. No destructors are called.
2504 template<class Disposer>
2505 void clear_and_dispose(Disposer disposer)
2506 {
2507 if(!constant_time_size || !this->empty()){
2508 size_type num_buckets = this->bucket_count();
2509 bucket_ptr b = this->priv_bucket_pointer();
2510 typename typeof_node_disposer<Disposer>::type d(disposer, &this->priv_value_traits());
2511 for(; num_buckets--; ++b){
2512 b->clear_and_dispose(d);
2513 }
2514 this->priv_size_traits().set_size(size_type(0));
2515 }
2516 this->priv_initialize_cache();
2517 }
2518
2519 //! <b>Effects</b>: Returns the number of contained elements with the given value
2520 //!
2521 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2522 //!
2523 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2524 BOOST_INTRUSIVE_FORCEINLINE size_type count(const key_type &key) const
2525 { return this->count(key, this->priv_hasher(), this->priv_equal()); }
2526
2527 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2528 //! the same hash values as the stored hasher. The difference is that
2529 //! "hash_func" hashes the given key instead of the value_type.
2530 //!
2531 //! "equal_func" must be a equality function that induces
2532 //! the same equality as key_equal. The difference is that
2533 //! "equal_func" compares an arbitrary key with the contained values.
2534 //!
2535 //! <b>Effects</b>: Returns the number of contained elements with the given key
2536 //!
2537 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2538 //!
2539 //! <b>Throws</b>: If hash_func or equal throw.
2540 template<class KeyType, class KeyHasher, class KeyEqual>
2541 size_type count(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2542 {
2543 size_type cnt;
2544 size_type n_bucket;
2545 this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt);
2546 return cnt;
2547 }
2548
2549 //! <b>Effects</b>: Finds an iterator to the first element is equal to
2550 //! "value" or end() if that element does not exist.
2551 //!
2552 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2553 //!
2554 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2555 BOOST_INTRUSIVE_FORCEINLINE iterator find(const key_type &key)
2556 { return this->find(key, this->priv_hasher(), this->priv_equal()); }
2557
2558 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2559 //! the same hash values as the stored hasher. The difference is that
2560 //! "hash_func" hashes the given key instead of the value_type.
2561 //!
2562 //! "equal_func" must be a equality function that induces
2563 //! the same equality as key_equal. The difference is that
2564 //! "equal_func" compares an arbitrary key with the contained values.
2565 //!
2566 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2567 //! "key" according to the given hash and equality functor or end() if
2568 //! that element does not exist.
2569 //!
2570 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2571 //!
2572 //! <b>Throws</b>: If hash_func or equal_func throw.
2573 //!
2574 //! <b>Note</b>: This function is used when constructing a value_type
2575 //! is expensive and the value_type can be compared with a cheaper
2576 //! key type. Usually this key is part of the value_type.
2577 template<class KeyType, class KeyHasher, class KeyEqual>
2578 iterator find(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
2579 {
2580 size_type bucket_n;
2581 std::size_t hash;
2582 siterator prev;
2583 return iterator( this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev)
2584 , &this->get_bucket_value_traits());
2585 }
2586
2587 //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
2588 //! "key" or end() if that element does not exist.
2589 //!
2590 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2591 //!
2592 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2593 BOOST_INTRUSIVE_FORCEINLINE const_iterator find(const key_type &key) const
2594 { return this->find(key, this->priv_hasher(), this->priv_equal()); }
2595
2596 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2597 //! the same hash values as the stored hasher. The difference is that
2598 //! "hash_func" hashes the given key instead of the value_type.
2599 //!
2600 //! "equal_func" must be a equality function that induces
2601 //! the same equality as key_equal. The difference is that
2602 //! "equal_func" compares an arbitrary key with the contained values.
2603 //!
2604 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2605 //! "key" according to the given hasher and equality functor or end() if
2606 //! that element does not exist.
2607 //!
2608 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2609 //!
2610 //! <b>Throws</b>: If hash_func or equal_func throw.
2611 //!
2612 //! <b>Note</b>: This function is used when constructing a value_type
2613 //! is expensive and the value_type can be compared with a cheaper
2614 //! key type. Usually this key is part of the value_type.
2615 template<class KeyType, class KeyHasher, class KeyEqual>
2616 const_iterator find
2617 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2618 {
2619 size_type bucket_n;
2620 std::size_t hash_value;
2621 siterator prev;
2622 return const_iterator( this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev)
2623 , &this->get_bucket_value_traits());
2624 }
2625
2626 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2627 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2628 //! elements exist.
2629 //!
2630 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2631 //!
2632 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2633 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type &key)
2634 { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
2635
2636 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2637 //! the same hash values as the stored hasher. The difference is that
2638 //! "hash_func" hashes the given key instead of the value_type.
2639 //!
2640 //! "equal_func" must be a equality function that induces
2641 //! the same equality as key_equal. The difference is that
2642 //! "equal_func" compares an arbitrary key with the contained values.
2643 //!
2644 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2645 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2646 //! elements exist.
2647 //!
2648 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2649 //! Worst case O(this->size()).
2650 //!
2651 //! <b>Throws</b>: If hash_func or the equal_func throw.
2652 //!
2653 //! <b>Note</b>: This function is used when constructing a value_type
2654 //! is expensive and the value_type can be compared with a cheaper
2655 //! key type. Usually this key is part of the value_type.
2656 template<class KeyType, class KeyHasher, class KeyEqual>
2657 std::pair<iterator,iterator> equal_range
2658 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
2659 {
2660 std::pair<siterator, siterator> ret =
2661 this->priv_equal_range(key, hash_func, equal_func);
2662 return std::pair<iterator, iterator>
2663 ( iterator(ret.first, &this->get_bucket_value_traits())
2664 , iterator(ret.second, &this->get_bucket_value_traits()));
2665 }
2666
2667 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2668 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2669 //! elements exist.
2670 //!
2671 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2672 //!
2673 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2674 BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
2675 equal_range(const key_type &key) const
2676 { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
2677
2678 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2679 //! the same hash values as the stored hasher. The difference is that
2680 //! "hash_func" hashes the given key instead of the value_type.
2681 //!
2682 //! "equal_func" must be a equality function that induces
2683 //! the same equality as key_equal. The difference is that
2684 //! "equal_func" compares an arbitrary key with the contained values.
2685 //!
2686 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2687 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2688 //! elements exist.
2689 //!
2690 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2691 //! Worst case O(this->size()).
2692 //!
2693 //! <b>Throws</b>: If the hasher or equal_func throw.
2694 //!
2695 //! <b>Note</b>: This function is used when constructing a value_type
2696 //! is expensive and the value_type can be compared with a cheaper
2697 //! key type. Usually this key is part of the value_type.
2698 template<class KeyType, class KeyHasher, class KeyEqual>
2699 std::pair<const_iterator,const_iterator> equal_range
2700 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2701 {
2702 std::pair<siterator, siterator> ret =
2703 this->priv_equal_range(key, hash_func, equal_func);
2704 return std::pair<const_iterator, const_iterator>
2705 ( const_iterator(ret.first, &this->get_bucket_value_traits())
2706 , const_iterator(ret.second, &this->get_bucket_value_traits()));
2707 }
2708
2709 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2710
2711 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2712 //! appropriate type. Otherwise the behavior is undefined.
2713 //!
2714 //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
2715 //! that points to the value
2716 //!
2717 //! <b>Complexity</b>: Constant.
2718 //!
2719 //! <b>Throws</b>: If the internal hash function throws.
2720 iterator iterator_to(reference value);
2721
2722 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2723 //! appropriate type. Otherwise the behavior is undefined.
2724 //!
2725 //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
2726 //! unordered_set that points to the value
2727 //!
2728 //! <b>Complexity</b>: Constant.
2729 //!
2730 //! <b>Throws</b>: If the internal hash function throws.
2731 const_iterator iterator_to(const_reference value) const;
2732
2733 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2734 //! appropriate type. Otherwise the behavior is undefined.
2735 //!
2736 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2737 //! that points to the value
2738 //!
2739 //! <b>Complexity</b>: Constant.
2740 //!
2741 //! <b>Throws</b>: Nothing.
2742 //!
2743 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2744 //! is stateless.
2745 static local_iterator s_local_iterator_to(reference value);
2746
2747 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2748 //! appropriate type. Otherwise the behavior is undefined.
2749 //!
2750 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2751 //! the unordered_set that points to the value
2752 //!
2753 //! <b>Complexity</b>: Constant.
2754 //!
2755 //! <b>Throws</b>: Nothing.
2756 //!
2757 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2758 //! is stateless.
2759 static const_local_iterator s_local_iterator_to(const_reference value);
2760
2761 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2762 //! appropriate type. Otherwise the behavior is undefined.
2763 //!
2764 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2765 //! that points to the value
2766 //!
2767 //! <b>Complexity</b>: Constant.
2768 //!
2769 //! <b>Throws</b>: Nothing.
2770 local_iterator local_iterator_to(reference value);
2771
2772 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2773 //! appropriate type. Otherwise the behavior is undefined.
2774 //!
2775 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2776 //! the unordered_set that points to the value
2777 //!
2778 //! <b>Complexity</b>: Constant.
2779 //!
2780 //! <b>Throws</b>: Nothing.
2781 const_local_iterator local_iterator_to(const_reference value) const;
2782
2783 //! <b>Effects</b>: Returns the number of buckets passed in the constructor
2784 //! or the last rehash function.
2785 //!
2786 //! <b>Complexity</b>: Constant.
2787 //!
2788 //! <b>Throws</b>: Nothing.
2789 size_type bucket_count() const;
2790
2791 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2792 //!
2793 //! <b>Effects</b>: Returns the number of elements in the nth bucket.
2794 //!
2795 //! <b>Complexity</b>: Constant.
2796 //!
2797 //! <b>Throws</b>: Nothing.
2798 size_type bucket_size(size_type n) const;
2799 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2800
2801 //! <b>Effects</b>: Returns the index of the bucket in which elements
2802 //! with keys equivalent to k would be found, if any such element existed.
2803 //!
2804 //! <b>Complexity</b>: Constant.
2805 //!
2806 //! <b>Throws</b>: If the hash functor throws.
2807 //!
2808 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2809 BOOST_INTRUSIVE_FORCEINLINE size_type bucket(const key_type& k) const
2810 { return this->bucket(k, this->priv_hasher()); }
2811
2812 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2813 //! the same hash values as the stored hasher. The difference is that
2814 //! "hash_func" hashes the given key instead of the value_type.
2815 //!
2816 //! <b>Effects</b>: Returns the index of the bucket in which elements
2817 //! with keys equivalent to k would be found, if any such element existed.
2818 //!
2819 //! <b>Complexity</b>: Constant.
2820 //!
2821 //! <b>Throws</b>: If hash_func throws.
2822 //!
2823 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2824 template<class KeyType, class KeyHasher>
2825 BOOST_INTRUSIVE_FORCEINLINE size_type bucket(const KeyType& k, KeyHasher hash_func) const
2826 { return this->priv_hash_to_bucket(hash_func(k)); }
2827
2828 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2829 //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
2830 //! or the last rehash function.
2831 //!
2832 //! <b>Complexity</b>: Constant.
2833 //!
2834 //! <b>Throws</b>: Nothing.
2835 bucket_ptr bucket_pointer() const;
2836
2837 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2838 //!
2839 //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
2840 //! of the sequence stored in the bucket n.
2841 //!
2842 //! <b>Complexity</b>: Constant.
2843 //!
2844 //! <b>Throws</b>: Nothing.
2845 //!
2846 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2847 //! containing all of the elements in the nth bucket.
2848 local_iterator begin(size_type n);
2849
2850 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2851 //!
2852 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2853 //! of the sequence stored in the bucket n.
2854 //!
2855 //! <b>Complexity</b>: Constant.
2856 //!
2857 //! <b>Throws</b>: Nothing.
2858 //!
2859 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2860 //! containing all of the elements in the nth bucket.
2861 const_local_iterator begin(size_type n) const;
2862
2863 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2864 //!
2865 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2866 //! of the sequence stored in the bucket n.
2867 //!
2868 //! <b>Complexity</b>: Constant.
2869 //!
2870 //! <b>Throws</b>: Nothing.
2871 //!
2872 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2873 //! containing all of the elements in the nth bucket.
2874 const_local_iterator cbegin(size_type n) const;
2875
2876 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2877 //!
2878 //! <b>Effects</b>: Returns a local_iterator pointing to the end
2879 //! of the sequence stored in the bucket n.
2880 //!
2881 //! <b>Complexity</b>: Constant.
2882 //!
2883 //! <b>Throws</b>: Nothing.
2884 //!
2885 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2886 //! containing all of the elements in the nth bucket.
2887 local_iterator end(size_type n);
2888
2889 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2890 //!
2891 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2892 //! of the sequence stored in the bucket n.
2893 //!
2894 //! <b>Complexity</b>: Constant.
2895 //!
2896 //! <b>Throws</b>: Nothing.
2897 //!
2898 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2899 //! containing all of the elements in the nth bucket.
2900 const_local_iterator end(size_type n) const;
2901
2902 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2903 //!
2904 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2905 //! of the sequence stored in the bucket n.
2906 //!
2907 //! <b>Complexity</b>: Constant.
2908 //!
2909 //! <b>Throws</b>: Nothing.
2910 //!
2911 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2912 //! containing all of the elements in the nth bucket.
2913 const_local_iterator cend(size_type n) const;
2914 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2915
2916 //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
2917 //! or the same as the old bucket array with a different length. new_size is the length of the
2918 //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
2919 //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
2920 //! 'new_bucket_traits' copy constructor should not throw.
2921 //!
2922 //! <b>Effects</b>:
2923 //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is false,
2924 //! unlinks values from the old bucket and inserts then in the new one according
2925 //! to the hash value of values.
2926 //!
2927 //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is true,
2928 //! the implementations avoids moving values as much as possible.
2929 //!
2930 //! Bucket traits hold by *this is assigned from new_bucket_traits.
2931 //! If the container is configured as incremental<>, the split bucket is set
2932 //! to the new bucket_count().
2933 //!
2934 //! If store_hash option is true, this method does not use the hash function.
2935 //! If false, the implementation tries to minimize calls to the hash function
2936 //! (e.g. once for equivalent values if optimize_multikey<true> is true).
2937 //!
2938 //! If rehash is successful updates the internal bucket_traits with new_bucket_traits.
2939 //!
2940 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
2941 //!
2942 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
2943 BOOST_INTRUSIVE_FORCEINLINE void rehash(const bucket_traits &new_bucket_traits)
2944 { this->rehash_impl(new_bucket_traits, false); }
2945
2946 //! <b>Note</b>: This function is used when keys from inserted elements are changed
2947 //! (e.g. a language change when key is a string) but uniqueness and hash properties are
2948 //! preserved so a fast full rehash recovers invariants for *this without extracting and
2949 //! reinserting all elements again.
2950 //!
2951 //! <b>Requires</b>: Calls produced to the hash function should not alter the value uniqueness
2952 //! properties of already inserted elements. If hasher(key1) == hasher(key2) was true when
2953 //! elements were inserted, it shall be true during calls produced in the execution of this function.
2954 //!
2955 //! key_equal is not called inside this function so it is assumed that key_equal(value1, value2)
2956 //! should produce the same results as before for inserted elements.
2957 //!
2958 //! <b>Effects</b>: Reprocesses all values hold by *this, recalculating their hash values
2959 //! and redistributing them though the buckets.
2960 //!
2961 //! If store_hash option is true, this method uses the hash function and updates the stored hash value.
2962 //!
2963 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
2964 //!
2965 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
2966 BOOST_INTRUSIVE_FORCEINLINE void full_rehash()
2967 { this->rehash_impl(this->priv_bucket_traits(), true); }
2968
2969 //! <b>Requires</b>:
2970 //!
2971 //! <b>Effects</b>:
2972 //!
2973 //! <b>Complexity</b>:
2974 //!
2975 //! <b>Throws</b>:
2976 //!
2977 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2978 bool incremental_rehash(bool grow = true)
2979 {
2980 //This function is only available for containers with incremental hashing
2981 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
2982 const size_type split_idx = this->priv_split_traits().get_size();
2983 const size_type bucket_cnt = this->bucket_count();
2984 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
2985 bool ret = false;
2986
2987 if(grow){
2988 //Test if the split variable can be changed
2989 if((ret = split_idx < bucket_cnt)){
2990 const size_type bucket_to_rehash = split_idx - bucket_cnt/2;
2991 bucket_type &old_bucket = buck_ptr[bucket_to_rehash];
2992 this->priv_split_traits().increment();
2993
2994 //Anti-exception stuff: if an exception is thrown while
2995 //moving elements from old_bucket to the target bucket, all moved
2996 //elements are moved back to the original one.
2997 detail::incremental_rehash_rollback<bucket_type, split_traits> rollback
2998 ( buck_ptr[split_idx], old_bucket, this->priv_split_traits());
2999 for( siterator before_i(old_bucket.before_begin()), i(old_bucket.begin()), end_sit(old_bucket.end())
3000 ; i != end_sit; i = before_i, ++i){
3001 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3002 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
3003 const size_type new_n = this->priv_hash_to_bucket(hash_value);
3004 siterator const last = (priv_last_in_group)(i);
3005 if(new_n == bucket_to_rehash){
3006 before_i = last;
3007 }
3008 else{
3009 bucket_type &new_b = buck_ptr[new_n];
3010 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
3011 }
3012 }
3013 rollback.release();
3014 this->priv_erasure_update_cache();
3015 }
3016 }
3017 else if((ret = split_idx > bucket_cnt/2)){ //!grow
3018 const size_type target_bucket_num = split_idx - 1 - bucket_cnt/2;
3019 bucket_type &target_bucket = buck_ptr[target_bucket_num];
3020 bucket_type &source_bucket = buck_ptr[split_idx-1];
3021 target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket);
3022 this->priv_split_traits().decrement();
3023 this->priv_insertion_update_cache(target_bucket_num);
3024 }
3025 return ret;
3026 }
3027
3028 //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
3029 //! this->bucket_count()/2 or this->bucket_count()*2, or
3030 //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
3031 //! and does nothing.
3032 //!
3033 //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
3034 //! and transfers all the objects from old buckets to the new ones.
3035 //!
3036 //! <b>Complexity</b>: Linear to size().
3037 //!
3038 //! <b>Throws</b>: Nothing
3039 //!
3040 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
3041 bool incremental_rehash(const bucket_traits &new_bucket_traits)
3042 {
3043 //This function is only available for containers with incremental hashing
3044 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
3045 size_type const new_bucket_traits_size = new_bucket_traits.bucket_count();
3046 size_type const cur_bucket_traits = this->bucket_count();
3047 const size_type split_idx = this->split_count();
3048
3049 //Test new bucket size is consistent with internal bucket size and split count
3050 if(new_bucket_traits_size/2 == cur_bucket_traits){
3051 if(!(split_idx >= cur_bucket_traits))
3052 return false;
3053 }
3054 else if(new_bucket_traits_size == cur_bucket_traits/2){
3055 if(!(split_idx <= new_bucket_traits_size))
3056 return false;
3057 }
3058 else{
3059 return false;
3060 }
3061
3062 const size_type ini_n = this->priv_get_cache_bucket_num();
3063 const bucket_ptr old_buckets = this->priv_bucket_pointer();
3064 this->priv_bucket_traits() = new_bucket_traits;
3065 if(new_bucket_traits.bucket_begin() != old_buckets){
3066 for(size_type n = ini_n; n < split_idx; ++n){
3067 bucket_type &new_bucket = new_bucket_traits.bucket_begin()[n];
3068 bucket_type &old_bucket = old_buckets[n];
3069 new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket);
3070 }
3071 //Put cache to safe position
3072 this->priv_initialize_cache();
3073 this->priv_insertion_update_cache(ini_n);
3074 }
3075 return true;
3076 }
3077
3078 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3079
3080 //! <b>Requires</b>: incremental<> option must be set
3081 //!
3082 //! <b>Effects</b>: returns the current split count
3083 //!
3084 //! <b>Complexity</b>: Constant
3085 //!
3086 //! <b>Throws</b>: Nothing
3087 size_type split_count() const;
3088
3089 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
3090 //! the container that is bigger or equal than n. This suggestion can be
3091 //! used to create bucket arrays with a size that will usually improve
3092 //! container's performance. If such value does not exist, the
3093 //! higher possible value is returned.
3094 //!
3095 //! <b>Complexity</b>: Amortized constant time.
3096 //!
3097 //! <b>Throws</b>: Nothing.
3098 static size_type suggested_upper_bucket_count(size_type n);
3099
3100 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
3101 //! the container that is smaller or equal than n. This suggestion can be
3102 //! used to create bucket arrays with a size that will usually improve
3103 //! container's performance. If such value does not exist, the
3104 //! lowest possible value is returned.
3105 //!
3106 //! <b>Complexity</b>: Amortized constant time.
3107 //!
3108 //! <b>Throws</b>: Nothing.
3109 static size_type suggested_lower_bucket_count(size_type n);
3110 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3111
3112
3113 friend bool operator==(const hashtable_impl &x, const hashtable_impl &y)
3114 {
3115 //Taken from N3068
3116 if(constant_time_size && x.size() != y.size()){
3117 return false;
3118 }
3119 for (const_iterator ix = x.cbegin(), ex = x.cend(); ix != ex; ++ix){
3120 std::pair<const_iterator, const_iterator> eqx(x.equal_range(key_of_value()(*ix))),
3121 eqy(y.equal_range(key_of_value()(*ix)));
3122 if (boost::intrusive::iterator_distance(eqx.first, eqx.second) !=
3123 boost::intrusive::iterator_distance(eqy.first, eqy.second) ||
3124 !(priv_algo_is_permutation)(eqx.first, eqx.second, eqy.first) ){
3125 return false;
3126 }
3127 ix = eqx.second;
3128 }
3129 return true;
3130 }
3131
3132 friend bool operator!=(const hashtable_impl &x, const hashtable_impl &y)
3133 { return !(x == y); }
3134
3135 friend bool operator<(const hashtable_impl &x, const hashtable_impl &y)
3136 { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
3137
3138 friend bool operator>(const hashtable_impl &x, const hashtable_impl &y)
3139 { return y < x; }
3140
3141 friend bool operator<=(const hashtable_impl &x, const hashtable_impl &y)
3142 { return !(y < x); }
3143
3144 friend bool operator>=(const hashtable_impl &x, const hashtable_impl &y)
3145 { return !(x < y); }
3146
3147 /// @cond
3148 BOOST_INTRUSIVE_FORCEINLINE void check() const {}
3149 private:
3150
3151 void rehash_impl(const bucket_traits &new_bucket_traits, bool do_full_rehash)
3152 {
3153 const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
3154 size_type new_bucket_count = new_bucket_traits.bucket_count();
3155 const bucket_ptr old_buckets = this->priv_bucket_pointer();
3156 size_type old_bucket_count = this->bucket_count();
3157
3158 //Check power of two bucket array if the option is activated
3159 BOOST_INTRUSIVE_INVARIANT_ASSERT
3160 (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
3161
3162 size_type n = this->priv_get_cache_bucket_num();
3163 const bool same_buffer = old_buckets == new_buckets;
3164 //If the new bucket length is a common factor
3165 //of the old one we can avoid hash calculations.
3166 const bool fast_shrink = (!do_full_rehash) && (!incremental) && (old_bucket_count >= new_bucket_count) &&
3167 (power_2_buckets || (old_bucket_count % new_bucket_count) == 0);
3168 //If we are shrinking the same bucket array and it's
3169 //is a fast shrink, just rehash the last nodes
3170 size_type new_first_bucket_num = new_bucket_count;
3171 if(same_buffer && fast_shrink && (n < new_bucket_count)){
3172 new_first_bucket_num = n;
3173 n = new_bucket_count;
3174 }
3175
3176 //Anti-exception stuff: they destroy the elements if something goes wrong.
3177 //If the source and destination buckets are the same, the second rollback function
3178 //is harmless, because all elements have been already unlinked and destroyed
3179 typedef detail::init_disposer<node_algorithms> NodeDisposer;
3180 typedef detail::exception_array_disposer<bucket_type, NodeDisposer, size_type> ArrayDisposer;
3181 NodeDisposer node_disp;
3182 ArrayDisposer rollback1(new_buckets[0], node_disp, new_bucket_count);
3183 ArrayDisposer rollback2(old_buckets[0], node_disp, old_bucket_count);
3184
3185 //Put size in a safe value for rollback exception
3186 size_type const size_backup = this->priv_size_traits().get_size();
3187 this->priv_size_traits().set_size(0);
3188 //Put cache to safe position
3189 this->priv_initialize_cache();
3190 this->priv_insertion_update_cache(size_type(0u));
3191
3192 //Iterate through nodes
3193 for(; n < old_bucket_count; ++n){
3194 bucket_type &old_bucket = old_buckets[n];
3195 if(!fast_shrink){
3196 for( siterator before_i(old_bucket.before_begin()), i(old_bucket.begin()), end_sit(old_bucket.end())
3197 ; i != end_sit
3198 ; i = before_i, ++i){
3199
3200 //First obtain hash value (and store it if do_full_rehash)
3201 std::size_t hash_value;
3202 if(do_full_rehash){
3203 value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3204 hash_value = this->priv_hasher()(key_of_value()(v));
3205 node_functions_t::store_hash(pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(v)), hash_value, store_hash_t());
3206 }
3207 else{
3208 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3209 hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
3210 }
3211
3212 //Now calculate the new bucket position
3213 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>
3214 (hash_value, new_bucket_count, new_bucket_count);
3215
3216 //Update first used bucket cache
3217 if(cache_begin && new_n < new_first_bucket_num)
3218 new_first_bucket_num = new_n;
3219
3220 //If the target bucket is new, transfer the whole group
3221 siterator const last = (priv_last_in_group)(i);
3222
3223 if(same_buffer && new_n == n){
3224 before_i = last;
3225 }
3226 else{
3227 bucket_type &new_b = new_buckets[new_n];
3228 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
3229 }
3230 }
3231 }
3232 else{
3233 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(n, new_bucket_count, new_bucket_count);
3234 if(cache_begin && new_n < new_first_bucket_num)
3235 new_first_bucket_num = new_n;
3236 bucket_type &new_b = new_buckets[new_n];
3237 new_b.splice_after( new_b.before_begin()
3238 , old_bucket
3239 , old_bucket.before_begin()
3240 , bucket_plus_vtraits_t::priv_get_last(old_bucket, optimize_multikey_t()));
3241 }
3242 }
3243
3244 this->priv_size_traits().set_size(size_backup);
3245 this->priv_split_traits().set_size(new_bucket_count);
3246 if(&new_bucket_traits != &this->priv_bucket_traits()){
3247 this->priv_bucket_traits() = new_bucket_traits;
3248 }
3249 this->priv_initialize_cache();
3250 this->priv_insertion_update_cache(new_first_bucket_num);
3251 rollback1.release();
3252 rollback2.release();
3253 }
3254
3255 template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
3256 void priv_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
3257 {
3258 this->clear_and_dispose(disposer);
3259 if(!constant_time_size || !src.empty()){
3260 const size_type src_bucket_count = src.bucket_count();
3261 const size_type dst_bucket_count = this->bucket_count();
3262 //Check power of two bucket array if the option is activated
3263 BOOST_INTRUSIVE_INVARIANT_ASSERT
3264 (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
3265 BOOST_INTRUSIVE_INVARIANT_ASSERT
3266 (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
3267 //If src bucket count is bigger or equal, structural copy is possible
3268 const bool structural_copy = (!incremental) && (src_bucket_count >= dst_bucket_count) &&
3269 (power_2_buckets || (src_bucket_count % dst_bucket_count) == 0);
3270 if(structural_copy){
3271 this->priv_structural_clone_from(src, cloner, disposer);
3272 }
3273 else{
3274 //Unlike previous cloning algorithm, this can throw
3275 //if cloner, hasher or comparison functor throw
3276 typedef typename detail::if_c< detail::is_const<MaybeConstHashtableImpl>::value
3277 , typename MaybeConstHashtableImpl::const_iterator
3278 , typename MaybeConstHashtableImpl::iterator
3279 >::type clone_iterator;
3280 clone_iterator b(src.begin()), e(src.end());
3281 detail::exception_disposer<hashtable_impl, Disposer> rollback(*this, disposer);
3282 for(; b != e; ++b){
3283 //No need to check for duplicates and insert it in the first position
3284 //as this is an unordered container. So use minimal insertion code
3285 std::size_t const hash_to_store = this->priv_stored_or_compute_hash(*b, store_hash_t());;
3286 size_type const bucket_number = this->priv_hash_to_bucket(hash_to_store);
3287 typedef typename detail::if_c
3288 <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
3289 reference_type r = *b;
3290 this->priv_clone_front_in_bucket<reference_type>(bucket_number, r, hash_to_store, cloner);
3291 }
3292 rollback.release();
3293 }
3294 }
3295 }
3296
3297 template<class ValueReference, class Cloner>
3298 void priv_clone_front_in_bucket( size_type const bucket_number
3299 , typename detail::identity<ValueReference>::type src_ref
3300 , std::size_t const hash_to_store, Cloner cloner)
3301 {
3302 //No need to check for duplicates and insert it in the first position
3303 //as this is an unordered container. So use minimal insertion code
3304 //std::size_t const hash_value = this->priv_stored_or_compute_hash(src_ref, store_hash_t());;
3305 //size_type const bucket_number = this->priv_hash_to_bucket(hash_value);
3306 bucket_type &cur_bucket = this->priv_bucket_pointer()[bucket_number];
3307 siterator const prev(cur_bucket.before_begin());
3308 //Just check if the cloned node is equal to the first inserted value in the new bucket
3309 //as equal src values were contiguous and they should be already inserted in the
3310 //destination bucket.
3311 bool const next_is_in_group = optimize_multikey && !cur_bucket.empty() &&
3312 this->priv_equal()( key_of_value()(src_ref)
3313 , key_of_value()(this->priv_value_from_slist_node((++siterator(prev)).pointed_node())));
3314 this->priv_insert_equal_after_find(*cloner(src_ref), bucket_number, hash_to_store, prev, next_is_in_group);
3315 }
3316
3317 template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
3318 void priv_structural_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
3319 {
3320 //First clone the first ones
3321 const size_type src_bucket_count = src.bucket_count();
3322 const size_type dst_bucket_count = this->bucket_count();
3323 const bucket_ptr src_buckets = src.priv_bucket_pointer();
3324 const bucket_ptr dst_buckets = this->priv_bucket_pointer();
3325 size_type constructed = 0;
3326 typedef node_cast_adaptor< detail::node_disposer<Disposer, value_traits, CircularSListAlgorithms>
3327 , slist_node_ptr, node_ptr > NodeDisposer;
3328 NodeDisposer node_disp(disposer, &this->priv_value_traits());
3329
3330 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
3331 rollback(dst_buckets[0], node_disp, constructed);
3332 //Now insert the remaining ones using the modulo trick
3333 for( //"constructed" already initialized
3334 ; constructed < src_bucket_count
3335 ; ++constructed){
3336 //Since incremental hashing can't be structurally copied, avoid hash_to_bucket_split
3337 const std::size_t new_n = detail::hash_to_bucket(constructed, dst_bucket_count, detail::bool_<power_2_buckets>());
3338 bucket_type &src_b = src_buckets[constructed];
3339 for( siterator b(src_b.begin()), e(src_b.end()); b != e; ++b){
3340 slist_node_ptr const n(b.pointed_node());
3341 typedef typename detail::if_c
3342 <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
3343 reference_type r = this->priv_value_from_slist_node(n);
3344 this->priv_clone_front_in_bucket<reference_type>
3345 (new_n, r, this->priv_stored_hash(n, store_hash_t()), cloner);
3346 }
3347 }
3348 this->priv_hasher() = src.priv_hasher();
3349 this->priv_equal() = src.priv_equal();
3350 rollback.release();
3351 this->priv_size_traits().set_size(src.priv_size_traits().get_size());
3352 this->priv_split_traits().set_size(dst_bucket_count);
3353 this->priv_insertion_update_cache(0u);
3354 this->priv_erasure_update_cache();
3355 }
3356
3357 std::size_t priv_hash_to_bucket(std::size_t hash_value) const
3358 {
3359 return detail::hash_to_bucket_split<power_2_buckets, incremental>
3360 (hash_value, this->priv_bucket_traits().bucket_count(), this->priv_split_traits().get_size());
3361 }
3362
3363 iterator priv_insert_equal_after_find(reference value, size_type bucket_num, std::size_t hash_value, siterator prev, bool const next_is_in_group)
3364 {
3365 //Now store hash if needed
3366 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
3367 node_functions_t::store_hash(n, hash_value, store_hash_t());
3368 //Checks for some modes
3369 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(n));
3370 //Shortcut to optimize_multikey cases
3371 group_functions_t::insert_in_group
3372 ( next_is_in_group ? detail::dcast_bucket_ptr<node>((++siterator(prev)).pointed_node()) : n
3373 , n, optimize_multikey_t());
3374 //Update cache and increment size if needed
3375 this->priv_insertion_update_cache(bucket_num);
3376 this->priv_size_traits().increment();
3377 //Insert the element in the bucket after it
3378 return iterator(bucket_type::s_insert_after(prev, *n), &this->get_bucket_value_traits());
3379 }
3380
3381 template<class KeyType, class KeyHasher, class KeyEqual>
3382 siterator priv_find //In case it is not found previt is bucket.before_begin()
3383 ( const KeyType &key, KeyHasher hash_func
3384 , KeyEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
3385 {
3386 h = hash_func(key);
3387 return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt);
3388 }
3389
3390 template<class KeyType, class KeyEqual>
3391 bool priv_is_value_equal_to_key(const value_type &v, const std::size_t h, const KeyType &key, KeyEqual equal_func) const
3392 {
3393 (void)h;
3394 return (!compare_hash || this->priv_stored_or_compute_hash(v, store_hash_t()) == h) && equal_func(key, key_of_value()(v));
3395 }
3396
3397 //return previous iterator to the next equal range group in case
3398 static siterator priv_last_in_group(const siterator &it_first_in_group)
3399 {
3400 return bucket_type::s_iterator_to
3401 (*group_functions_t::get_last_in_group
3402 (detail::dcast_bucket_ptr<node>(it_first_in_group.pointed_node()), optimize_multikey_t()));
3403 }
3404
3405 template<class KeyType, class KeyEqual>
3406 siterator priv_find_with_hash //In case it is not found previt is bucket.before_begin()
3407 ( const KeyType &key, KeyEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const
3408 {
3409 bucket_number = this->priv_hash_to_bucket(h);
3410 bucket_type &b = this->priv_bucket_pointer()[bucket_number];
3411 previt = b.before_begin();
3412 siterator it = previt;
3413 siterator const endit = b.end();
3414
3415 while(++it != endit){
3416 if(this->priv_is_value_equal_to_key(this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func)){
3417 return it;
3418 }
3419 previt = it = (priv_last_in_group)(it);
3420 }
3421 previt = b.before_begin();
3422 return this->priv_invalid_local_it();
3423 }
3424
3425 template<class KeyType, class KeyHasher, class KeyEqual>
3426 std::pair<siterator, siterator> priv_local_equal_range
3427 ( const KeyType &key
3428 , KeyHasher hash_func
3429 , KeyEqual equal_func
3430 , size_type &found_bucket
3431 , size_type &cnt) const
3432 {
3433 size_type internal_cnt = 0;
3434 //Let's see if the element is present
3435
3436 siterator prev;
3437 size_type n_bucket;
3438 std::size_t h;
3439 std::pair<siterator, siterator> to_return
3440 ( this->priv_find(key, hash_func, equal_func, n_bucket, h, prev)
3441 , this->priv_invalid_local_it());
3442
3443 if(to_return.first != to_return.second){
3444 found_bucket = n_bucket;
3445 //If it's present, find the first that it's not equal in
3446 //the same bucket
3447 bucket_type &b = this->priv_bucket_pointer()[n_bucket];
3448 siterator it = to_return.first;
3449 ++internal_cnt; //At least one is found
3450 if(optimize_multikey){
3451 to_return.second = ++(priv_last_in_group)(it);
3452 internal_cnt += boost::intrusive::iterator_distance(++it, to_return.second);
3453 }
3454 else{
3455 siterator const bend = b.end();
3456 while(++it != bend &&
3457 this->priv_is_value_equal_to_key(this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func)){
3458 ++internal_cnt;
3459 }
3460 to_return.second = it;
3461 }
3462 }
3463 cnt = internal_cnt;
3464 return to_return;
3465 }
3466
3467 template<class KeyType, class KeyHasher, class KeyEqual>
3468 std::pair<siterator, siterator> priv_equal_range
3469 ( const KeyType &key
3470 , KeyHasher hash_func
3471 , KeyEqual equal_func) const
3472 {
3473 size_type n_bucket;
3474 size_type cnt;
3475
3476 //Let's see if the element is present
3477 std::pair<siterator, siterator> to_return
3478 (this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt));
3479 //If not, find the next element as ".second" if ".second" local iterator
3480 //is not pointing to an element.
3481 bucket_ptr const bp = this->priv_bucket_pointer();
3482 if(to_return.first != to_return.second &&
3483 to_return.second == bp[n_bucket].end()){
3484 to_return.second = this->priv_invalid_local_it();
3485 ++n_bucket;
3486 for( const size_type max_bucket = this->bucket_count()
3487 ; n_bucket != max_bucket
3488 ; ++n_bucket){
3489 bucket_type &b = bp[n_bucket];
3490 if(!b.empty()){
3491 to_return.second = b.begin();
3492 break;
3493 }
3494 }
3495 }
3496 return to_return;
3497 }
3498
3499 std::size_t priv_get_bucket_num(siterator it)
3500 { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); }
3501
3502 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) //store_hash
3503 {
3504 return this->priv_hash_to_bucket
3505 (this->priv_stored_hash(it.pointed_node(), store_hash_t()));
3506 }
3507
3508 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) //NO store_hash
3509 { return this->priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); }
3510
3511 static siterator priv_get_previous(bucket_type &b, siterator i)
3512 { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); }
3513
3514 /// @endcond
3515 };
3516
3517 /// @cond
3518 template < class T
3519 , bool UniqueKeys
3520 , class PackedOptions
3521 >
3522 struct make_bucket_traits
3523 {
3524 //Real value traits must be calculated from options
3525 typedef typename detail::get_value_traits
3526 <T, typename PackedOptions::proto_value_traits>::type value_traits;
3527
3528 typedef typename PackedOptions::bucket_traits specified_bucket_traits;
3529
3530 //Real bucket traits must be calculated from options and calculated value_traits
3531 typedef typename get_slist_impl
3532 <typename reduced_slist_node_traits
3533 <typename value_traits::node_traits>::type
3534 >::type slist_impl;
3535
3536 typedef typename
3537 detail::if_c< detail::is_same
3538 < specified_bucket_traits
3539 , default_bucket_traits
3540 >::value
3541 , bucket_traits_impl<slist_impl>
3542 , specified_bucket_traits
3543 >::type type;
3544 };
3545 /// @endcond
3546
3547 //! Helper metafunction to define a \c hashtable that yields to the same type when the
3548 //! same options (either explicitly or implicitly) are used.
3549 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3550 template<class T, class ...Options>
3551 #else
3552 template<class T, class O1 = void, class O2 = void
3553 , class O3 = void, class O4 = void
3554 , class O5 = void, class O6 = void
3555 , class O7 = void, class O8 = void
3556 , class O9 = void, class O10= void
3557 >
3558 #endif
3559 struct make_hashtable
3560 {
3561 /// @cond
3562 typedef typename pack_options
3563 < hashtable_defaults,
3564 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3565 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3566 #else
3567 Options...
3568 #endif
3569 >::type packed_options;
3570
3571 typedef typename detail::get_value_traits
3572 <T, typename packed_options::proto_value_traits>::type value_traits;
3573
3574 typedef typename make_bucket_traits
3575 <T, false, packed_options>::type bucket_traits;
3576
3577 typedef hashtable_impl
3578 < value_traits
3579 , typename packed_options::key_of_value
3580 , typename packed_options::hash
3581 , typename packed_options::equal
3582 , bucket_traits
3583 , typename packed_options::size_type
3584 , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
3585 |(std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
3586 |(std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
3587 |(std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
3588 |(std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
3589 |(std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
3590 > implementation_defined;
3591
3592 /// @endcond
3593 typedef implementation_defined type;
3594 };
3595
3596 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3597
3598 #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3599 template<class T, class ...Options>
3600 #else
3601 template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
3602 #endif
3603 class hashtable
3604 : public make_hashtable<T,
3605 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3606 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3607 #else
3608 Options...
3609 #endif
3610 >::type
3611 {
3612 typedef typename make_hashtable<T,
3613 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3614 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3615 #else
3616 Options...
3617 #endif
3618 >::type Base;
3619 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
3620
3621 public:
3622 typedef typename Base::value_traits value_traits;
3623 typedef typename Base::iterator iterator;
3624 typedef typename Base::const_iterator const_iterator;
3625 typedef typename Base::bucket_ptr bucket_ptr;
3626 typedef typename Base::size_type size_type;
3627 typedef typename Base::hasher hasher;
3628 typedef typename Base::bucket_traits bucket_traits;
3629 typedef typename Base::key_equal key_equal;
3630
3631 //Assert if passed value traits are compatible with the type
3632 BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
3633
3634 BOOST_INTRUSIVE_FORCEINLINE explicit hashtable ( const bucket_traits &b_traits
3635 , const hasher & hash_func = hasher()
3636 , const key_equal &equal_func = key_equal()
3637 , const value_traits &v_traits = value_traits())
3638 : Base(b_traits, hash_func, equal_func, v_traits)
3639 {}
3640
3641 BOOST_INTRUSIVE_FORCEINLINE hashtable(BOOST_RV_REF(hashtable) x)
3642 : Base(BOOST_MOVE_BASE(Base, x))
3643 {}
3644
3645 BOOST_INTRUSIVE_FORCEINLINE hashtable& operator=(BOOST_RV_REF(hashtable) x)
3646 { return static_cast<hashtable&>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
3647
3648 template <class Cloner, class Disposer>
3649 BOOST_INTRUSIVE_FORCEINLINE void clone_from(const hashtable &src, Cloner cloner, Disposer disposer)
3650 { Base::clone_from(src, cloner, disposer); }
3651
3652 template <class Cloner, class Disposer>
3653 BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(hashtable) src, Cloner cloner, Disposer disposer)
3654 { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
3655 };
3656
3657 #endif
3658
3659 } //namespace intrusive
3660 } //namespace boost
3661
3662 #include <boost/intrusive/detail/config_end.hpp>
3663
3664 #endif //BOOST_INTRUSIVE_HASHTABLE_HPP