1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2015
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
13 #define BOOST_INTRUSIVE_HASHTABLE_HPP
15 #include <boost/intrusive/detail/config_begin.hpp>
16 #include <boost/intrusive/intrusive_fwd.hpp>
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>
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>
38 #include <boost/container_hash/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>
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
50 #if defined(BOOST_HAS_PRAGMA_ONCE)
59 template<class InputIt, class T>
60 InputIt priv_algo_find(InputIt first, InputIt last, const T& value)
62 for (; first != last; ++first) {
63 if (*first == value) {
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)
74 typename boost::intrusive::iterator_traits<InputIt>::difference_type ret = 0;
75 for (; first != last; ++first) {
76 if (*first == value) {
83 template <class ForwardIterator1, class ForwardIterator2>
84 bool priv_algo_is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
87 boost::intrusive::iterator_traits<ForwardIterator2>::difference_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))
99 //Establish last2 assuming equal ranges by iterating over the
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.
107 distance_type matches = (priv_algo_count)(first2, last2, *scan);
108 if (0 == matches || (priv_algo_count)(scan, last1, *scan) != matches){
115 template<int Dummy = 0>
116 struct prime_list_holder
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_)
123 return n < std::size_t(SizeType(-1)) ? static_cast<SizeType>(n) : SizeType(-1);
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_)
129 return static_cast<SizeType>(n);
132 template <class SizeType> //sizeof(SizeType) > sizeof(std::size_t)
133 static SizeType suggested_upper_bucket_count_dispatch(SizeType n, detail::true_)
135 std::size_t const c = n > std::size_t(-1)
137 : suggested_upper_bucket_count_impl(static_cast<std::size_t>(n));
138 return static_cast<SizeType>(c);
141 template <class SizeType> //sizeof(SizeType) > sizeof(std::size_t)
142 static SizeType suggested_lower_bucket_count_dispatch(SizeType n, detail::true_)
144 std::size_t const c = n > std::size_t(-1)
146 : suggested_lower_bucket_count_impl(static_cast<std::size_t>(n));
147 return static_cast<SizeType>(c);
150 template <class SizeType>
151 static SizeType suggested_upper_bucket_count_dispatch(SizeType n, detail::false_)
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))>());
158 template <class SizeType>
159 static SizeType suggested_lower_bucket_count_dispatch(SizeType n, detail::false_)
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))>());
165 static const std::size_t prime_list[];
166 static const std::size_t prime_list_size;
168 static std::size_t suggested_lower_bucket_count_impl(std::size_t n)
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);
179 static std::size_t suggested_upper_bucket_count_impl(std::size_t n)
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);
190 template <class SizeType>
191 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_upper_bucket_count(SizeType n)
193 return (suggested_upper_bucket_count_dispatch)(n, detail::bool_<(sizeof(SizeType) > sizeof(std::size_t))>());
196 template <class SizeType>
197 static BOOST_INTRUSIVE_FORCEINLINE SizeType suggested_lower_bucket_count(SizeType n)
199 return (suggested_lower_bucket_count_dispatch)(n, detail::bool_<(sizeof(SizeType) > sizeof(std::size_t))>());
203 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
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)
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
252 BOOST_INTRUSIVE_PRIME_C(4294967291), BOOST_INTRUSIVE_PRIME_C(4294967295) //Upper limit, just in case
256 #undef BOOST_INTRUSIVE_PRIME_C
257 #undef BOOST_INTRUSIVE_64_BIT_SIZE_T
259 #endif //#if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
262 const std::size_t prime_list_holder<Dummy>::prime_list_size
263 = sizeof(prime_list)/sizeof(std::size_t);
265 struct hash_bool_flags
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;
277 template<class SupposedValueTraits>
278 struct get_slist_impl_from_supposed_value_traits
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
289 template<class SupposedValueTraits>
290 struct unordered_bucket_impl
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;
299 template<class SupposedValueTraits>
300 struct unordered_bucket_ptr_impl
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;
307 typedef typename pointer_traits
308 <node_ptr>::template rebind_pointer
309 < bucket_type >::type implementation_defined;
310 typedef implementation_defined type;
314 struct store_hash_is_true
317 struct two_or_three {yes_type _[2u + (unsigned)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)*2u;
324 struct optimize_multikey_is_true
327 struct two_or_three {yes_type _[2u + (unsigned)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)*2u;
333 struct insert_commit_data_impl
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)
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));
346 template<class NodeTraits>
347 struct group_functions
349 // A group is reverse-linked
351 // A is "first in group"
352 // C is "last in group"
353 // __________________
355 // | | | | | | <- Group links
358 // A|_| B|_| C|_| D|_|
360 // ^ | ^ | ^ | ^ V <- Bucket links
361 // _ _____| |_____| |______| |____| |
363 // ^________________________________|
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;
377 static slist_node_ptr get_bucket_before_begin
378 (slist_node_ptr bucket_beg, slist_node_ptr bucket_end, node_ptr p)
380 //First find the last node of p's group.
381 //This requires checking the first node of the next group or
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)){
388 nxt = node_traits::get_next(nxt);
391 //If we've reached the bucket node just return it.
392 if(bucket_beg <= nxt && nxt <= bucket_end){
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);
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);
409 static node_ptr get_prev_to_first_in_group(slist_node_ptr bucket_node, node_ptr first_in_group)
411 node_ptr nb = detail::dcast_bucket_ptr<node>(bucket_node);
413 while((n = node_traits::get_next(nb)) != first_in_group){
414 nb = group_traits::get_next(n); //go to last in group
419 static void erase_from_group(slist_node_ptr end_ptr, node_ptr to_erase_ptr, detail::true_)
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);
429 BOOST_INTRUSIVE_FORCEINLINE static void erase_from_group(slist_node_ptr, node_ptr, detail::false_)
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); }
435 BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_last_in_group(node_ptr n, detail::false_)
438 static node_ptr get_first_in_group(node_ptr n, detail::true_)
441 while(n == node_traits::get_next((ng = group_traits::get_next(n)))){
447 BOOST_INTRUSIVE_FORCEINLINE static node_ptr next_group_if_first_in_group(node_ptr ptr)
448 { return node_traits::get_next(group_traits::get_next(ptr)); }
450 BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_first_in_group(node_ptr n, detail::false_)
453 BOOST_INTRUSIVE_FORCEINLINE static void insert_in_group(node_ptr first_in_group, node_ptr n, true_)
454 { group_algorithms::link_after(first_in_group, n); }
456 static void insert_in_group(node_ptr, node_ptr, false_)
459 static node_ptr split_group(node_ptr const new_first_in_group)
461 node_ptr const first((get_first_in_group)(new_first_in_group, detail::true_()));
462 if(first != new_first_in_group){
463 node_ptr const last = group_traits::get_next(first);
464 group_traits::set_next(first, group_traits::get_next(new_first_in_group));
465 group_traits::set_next(new_first_in_group, last);
471 template<class BucketType, class SplitTraits>
472 class incremental_rehash_rollback
475 typedef BucketType bucket_type;
476 typedef SplitTraits split_traits;
478 incremental_rehash_rollback();
479 incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
480 incremental_rehash_rollback (const incremental_rehash_rollback &);
483 incremental_rehash_rollback
484 (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_tr)
485 : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
486 , split_traits_(split_tr), released_(false)
489 BOOST_INTRUSIVE_FORCEINLINE void release()
490 { released_ = true; }
492 ~incremental_rehash_rollback()
495 //If an exception is thrown, just put all moved nodes back in the old bucket
496 //and move back the split mark.
497 destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_);
498 split_traits_.decrement();
503 bucket_type &source_bucket_;
504 bucket_type &destiny_bucket_;
505 split_traits &split_traits_;
509 template<class NodeTraits>
510 struct node_functions
512 BOOST_INTRUSIVE_FORCEINLINE static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_)
513 { return NodeTraits::set_hash(p, h); }
515 BOOST_INTRUSIVE_FORCEINLINE static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_)
519 BOOST_INTRUSIVE_FORCEINLINE std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
520 { return hash_value % bucket_cnt; }
522 BOOST_INTRUSIVE_FORCEINLINE std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
523 { return hash_value & (bucket_cnt - 1); }
525 template<bool Power2Buckets, bool Incremental>
526 std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split)
528 std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
529 BOOST_IF_CONSTEXPR(Incremental)
530 bucket_number -= static_cast<std::size_t>(bucket_number >= split)*(bucket_cnt/2);
531 return bucket_number;
534 } //namespace detail {
536 //!This metafunction will obtain the type of a bucket
537 //!from the value_traits or hook option to be used with
539 template<class ValueTraitsOrHookOption>
540 struct unordered_bucket
541 : public detail::unordered_bucket_impl
542 <typename ValueTraitsOrHookOption::
543 template pack<empty>::proto_value_traits
547 //!This metafunction will obtain the type of a bucket pointer
548 //!from the value_traits or hook option to be used with
550 template<class ValueTraitsOrHookOption>
551 struct unordered_bucket_ptr
552 : public detail::unordered_bucket_ptr_impl
553 <typename ValueTraitsOrHookOption::
554 template pack<empty>::proto_value_traits
558 //!This metafunction will obtain the type of the default bucket traits
559 //!(when the user does not specify the bucket_traits<> option) from the
560 //!value_traits or hook option to be used with
562 template<class ValueTraitsOrHookOption>
563 struct unordered_default_bucket_traits
565 typedef typename ValueTraitsOrHookOption::
566 template pack<empty>::proto_value_traits supposed_value_traits;
567 typedef typename detail::
568 get_slist_impl_from_supposed_value_traits
569 <supposed_value_traits>::type slist_impl;
570 typedef bucket_traits_impl
571 <slist_impl> implementation_defined;
572 typedef implementation_defined type;
575 struct default_bucket_traits;
577 //hashtable default hook traits
578 struct default_hashtable_hook_applier
579 { template <class T> struct apply{ typedef typename T::default_hashtable_hook type; }; };
582 struct is_default_hook_tag<default_hashtable_hook_applier>
583 { static const bool value = true; };
585 struct hashtable_defaults
587 typedef default_hashtable_hook_applier proto_value_traits;
588 typedef std::size_t size_type;
589 typedef void key_of_value;
592 typedef default_bucket_traits bucket_traits;
593 static const bool constant_time_size = true;
594 static const bool power_2_buckets = false;
595 static const bool cache_begin = false;
596 static const bool compare_hash = false;
597 static const bool incremental = false;
600 template<class ValueTraits, bool IsConst>
601 struct downcast_node_to_value_t
602 : public detail::node_to_value<ValueTraits, IsConst>
604 typedef detail::node_to_value<ValueTraits, IsConst> base_t;
605 typedef typename base_t::result_type result_type;
606 typedef ValueTraits value_traits;
607 typedef typename get_slist_impl
608 <typename reduced_slist_node_traits
609 <typename value_traits::node_traits>::type
611 typedef typename detail::add_const_if_c
612 <typename slist_impl::node, IsConst>::type & first_argument_type;
613 typedef typename detail::add_const_if_c
614 < typename ValueTraits::node_traits::node
615 , IsConst>::type & intermediate_argument_type;
616 typedef typename pointer_traits
617 <typename ValueTraits::pointer>::
618 template rebind_pointer
619 <const ValueTraits>::type const_value_traits_ptr;
621 BOOST_INTRUSIVE_FORCEINLINE downcast_node_to_value_t(const_value_traits_ptr ptr)
625 BOOST_INTRUSIVE_FORCEINLINE result_type operator()(first_argument_type arg) const
626 { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
629 template<class F, class SlistNodePtr, class NodePtr>
630 struct node_cast_adaptor
631 //Use public inheritance to avoid MSVC bugs with closures
632 : public detail::ebo_functor_holder<F>
634 typedef detail::ebo_functor_holder<F> base_t;
636 typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
637 typedef typename pointer_traits<NodePtr>::element_type node;
639 template<class ConvertibleToF, class RealValuTraits>
640 BOOST_INTRUSIVE_FORCEINLINE node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
641 : base_t(base_t(c2f, traits))
644 BOOST_INTRUSIVE_FORCEINLINE typename base_t::node_ptr operator()(const slist_node &to_clone)
645 { return base_t::operator()(static_cast<const node &>(to_clone)); }
647 BOOST_INTRUSIVE_FORCEINLINE void operator()(SlistNodePtr to_clone)
649 base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
653 //bucket_plus_vtraits stores ValueTraits + BucketTraits
654 //this data is needed by iterators to obtain the
655 //value from the iterator and detect the bucket
656 template<class ValueTraits, class BucketTraits>
657 struct bucket_plus_vtraits
659 typedef BucketTraits bucket_traits;
660 typedef ValueTraits value_traits;
662 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
665 detail::get_slist_impl_from_supposed_value_traits
666 <value_traits>::type slist_impl;
667 typedef typename value_traits::node_traits node_traits;
668 typedef unordered_group_adapter<node_traits> group_traits;
669 typedef typename slist_impl::iterator siterator;
670 typedef bucket_impl<slist_impl> bucket_type;
671 typedef detail::group_functions<node_traits> group_functions_t;
672 typedef typename slist_impl::node_algorithms node_algorithms;
673 typedef typename slist_impl::node_ptr slist_node_ptr;
674 typedef typename node_traits::node_ptr node_ptr;
675 typedef typename node_traits::node node;
676 typedef typename value_traits::value_type value_type;
677 typedef typename value_traits::pointer pointer;
678 typedef typename value_traits::const_pointer const_pointer;
679 typedef typename pointer_traits<pointer>::reference reference;
680 typedef typename pointer_traits
681 <const_pointer>::reference const_reference;
682 typedef circular_slist_algorithms<group_traits> group_algorithms;
683 typedef typename pointer_traits
684 <typename value_traits::pointer>::
685 template rebind_pointer
686 <const value_traits>::type const_value_traits_ptr;
687 typedef typename pointer_traits
688 <typename value_traits::pointer>::
689 template rebind_pointer
690 <const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
691 typedef typename detail::unordered_bucket_ptr_impl
692 <value_traits>::type bucket_ptr;
694 template<class BucketTraitsType>
695 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
696 : data(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
699 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x)
700 { data.bucket_traits_ = x.data.bucket_traits_; return *this; }
702 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr priv_value_traits_ptr() const
703 { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
705 //bucket_value_traits
707 BOOST_INTRUSIVE_FORCEINLINE const bucket_plus_vtraits &get_bucket_value_traits() const
710 BOOST_INTRUSIVE_FORCEINLINE bucket_plus_vtraits &get_bucket_value_traits()
713 BOOST_INTRUSIVE_FORCEINLINE const_bucket_value_traits_ptr bucket_value_traits_ptr() const
714 { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
718 BOOST_INTRUSIVE_FORCEINLINE const value_traits &priv_value_traits() const
719 { return this->data; }
721 BOOST_INTRUSIVE_FORCEINLINE value_traits &priv_value_traits()
722 { return this->data; }
726 BOOST_INTRUSIVE_FORCEINLINE const bucket_traits &priv_bucket_traits() const
727 { return this->data.bucket_traits_; }
729 BOOST_INTRUSIVE_FORCEINLINE bucket_traits &priv_bucket_traits()
730 { return this->data.bucket_traits_; }
733 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_bucket_pointer() const BOOST_NOEXCEPT
734 { return this->priv_bucket_traits().bucket_begin(); }
736 std::size_t priv_bucket_count() const BOOST_NOEXCEPT
738 const std::size_t bc = this->priv_bucket_traits().bucket_count();
742 BOOST_INTRUSIVE_FORCEINLINE bucket_type &priv_bucket(std::size_t n) const BOOST_NOEXCEPT
744 BOOST_INTRUSIVE_INVARIANT_ASSERT(n < this->priv_bucket_traits().bucket_count());
745 return priv_bucket_pointer()[std::ptrdiff_t(n)];
748 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_invalid_bucket() const
750 const bucket_traits &rbt = this->priv_bucket_traits();
751 return rbt.bucket_begin() + std::ptrdiff_t(rbt.bucket_count());
754 BOOST_INTRUSIVE_FORCEINLINE siterator priv_invalid_local_it() const
755 { return this->priv_bucket_traits().bucket_begin()->before_begin(); }
757 template<class NodeDisposer>
758 static std::size_t priv_erase_from_single_bucket(bucket_type &b, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::true_) //optimize multikey
761 siterator const sfirst(++siterator(sbefore_first));
763 node_ptr const nf = detail::dcast_bucket_ptr<node>(sfirst.pointed_node());
764 node_ptr const nl = detail::dcast_bucket_ptr<node>(slast.pointed_node());
765 node_ptr const ne = detail::dcast_bucket_ptr<node>(b.end().pointed_node());
767 if(group_functions_t::next_group_if_first_in_group(nf) != nf) {
768 // The node is at the beginning of a group.
770 group_functions_t::split_group(nl);
774 node_ptr const group1 = group_functions_t::split_group(nf);
776 node_ptr const group2 = group_functions_t::split_group(ne);
777 if(nf == group2) { //Both first and last in the same group
778 //so join group1 and group2
779 node_ptr const end1 = group_traits::get_next(group1);
780 node_ptr const end2 = group_traits::get_next(group2);
781 group_traits::set_next(group1, end2);
782 group_traits::set_next(group2, end1);
787 siterator it(++siterator(sbefore_first));
789 node_disposer((it++).pointed_node());
792 b.erase_after(sbefore_first, slast);
797 template<class NodeDisposer>
798 static std::size_t priv_erase_from_single_bucket(bucket_type &b, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::false_) //optimize multikey
801 siterator it(++siterator(sbefore_first));
803 node_disposer((it++).pointed_node());
806 b.erase_after(sbefore_first, slast);
810 template<class NodeDisposer>
811 static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::true_) //optimize multikey
813 node_ptr const ne(detail::dcast_bucket_ptr<node>(b.end().pointed_node()));
814 node_ptr n(detail::dcast_bucket_ptr<node>(i.pointed_node()));
815 node_ptr pos = node_traits::get_next(group_traits::get_next(n));
817 node_ptr nn(node_traits::get_next(n));
820 //Node is the first of the group
821 bn = group_functions_t::get_prev_to_first_in_group(ne, n);
823 //Unlink the rest of the group if it's not the last node of its group
824 if(nn != ne && group_traits::get_next(nn) == n){
825 group_algorithms::unlink_after(nn);
828 else if(nn != ne && group_traits::get_next(nn) == n){
829 //Node is not the end of the group
830 bn = group_traits::get_next(n);
831 group_algorithms::unlink_after(nn);
834 //Node is the end of the group
835 bn = group_traits::get_next(n);
836 node_ptr const x(group_algorithms::get_previous_node(n));
837 group_algorithms::unlink_after(x);
839 b.erase_after_and_dispose(bucket_type::s_iterator_to(*bn), node_disposer);
842 template<class NodeDisposer>
843 BOOST_INTRUSIVE_FORCEINLINE static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::false_) //optimize multikey
844 { b.erase_after_and_dispose(b.previous(i), node_disposer); }
846 template<class NodeDisposer, bool OptimizeMultikey>
847 std::size_t priv_erase_node_range( siterator const &before_first_it, std::size_t const first_bucket
848 , siterator const &last_it, std::size_t const last_bucket
849 , NodeDisposer node_disposer, detail::bool_<OptimizeMultikey> optimize_multikey_tag)
851 std::size_t num_erased(0);
852 siterator last_step_before_it;
853 if(first_bucket != last_bucket){
854 bucket_type *b = &this->priv_bucket(0);
855 num_erased += this->priv_erase_from_single_bucket
856 (b[first_bucket], before_first_it, b[first_bucket].end(), node_disposer, optimize_multikey_tag);
857 for(std::size_t i = 0, n = (last_bucket - first_bucket - 1); i != n; ++i){
858 num_erased += this->priv_erase_whole_bucket(b[first_bucket+i+1], node_disposer);
860 last_step_before_it = b[last_bucket].before_begin();
863 last_step_before_it = before_first_it;
865 num_erased += this->priv_erase_from_single_bucket
866 (this->priv_bucket(last_bucket), last_step_before_it, last_it, node_disposer, optimize_multikey_tag);
870 static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
872 //First find the last node of p's group.
873 //This requires checking the first node of the next group or
875 slist_node_ptr end_ptr(b.end().pointed_node());
876 node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr<node>(end_ptr)));
877 node_ptr last_node_group(possible_end);
879 while(end_ptr != possible_end){
880 last_node_group = group_traits::get_next(detail::dcast_bucket_ptr<node>(possible_end));
881 possible_end = node_traits::get_next(last_node_group);
883 return bucket_type::s_iterator_to(*last_node_group);
886 template<class NodeDisposer>
887 std::size_t priv_erase_whole_bucket(bucket_type &b, NodeDisposer node_disposer)
889 std::size_t num_erased = 0;
890 siterator b_begin(b.before_begin());
891 siterator nxt(b_begin);
893 siterator const end_sit(b.end());
894 while(nxt != end_sit){
895 //No need to init group links as we'll delete all bucket nodes
896 nxt = bucket_type::s_erase_after_and_dispose(b_begin, node_disposer);
902 BOOST_INTRUSIVE_FORCEINLINE static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
903 { return b.previous(b.end()); }
905 static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
907 node_ptr const elem(detail::dcast_bucket_ptr<node>(i.pointed_node()));
908 node_ptr const prev_in_group(group_traits::get_next(elem));
909 bool const first_in_group = node_traits::get_next(prev_in_group) != elem;
910 typename bucket_type::node &n = first_in_group
911 ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem)
912 : *group_traits::get_next(elem)
914 return bucket_type::s_iterator_to(n);
917 BOOST_INTRUSIVE_FORCEINLINE static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
918 { return b.previous(i); }
920 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey
922 const bucket_type &f = this->priv_bucket(0u);
923 const bucket_type &l = this->priv_bucket(this->priv_bucket_count() - 1u);
924 slist_node_ptr bb = group_functions_t::get_bucket_before_begin
925 ( f.end().pointed_node()
926 , l.end().pointed_node()
927 , detail::dcast_bucket_ptr<node>(it.pointed_node()));
928 //Now get the bucket_impl from the iterator
929 const bucket_type &b = static_cast<const bucket_type&>
930 (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb)));
931 //Now just calculate the index b has in the bucket array
932 return static_cast<std::size_t>(&b - &f);
935 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey
937 const bucket_type &f = this->priv_bucket(0u);
938 const bucket_type &l = this->priv_bucket(this->priv_bucket_count() - 1u);
939 slist_node_ptr first_ptr(f.cend().pointed_node())
940 , last_ptr (l.cend().pointed_node());
942 //The end node is embedded in the singly linked list:
943 //iterate until we reach it.
944 while(!(std::less_equal<slist_node_ptr>()(first_ptr, it.pointed_node()) &&
945 std::less_equal<slist_node_ptr>()(it.pointed_node(), last_ptr))){
948 //Now get the bucket_impl from the iterator
949 const bucket_type &b = static_cast<const bucket_type&>
950 (bucket_type::container_from_end_iterator(it));
952 //Now just calculate the index b has in the bucket array
953 return static_cast<std::size_t>(&b - &f);
956 BOOST_INTRUSIVE_FORCEINLINE static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash
957 { return node_traits::get_hash(detail::dcast_bucket_ptr<node>(n)); }
959 BOOST_INTRUSIVE_FORCEINLINE static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash
960 { return std::size_t(-1); }
962 BOOST_INTRUSIVE_FORCEINLINE node &priv_value_to_node(reference v)
963 { return *this->priv_value_traits().to_node_ptr(v); }
965 BOOST_INTRUSIVE_FORCEINLINE const node &priv_value_to_node(const_reference v) const
966 { return *this->priv_value_traits().to_node_ptr(v); }
968 BOOST_INTRUSIVE_FORCEINLINE reference priv_value_from_slist_node(slist_node_ptr n)
969 { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
971 BOOST_INTRUSIVE_FORCEINLINE const_reference priv_value_from_slist_node(slist_node_ptr n) const
972 { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
974 void priv_clear_buckets(const bucket_ptr buckets_ptr, const std::size_t bucket_cnt)
976 bucket_ptr buckets_it = buckets_ptr;
977 for(std::size_t bucket_i = 0; bucket_i != bucket_cnt; ++buckets_it, ++bucket_i){
978 BOOST_IF_CONSTEXPR(safemode_or_autounlink){
979 buckets_it->clear_and_dispose(detail::init_disposer<node_algorithms>());
987 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
988 { return node_traits::get_hash(this->priv_value_traits().to_node_ptr(v)); }
990 typedef hashtable_iterator<bucket_plus_vtraits, false> iterator;
991 typedef hashtable_iterator<bucket_plus_vtraits, true> const_iterator;
993 BOOST_INTRUSIVE_FORCEINLINE iterator end() BOOST_NOEXCEPT
994 { return iterator(this->priv_invalid_local_it(), 0); }
996 BOOST_INTRUSIVE_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT
997 { return this->cend(); }
999 BOOST_INTRUSIVE_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT
1000 { return const_iterator(this->priv_invalid_local_it(), 0); }
1003 struct data_type : public ValueTraits
1005 template<class BucketTraitsType>
1006 BOOST_INTRUSIVE_FORCEINLINE data_type(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
1007 : ValueTraits(val_traits), bucket_traits_(::boost::forward<BucketTraitsType>(b_traits))
1010 bucket_traits bucket_traits_;
1014 template<class Hash, class>
1021 struct get_hash<void, T>
1023 typedef ::boost::hash<T> type;
1026 template<class EqualTo, class>
1029 typedef EqualTo type;
1033 struct get_equal_to<void, T>
1035 typedef std::equal_to<T> type;
1038 template<class KeyOfValue, class T>
1039 struct get_hash_key_of_value
1041 typedef KeyOfValue type;
1045 struct get_hash_key_of_value<void, T>
1047 typedef ::boost::intrusive::detail::identity<T> type;
1050 template<class T, class VoidOrKeyOfValue>
1051 struct hash_key_types_base
1053 typedef typename get_hash_key_of_value
1054 < VoidOrKeyOfValue, T>::type key_of_value;
1055 typedef typename key_of_value::type key_type;
1058 template<class T, class VoidOrKeyOfValue, class VoidOrKeyHash>
1059 struct hash_key_hash
1062 , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
1066 template<class T, class VoidOrKeyOfValue, class VoidOrKeyEqual>
1067 struct hash_key_equal
1070 , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
1076 //Stores bucket_plus_vtraits plust the hash function
1077 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class BucketTraits>
1078 struct bucket_hash_t
1079 //Use public inheritance to avoid MSVC bugs with closures
1080 : public detail::ebo_functor_holder
1081 <typename hash_key_hash < typename bucket_plus_vtraits<ValueTraits,BucketTraits>::value_traits::value_type
1086 , bucket_plus_vtraits<ValueTraits, BucketTraits> //4
1088 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::value_traits value_traits;
1089 typedef typename value_traits::value_type value_type;
1090 typedef typename value_traits::node_traits node_traits;
1091 typedef hash_key_hash
1092 < value_type, VoidOrKeyOfValue, VoidOrKeyHash> hash_key_hash_t;
1093 typedef typename hash_key_hash_t::type hasher;
1094 typedef typename hash_key_types_base<value_type, VoidOrKeyOfValue>::key_of_value key_of_value;
1096 typedef BucketTraits bucket_traits;
1097 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
1098 typedef detail::ebo_functor_holder<hasher> base_t;
1100 template<class BucketTraitsType>
1101 BOOST_INTRUSIVE_FORCEINLINE bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h)
1102 : detail::ebo_functor_holder<hasher>(h), bucket_plus_vtraits_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
1105 BOOST_INTRUSIVE_FORCEINLINE const hasher &priv_hasher() const
1106 { return this->base_t::get(); }
1108 hasher &priv_hasher()
1109 { return this->base_t::get(); }
1111 using bucket_plus_vtraits_t::priv_stored_or_compute_hash; //For store_hash == true
1113 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
1114 { return this->priv_hasher()(key_of_value()(v)); }
1117 template<class ValueTraits, class BucketTraits, class VoidOrKeyOfValue, class VoidOrKeyEqual>
1118 struct hashtable_equal_holder
1120 typedef detail::ebo_functor_holder
1121 < typename hash_key_equal < typename bucket_plus_vtraits<ValueTraits, BucketTraits>::value_traits::value_type
1129 //bucket_hash_equal_t
1130 //Stores bucket_hash_t and the equality function when the first
1131 //non-empty bucket shall not be cached.
1132 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, bool>
1133 struct bucket_hash_equal_t
1134 //Use public inheritance to avoid MSVC bugs with closures
1135 : public bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> //3
1136 , public hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type //equal
1138 typedef typename hashtable_equal_holder
1139 <ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type equal_holder_t;
1140 typedef bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> bucket_hash_type;
1141 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1142 typedef ValueTraits value_traits;
1143 typedef typename equal_holder_t::functor_type key_equal;
1144 typedef typename bucket_hash_type::hasher hasher;
1145 typedef BucketTraits bucket_traits;
1146 typedef typename bucket_plus_vtraits_t::slist_impl slist_impl;
1147 typedef typename slist_impl::iterator siterator;
1148 typedef bucket_impl<slist_impl> bucket_type;
1149 typedef typename detail::unordered_bucket_ptr_impl<value_traits>::type bucket_ptr;
1151 template<class BucketTraitsType>
1152 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const key_equal &e)
1153 : bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
1157 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_get_cache()
1158 { return this->bucket_hash_type::priv_bucket_pointer(); }
1160 BOOST_INTRUSIVE_FORCEINLINE void priv_set_cache(bucket_ptr)
1163 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_get_cache_bucket_num()
1166 BOOST_INTRUSIVE_FORCEINLINE void priv_initialize_cache()
1169 BOOST_INTRUSIVE_FORCEINLINE void priv_swap_cache(bucket_hash_equal_t &)
1172 siterator priv_begin() const
1175 std::size_t bucket_cnt = this->bucket_hash_type::priv_bucket_count();
1176 for (n = 0; n < bucket_cnt; ++n){
1177 bucket_type &b = this->bucket_hash_type::priv_bucket(n);
1182 return this->bucket_hash_type::priv_invalid_local_it();
1185 BOOST_INTRUSIVE_FORCEINLINE void priv_insertion_update_cache(std::size_t)
1188 BOOST_INTRUSIVE_FORCEINLINE void priv_erasure_update_cache_range(std::size_t, std::size_t)
1191 BOOST_INTRUSIVE_FORCEINLINE void priv_erasure_update_cache()
1194 BOOST_INTRUSIVE_FORCEINLINE const key_equal &priv_equal() const
1195 { return this->equal_holder_t::get(); }
1197 BOOST_INTRUSIVE_FORCEINLINE key_equal &priv_equal()
1198 { return this->equal_holder_t::get(); }
1201 //bucket_hash_equal_t
1202 //Stores bucket_hash_t and the equality function when the first
1203 //non-empty bucket shall be cached.
1204 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits> //cache_begin == true version
1205 struct bucket_hash_equal_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, true>
1206 //Use public inheritance to avoid MSVC bugs with closures
1207 : bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> //2
1208 , hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type
1210 typedef typename hashtable_equal_holder
1211 <ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual>::type equal_holder_t;
1213 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1214 typedef ValueTraits value_traits;
1215 typedef typename equal_holder_t::functor_type key_equal;
1216 typedef bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits> bucket_hash_type;
1217 typedef typename bucket_hash_type::hasher hasher;
1218 typedef BucketTraits bucket_traits;
1219 typedef typename bucket_plus_vtraits_t::slist_impl::iterator siterator;
1221 template<class BucketTraitsType>
1222 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const key_equal &e)
1223 : bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
1227 typedef typename detail::unordered_bucket_ptr_impl
1228 <typename bucket_hash_type::value_traits>::type bucket_ptr;
1230 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr priv_get_cache() const
1231 { return cached_begin_; }
1233 BOOST_INTRUSIVE_FORCEINLINE void priv_set_cache(bucket_ptr p)
1234 { cached_begin_ = p; }
1236 BOOST_INTRUSIVE_FORCEINLINE std::size_t priv_get_cache_bucket_num()
1237 { return std::size_t(this->cached_begin_ - this->bucket_hash_type::priv_bucket_pointer()); }
1239 BOOST_INTRUSIVE_FORCEINLINE void priv_initialize_cache()
1240 { this->cached_begin_ = this->bucket_hash_type::priv_invalid_bucket(); }
1242 BOOST_INTRUSIVE_FORCEINLINE void priv_swap_cache(bucket_hash_equal_t &other)
1243 { ::boost::adl_move_swap(this->cached_begin_, other.cached_begin_); }
1245 siterator priv_begin() const
1247 if(this->cached_begin_ == this->bucket_hash_type::priv_invalid_bucket()){
1248 return this->bucket_hash_type::priv_invalid_local_it();
1251 return this->cached_begin_->begin();
1255 void priv_insertion_update_cache(std::size_t insertion_bucket)
1257 BOOST_INTRUSIVE_INVARIANT_ASSERT(insertion_bucket < this->bucket_hash_type::priv_bucket_count());
1258 bucket_ptr p = this->bucket_hash_type::priv_bucket_pointer() + std::ptrdiff_t(insertion_bucket);
1259 if(p < this->cached_begin_){
1260 this->cached_begin_ = p;
1264 BOOST_INTRUSIVE_FORCEINLINE const key_equal &priv_equal() const
1265 { return this->equal_holder_t::get(); }
1267 BOOST_INTRUSIVE_FORCEINLINE key_equal &priv_equal()
1268 { return this->equal_holder_t::get(); }
1270 void priv_erasure_update_cache_range(std::size_t first_bucket_num, std::size_t last_bucket_num)
1272 //If the last bucket is the end, the cache must be updated
1273 //to the last position if all
1274 if(this->priv_get_cache_bucket_num() == first_bucket_num &&
1275 this->bucket_hash_type::priv_bucket(first_bucket_num).empty() ){
1276 this->priv_set_cache(this->bucket_hash_type::priv_bucket_pointer() + std::ptrdiff_t(last_bucket_num));
1277 this->priv_erasure_update_cache();
1281 void priv_erasure_update_cache()
1283 if(this->cached_begin_ != this->bucket_hash_type::priv_invalid_bucket()){
1284 std::size_t current_n = std::size_t(this->priv_get_cache() - this->bucket_hash_type::priv_bucket_pointer());
1285 for( const std::size_t num_buckets = this->bucket_hash_type::priv_bucket_count()
1286 ; current_n < num_buckets
1287 ; ++current_n, ++cached_begin_){
1288 if(!cached_begin_->empty()){
1292 this->priv_initialize_cache();
1296 bucket_ptr cached_begin_;
1299 //This wrapper around size_traits is used
1300 //to maintain minimal container size with compilers like MSVC
1301 //that have problems with EBO and multiple empty base classes
1302 template<class DeriveFrom, class SizeType, bool>
1303 struct hashtable_size_traits_wrapper
1306 template<class Base, class Arg0, class Arg1, class Arg2>
1307 hashtable_size_traits_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
1308 , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
1309 : DeriveFrom(::boost::forward<Base>(base)
1310 , ::boost::forward<Arg0>(arg0)
1311 , ::boost::forward<Arg1>(arg1)
1312 , ::boost::forward<Arg2>(arg2))
1314 typedef detail::size_holder < true, SizeType> size_traits;//size_traits
1316 size_traits size_traits_;
1318 typedef const size_traits & size_traits_const_t;
1319 typedef size_traits & size_traits_t;
1321 BOOST_INTRUSIVE_FORCEINLINE size_traits_const_t priv_size_traits() const
1322 { return size_traits_; }
1324 BOOST_INTRUSIVE_FORCEINLINE size_traits_t priv_size_traits()
1325 { return size_traits_; }
1328 template<class DeriveFrom, class SizeType>
1329 struct hashtable_size_traits_wrapper<DeriveFrom, SizeType, false>
1332 template<class Base, class Arg0, class Arg1, class Arg2>
1333 hashtable_size_traits_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
1334 , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
1335 : DeriveFrom(::boost::forward<Base>(base)
1336 , ::boost::forward<Arg0>(arg0)
1337 , ::boost::forward<Arg1>(arg1)
1338 , ::boost::forward<Arg2>(arg2))
1341 typedef detail::size_holder< false, SizeType> size_traits;
1343 typedef size_traits size_traits_const_t;
1344 typedef size_traits size_traits_t;
1346 BOOST_INTRUSIVE_FORCEINLINE size_traits priv_size_traits() const
1347 { return size_traits(); }
1351 //Stores bucket_hash_equal_t and split_traits
1352 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
1353 struct hashdata_internal
1354 : public hashtable_size_traits_wrapper
1355 < bucket_hash_equal_t
1356 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1358 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
1361 , (BoolFlags & hash_bool_flags::incremental_pos) != 0
1364 typedef hashtable_size_traits_wrapper
1365 < bucket_hash_equal_t
1366 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1368 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
1371 , (BoolFlags & hash_bool_flags::incremental_pos) != 0
1373 typedef typename internal_type::key_equal key_equal;
1374 typedef typename internal_type::hasher hasher;
1375 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1376 typedef SizeType size_type;
1377 typedef typename internal_type::size_traits split_traits;
1378 typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
1379 typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
1380 typedef typename bucket_plus_vtraits_t::siterator siterator;
1381 typedef typename bucket_plus_vtraits_t::bucket_traits bucket_traits;
1382 typedef typename bucket_plus_vtraits_t::value_traits value_traits;
1383 typedef typename bucket_plus_vtraits_t::bucket_type bucket_type;
1384 typedef typename value_traits::value_type value_type;
1385 typedef typename value_traits::pointer pointer;
1386 typedef typename value_traits::const_pointer const_pointer;
1387 typedef typename pointer_traits<pointer>::reference reference;
1388 typedef typename pointer_traits
1389 <const_pointer>::reference const_reference;
1390 typedef typename value_traits::node_traits node_traits;
1391 typedef typename node_traits::node node;
1392 typedef typename node_traits::node_ptr node_ptr;
1393 typedef typename node_traits::const_node_ptr const_node_ptr;
1394 typedef detail::node_functions<node_traits> node_functions_t;
1395 typedef typename get_slist_impl
1396 <typename reduced_slist_node_traits
1397 <typename value_traits::node_traits>::type
1399 typedef typename slist_impl::node_algorithms node_algorithms;
1400 typedef typename slist_impl::node_ptr slist_node_ptr;
1402 typedef hash_key_types_base
1403 < typename ValueTraits::value_type
1406 typedef typename hash_types_base::key_of_value key_of_value;
1408 static const bool store_hash = detail::store_hash_is_true<node_traits>::value;
1409 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
1410 static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
1412 typedef detail::bool_<store_hash> store_hash_t;
1414 typedef detail::transform_iterator
1415 < typename slist_impl::iterator
1416 , downcast_node_to_value_t
1418 , false> > local_iterator;
1420 typedef detail::transform_iterator
1421 < typename slist_impl::iterator
1422 , downcast_node_to_value_t
1424 , true> > const_local_iterator;
1427 template<class BucketTraitsType>
1428 hashdata_internal( const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits
1429 , const hasher & h, const key_equal &e)
1430 : internal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
1433 BOOST_INTRUSIVE_FORCEINLINE typename internal_type::size_traits_t priv_split_traits()
1434 { return this->priv_size_traits(); }
1436 BOOST_INTRUSIVE_FORCEINLINE typename internal_type::size_traits_const_t priv_split_traits() const
1437 { return this->priv_size_traits(); }
1439 ~hashdata_internal()
1440 { this->priv_clear_buckets(); }
1442 void priv_clear_buckets()
1444 const std::size_t cache_num = std::size_t(this->priv_get_cache() - this->internal_type::priv_bucket_pointer());
1445 this->internal_type::priv_clear_buckets
1446 ( this->priv_get_cache()
1447 , this->internal_type::priv_bucket_count() - cache_num);
1450 void priv_clear_buckets_and_cache()
1452 this->priv_clear_buckets();
1453 this->priv_initialize_cache();
1456 void priv_initialize_buckets_and_cache()
1458 this->internal_type::priv_clear_buckets
1459 ( this->internal_type::priv_bucket_pointer()
1460 , this->internal_type::priv_bucket_count());
1461 this->priv_initialize_cache();
1464 typedef hashtable_iterator<bucket_plus_vtraits_t, false> iterator;
1465 typedef hashtable_iterator<bucket_plus_vtraits_t, true> const_iterator;
1467 static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_ true_value)
1468 { return bucket_plus_vtraits<ValueTraits, BucketTraits>::priv_stored_hash(n, true_value); }
1470 static std::size_t priv_stored_hash(slist_node_ptr n, detail::false_ false_value)
1471 { return bucket_plus_vtraits<ValueTraits, BucketTraits>::priv_stored_hash(n, false_value); }
1474 BOOST_INTRUSIVE_FORCEINLINE SizeType split_count() const BOOST_NOEXCEPT
1475 { return this->priv_split_traits().get_size(); }
1477 BOOST_INTRUSIVE_FORCEINLINE iterator iterator_to(reference value) BOOST_NOEXCEPT
1479 return iterator(bucket_type::s_iterator_to
1480 (this->priv_value_to_node(value)), &this->get_bucket_value_traits());
1483 const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
1485 siterator const sit = bucket_type::s_iterator_to
1486 ( *pointer_traits<node_ptr>::const_cast_from
1487 (pointer_traits<const_node_ptr>::pointer_to(this->priv_value_to_node(value)))
1489 return const_iterator(sit, &this->get_bucket_value_traits());
1492 static local_iterator s_local_iterator_to(reference value) BOOST_NOEXCEPT
1494 BOOST_STATIC_ASSERT((!stateful_value_traits));
1495 siterator sit = bucket_type::s_iterator_to(*value_traits::to_node_ptr(value));
1496 return local_iterator(sit, const_value_traits_ptr());
1499 static const_local_iterator s_local_iterator_to(const_reference value) BOOST_NOEXCEPT
1501 BOOST_STATIC_ASSERT((!stateful_value_traits));
1502 siterator const sit = bucket_type::s_iterator_to
1503 ( *pointer_traits<node_ptr>::const_cast_from
1504 (value_traits::to_node_ptr(value))
1506 return const_local_iterator(sit, const_value_traits_ptr());
1509 local_iterator local_iterator_to(reference value) BOOST_NOEXCEPT
1511 siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value));
1512 return local_iterator(sit, this->priv_value_traits_ptr());
1515 const_local_iterator local_iterator_to(const_reference value) const BOOST_NOEXCEPT
1517 siterator sit = bucket_type::s_iterator_to
1518 ( *pointer_traits<node_ptr>::const_cast_from
1519 (pointer_traits<const_node_ptr>::pointer_to(this->priv_value_to_node(value)))
1521 return const_local_iterator(sit, this->priv_value_traits_ptr());
1524 BOOST_INTRUSIVE_FORCEINLINE size_type bucket_count() const BOOST_NOEXCEPT
1525 { return size_type(this->priv_bucket_count()); }
1527 BOOST_INTRUSIVE_FORCEINLINE size_type bucket_size(size_type n) const BOOST_NOEXCEPT
1528 { return (size_type)this->priv_bucket(n).size(); }
1530 BOOST_INTRUSIVE_FORCEINLINE bucket_ptr bucket_pointer() const BOOST_NOEXCEPT
1531 { return this->priv_bucket_pointer(); }
1533 BOOST_INTRUSIVE_FORCEINLINE local_iterator begin(size_type n) BOOST_NOEXCEPT
1534 { return local_iterator(this->priv_bucket(n).begin(), this->priv_value_traits_ptr()); }
1536 BOOST_INTRUSIVE_FORCEINLINE const_local_iterator begin(size_type n) const BOOST_NOEXCEPT
1537 { return this->cbegin(n); }
1539 static BOOST_INTRUSIVE_FORCEINLINE size_type suggested_upper_bucket_count(size_type n) BOOST_NOEXCEPT
1541 return prime_list_holder<0>::suggested_upper_bucket_count(n);
1544 static BOOST_INTRUSIVE_FORCEINLINE size_type suggested_lower_bucket_count(size_type n) BOOST_NOEXCEPT
1546 return prime_list_holder<0>::suggested_lower_bucket_count(n);
1549 const_local_iterator cbegin(size_type n) const BOOST_NOEXCEPT
1551 return const_local_iterator
1552 ( this->priv_bucket(n).begin()
1553 , this->priv_value_traits_ptr());
1556 using internal_type::end;
1557 using internal_type::cend;
1559 local_iterator end(size_type n) BOOST_NOEXCEPT
1560 { return local_iterator(this->priv_bucket(n).end(), this->priv_value_traits_ptr()); }
1562 BOOST_INTRUSIVE_FORCEINLINE const_local_iterator end(size_type n) const BOOST_NOEXCEPT
1563 { return this->cend(n); }
1565 const_local_iterator cend(size_type n) const BOOST_NOEXCEPT
1567 return const_local_iterator
1568 ( this->priv_bucket(n).end()
1569 , this->priv_value_traits_ptr());
1572 //Public functions for hashtable_impl
1574 BOOST_INTRUSIVE_FORCEINLINE iterator begin() BOOST_NOEXCEPT
1575 { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1577 BOOST_INTRUSIVE_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT
1578 { return this->cbegin(); }
1580 BOOST_INTRUSIVE_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT
1581 { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1583 BOOST_INTRUSIVE_FORCEINLINE hasher hash_function() const
1584 { return this->priv_hasher(); }
1586 BOOST_INTRUSIVE_FORCEINLINE key_equal key_eq() const
1587 { return this->priv_equal(); }
1592 //! The class template hashtable is an intrusive hash table container, that
1593 //! is used to construct intrusive unordered_set and unordered_multiset containers. The
1594 //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
1596 //! hashtable is a semi-intrusive container: each object to be stored in the
1597 //! container must contain a proper hook, but the container also needs
1598 //! additional auxiliary memory to work: hashtable needs a pointer to an array
1599 //! of type `bucket_type` to be passed in the constructor. This bucket array must
1600 //! have at least the same lifetime as the container. This makes the use of
1601 //! hashtable more complicated than purely intrusive containers.
1602 //! `bucket_type` is default-constructible, copyable and assignable
1604 //! The template parameter \c T is the type to be managed by the container.
1605 //! The user can specify additional options and if no options are provided
1606 //! default options are used.
1608 //! The container supports the following options:
1609 //! \c base_hook<>/member_hook<>/value_traits<>,
1610 //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
1611 //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
1613 //! hashtable only provides forward iterators but it provides 4 iterator types:
1614 //! iterator and const_iterator to navigate through the whole container and
1615 //! local_iterator and const_local_iterator to navigate through the values
1616 //! stored in a single bucket. Local iterators are faster and smaller.
1618 //! It's not recommended to use non constant-time size hashtables because several
1619 //! key functions, like "empty()", become non-constant time functions. Non
1620 //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
1622 //! hashtables, does not make automatic rehashings nor
1623 //! offers functions related to a load factor. Rehashing can be explicitly requested
1624 //! and the user must provide a new bucket array that will be used from that moment.
1626 //! Since no automatic rehashing is done, iterators are never invalidated when
1627 //! inserting or erasing elements. Iterators are only invalidated when rehashing.
1628 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1629 template<class T, class ...Options>
1631 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
1633 class hashtable_impl
1634 : private hashtable_size_traits_wrapper
1637 , VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1638 , BucketTraits, SizeType
1639 , BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos) //1
1642 , (BoolFlags & hash_bool_flags::constant_time_size_pos) != 0
1645 typedef hashtable_size_traits_wrapper
1648 , VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
1649 , BucketTraits, SizeType
1650 , BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos) //1
1653 , (BoolFlags & hash_bool_flags::constant_time_size_pos) != 0
1655 typedef typename internal_type::size_traits size_traits;
1656 typedef hash_key_types_base
1657 < typename ValueTraits::value_type
1661 typedef ValueTraits value_traits;
1664 typedef BucketTraits bucket_traits;
1666 typedef typename internal_type::slist_impl slist_impl;
1667 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
1668 typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
1670 using internal_type::begin;
1671 using internal_type::cbegin;
1672 using internal_type::end;
1673 using internal_type::cend;
1674 using internal_type::hash_function;
1675 using internal_type::key_eq;
1676 using internal_type::bucket_size;
1677 using internal_type::bucket_count;
1678 using internal_type::local_iterator_to;
1679 using internal_type::s_local_iterator_to;
1680 using internal_type::iterator_to;
1681 using internal_type::bucket_pointer;
1682 using internal_type::suggested_upper_bucket_count;
1683 using internal_type::suggested_lower_bucket_count;
1684 using internal_type::split_count;
1688 typedef typename value_traits::pointer pointer;
1689 typedef typename value_traits::const_pointer const_pointer;
1690 typedef typename value_traits::value_type value_type;
1691 typedef typename hash_types_base::key_type key_type;
1692 typedef typename hash_types_base::key_of_value key_of_value;
1693 typedef typename pointer_traits<pointer>::reference reference;
1694 typedef typename pointer_traits<const_pointer>::reference const_reference;
1695 typedef typename pointer_traits<pointer>::difference_type difference_type;
1696 typedef SizeType size_type;
1697 typedef typename internal_type::key_equal key_equal;
1698 typedef typename internal_type::hasher hasher;
1699 typedef bucket_impl<slist_impl> bucket_type;
1700 typedef typename internal_type::bucket_ptr bucket_ptr;
1701 typedef typename slist_impl::iterator siterator;
1702 typedef typename slist_impl::const_iterator const_siterator;
1703 typedef typename internal_type::iterator iterator;
1704 typedef typename internal_type::const_iterator const_iterator;
1705 typedef typename internal_type::local_iterator local_iterator;
1706 typedef typename internal_type::const_local_iterator const_local_iterator;
1707 typedef typename value_traits::node_traits node_traits;
1708 typedef typename node_traits::node node;
1709 typedef typename pointer_traits
1710 <pointer>::template rebind_pointer
1711 < node >::type node_ptr;
1712 typedef typename pointer_traits
1713 <pointer>::template rebind_pointer
1714 < const node >::type const_node_ptr;
1715 typedef typename pointer_traits
1716 <node_ptr>::reference node_reference;
1717 typedef typename pointer_traits
1718 <const_node_ptr>::reference const_node_reference;
1719 typedef typename slist_impl::node_algorithms node_algorithms;
1721 static const bool stateful_value_traits = internal_type::stateful_value_traits;
1722 static const bool store_hash = internal_type::store_hash;
1724 static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
1725 static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
1726 static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
1727 static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
1728 static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
1729 static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
1731 static const bool optimize_multikey
1732 = detail::optimize_multikey_is_true<node_traits>::value && !unique_keys;
1735 static const bool is_multikey = !unique_keys;
1738 //Configuration error: compare_hash<> can't be specified without store_hash<>
1739 //See documentation for more explanations
1740 BOOST_STATIC_ASSERT((!compare_hash || store_hash));
1742 typedef typename slist_impl::node_ptr slist_node_ptr;
1743 typedef typename pointer_traits
1744 <slist_node_ptr>::template rebind_pointer
1745 < void >::type void_pointer;
1746 //We'll define group traits, but these won't be instantiated if
1747 //optimize_multikey is not true
1748 typedef unordered_group_adapter<node_traits> group_traits;
1749 typedef circular_slist_algorithms<group_traits> group_algorithms;
1750 typedef typename internal_type::store_hash_t store_hash_t;
1751 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
1752 typedef detail::bool_<cache_begin> cache_begin_t;
1753 typedef detail::bool_<power_2_buckets> power_2_buckets_t;
1754 typedef typename internal_type::split_traits split_traits;
1755 typedef detail::group_functions<node_traits> group_functions_t;
1756 typedef detail::node_functions<node_traits> node_functions_t;
1759 //noncopyable, movable
1760 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
1762 static const bool safemode_or_autounlink = internal_type::safemode_or_autounlink;
1764 //Constant-time size is incompatible with auto-unlink hooks!
1765 BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
1766 //Cache begin is incompatible with auto-unlink hooks!
1767 BOOST_STATIC_ASSERT(!(cache_begin && ((int)value_traits::link_mode == (int)auto_unlink)));
1769 template<class Disposer>
1770 struct typeof_node_disposer
1772 typedef node_cast_adaptor
1773 < detail::node_disposer< Disposer, value_traits, CircularSListAlgorithms>
1774 , slist_node_ptr, node_ptr > type;
1777 template<class Disposer>
1778 typename typeof_node_disposer<Disposer>::type
1779 make_node_disposer(const Disposer &disposer) const
1781 typedef typename typeof_node_disposer<Disposer>::type return_t;
1782 return return_t(disposer, &this->priv_value_traits());
1788 typedef detail::insert_commit_data_impl insert_commit_data;
1793 //! <b>Requires</b>: buckets must not be being used by any other resource.
1795 //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
1796 //! to the bucket array and copies of the key_hasher and equal_func functors.
1798 //! <b>Complexity</b>: Constant.
1800 //! <b>Throws</b>: If value_traits::node_traits::node
1801 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1802 //! or the copy constructor or invocation of hash_func or equal_func throws.
1804 //! <b>Notes</b>: buckets array must be disposed only after
1805 //! *this is disposed.
1806 explicit hashtable_impl ( const bucket_traits &b_traits
1807 , const hasher & hash_func = hasher()
1808 , const key_equal &equal_func = key_equal()
1809 , const value_traits &v_traits = value_traits())
1810 : internal_type(v_traits, b_traits, hash_func, equal_func)
1812 this->priv_initialize_buckets_and_cache();
1813 this->priv_size_traits().set_size(size_type(0));
1814 size_type bucket_sz = this->bucket_count();
1815 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
1816 //Check power of two bucket array if the option is activated
1817 BOOST_INTRUSIVE_INVARIANT_ASSERT
1818 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
1819 this->priv_split_traits().set_size(size_type(bucket_sz>>1u));
1822 //! <b>Requires</b>: buckets must not be being used by any other resource
1823 //! and dereferencing iterator must yield an lvalue of type value_type.
1825 //! <b>Effects</b>: Constructs an empty container and inserts elements from
1828 //! <b>Complexity</b>: If N is distance(b, e): Average case is O(N)
1829 //! (with a good hash function and with buckets_len >= N),worst case O(N^2).
1831 //! <b>Throws</b>: If value_traits::node_traits::node
1832 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1833 //! or the copy constructor or invocation of hasher or key_equal throws.
1835 //! <b>Notes</b>: buckets array must be disposed only after
1836 //! *this is disposed.
1837 template<class Iterator>
1838 hashtable_impl ( bool unique, Iterator b, Iterator e
1839 , const bucket_traits &b_traits
1840 , const hasher & hash_func = hasher()
1841 , const key_equal &equal_func = key_equal()
1842 , const value_traits &v_traits = value_traits())
1843 : internal_type(v_traits, b_traits, hash_func, equal_func)
1845 this->priv_initialize_buckets_and_cache();
1846 this->priv_size_traits().set_size(size_type(0));
1847 size_type bucket_sz = this->bucket_count();
1848 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
1849 //Check power of two bucket array if the option is activated
1850 BOOST_INTRUSIVE_INVARIANT_ASSERT
1851 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
1852 this->priv_split_traits().set_size(size_type(bucket_sz>>1u));
1855 this->insert_unique(b, e);
1857 this->insert_equal(b, e);
1860 //! <b>Effects</b>: Constructs a container moving resources from another container.
1861 //! Internal value traits, bucket traits, hasher and comparison are move constructed and
1862 //! nodes belonging to x are linked to *this.
1864 //! <b>Complexity</b>: Constant.
1866 //! <b>Throws</b>: If value_traits::node_traits::node's
1867 //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1868 //! or the move constructor of value traits, bucket traits, hasher or comparison throws.
1869 hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
1870 : internal_type( ::boost::move(x.priv_value_traits())
1871 , ::boost::move(x.priv_bucket_traits())
1872 , ::boost::move(x.priv_hasher())
1873 , ::boost::move(x.priv_equal())
1876 this->priv_swap_cache(x);
1877 x.priv_initialize_cache();
1878 this->priv_size_traits().set_size(x.priv_size_traits().get_size());
1879 x.priv_size_traits().set_size(size_type(0));
1880 this->priv_split_traits().set_size(x.priv_split_traits().get_size());
1881 x.priv_split_traits().set_size(size_type(0));
1884 //! <b>Effects</b>: Equivalent to swap.
1886 hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
1887 { this->swap(x); return *this; }
1889 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1890 //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
1891 //! are not deleted (i.e. no destructors are called).
1893 //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
1894 //! it's a safe-mode or auto-unlink value. Otherwise constant.
1896 //! <b>Throws</b>: Nothing.
1899 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
1901 //! <b>Complexity</b>: Amortized constant time.
1902 //! Worst case (empty unordered_set): O(this->bucket_count())
1904 //! <b>Throws</b>: Nothing.
1905 iterator begin() BOOST_NOEXCEPT;
1907 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1908 //! of the unordered_set.
1910 //! <b>Complexity</b>: Amortized constant time.
1911 //! Worst case (empty unordered_set): O(this->bucket_count())
1913 //! <b>Throws</b>: Nothing.
1914 const_iterator begin() const BOOST_NOEXCEPT;
1916 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1917 //! of the unordered_set.
1919 //! <b>Complexity</b>: Amortized constant time.
1920 //! Worst case (empty unordered_set): O(this->bucket_count())
1922 //! <b>Throws</b>: Nothing.
1923 const_iterator cbegin() const BOOST_NOEXCEPT;
1925 //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
1927 //! <b>Complexity</b>: Constant.
1929 //! <b>Throws</b>: Nothing.
1930 iterator end() BOOST_NOEXCEPT;
1932 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1934 //! <b>Complexity</b>: Constant.
1936 //! <b>Throws</b>: Nothing.
1937 const_iterator end() const BOOST_NOEXCEPT;
1939 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1941 //! <b>Complexity</b>: Constant.
1943 //! <b>Throws</b>: Nothing.
1944 const_iterator cend() const BOOST_NOEXCEPT;
1946 //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
1948 //! <b>Complexity</b>: Constant.
1950 //! <b>Throws</b>: If hasher copy-constructor throws.
1951 hasher hash_function() const;
1953 //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
1955 //! <b>Complexity</b>: Constant.
1957 //! <b>Throws</b>: If key_equal copy-constructor throws.
1958 key_equal key_eq() const;
1962 //! <b>Effects</b>: Returns true if the container is empty.
1964 //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
1965 //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
1966 //! Otherwise constant.
1968 //! <b>Throws</b>: Nothing.
1969 bool empty() const BOOST_NOEXCEPT
1971 BOOST_IF_CONSTEXPR(constant_time_size){
1972 return !this->size();
1974 else if(cache_begin){
1975 return this->begin() == this->end();
1978 size_type bucket_cnt = this->bucket_count();
1979 const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
1980 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
1989 //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
1991 //! <b>Complexity</b>: Linear to elements contained in *this if
1992 //! constant_time_size is false. Constant-time otherwise.
1994 //! <b>Throws</b>: Nothing.
1995 size_type size() const BOOST_NOEXCEPT
1997 BOOST_IF_CONSTEXPR(constant_time_size)
1998 return this->priv_size_traits().get_size();
2000 std::size_t len = 0;
2001 std::size_t bucket_cnt = this->bucket_count();
2002 const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
2003 for (std::size_t n = 0; n < bucket_cnt; ++n, ++b){
2006 BOOST_INTRUSIVE_INVARIANT_ASSERT((len <= SizeType(-1)));
2007 return size_type(len);
2011 //! <b>Requires</b>: the hasher and the equality function unqualified swap
2012 //! call should not throw.
2014 //! <b>Effects</b>: Swaps the contents of two unordered_sets.
2015 //! Swaps also the contained bucket array and equality and hasher functors.
2017 //! <b>Complexity</b>: Constant.
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)
2024 ::boost::adl_move_swap(this->priv_equal(), other.priv_equal());
2025 ::boost::adl_move_swap(this->priv_hasher(), other.priv_hasher());
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());
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.
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.
2044 //! If store_hash option is true, this method does not use the hash function.
2046 //! If any operation throws, all cloned elements are unlinked and disposed
2047 //! calling Disposer::operator()(pointer).
2049 //! <b>Complexity</b>: Linear to erased plus inserted elements.
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); }
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.
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.
2067 //! If store_hash option is true, this method does not use the hash function.
2069 //! If any operation throws, all cloned elements are unlinked and disposed
2070 //! calling Disposer::operator()(pointer).
2072 //! <b>Complexity</b>: Linear to erased plus inserted elements.
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); }
2080 //! <b>Requires</b>: value must be an lvalue
2082 //! <b>Effects</b>: Inserts the value into the unordered_set.
2084 //! <b>Returns</b>: An iterator to the inserted value.
2086 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2088 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
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)
2094 size_type bucket_num;
2095 std::size_t hash_value;
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);
2103 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
2104 //! of type value_type.
2106 //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
2108 //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
2109 //! Worst case O(N*this->size()).
2111 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
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)
2119 this->insert_equal(*b);
2122 //! <b>Requires</b>: value must be an lvalue
2124 //! <b>Effects</b>: Tries to inserts value into the unordered_set.
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
2132 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2134 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
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)
2140 insert_commit_data commit_data;
2141 std::pair<iterator, bool> ret = this->insert_unique_check(key_of_value()(value), commit_data);
2143 ret.first = this->insert_unique_commit(value, commit_data);
2148 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
2149 //! of type value_type.
2151 //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
2153 //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
2154 //! Worst case O(N*this->size()).
2156 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
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)
2164 this->insert_unique(*b);
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.
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.
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.
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.
2184 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2186 //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
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.
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.
2198 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
2199 //! objects are inserted or erased from the unordered_set.
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)
2209 size_type bucket_num;
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());
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.
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.
2226 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2228 //! <b>Throws</b>: If hasher or key_compare throw. Strong guarantee.
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.
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.
2240 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
2241 //! objects are inserted or erased from the unordered_set.
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); }
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".
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.
2256 //! <b>Returns</b>: An iterator to the newly inserted object.
2258 //! <b>Complexity</b>: Constant time.
2260 //! <b>Throws</b>: Nothing.
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.
2266 //! After a successful rehashing insert_commit_data remains valid.
2267 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
2269 size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash);
2270 bucket_type &b = this->priv_bucket(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());
2280 //! <b>Effects</b>: Erases the element pointed to by i.
2282 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2284 //! <b>Throws</b>: Nothing.
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) BOOST_NOEXCEPT
2289 { this->erase_and_dispose(i, detail::null_disposer()); }
2291 //! <b>Effects</b>: Erases the range pointed to by b end e.
2293 //! <b>Complexity</b>: Average case O(distance(b, e)),
2294 //! worst case O(this->size()).
2296 //! <b>Throws</b>: Nothing.
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) BOOST_NOEXCEPT
2301 { this->erase_and_dispose(b, e, detail::null_disposer()); }
2303 //! <b>Effects</b>: Erases all the elements with the given value.
2305 //! <b>Returns</b>: The number of erased elements.
2307 //! <b>Complexity</b>: Average case O(this->count(value)).
2308 //! Worst case O(this->size()).
2310 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2311 //! Basic guarantee.
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()); }
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.
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.
2326 //! <b>Effects</b>: Erases all the elements that have the same hash and
2327 //! compare equal with the given key.
2329 //! <b>Returns</b>: The number of erased elements.
2331 //! <b>Complexity</b>: Average case O(this->count(value)).
2332 //! Worst case O(this->size()).
2334 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
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()); }
2342 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2344 //! <b>Effects</b>: Erases the element pointed to by i.
2345 //! Disposer::operator()(pointer) is called for the removed element.
2347 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2349 //! <b>Throws</b>: Nothing.
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) BOOST_NOEXCEPT
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(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);
2366 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2368 //! <b>Effects</b>: Erases the range pointed to by b end e.
2369 //! Disposer::operator()(pointer) is called for the removed elements.
2371 //! <b>Complexity</b>: Average case O(distance(b, e)),
2372 //! worst case O(this->size()).
2374 //! <b>Throws</b>: Nothing.
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) BOOST_NOEXCEPT
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);
2386 siterator before_first_local_it
2387 = this->priv_get_previous(this->priv_bucket(first_bucket_num), first_local_it);
2388 size_type last_bucket_num;
2389 siterator last_local_it;
2391 //For the end iterator, we will assign the end iterator
2392 //of the last bucket
2393 if(e == this->end()){
2394 last_bucket_num = size_type(this->bucket_count() - 1u);
2395 last_local_it = this->priv_bucket(last_bucket_num).end();
2398 last_local_it = e.slist_it();
2399 last_bucket_num = this->priv_get_bucket_num(last_local_it);
2401 size_type const num_erased = (size_type)this->priv_erase_node_range
2402 ( before_first_local_it, first_bucket_num, last_local_it, last_bucket_num
2403 , make_node_disposer(disposer), optimize_multikey_t());
2404 this->priv_size_traits().set_size(size_type(this->priv_size_traits().get_size()-num_erased));
2405 this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
2409 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2411 //! <b>Effects</b>: Erases all the elements with the given value.
2412 //! Disposer::operator()(pointer) is called for the removed elements.
2414 //! <b>Returns</b>: The number of erased elements.
2416 //! <b>Complexity</b>: Average case O(this->count(value)).
2417 //! Worst case O(this->size()).
2419 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2420 //! Basic guarantee.
2422 //! <b>Note</b>: Invalidates the iterators (but not the references)
2423 //! to the erased elements. No destructors are called.
2424 template<class Disposer>
2425 BOOST_INTRUSIVE_FORCEINLINE size_type erase_and_dispose(const key_type &key, Disposer disposer)
2426 { return this->erase_and_dispose(key, this->priv_hasher(), this->priv_equal(), disposer); }
2428 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2430 //! <b>Effects</b>: Erases all the elements with the given key.
2431 //! according to the comparison functor "equal_func".
2432 //! Disposer::operator()(pointer) is called for the removed elements.
2434 //! <b>Returns</b>: The number of erased elements.
2436 //! <b>Complexity</b>: Average case O(this->count(value)).
2437 //! Worst case O(this->size()).
2439 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
2441 //! <b>Note</b>: Invalidates the iterators
2442 //! to the erased elements.
2443 template<class KeyType, class KeyHasher, class KeyEqual, class Disposer>
2444 size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
2445 ,KeyEqual equal_func, Disposer disposer)
2447 size_type bucket_num;
2450 siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
2451 bool const success = it != this->priv_invalid_local_it();
2455 if(optimize_multikey){
2456 cnt = this->priv_erase_from_single_bucket
2457 (this->priv_bucket(bucket_num), prev, ++(priv_last_in_group)(it), make_node_disposer(disposer), optimize_multikey_t());
2460 bucket_type &b = this->priv_bucket(bucket_num);
2461 siterator const end_sit = b.end();
2465 }while(it != end_sit &&
2466 this->priv_is_value_equal_to_key
2467 (this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func));
2468 bucket_type::s_erase_after_and_dispose(prev, it, make_node_disposer(disposer));
2470 this->priv_size_traits().set_size(size_type(this->priv_size_traits().get_size()-cnt));
2471 this->priv_erasure_update_cache();
2474 return static_cast<size_type>(cnt);
2477 //! <b>Effects</b>: Erases all of the elements.
2479 //! <b>Complexity</b>: Linear to the number of elements on the container.
2480 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
2482 //! <b>Throws</b>: Nothing.
2484 //! <b>Note</b>: Invalidates the iterators (but not the references)
2485 //! to the erased elements. No destructors are called.
2486 void clear() BOOST_NOEXCEPT
2488 this->priv_clear_buckets_and_cache();
2489 this->priv_size_traits().set_size(size_type(0));
2492 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
2494 //! <b>Effects</b>: Erases all of the elements.
2496 //! <b>Complexity</b>: Linear to the number of elements on the container.
2497 //! Disposer::operator()(pointer) is called for the removed elements.
2499 //! <b>Throws</b>: Nothing.
2501 //! <b>Note</b>: Invalidates the iterators (but not the references)
2502 //! to the erased elements. No destructors are called.
2503 template<class Disposer>
2504 void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
2506 if(!constant_time_size || !this->empty()){
2507 size_type num_buckets = this->bucket_count();
2508 bucket_ptr b = this->priv_bucket_pointer();
2509 typename typeof_node_disposer<Disposer>::type d(disposer, &this->priv_value_traits());
2510 for(; num_buckets--; ++b){
2511 b->clear_and_dispose(d);
2513 this->priv_size_traits().set_size(size_type(0));
2515 this->priv_initialize_cache();
2518 //! <b>Effects</b>: Returns the number of contained elements with the given value
2520 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2522 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2523 BOOST_INTRUSIVE_FORCEINLINE size_type count(const key_type &key) const
2524 { return this->count(key, this->priv_hasher(), this->priv_equal()); }
2526 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2527 //! the same hash values as the stored hasher. The difference is that
2528 //! "hash_func" hashes the given key instead of the value_type.
2530 //! "equal_func" must be a equality function that induces
2531 //! the same equality as key_equal. The difference is that
2532 //! "equal_func" compares an arbitrary key with the contained values.
2534 //! <b>Effects</b>: Returns the number of contained elements with the given key
2536 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2538 //! <b>Throws</b>: If hash_func or equal throw.
2539 template<class KeyType, class KeyHasher, class KeyEqual>
2540 size_type count(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2544 this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt);
2548 //! <b>Effects</b>: Finds an iterator to the first element is equal to
2549 //! "value" or end() if that element does not exist.
2551 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2553 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2554 BOOST_INTRUSIVE_FORCEINLINE iterator find(const key_type &key)
2555 { return this->find(key, this->priv_hasher(), this->priv_equal()); }
2557 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2558 //! the same hash values as the stored hasher. The difference is that
2559 //! "hash_func" hashes the given key instead of the value_type.
2561 //! "equal_func" must be a equality function that induces
2562 //! the same equality as key_equal. The difference is that
2563 //! "equal_func" compares an arbitrary key with the contained values.
2565 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2566 //! "key" according to the given hash and equality functor or end() if
2567 //! that element does not exist.
2569 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2571 //! <b>Throws</b>: If hash_func or equal_func throw.
2573 //! <b>Note</b>: This function is used when constructing a value_type
2574 //! is expensive and the value_type can be compared with a cheaper
2575 //! key type. Usually this key is part of the value_type.
2576 template<class KeyType, class KeyHasher, class KeyEqual>
2577 iterator find(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
2582 return iterator( this->priv_find(key, hash_func, equal_func, bucket_n, h, prev)
2583 , &this->get_bucket_value_traits());
2586 //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
2587 //! "key" or end() if that element does not exist.
2589 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2591 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2592 BOOST_INTRUSIVE_FORCEINLINE const_iterator find(const key_type &key) const
2593 { return this->find(key, this->priv_hasher(), this->priv_equal()); }
2595 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2596 //! the same hash values as the stored hasher. The difference is that
2597 //! "hash_func" hashes the given key instead of the value_type.
2599 //! "equal_func" must be a equality function that induces
2600 //! the same equality as key_equal. The difference is that
2601 //! "equal_func" compares an arbitrary key with the contained values.
2603 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2604 //! "key" according to the given hasher and equality functor or end() if
2605 //! that element does not exist.
2607 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2609 //! <b>Throws</b>: If hash_func or equal_func throw.
2611 //! <b>Note</b>: This function is used when constructing a value_type
2612 //! is expensive and the value_type can be compared with a cheaper
2613 //! key type. Usually this key is part of the value_type.
2614 template<class KeyType, class KeyHasher, class KeyEqual>
2616 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2619 std::size_t hash_value;
2621 return const_iterator( this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev)
2622 , &this->get_bucket_value_traits());
2625 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2626 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2629 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2631 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2632 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type &key)
2633 { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
2635 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2636 //! the same hash values as the stored hasher. The difference is that
2637 //! "hash_func" hashes the given key instead of the value_type.
2639 //! "equal_func" must be a equality function that induces
2640 //! the same equality as key_equal. The difference is that
2641 //! "equal_func" compares an arbitrary key with the contained values.
2643 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2644 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2647 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2648 //! Worst case O(this->size()).
2650 //! <b>Throws</b>: If hash_func or the equal_func throw.
2652 //! <b>Note</b>: This function is used when constructing a value_type
2653 //! is expensive and the value_type can be compared with a cheaper
2654 //! key type. Usually this key is part of the value_type.
2655 template<class KeyType, class KeyHasher, class KeyEqual>
2656 std::pair<iterator,iterator> equal_range
2657 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
2659 std::pair<siterator, siterator> ret =
2660 this->priv_equal_range(key, hash_func, equal_func);
2661 return std::pair<iterator, iterator>
2662 ( iterator(ret.first, &this->get_bucket_value_traits())
2663 , iterator(ret.second, &this->get_bucket_value_traits()));
2666 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2667 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2670 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2672 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2673 BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
2674 equal_range(const key_type &key) const
2675 { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
2677 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2678 //! the same hash values as the stored hasher. The difference is that
2679 //! "hash_func" hashes the given key instead of the value_type.
2681 //! "equal_func" must be a equality function that induces
2682 //! the same equality as key_equal. The difference is that
2683 //! "equal_func" compares an arbitrary key with the contained values.
2685 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2686 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2689 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2690 //! Worst case O(this->size()).
2692 //! <b>Throws</b>: If the hasher or equal_func throw.
2694 //! <b>Note</b>: This function is used when constructing a value_type
2695 //! is expensive and the value_type can be compared with a cheaper
2696 //! key type. Usually this key is part of the value_type.
2697 template<class KeyType, class KeyHasher, class KeyEqual>
2698 std::pair<const_iterator,const_iterator> equal_range
2699 (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
2701 std::pair<siterator, siterator> ret =
2702 this->priv_equal_range(key, hash_func, equal_func);
2703 return std::pair<const_iterator, const_iterator>
2704 ( const_iterator(ret.first, &this->get_bucket_value_traits())
2705 , const_iterator(ret.second, &this->get_bucket_value_traits()));
2708 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2710 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2711 //! appropriate type. Otherwise the behavior is undefined.
2713 //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
2714 //! that points to the value
2716 //! <b>Complexity</b>: Constant.
2718 //! <b>Throws</b>: If the internal hash function throws.
2719 iterator iterator_to(reference value) BOOST_NOEXCEPT;
2721 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2722 //! appropriate type. Otherwise the behavior is undefined.
2724 //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
2725 //! unordered_set that points to the value
2727 //! <b>Complexity</b>: Constant.
2729 //! <b>Throws</b>: If the internal hash function throws.
2730 const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT;
2732 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2733 //! appropriate type. Otherwise the behavior is undefined.
2735 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2736 //! that points to the value
2738 //! <b>Complexity</b>: Constant.
2740 //! <b>Throws</b>: Nothing.
2742 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2744 static local_iterator s_local_iterator_to(reference value) BOOST_NOEXCEPT;
2746 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2747 //! appropriate type. Otherwise the behavior is undefined.
2749 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2750 //! the unordered_set that points to the value
2752 //! <b>Complexity</b>: Constant.
2754 //! <b>Throws</b>: Nothing.
2756 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2758 static const_local_iterator s_local_iterator_to(const_reference value) BOOST_NOEXCEPT;
2760 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2761 //! appropriate type. Otherwise the behavior is undefined.
2763 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2764 //! that points to the value
2766 //! <b>Complexity</b>: Constant.
2768 //! <b>Throws</b>: Nothing.
2769 local_iterator local_iterator_to(reference value) BOOST_NOEXCEPT;
2771 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2772 //! appropriate type. Otherwise the behavior is undefined.
2774 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2775 //! the unordered_set that points to the value
2777 //! <b>Complexity</b>: Constant.
2779 //! <b>Throws</b>: Nothing.
2780 const_local_iterator local_iterator_to(const_reference value) const BOOST_NOEXCEPT;
2782 //! <b>Effects</b>: Returns the number of buckets passed in the constructor
2783 //! or the last rehash function.
2785 //! <b>Complexity</b>: Constant.
2787 //! <b>Throws</b>: Nothing.
2788 size_type bucket_count() const BOOST_NOEXCEPT;
2790 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2792 //! <b>Effects</b>: Returns the number of elements in the nth bucket.
2794 //! <b>Complexity</b>: Constant.
2796 //! <b>Throws</b>: Nothing.
2797 size_type bucket_size(size_type n) const BOOST_NOEXCEPT;
2798 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2800 //! <b>Effects</b>: Returns the index of the bucket in which elements
2801 //! with keys equivalent to k would be found, if any such element existed.
2803 //! <b>Complexity</b>: Constant.
2805 //! <b>Throws</b>: If the hash functor throws.
2807 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2808 BOOST_INTRUSIVE_FORCEINLINE size_type bucket(const key_type& k) const
2809 { return this->bucket(k, this->priv_hasher()); }
2811 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2812 //! the same hash values as the stored hasher. The difference is that
2813 //! "hash_func" hashes the given key instead of the value_type.
2815 //! <b>Effects</b>: Returns the index of the bucket in which elements
2816 //! with keys equivalent to k would be found, if any such element existed.
2818 //! <b>Complexity</b>: Constant.
2820 //! <b>Throws</b>: If hash_func throws.
2822 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2823 template<class KeyType, class KeyHasher>
2824 BOOST_INTRUSIVE_FORCEINLINE size_type bucket(const KeyType& k, KeyHasher hash_func) const
2825 { return this->priv_hash_to_bucket(hash_func(k)); }
2827 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2828 //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
2829 //! or the last rehash function.
2831 //! <b>Complexity</b>: Constant.
2833 //! <b>Throws</b>: Nothing.
2834 bucket_ptr bucket_pointer() const BOOST_NOEXCEPT;
2836 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2838 //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
2839 //! of the sequence stored in the bucket n.
2841 //! <b>Complexity</b>: Constant.
2843 //! <b>Throws</b>: Nothing.
2845 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2846 //! containing all of the elements in the nth bucket.
2847 local_iterator begin(size_type n) BOOST_NOEXCEPT;
2849 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2851 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2852 //! of the sequence stored in the bucket n.
2854 //! <b>Complexity</b>: Constant.
2856 //! <b>Throws</b>: Nothing.
2858 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2859 //! containing all of the elements in the nth bucket.
2860 const_local_iterator begin(size_type n) const BOOST_NOEXCEPT;
2862 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2864 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2865 //! of the sequence stored in the bucket n.
2867 //! <b>Complexity</b>: Constant.
2869 //! <b>Throws</b>: Nothing.
2871 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2872 //! containing all of the elements in the nth bucket.
2873 const_local_iterator cbegin(size_type n) const BOOST_NOEXCEPT;
2875 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2877 //! <b>Effects</b>: Returns a local_iterator pointing to the end
2878 //! of the sequence stored in the bucket n.
2880 //! <b>Complexity</b>: Constant.
2882 //! <b>Throws</b>: Nothing.
2884 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2885 //! containing all of the elements in the nth bucket.
2886 local_iterator end(size_type n) BOOST_NOEXCEPT;
2888 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2890 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2891 //! of the sequence stored in the bucket n.
2893 //! <b>Complexity</b>: Constant.
2895 //! <b>Throws</b>: Nothing.
2897 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2898 //! containing all of the elements in the nth bucket.
2899 const_local_iterator end(size_type n) const BOOST_NOEXCEPT;
2901 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2903 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2904 //! of the sequence stored in the bucket n.
2906 //! <b>Complexity</b>: Constant.
2908 //! <b>Throws</b>: Nothing.
2910 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2911 //! containing all of the elements in the nth bucket.
2912 const_local_iterator cend(size_type n) const BOOST_NOEXCEPT;
2913 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2915 //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
2916 //! or the same as the old bucket array with a different length. new_size is the length of the
2917 //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
2918 //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
2919 //! 'new_bucket_traits' copy constructor should not throw.
2922 //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is false,
2923 //! unlinks values from the old bucket and inserts then in the new one according
2924 //! to the hash value of values.
2926 //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is true,
2927 //! the implementations avoids moving values as much as possible.
2929 //! Bucket traits hold by *this is assigned from new_bucket_traits.
2930 //! If the container is configured as incremental<>, the split bucket is set
2931 //! to the new bucket_count().
2933 //! If store_hash option is true, this method does not use the hash function.
2934 //! If false, the implementation tries to minimize calls to the hash function
2935 //! (e.g. once for equivalent values if optimize_multikey<true> is true).
2937 //! If rehash is successful updates the internal bucket_traits with new_bucket_traits.
2939 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
2941 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
2942 BOOST_INTRUSIVE_FORCEINLINE void rehash(const bucket_traits &new_bucket_traits)
2943 { this->rehash_impl(new_bucket_traits, false); }
2945 //! <b>Note</b>: This function is used when keys from inserted elements are changed
2946 //! (e.g. a language change when key is a string) but uniqueness and hash properties are
2947 //! preserved so a fast full rehash recovers invariants for *this without extracting and
2948 //! reinserting all elements again.
2950 //! <b>Requires</b>: Calls produced to the hash function should not alter the value uniqueness
2951 //! properties of already inserted elements. If hasher(key1) == hasher(key2) was true when
2952 //! elements were inserted, it shall be true during calls produced in the execution of this function.
2954 //! key_equal is not called inside this function so it is assumed that key_equal(value1, value2)
2955 //! should produce the same results as before for inserted elements.
2957 //! <b>Effects</b>: Reprocesses all values hold by *this, recalculating their hash values
2958 //! and redistributing them though the buckets.
2960 //! If store_hash option is true, this method uses the hash function and updates the stored hash value.
2962 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
2964 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
2965 BOOST_INTRUSIVE_FORCEINLINE void full_rehash()
2966 { this->rehash_impl(this->priv_bucket_traits(), true); }
2968 //! <b>Requires</b>:
2972 //! <b>Complexity</b>:
2976 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2977 bool incremental_rehash(bool grow = true)
2979 //This function is only available for containers with incremental hashing
2980 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
2981 const std::size_t split_idx = this->priv_split_traits().get_size();
2982 const std::size_t bucket_cnt = this->bucket_count();
2986 //Test if the split variable can be changed
2987 if((ret = split_idx < bucket_cnt)){
2988 const std::size_t bucket_to_rehash = split_idx - bucket_cnt/2u;
2989 bucket_type &old_bucket = this->priv_bucket(bucket_to_rehash);
2990 this->priv_split_traits().increment();
2992 //Anti-exception stuff: if an exception is thrown while
2993 //moving elements from old_bucket to the target bucket, all moved
2994 //elements are moved back to the original one.
2995 detail::incremental_rehash_rollback<bucket_type, split_traits> rollback
2996 ( this->priv_bucket(split_idx), old_bucket, this->priv_split_traits());
2997 for( siterator before_i(old_bucket.before_begin()), i(old_bucket.begin()), end_sit(old_bucket.end())
2998 ; i != end_sit; i = before_i, ++i){
2999 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3000 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
3001 const std::size_t new_n = this->priv_hash_to_bucket(hash_value);
3002 siterator const last = (priv_last_in_group)(i);
3003 if(new_n == bucket_to_rehash){
3007 bucket_type &new_b = this->priv_bucket(new_n);
3008 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
3012 this->priv_erasure_update_cache();
3015 else if((ret = split_idx > bucket_cnt/2u)){ //!grow
3016 const std::size_t target_bucket_num = split_idx - 1u - bucket_cnt/2u;
3017 bucket_type &target_bucket = this->priv_bucket(target_bucket_num);
3018 bucket_type &source_bucket = this->priv_bucket(split_idx-1u);
3019 target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket);
3020 this->priv_split_traits().decrement();
3021 this->priv_insertion_update_cache(target_bucket_num);
3026 //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
3027 //! this->bucket_count()/2 or this->bucket_count()*2, or
3028 //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
3029 //! and does nothing.
3031 //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
3032 //! and transfers all the objects from old buckets to the new ones.
3034 //! <b>Complexity</b>: Linear to size().
3036 //! <b>Throws</b>: Nothing
3038 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
3039 bool incremental_rehash(const bucket_traits &new_bucket_traits) BOOST_NOEXCEPT
3041 //This function is only available for containers with incremental hashing
3042 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
3043 std::size_t new_bucket_count = new_bucket_traits.bucket_count();
3044 BOOST_INTRUSIVE_INVARIANT_ASSERT(sizeof(SizeType) >= sizeof(std::size_t) || new_bucket_count <= SizeType(-1));
3045 size_type const new_bucket_traits_size = static_cast<SizeType>(new_bucket_count);
3046 size_type const cur_bucket_traits = this->bucket_count();
3047 const size_type split_idx = this->split_count();
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))
3054 else if(new_bucket_traits_size == cur_bucket_traits/2){
3055 if(!(split_idx <= new_bucket_traits_size))
3062 const size_type ini_n = (size_type)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()[difference_type(n)];
3068 bucket_type &old_bucket = old_buckets[difference_type(n)];
3069 new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket);
3071 //Put cache to safe position
3072 this->priv_initialize_cache();
3073 this->priv_insertion_update_cache(ini_n);
3078 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3080 //! <b>Requires</b>: incremental<> option must be set
3082 //! <b>Effects</b>: returns the current split count
3084 //! <b>Complexity</b>: Constant
3086 //! <b>Throws</b>: Nothing
3087 size_type split_count() const BOOST_NOEXCEPT;
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.
3095 //! <b>Complexity</b>: Amortized constant time.
3097 //! <b>Throws</b>: Nothing.
3098 static size_type suggested_upper_bucket_count(size_type n) BOOST_NOEXCEPT;
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.
3106 //! <b>Complexity</b>: Amortized constant time.
3108 //! <b>Throws</b>: Nothing.
3109 static size_type suggested_lower_bucket_count(size_type n) BOOST_NOEXCEPT;
3110 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3113 friend bool operator==(const hashtable_impl &x, const hashtable_impl &y)
3116 if(constant_time_size && x.size() != y.size()){
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) ){
3132 friend bool operator!=(const hashtable_impl &x, const hashtable_impl &y)
3133 { return !(x == y); }
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()); }
3138 friend bool operator>(const hashtable_impl &x, const hashtable_impl &y)
3141 friend bool operator<=(const hashtable_impl &x, const hashtable_impl &y)
3142 { return !(y < x); }
3144 friend bool operator>=(const hashtable_impl &x, const hashtable_impl &y)
3145 { return !(x < y); }
3148 BOOST_INTRUSIVE_FORCEINLINE void check() const {}
3151 void rehash_impl(const bucket_traits &new_bucket_traits, bool do_full_rehash)
3153 std::size_t nbc = new_bucket_traits.bucket_count();
3154 BOOST_INTRUSIVE_INVARIANT_ASSERT(sizeof(SizeType) >= sizeof(std::size_t) || nbc <= SizeType(-1));
3156 const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
3157 size_type new_bucket_count = static_cast<SizeType>(nbc);
3158 const bucket_ptr old_buckets = this->priv_bucket_pointer();
3159 size_type old_bucket_count = this->bucket_count();
3161 //Check power of two bucket array if the option is activated
3162 BOOST_INTRUSIVE_INVARIANT_ASSERT
3163 (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
3165 size_type n = (size_type)this->priv_get_cache_bucket_num();
3166 const bool same_buffer = old_buckets == new_buckets;
3167 //If the new bucket length is a common factor
3168 //of the old one we can avoid hash calculations.
3169 const bool fast_shrink = (!do_full_rehash) && (!incremental) && (old_bucket_count >= new_bucket_count) &&
3170 (power_2_buckets || (old_bucket_count % new_bucket_count) == 0);
3171 //If we are shrinking the same bucket array and it's
3172 //is a fast shrink, just rehash the last nodes
3173 size_type new_first_bucket_num = new_bucket_count;
3174 if(same_buffer && fast_shrink && (n < new_bucket_count)){
3175 new_first_bucket_num = n;
3176 n = new_bucket_count;
3179 //Anti-exception stuff: they destroy the elements if something goes wrong.
3180 //If the source and destination buckets are the same, the second rollback function
3181 //is harmless, because all elements have been already unlinked and destroyed
3182 typedef detail::init_disposer<node_algorithms> NodeDisposer;
3183 typedef detail::exception_array_disposer<bucket_type, NodeDisposer, size_type> ArrayDisposer;
3184 NodeDisposer node_disp;
3185 ArrayDisposer rollback1(new_buckets[0], node_disp, new_bucket_count);
3186 ArrayDisposer rollback2(old_buckets[0], node_disp, old_bucket_count);
3188 //Put size in a safe value for rollback exception
3189 size_type const size_backup = this->priv_size_traits().get_size();
3190 this->priv_size_traits().set_size(0);
3191 //Put cache to safe position
3192 this->priv_initialize_cache();
3193 this->priv_insertion_update_cache(size_type(0u));
3195 //Iterate through nodes
3196 for(; n < old_bucket_count; ++n){
3197 bucket_type &old_bucket = old_buckets[difference_type(n)];
3199 for( siterator before_i(old_bucket.before_begin()), i(old_bucket.begin()), end_sit(old_bucket.end())
3201 ; i = before_i, ++i){
3203 //First obtain hash value (and store it if do_full_rehash)
3204 std::size_t hash_value;
3206 value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3207 hash_value = this->priv_hasher()(key_of_value()(v));
3208 node_functions_t::store_hash(pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(v)), hash_value, store_hash_t());
3211 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
3212 hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
3215 //Now calculate the new bucket position
3216 const size_type new_n = (size_type)detail::hash_to_bucket_split<power_2_buckets, incremental>
3217 (hash_value, new_bucket_count, new_bucket_count);
3219 //Update first used bucket cache
3220 if(cache_begin && new_n < new_first_bucket_num)
3221 new_first_bucket_num = new_n;
3223 //If the target bucket is new, transfer the whole group
3224 siterator const last = (priv_last_in_group)(i);
3226 if(same_buffer && new_n == n){
3230 bucket_type &new_b = new_buckets[difference_type(new_n)];
3231 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
3236 const size_type new_n = (size_type)detail::hash_to_bucket_split
3237 <power_2_buckets, incremental>(n, new_bucket_count, new_bucket_count);
3238 if(cache_begin && new_n < new_first_bucket_num)
3239 new_first_bucket_num = new_n;
3240 bucket_type &new_b = new_buckets[difference_type(new_n)];
3241 new_b.splice_after( new_b.before_begin()
3243 , old_bucket.before_begin()
3244 , bucket_plus_vtraits_t::priv_get_last(old_bucket, optimize_multikey_t()));
3248 this->priv_size_traits().set_size(size_backup);
3249 this->priv_split_traits().set_size(new_bucket_count);
3250 if(&new_bucket_traits != &this->priv_bucket_traits()){
3251 this->priv_bucket_traits() = new_bucket_traits;
3253 this->priv_initialize_cache();
3254 this->priv_insertion_update_cache(new_first_bucket_num);
3255 rollback1.release();
3256 rollback2.release();
3259 template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
3260 void priv_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
3262 this->clear_and_dispose(disposer);
3263 if(!constant_time_size || !src.empty()){
3264 const size_type src_bucket_count = src.bucket_count();
3265 const size_type dst_bucket_count = this->bucket_count();
3266 //Check power of two bucket array if the option is activated
3267 BOOST_INTRUSIVE_INVARIANT_ASSERT
3268 (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
3269 BOOST_INTRUSIVE_INVARIANT_ASSERT
3270 (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
3271 //If src bucket count is bigger or equal, structural copy is possible
3272 const bool structural_copy = (!incremental) && (src_bucket_count >= dst_bucket_count) &&
3273 (power_2_buckets || (src_bucket_count % dst_bucket_count) == 0);
3274 if(structural_copy){
3275 this->priv_structural_clone_from(src, cloner, disposer);
3278 //Unlike previous cloning algorithm, this can throw
3279 //if cloner, hasher or comparison functor throw
3280 typedef typename detail::if_c< detail::is_const<MaybeConstHashtableImpl>::value
3281 , typename MaybeConstHashtableImpl::const_iterator
3282 , typename MaybeConstHashtableImpl::iterator
3283 >::type clone_iterator;
3284 clone_iterator b(src.begin()), e(src.end());
3285 detail::exception_disposer<hashtable_impl, Disposer> rollback(*this, disposer);
3287 //No need to check for duplicates and insert it in the first position
3288 //as this is an unordered container. So use minimal insertion code
3289 std::size_t const hash_to_store = this->priv_stored_or_compute_hash(*b, store_hash_t());;
3290 size_type const bucket_number = this->priv_hash_to_bucket(hash_to_store);
3291 typedef typename detail::if_c
3292 <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
3293 reference_type r = *b;
3294 this->priv_clone_front_in_bucket<reference_type>(bucket_number, r, hash_to_store, cloner);
3301 template<class ValueReference, class Cloner>
3302 void priv_clone_front_in_bucket( size_type const bucket_number
3303 , typename detail::identity<ValueReference>::type src_ref
3304 , std::size_t const hash_to_store, Cloner cloner)
3306 //No need to check for duplicates and insert it in the first position
3307 //as this is an unordered container. So use minimal insertion code
3308 //std::size_t const hash_value = this->priv_stored_or_compute_hash(src_ref, store_hash_t());;
3309 //size_type const bucket_number = this->priv_hash_to_bucket(hash_value);
3310 bucket_type &cur_bucket = this->priv_bucket(bucket_number);
3311 siterator const prev(cur_bucket.before_begin());
3312 //Just check if the cloned node is equal to the first inserted value in the new bucket
3313 //as equal src values were contiguous and they should be already inserted in the
3314 //destination bucket.
3315 bool const next_is_in_group = optimize_multikey && !cur_bucket.empty() &&
3316 this->priv_equal()( key_of_value()(src_ref)
3317 , key_of_value()(this->priv_value_from_slist_node((++siterator(prev)).pointed_node())));
3318 this->priv_insert_equal_after_find(*cloner(src_ref), bucket_number, hash_to_store, prev, next_is_in_group);
3321 template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
3322 void priv_structural_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
3324 //First clone the first ones
3325 const size_type src_bucket_count = src.bucket_count();
3326 const size_type dst_bucket_count = this->bucket_count();
3327 size_type constructed = 0;
3328 typedef node_cast_adaptor< detail::node_disposer<Disposer, value_traits, CircularSListAlgorithms>
3329 , slist_node_ptr, node_ptr > NodeDisposer;
3330 NodeDisposer node_disp(disposer, &this->priv_value_traits());
3332 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
3333 rollback(this->priv_bucket(0), node_disp, constructed);
3334 //Now insert the remaining ones using the modulo trick
3335 for( //"constructed" already initialized
3336 ; constructed < src_bucket_count
3338 //Since incremental hashing can't be structurally copied, avoid hash_to_bucket_split
3339 const size_type new_n = (size_type) detail::hash_to_bucket(constructed, dst_bucket_count, detail::bool_<power_2_buckets>());
3340 bucket_type &src_b = src.priv_bucket(constructed);
3341 for( siterator b(src_b.begin()), e(src_b.end()); b != e; ++b){
3342 slist_node_ptr const n(b.pointed_node());
3343 typedef typename detail::if_c
3344 <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
3345 reference_type r = this->priv_value_from_slist_node(n);
3346 this->priv_clone_front_in_bucket<reference_type>
3347 (new_n, r, this->priv_stored_hash(n, store_hash_t()), cloner);
3350 this->priv_hasher() = src.priv_hasher();
3351 this->priv_equal() = src.priv_equal();
3353 this->priv_size_traits().set_size(src.priv_size_traits().get_size());
3354 this->priv_split_traits().set_size(dst_bucket_count);
3355 this->priv_insertion_update_cache(0u);
3356 this->priv_erasure_update_cache();
3359 size_type priv_hash_to_bucket(std::size_t hash_value) const
3361 return static_cast<size_type>(detail::hash_to_bucket_split<power_2_buckets, incremental>
3362 (hash_value, this->priv_bucket_traits().bucket_count(), this->priv_split_traits().get_size()));
3365 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)
3367 //Now store hash if needed
3368 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
3369 node_functions_t::store_hash(n, hash_value, store_hash_t());
3370 //Checks for some modes
3371 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(n));
3372 //Shortcut to optimize_multikey cases
3373 group_functions_t::insert_in_group
3374 ( next_is_in_group ? detail::dcast_bucket_ptr<node>((++siterator(prev)).pointed_node()) : n
3375 , n, optimize_multikey_t());
3376 //Update cache and increment size if needed
3377 this->priv_insertion_update_cache(bucket_num);
3378 this->priv_size_traits().increment();
3379 //Insert the element in the bucket after it
3380 return iterator(bucket_type::s_insert_after(prev, *n), &this->get_bucket_value_traits());
3383 template<class KeyType, class KeyHasher, class KeyEqual>
3384 siterator priv_find //In case it is not found previt is bucket.before_begin()
3385 ( const KeyType &key, KeyHasher hash_func
3386 , KeyEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
3389 return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt);
3392 template<class KeyType, class KeyEqual>
3393 bool priv_is_value_equal_to_key(const value_type &v, const std::size_t h, const KeyType &key, KeyEqual equal_func) const
3396 return (!compare_hash || this->priv_stored_or_compute_hash(v, store_hash_t()) == h) && equal_func(key, key_of_value()(v));
3399 //return previous iterator to the next equal range group in case
3400 static siterator priv_last_in_group(const siterator &it_first_in_group) BOOST_NOEXCEPT
3402 return bucket_type::s_iterator_to
3403 (*group_functions_t::get_last_in_group
3404 (detail::dcast_bucket_ptr<node>(it_first_in_group.pointed_node()), optimize_multikey_t()));
3407 template<class KeyType, class KeyEqual>
3408 siterator priv_find_with_hash //In case it is not found previt is bucket.before_begin()
3409 ( const KeyType &key, KeyEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const
3411 bucket_number = this->priv_hash_to_bucket(h);
3412 bucket_type &b = this->priv_bucket(bucket_number);
3413 previt = b.before_begin();
3414 siterator it = previt;
3415 siterator const endit = b.end();
3417 while(++it != endit){
3418 if(this->priv_is_value_equal_to_key(this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func)){
3421 previt = it = (priv_last_in_group)(it);
3423 previt = b.before_begin();
3424 return this->priv_invalid_local_it();
3427 template<class KeyType, class KeyHasher, class KeyEqual>
3428 std::pair<siterator, siterator> priv_local_equal_range
3429 ( const KeyType &key
3430 , KeyHasher hash_func
3431 , KeyEqual equal_func
3432 , size_type &found_bucket
3433 , size_type &cnt) const
3435 std::size_t internal_cnt = 0;
3436 //Let's see if the element is present
3441 std::pair<siterator, siterator> to_return
3442 ( this->priv_find(key, hash_func, equal_func, n_bucket, h, prev)
3443 , this->priv_invalid_local_it());
3445 if(to_return.first != to_return.second){
3446 found_bucket = n_bucket;
3447 //If it's present, find the first that it's not equal in
3449 bucket_type &b = this->priv_bucket(n_bucket);
3450 siterator it = to_return.first;
3451 ++internal_cnt; //At least one is found
3452 if(optimize_multikey){
3453 to_return.second = ++(priv_last_in_group)(it);
3454 internal_cnt += boost::intrusive::iterator_udistance(++it, to_return.second);
3457 siterator const bend = b.end();
3458 while(++it != bend &&
3459 this->priv_is_value_equal_to_key(this->priv_value_from_slist_node(it.pointed_node()), h, key, equal_func)){
3462 to_return.second = it;
3465 cnt = size_type(internal_cnt);
3469 template<class KeyType, class KeyHasher, class KeyEqual>
3470 std::pair<siterator, siterator> priv_equal_range
3471 ( const KeyType &key
3472 , KeyHasher hash_func
3473 , KeyEqual equal_func) const
3478 //Let's see if the element is present
3479 std::pair<siterator, siterator> to_return
3480 (this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt));
3481 //If not, find the next element as ".second" if ".second" local iterator
3482 //is not pointing to an element.
3483 if(to_return.first != to_return.second &&
3484 to_return.second == this->priv_bucket(n_bucket).end()){
3485 to_return.second = this->priv_invalid_local_it();
3487 for( const size_type max_bucket = this->bucket_count()
3488 ; n_bucket != max_bucket
3490 bucket_type &b = this->priv_bucket(n_bucket);
3492 to_return.second = b.begin();
3500 size_type priv_get_bucket_num(siterator it) BOOST_NOEXCEPT
3501 { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); }
3503 size_type priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) BOOST_NOEXCEPT //store_hash
3505 return (size_type)this->priv_hash_to_bucket
3506 (this->priv_stored_hash(it.pointed_node(), store_hash_t()));
3509 size_type priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) BOOST_NOEXCEPT //NO store_hash
3510 { return (size_type)this->priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); }
3512 static siterator priv_get_previous(bucket_type &b, siterator i) BOOST_NOEXCEPT
3513 { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); }
3521 , class PackedOptions
3523 struct make_bucket_traits
3525 //Real value traits must be calculated from options
3526 typedef typename detail::get_value_traits
3527 <T, typename PackedOptions::proto_value_traits>::type value_traits;
3529 typedef typename PackedOptions::bucket_traits specified_bucket_traits;
3531 //Real bucket traits must be calculated from options and calculated value_traits
3532 typedef typename get_slist_impl
3533 <typename reduced_slist_node_traits
3534 <typename value_traits::node_traits>::type
3538 detail::if_c< detail::is_same
3539 < specified_bucket_traits
3540 , default_bucket_traits
3542 , bucket_traits_impl<slist_impl>
3543 , specified_bucket_traits
3548 //! Helper metafunction to define a \c hashtable that yields to the same type when the
3549 //! same options (either explicitly or implicitly) are used.
3550 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3551 template<class T, class ...Options>
3553 template<class T, class O1 = void, class O2 = void
3554 , class O3 = void, class O4 = void
3555 , class O5 = void, class O6 = void
3556 , class O7 = void, class O8 = void
3557 , class O9 = void, class O10= void
3560 struct make_hashtable
3563 typedef typename pack_options
3564 < hashtable_defaults,
3565 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3566 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3570 >::type packed_options;
3572 typedef typename detail::get_value_traits
3573 <T, typename packed_options::proto_value_traits>::type value_traits;
3575 typedef typename make_bucket_traits
3576 <T, false, packed_options>::type bucket_traits;
3578 typedef hashtable_impl
3580 , typename packed_options::key_of_value
3581 , typename packed_options::hash
3582 , typename packed_options::equal
3584 , typename packed_options::size_type
3585 , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
3586 |(std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
3587 |(std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
3588 |(std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
3589 |(std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
3590 |(std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
3591 > implementation_defined;
3594 typedef implementation_defined type;
3597 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3599 #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3600 template<class T, class ...Options>
3602 template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
3605 : public make_hashtable<T,
3606 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3607 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3613 typedef typename make_hashtable<T,
3614 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3615 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3620 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
3623 typedef typename Base::value_traits value_traits;
3624 typedef typename Base::iterator iterator;
3625 typedef typename Base::const_iterator const_iterator;
3626 typedef typename Base::bucket_ptr bucket_ptr;
3627 typedef typename Base::size_type size_type;
3628 typedef typename Base::hasher hasher;
3629 typedef typename Base::bucket_traits bucket_traits;
3630 typedef typename Base::key_equal key_equal;
3632 //Assert if passed value traits are compatible with the type
3633 BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
3635 BOOST_INTRUSIVE_FORCEINLINE explicit hashtable ( const bucket_traits &b_traits
3636 , const hasher & hash_func = hasher()
3637 , const key_equal &equal_func = key_equal()
3638 , const value_traits &v_traits = value_traits())
3639 : Base(b_traits, hash_func, equal_func, v_traits)
3642 BOOST_INTRUSIVE_FORCEINLINE hashtable(BOOST_RV_REF(hashtable) x)
3643 : Base(BOOST_MOVE_BASE(Base, x))
3646 BOOST_INTRUSIVE_FORCEINLINE hashtable& operator=(BOOST_RV_REF(hashtable) x)
3647 { return static_cast<hashtable&>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
3649 template <class Cloner, class Disposer>
3650 BOOST_INTRUSIVE_FORCEINLINE void clone_from(const hashtable &src, Cloner cloner, Disposer disposer)
3651 { Base::clone_from(src, cloner, disposer); }
3653 template <class Cloner, class Disposer>
3654 BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(hashtable) src, Cloner cloner, Disposer disposer)
3655 { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
3660 } //namespace intrusive
3663 #include <boost/intrusive/detail/config_end.hpp>
3665 #endif //BOOST_INTRUSIVE_HASHTABLE_HPP