1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2008
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>
17 #include <functional> //std::equal_to
18 #include <utility> //std::pair
19 #include <algorithm> //std::swap, std::lower_bound, std::upper_bound
20 #include <cstddef> //std::size_t
21 #include <iterator> //std::iterator_traits
23 #include <boost/intrusive/detail/assert.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/functional/hash.hpp>
26 //General intrusive utilities
27 #include <boost/intrusive/intrusive_fwd.hpp>
28 #include <boost/intrusive/detail/pointer_to_other.hpp>
29 #include <boost/intrusive/detail/hashtable_node.hpp>
30 #include <boost/intrusive/detail/transform_iterator.hpp>
31 #include <boost/intrusive/link_mode.hpp>
32 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
33 #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
34 //Implementation utilities
35 #include <boost/intrusive/trivial_value_traits.hpp>
36 #include <boost/intrusive/unordered_set_hook.hpp>
37 #include <boost/intrusive/slist.hpp>
38 #include <boost/intrusive/detail/mpl.hpp>
47 struct hash_bool_flags
49 static const std::size_t unique_keys_pos
= 1u;
50 static const std::size_t constant_time_size_pos
= 2u;
51 static const std::size_t power_2_buckets_pos
= 4u;
52 static const std::size_t cache_begin_pos
= 8u;
53 static const std::size_t compare_hash_pos
= 16u;
54 static const std::size_t incremental_pos
= 32u;
63 , std::size_t BoolFlags
67 typedef ValueTraits value_traits
;
70 typedef SizeType size_type
;
71 typedef BucketTraits bucket_traits
;
72 static const std::size_t bool_flags
= BoolFlags
;
77 , std::size_t BoolMask
82 <typename
UsetOpt::value_traits
83 ,typename
UsetOpt::hash
84 ,typename
UsetOpt::equal
85 ,typename
UsetOpt::size_type
86 ,typename
UsetOpt::bucket_traits
87 ,UsetOpt::bool_flags
& BoolMask
91 template <class NodeTraits
>
92 struct hash_reduced_slist_node_traits
94 template <class U
> static detail::one
test(...);
95 template <class U
> static detail::two
test(typename
U::reduced_slist_node_traits
* = 0);
96 static const bool value
= sizeof(test
<NodeTraits
>(0)) == sizeof(detail::two
);
99 template <class NodeTraits
>
100 struct apply_reduced_slist_node_traits
102 typedef typename
NodeTraits::reduced_slist_node_traits type
;
105 template <class NodeTraits
>
106 struct reduced_slist_node_traits
108 typedef typename
detail::eval_if_c
109 < hash_reduced_slist_node_traits
<NodeTraits
>::value
110 , apply_reduced_slist_node_traits
<NodeTraits
>
111 , detail::identity
<NodeTraits
>
115 template<class NodeTraits
>
116 struct get_slist_impl
118 typedef trivial_value_traits
<NodeTraits
, normal_link
> trivial_traits
;
120 //Reducing symbol length
121 struct type
: make_slist
122 < typename
NodeTraits::node
123 , boost::intrusive::value_traits
<trivial_traits
>
124 , boost::intrusive::constant_time_size
<false>
125 , boost::intrusive::size_type
<std::size_t>
130 template<class SupposedValueTraits
>
131 struct real_from_supposed_value_traits
133 typedef typename
detail::eval_if_c
134 < detail::external_value_traits_is_true
135 <SupposedValueTraits
>::value
136 , detail::eval_value_traits
137 <SupposedValueTraits
>
139 <SupposedValueTraits
>
143 template<class SupposedValueTraits
>
144 struct get_slist_impl_from_supposed_value_traits
147 real_from_supposed_value_traits
148 < SupposedValueTraits
>::type real_value_traits
;
149 typedef typename
detail::get_node_traits
150 <real_value_traits
>::type node_traits
;
151 typedef typename get_slist_impl
152 <typename reduced_slist_node_traits
157 template<class SupposedValueTraits
>
158 struct unordered_bucket_impl
162 get_slist_impl_from_supposed_value_traits
163 <SupposedValueTraits
>::type slist_impl
;
164 typedef detail::bucket_impl
<slist_impl
> implementation_defined
;
166 typedef implementation_defined type
;
169 template<class SupposedValueTraits
>
170 struct unordered_bucket_ptr_impl
173 typedef typename
detail::get_node_traits
174 <SupposedValueTraits
>::type::node_ptr node_ptr
;
175 typedef typename unordered_bucket_impl
176 <SupposedValueTraits
>::type bucket_type
;
177 typedef typename
boost::pointer_to_other
178 <node_ptr
, bucket_type
>::type implementation_defined
;
180 typedef implementation_defined type
;
184 struct store_hash_bool
187 struct two_or_three
{one _
[2 + Add
];};
188 template <class U
> static one
test(...);
189 template <class U
> static two_or_three
<U::store_hash
>
190 test (detail::bool_
<U::store_hash
>* = 0);
191 static const std::size_t value
= sizeof(test
<T
>(0));
195 struct store_hash_is_true
197 static const bool value
= store_hash_bool
<T
>::value
> sizeof(one
)*2;
201 struct optimize_multikey_bool
204 struct two_or_three
{one _
[2 + Add
];};
205 template <class U
> static one
test(...);
206 template <class U
> static two_or_three
<U::optimize_multikey
>
207 test (detail::bool_
<U::optimize_multikey
>* = 0);
208 static const std::size_t value
= sizeof(test
<T
>(0));
212 struct optimize_multikey_is_true
214 static const bool value
= optimize_multikey_bool
<T
>::value
> sizeof(one
)*2;
217 template<class Config
>
218 struct bucket_plus_size
219 : public detail::size_holder
220 < 0 != (Config::bool_flags
& hash_bool_flags::constant_time_size_pos
)
221 , typename
Config::size_type
>
223 typedef detail::size_holder
224 < 0 != (Config::bool_flags
& hash_bool_flags::constant_time_size_pos
)
225 , typename
Config::size_type
> size_traits
;
226 typedef typename
Config::bucket_traits bucket_traits
;
228 bucket_plus_size(const bucket_traits
&b_traits
)
229 : bucket_traits_(b_traits
)
231 bucket_traits bucket_traits_
;
234 template<class Config
>
236 : public detail::ebo_functor_holder
<typename
Config::hash
>
238 typedef typename
Config::hash hasher
;
239 typedef detail::size_holder
240 < 0 != (Config::bool_flags
& hash_bool_flags::constant_time_size_pos
)
241 , typename
Config::size_type
> size_traits
;
242 typedef typename
Config::bucket_traits bucket_traits
;
244 bucket_hash_t(const bucket_traits
&b_traits
, const hasher
& h
)
245 : detail::ebo_functor_holder
<hasher
>(h
), bucket_plus_size_(b_traits
)
248 bucket_plus_size
<Config
> bucket_plus_size_
;
251 template<class Config
, bool>
252 struct bucket_hash_equal_t
: public detail::ebo_functor_holder
<typename
Config::equal
>
254 typedef typename
Config::equal equal
;
255 typedef typename
Config::hash hasher
;
256 typedef typename
Config::bucket_traits bucket_traits
;
258 bucket_hash_equal_t(const bucket_traits
&b_traits
, const hasher
& h
, const equal
&e
)
259 : detail::ebo_functor_holder
<typename
Config::equal
>(e
), bucket_hash(b_traits
, h
)
261 bucket_hash_t
<Config
> bucket_hash
;
264 template<class Config
> //cache_begin == true version
265 struct bucket_hash_equal_t
<Config
, true>
266 : public detail::ebo_functor_holder
<typename
Config::equal
>
268 typedef typename
Config::equal equal
;
269 typedef typename
Config::hash hasher
;
270 typedef typename
Config::bucket_traits bucket_traits
;
271 typedef typename unordered_bucket_ptr_impl
272 <typename
Config::value_traits
>::type bucket_ptr
;
274 bucket_hash_equal_t(const bucket_traits
&b_traits
, const hasher
& h
, const equal
&e
)
275 : detail::ebo_functor_holder
<typename
Config::equal
>(e
), bucket_hash(b_traits
, h
)
277 bucket_hash_t
<Config
> bucket_hash
;
278 bucket_ptr cached_begin_
;
281 template<class Config
>
282 struct hashtable_data_t
: public Config::value_traits
284 static const std::size_t bool_flags
= Config::bool_flags
;
285 typedef typename
Config::value_traits value_traits
;
286 typedef typename
Config::equal equal
;
287 typedef typename
Config::hash hasher
;
288 typedef typename
Config::bucket_traits bucket_traits
;
290 hashtable_data_t( const bucket_traits
&b_traits
, const hasher
& h
291 , const equal
&e
, const value_traits
&val_traits
)
292 : Config::value_traits(val_traits
), internal_(b_traits
, h
, e
)
294 typedef typename
detail::usetopt_mask
296 , detail::hash_bool_flags::constant_time_size_pos
297 | detail::hash_bool_flags::incremental_pos
298 >::type masked_config_t
;
300 : public detail::size_holder
301 < 0 != (Config::bool_flags
& hash_bool_flags::incremental_pos
)
302 , typename
Config::size_type
>
304 internal(const bucket_traits
&b_traits
, const hasher
& h
, const equal
&e
)
305 : bucket_hash_equal_(b_traits
, h
, e
)
310 , 0 != (bool_flags
& hash_bool_flags::cache_begin_pos
)
311 > bucket_hash_equal_
;
315 struct insert_commit_data_impl
320 template<class NodeTraits
>
321 struct group_functions
323 typedef NodeTraits node_traits
;
324 typedef unordered_group_adapter
<node_traits
> group_traits
;
325 typedef typename
node_traits::node_ptr node_ptr
;
326 typedef typename
node_traits::node node
;
327 typedef typename reduced_slist_node_traits
328 <node_traits
>::type reduced_node_traits
;
329 typedef typename
reduced_node_traits::node_ptr slist_node_ptr
;
330 typedef typename
reduced_node_traits::node slist_node
;
331 typedef circular_slist_algorithms
<group_traits
> group_algorithms
;
333 static node_ptr
dcast_bucket_ptr(slist_node_ptr p
)
334 { return node_ptr(&static_cast<node
&>(*p
)); }
336 static slist_node_ptr priv_get_bucket_before_begin
337 (slist_node_ptr bucket_beg
, slist_node_ptr bucket_end
, node_ptr p
)
339 //First find the last node of p's group.
340 //This requires checking the first node of the next group or
342 node_ptr prev_node
= p
;
343 node_ptr
nxt(node_traits::get_next(p
));
344 while(!(bucket_beg
<= nxt
&& nxt
<= bucket_end
) &&
345 (group_traits::get_next(nxt
) == prev_node
)){
347 nxt
= node_traits::get_next(nxt
);
350 //If we've reached the bucket node just return it.
351 if(bucket_beg
<= nxt
&& nxt
<= bucket_end
){
355 //Otherwise, iterate using group links until the bucket node
356 node_ptr first_node_of_group
= nxt
;
357 node_ptr last_node_group
= group_traits::get_next(first_node_of_group
);
358 slist_node_ptr possible_end
= node_traits::get_next(last_node_group
);
360 while(!(bucket_beg
<= possible_end
&& possible_end
<= bucket_end
)){
361 first_node_of_group
= dcast_bucket_ptr(possible_end
);
362 last_node_group
= group_traits::get_next(first_node_of_group
);
363 possible_end
= node_traits::get_next(last_node_group
);
368 static node_ptr
priv_get_prev_to_first_in_group(slist_node_ptr bucket_node
, node_ptr first_in_group
)
370 //Just iterate using group links and obtain the node
371 //before "first_in_group)"
372 node_ptr prev_node
= dcast_bucket_ptr(bucket_node
);
373 node_ptr
nxt(node_traits::get_next(prev_node
));
374 while(nxt
!= first_in_group
){
375 prev_node
= group_traits::get_next(nxt
);
376 nxt
= node_traits::get_next(prev_node
);
381 static node_ptr
priv_get_first_in_group_of_last_in_group(node_ptr last_in_group
)
383 //Just iterate using group links and obtain the node
384 //before "last_in_group"
385 node_ptr possible_first
= group_traits::get_next(last_in_group
);
386 node_ptr possible_first_prev
= group_traits::get_next(possible_first
);
387 // The deleted node is at the end of the group, so the
388 // node in the group pointing to it is at the beginning
389 // of the group. Find that to change its pointer.
390 while(possible_first_prev
!= last_in_group
){
391 possible_first
= possible_first_prev
;
392 possible_first_prev
= group_traits::get_next(possible_first
);
394 return possible_first
;
398 static void priv_erase_from_group(slist_node_ptr end_ptr
, node_ptr to_erase_ptr
, detail::true_
)
400 node_ptr
nxt_ptr(node_traits::get_next(to_erase_ptr
));
401 node_ptr
prev_in_group_ptr(group_traits::get_next(to_erase_ptr
));
402 bool last_in_group
= (end_ptr
== nxt_ptr
) ||
403 (group_traits::get_next(nxt_ptr
) != to_erase_ptr
);
404 bool first_in_group
= node_traits::get_next(prev_in_group_ptr
) != to_erase_ptr
;
406 if(first_in_group
&& last_in_group
){
407 group_algorithms::init(to_erase_ptr
);
409 else if(first_in_group
){
410 group_algorithms::unlink_after(nxt_ptr
);
412 else if(last_in_group
){
413 node_ptr first_in_group
=
414 priv_get_first_in_group_of_last_in_group(to_erase_ptr
);
415 group_algorithms::unlink_after(first_in_group
);
418 group_algorithms::unlink_after(nxt_ptr
);
422 static void priv_erase_from_group(slist_node_ptr
, node_ptr
, detail::false_
)
425 static node_ptr
priv_get_last_in_group(node_ptr first_in_group
, detail::true_
)
426 { return group_traits::get_next(first_in_group
); }
428 static node_ptr
priv_get_last_in_group(node_ptr n
, detail::false_
)
432 template<class BucketType
, class SplitTraits
>
433 class incremental_rehash_rollback
436 typedef BucketType bucket_type
;
437 typedef SplitTraits split_traits
;
439 incremental_rehash_rollback();
440 incremental_rehash_rollback
& operator=(const incremental_rehash_rollback
&);
441 incremental_rehash_rollback (const incremental_rehash_rollback
&);
444 incremental_rehash_rollback
445 (bucket_type
&source_bucket
, bucket_type
&destiny_bucket
, split_traits
&split_traits
)
446 : source_bucket_(source_bucket
), destiny_bucket_(destiny_bucket
)
447 , split_traits_(split_traits
), released_(false)
451 { released_
= true; }
453 ~incremental_rehash_rollback()
456 //If an exception is thrown, just put all moved nodes back in the old bucket
457 //and move back the split mark.
458 destiny_bucket_
.splice_after(destiny_bucket_
.before_begin(), source_bucket_
);
459 split_traits_
.decrement();
464 bucket_type
&source_bucket_
;
465 bucket_type
&destiny_bucket_
;
466 split_traits
&split_traits_
;
470 } //namespace detail {
472 //!This metafunction will obtain the type of a bucket
473 //!from the value_traits or hook option to be used with
475 template<class ValueTraitsOrHookOption
>
476 struct unordered_bucket
477 : public detail::unordered_bucket_impl
478 <typename
ValueTraitsOrHookOption::
479 template pack
<none
>::value_traits
483 //!This metafunction will obtain the type of a bucket pointer
484 //!from the value_traits or hook option to be used with
486 template<class ValueTraitsOrHookOption
>
487 struct unordered_bucket_ptr
488 : public detail::unordered_bucket_ptr_impl
489 <typename
ValueTraitsOrHookOption::
490 template pack
<none
>::value_traits
494 //!This metafunction will obtain the type of the default bucket traits
495 //!(when the user does not specify the bucket_traits<> option) from the
496 //!value_traits or hook option to be used with
498 template<class ValueTraitsOrHookOption
>
499 struct unordered_default_bucket_traits
502 typedef typename
ValueTraitsOrHookOption::
503 template pack
<none
>::value_traits supposed_value_traits
;
504 typedef typename
detail::
505 get_slist_impl_from_supposed_value_traits
506 <supposed_value_traits
>::type slist_impl
;
507 typedef detail::bucket_traits_impl
508 <slist_impl
> implementation_defined
;
510 typedef implementation_defined type
;
513 struct default_bucket_traits
;
519 , base_hook
<detail::default_uset_hook
>
520 , constant_time_size
<true>
521 , size_type
<std::size_t>
522 , equal
<std::equal_to
<T
> >
523 , hash
<boost::hash
<T
> >
524 , bucket_traits
<default_bucket_traits
>
525 , power_2_buckets
<false>
527 , compare_hash
<false>
534 //! The class template hashtable is an intrusive hash table container, that
535 //! is used to construct intrusive unordered_set and unordered_multiset containers. The
536 //! no-throw guarantee holds only, if the Equal object and Hasher don't throw.
538 //! hashtable is a semi-intrusive container: each object to be stored in the
539 //! container must contain a proper hook, but the container also needs
540 //! additional auxiliary memory to work: hashtable needs a pointer to an array
541 //! of type `bucket_type` to be passed in the constructor. This bucket array must
542 //! have at least the same lifetime as the container. This makes the use of
543 //! hashtable more complicated than purely intrusive containers.
544 //! `bucket_type` is default-constructible, copyable and assignable
546 //! The template parameter \c T is the type to be managed by the container.
547 //! The user can specify additional options and if no options are provided
548 //! default options are used.
550 //! The container supports the following options:
551 //! \c base_hook<>/member_hook<>/value_traits<>,
552 //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
553 //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
555 //! hashtable only provides forward iterators but it provides 4 iterator types:
556 //! iterator and const_iterator to navigate through the whole container and
557 //! local_iterator and const_local_iterator to navigate through the values
558 //! stored in a single bucket. Local iterators are faster and smaller.
560 //! It's not recommended to use non constant-time size hashtables because several
561 //! key functions, like "empty()", become non-constant time functions. Non
562 //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
564 //! hashtables, does not make automatic rehashings nor
565 //! offers functions related to a load factor. Rehashing can be explicitly requested
566 //! and the user must provide a new bucket array that will be used from that moment.
568 //! Since no automatic rehashing is done, iterators are never invalidated when
569 //! inserting or erasing elements. Iterators are only invalidated when rehashing.
570 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
571 template<class T
, class ...Options
>
573 template<class Config
>
576 : private detail::clear_on_destructor_base
<hashtable_impl
<Config
> >
578 template<class C
> friend class detail::clear_on_destructor_base
;
580 typedef typename
Config::value_traits value_traits
;
583 static const bool external_value_traits
=
584 detail::external_value_traits_is_true
<value_traits
>::value
;
585 typedef typename
detail::eval_if_c
586 < external_value_traits
587 , detail::eval_value_traits
<value_traits
>
588 , detail::identity
<value_traits
>
589 >::type real_value_traits
;
590 typedef typename
Config::bucket_traits bucket_traits
;
591 static const bool external_bucket_traits
=
592 detail::external_bucket_traits_is_true
<bucket_traits
>::value
;
593 typedef typename
detail::eval_if_c
594 < external_bucket_traits
595 , detail::eval_bucket_traits
<bucket_traits
>
596 , detail::identity
<bucket_traits
>
597 >::type real_bucket_traits
;
598 typedef typename
detail::get_slist_impl
599 <typename
detail::reduced_slist_node_traits
600 <typename
real_value_traits::node_traits
>::type
604 typedef typename
real_value_traits::pointer pointer
;
605 typedef typename
real_value_traits::const_pointer const_pointer
;
606 typedef typename
std::iterator_traits
<pointer
>::value_type value_type
;
607 typedef typename
std::iterator_traits
<pointer
>::reference reference
;
608 typedef typename
std::iterator_traits
<const_pointer
>::reference const_reference
;
609 typedef typename
std::iterator_traits
<pointer
>::difference_type difference_type
;
610 typedef typename
Config::size_type size_type
;
611 typedef value_type key_type
;
612 typedef typename
Config::equal key_equal
;
613 typedef typename
Config::hash hasher
;
614 typedef detail::bucket_impl
<slist_impl
> bucket_type
;
615 typedef typename
boost::pointer_to_other
616 <pointer
, bucket_type
>::type bucket_ptr
;
617 typedef typename
slist_impl::iterator siterator
;
618 typedef typename
slist_impl::const_iterator const_siterator
;
619 typedef detail::hashtable_iterator
<hashtable_impl
, false> iterator
;
620 typedef detail::hashtable_iterator
<hashtable_impl
, true> const_iterator
;
621 typedef typename
real_value_traits::node_traits node_traits
;
622 typedef typename
node_traits::node node
;
623 typedef typename
boost::pointer_to_other
624 <pointer
, node
>::type node_ptr
;
625 typedef typename
boost::pointer_to_other
626 <node_ptr
, const node
>::type const_node_ptr
;
627 typedef typename
slist_impl::node_algorithms node_algorithms
;
629 static const bool stateful_value_traits
= detail::store_cont_ptr_on_it
<hashtable_impl
>::value
;
630 static const bool store_hash
= detail::store_hash_is_true
<node_traits
>::value
;
632 static const bool unique_keys
= 0 != (Config::bool_flags
& detail::hash_bool_flags::unique_keys_pos
);
633 static const bool constant_time_size
= 0 != (Config::bool_flags
& detail::hash_bool_flags::constant_time_size_pos
);
634 static const bool cache_begin
= 0 != (Config::bool_flags
& detail::hash_bool_flags::cache_begin_pos
);
635 static const bool compare_hash
= 0 != (Config::bool_flags
& detail::hash_bool_flags::compare_hash_pos
);
636 static const bool incremental
= 0 != (Config::bool_flags
& detail::hash_bool_flags::incremental_pos
);
637 static const bool power_2_buckets
= incremental
|| (0 != (Config::bool_flags
& detail::hash_bool_flags::power_2_buckets_pos
));
639 static const bool optimize_multikey
640 = detail::optimize_multikey_is_true
<node_traits
>::value
&& !unique_keys
;
645 //Configuration error: compare_hash<> can't be specified without store_hash<>
646 //See documentation for more explanations
647 BOOST_STATIC_ASSERT((!compare_hash
|| store_hash
));
649 typedef typename
slist_impl::node_ptr slist_node_ptr
;
650 typedef typename
boost::pointer_to_other
651 <slist_node_ptr
, void>::type void_pointer
;
652 //We'll define group traits, but these won't be instantiated if
653 //optimize_multikey is not true
654 typedef unordered_group_adapter
<node_traits
> group_traits
;
655 typedef circular_slist_algorithms
<group_traits
> group_algorithms
;
656 typedef detail::bool_
<store_hash
> store_hash_t
;
657 typedef detail::bool_
<optimize_multikey
> optimize_multikey_t
;
658 typedef detail::bool_
<cache_begin
> cache_begin_t
;
659 typedef detail::bool_
<power_2_buckets
> power_2_buckets_t
;
660 typedef detail::size_holder
<constant_time_size
, size_type
> size_traits
;
661 typedef detail::size_holder
<incremental
, size_type
> split_traits
;
662 typedef detail::group_functions
<node_traits
> group_functions_t
;
664 static const std::size_t hashtable_data_bool_flags_mask
=
665 ( detail::hash_bool_flags::cache_begin_pos
666 | detail::hash_bool_flags::constant_time_size_pos
667 | detail::hash_bool_flags::incremental_pos
669 typedef typename
detail::usetopt_mask
670 <Config
, hashtable_data_bool_flags_mask
>::type masked_config_t
;
671 detail::hashtable_data_t
<masked_config_t
> data_
;
673 template<bool IsConst
>
674 struct downcast_node_to_value
675 : public detail::node_to_value
<hashtable_impl
, IsConst
>
677 typedef detail::node_to_value
<hashtable_impl
, IsConst
> base_t
;
678 typedef typename
base_t::result_type result_type
;
679 typedef typename
detail::add_const_if_c
680 <typename
slist_impl::node
, IsConst
>::type
&first_argument_type
;
681 typedef typename
detail::add_const_if_c
682 <node
, IsConst
>::type
&intermediate_argument_type
;
684 downcast_node_to_value(const hashtable_impl
*cont
)
688 result_type
operator()(first_argument_type arg
) const
689 { return this->base_t::operator()(static_cast<intermediate_argument_type
>(arg
)); }
693 struct node_cast_adaptor
694 : private detail::ebo_functor_holder
<F
>
696 typedef detail::ebo_functor_holder
<F
> base_t
;
698 template<class ConvertibleToF
>
699 node_cast_adaptor(const ConvertibleToF
&c2f
, const hashtable_impl
*cont
)
700 : base_t(base_t(c2f
, cont
))
703 typename
base_t::node_ptr
operator()(const typename
slist_impl::node
&to_clone
)
704 { return base_t::operator()(static_cast<const node
&>(to_clone
)); }
706 void operator()(typename
slist_impl::node_ptr to_clone
)
707 { base_t::operator()(node_ptr(&static_cast<node
&>(*to_clone
))); }
712 hashtable_impl (const hashtable_impl
&);
713 hashtable_impl
operator =(const hashtable_impl
&);
715 enum { safemode_or_autounlink
=
716 (int)real_value_traits::link_mode
== (int)auto_unlink
||
717 (int)real_value_traits::link_mode
== (int)safe_link
};
719 //Constant-time size is incompatible with auto-unlink hooks!
720 BOOST_STATIC_ASSERT(!(constant_time_size
&& ((int)real_value_traits::link_mode
== (int)auto_unlink
)));
721 //Cache begin is incompatible with auto-unlink hooks!
722 BOOST_STATIC_ASSERT(!(cache_begin
&& ((int)real_value_traits::link_mode
== (int)auto_unlink
)));
724 template<class Disposer
>
725 node_cast_adaptor
<detail::node_disposer
<Disposer
, hashtable_impl
> >
726 make_node_disposer(const Disposer
&disposer
) const
727 { return node_cast_adaptor
<detail::node_disposer
<Disposer
, hashtable_impl
> >(disposer
, this); }
732 typedef detail::insert_commit_data_impl insert_commit_data
;
734 typedef detail::transform_iterator
735 < typename
slist_impl::iterator
736 , downcast_node_to_value
<false> > local_iterator
;
738 typedef detail::transform_iterator
739 < typename
slist_impl::iterator
740 , downcast_node_to_value
<true> > const_local_iterator
;
744 const real_value_traits
&get_real_value_traits(detail::bool_
<false>) const
745 { return this->data_
; }
747 const real_value_traits
&get_real_value_traits(detail::bool_
<true>) const
748 { return data_
.get_value_traits(*this); }
750 real_value_traits
&get_real_value_traits(detail::bool_
<false>)
751 { return this->data_
; }
753 real_value_traits
&get_real_value_traits(detail::bool_
<true>)
754 { return data_
.get_value_traits(*this); }
760 const real_value_traits
&get_real_value_traits() const
761 { return this->get_real_value_traits(detail::bool_
<external_value_traits
>()); }
763 real_value_traits
&get_real_value_traits()
764 { return this->get_real_value_traits(detail::bool_
<external_value_traits
>()); }
766 //! <b>Requires</b>: buckets must not be being used by any other resource.
768 //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
769 //! to the bucket array and copies of the key_hasher and equal_func functors.
771 //! <b>Complexity</b>: Constant.
773 //! <b>Throws</b>: If value_traits::node_traits::node
774 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
775 //! or the copy constructor or invocation of hash_func or equal_func throws.
777 //! <b>Notes</b>: buckets array must be disposed only after
778 //! *this is disposed.
779 hashtable_impl ( const bucket_traits
&b_traits
780 , const hasher
& hash_func
= hasher()
781 , const key_equal
&equal_func
= key_equal()
782 , const value_traits
&v_traits
= value_traits())
783 : data_(b_traits
, hash_func
, equal_func
, v_traits
)
785 priv_initialize_buckets();
786 this->priv_size_traits().set_size(size_type(0));
787 size_type bucket_size
= this->priv_buckets_len();
788 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_size
!= 0);
789 //Check power of two bucket array if the option is activated
790 BOOST_INTRUSIVE_INVARIANT_ASSERT
791 (!power_2_buckets
|| (0 == (bucket_size
& (bucket_size
-1))));
792 priv_split_traits().set_size(bucket_size
>>1);
795 //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
796 //! are not deleted (i.e. no destructors are called).
798 //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
799 //! it's a safe-mode or auto-unlink value. Otherwise constant.
801 //! <b>Throws</b>: Nothing.
805 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
807 //! <b>Complexity</b>: Amortized constant time.
808 //! Worst case (empty unordered_set): O(this->bucket_count())
810 //! <b>Throws</b>: Nothing.
812 { return iterator(this->priv_begin(), this); }
814 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
815 //! of the unordered_set.
817 //! <b>Complexity</b>: Amortized constant time.
818 //! Worst case (empty unordered_set): O(this->bucket_count())
820 //! <b>Throws</b>: Nothing.
821 const_iterator
begin() const
822 { return this->cbegin(); }
824 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
825 //! of the unordered_set.
827 //! <b>Complexity</b>: Amortized constant time.
828 //! Worst case (empty unordered_set): O(this->bucket_count())
830 //! <b>Throws</b>: Nothing.
831 const_iterator
cbegin() const
832 { return const_iterator(this->priv_begin(), this); }
834 //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
836 //! <b>Complexity</b>: Constant.
838 //! <b>Throws</b>: Nothing.
840 { return iterator(priv_invalid_local_it(), 0); }
842 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
844 //! <b>Complexity</b>: Constant.
846 //! <b>Throws</b>: Nothing.
847 const_iterator
end() const
848 { return this->cend(); }
850 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
852 //! <b>Complexity</b>: Constant.
854 //! <b>Throws</b>: Nothing.
855 const_iterator
cend() const
856 { return const_iterator(priv_invalid_local_it(), 0); }
858 //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
860 //! <b>Complexity</b>: Constant.
862 //! <b>Throws</b>: If hasher copy-constructor throws.
863 hasher
hash_function() const
864 { return this->priv_hasher(); }
866 //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
868 //! <b>Complexity</b>: Constant.
870 //! <b>Throws</b>: If key_equal copy-constructor throws.
871 key_equal
key_eq() const
872 { return this->priv_equal(); }
874 //! <b>Effects</b>: Returns true if the container is empty.
876 //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
877 //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
878 //! Otherwise constant.
880 //! <b>Throws</b>: Nothing.
883 if(constant_time_size
){
884 return !this->size();
886 else if(cache_begin
){
887 return this->begin() == this->end();
890 size_type buckets_len
= this->priv_buckets_len();
891 const bucket_type
*b
= detail::get_pointer(this->priv_buckets());
892 for (size_type n
= 0; n
< buckets_len
; ++n
, ++b
){
901 //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
903 //! <b>Complexity</b>: Linear to elements contained in *this if
904 //! constant_time_size is false. Constant-time otherwise.
906 //! <b>Throws</b>: Nothing.
907 size_type
size() const
909 if(constant_time_size
)
910 return this->priv_size_traits().get_size();
913 size_type buckets_len
= this->priv_buckets_len();
914 const bucket_type
*b
= detail::get_pointer(this->priv_buckets());
915 for (size_type n
= 0; n
< buckets_len
; ++n
, ++b
){
922 //! <b>Requires</b>: the hasher and the equality function unqualified swap
923 //! call should not throw.
925 //! <b>Effects</b>: Swaps the contents of two unordered_sets.
926 //! Swaps also the contained bucket array and equality and hasher functors.
928 //! <b>Complexity</b>: Constant.
930 //! <b>Throws</b>: If the swap() call for the comparison or hash functors
931 //! found using ADL throw. Basic guarantee.
932 void swap(hashtable_impl
& other
)
936 swap(this->priv_equal(), other
.priv_equal());
937 swap(this->priv_hasher(), other
.priv_hasher());
939 swap(this->priv_real_bucket_traits(), other
.priv_real_bucket_traits());
940 priv_swap_cache(cache_begin_t(), other
);
941 if(constant_time_size
){
942 size_type backup
= this->priv_size_traits().get_size();
943 this->priv_size_traits().set_size(other
.priv_size_traits().get_size());
944 other
.priv_size_traits().set_size(backup
);
946 else if(incremental
){
947 size_type backup
= this->priv_split_traits().get_size();
948 this->priv_split_traits().set_size(other
.priv_split_traits().get_size());
949 other
.priv_split_traits().set_size(backup
);
953 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
954 //! Cloner should yield to nodes that compare equal and produce the same
955 //! hash than the original node.
957 //! <b>Effects</b>: Erases all the elements from *this
958 //! calling Disposer::operator()(pointer), clones all the
959 //! elements from src calling Cloner::operator()(const_reference )
960 //! and inserts them on *this. The hash function and the equality
961 //! predicate are copied from the source.
963 //! If store_hash option is true, this method does not use the hash function.
965 //! If any operation throws, all cloned elements are unlinked and disposed
966 //! calling Disposer::operator()(pointer).
968 //! <b>Complexity</b>: Linear to erased plus inserted elements.
970 //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
971 //! throws. Basic guarantee.
972 template <class Cloner
, class Disposer
>
973 void clone_from(const hashtable_impl
&src
, Cloner cloner
, Disposer disposer
)
975 this->clear_and_dispose(disposer
);
976 if(!constant_time_size
|| !src
.empty()){
977 const size_type src_bucket_count
= src
.bucket_count();
978 const size_type dst_bucket_count
= this->bucket_count();
979 //Check power of two bucket array if the option is activated
980 BOOST_INTRUSIVE_INVARIANT_ASSERT
981 (!power_2_buckets
|| (0 == (src_bucket_count
& (src_bucket_count
-1))));
982 BOOST_INTRUSIVE_INVARIANT_ASSERT
983 (!power_2_buckets
|| (0 == (dst_bucket_count
& (dst_bucket_count
-1))));
985 //If src bucket count is bigger or equal, structural copy is possible
986 if(!incremental
&& (src_bucket_count
>= dst_bucket_count
)){
987 //First clone the first ones
988 const bucket_ptr src_buckets
= src
.priv_buckets();
989 const bucket_ptr dst_buckets
= this->priv_buckets();
990 size_type constructed
;
991 typedef node_cast_adaptor
<detail::node_disposer
<Disposer
, hashtable_impl
> > NodeDisposer
;
992 typedef node_cast_adaptor
<detail::node_cloner
<Cloner
, hashtable_impl
> > NodeCloner
;
993 NodeDisposer
node_disp(disposer
, this);
995 detail::exception_array_disposer
<bucket_type
, NodeDisposer
>
996 rollback(dst_buckets
[0], node_disp
, constructed
);
998 ; constructed
< dst_bucket_count
1000 dst_buckets
[constructed
].clone_from
1001 ( src_buckets
[constructed
]
1002 , NodeCloner(cloner
, this), node_disp
);
1004 if(src_bucket_count
!= dst_bucket_count
){
1005 //Now insert the remaining ones using the modulo trick
1006 for(//"constructed" comes from the previous loop
1007 ; constructed
< src_bucket_count
1009 bucket_type
&dst_b
=
1010 dst_buckets
[priv_hash_to_bucket(constructed
, dst_bucket_count
, dst_bucket_count
)];
1011 bucket_type
&src_b
= src_buckets
[constructed
];
1012 for( siterator
b(src_b
.begin()), e(src_b
.end())
1015 dst_b
.push_front(*(NodeCloner(cloner
, this)(*b
.pointed_node())));
1019 this->priv_hasher() = src
.priv_hasher();
1020 this->priv_equal() = src
.priv_equal();
1022 this->priv_size_traits().set_size(src
.priv_size_traits().get_size());
1023 this->priv_split_traits().set_size(dst_bucket_count
);
1024 priv_insertion_update_cache(0u);
1025 priv_erasure_update_cache();
1027 else if(store_hash
){
1028 //Unlike previous cloning algorithm, this can throw
1029 //if cloner, hasher or comparison functor throw
1030 const_iterator
b(src
.begin()), e(src
.end());
1031 detail::exception_disposer
<hashtable_impl
, Disposer
>
1032 rollback(*this, disposer
);
1034 std::size_t hash_value
= this->priv_stored_or_compute_hash(*b
, store_hash_t());;
1035 this->priv_insert_equal_with_hash(*cloner(*b
), hash_value
);
1040 //Unlike previous cloning algorithm, this can throw
1041 //if cloner, hasher or comparison functor throw
1042 const_iterator
b(src
.begin()), e(src
.end());
1043 detail::exception_disposer
<hashtable_impl
, Disposer
>
1044 rollback(*this, disposer
);
1046 this->insert_equal(*cloner(*b
));
1053 //! <b>Requires</b>: value must be an lvalue
1055 //! <b>Effects</b>: Inserts the value into the unordered_set.
1057 //! <b>Returns</b>: An iterator to the inserted value.
1059 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1061 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
1063 //! <b>Note</b>: Does not affect the validity of iterators and references.
1064 //! No copy-constructors are called.
1065 iterator
insert_equal(reference value
)
1067 size_type bucket_num
;
1068 std::size_t hash_value
;
1070 siterator it
= this->priv_find
1071 (value
, this->priv_hasher(), this->priv_equal(), bucket_num
, hash_value
, prev
);
1072 return priv_insert_equal_find(value
, bucket_num
, hash_value
, it
);
1075 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1076 //! of type value_type.
1078 //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
1080 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
1081 //! Worst case O(N*this->size()).
1083 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
1085 //! <b>Note</b>: Does not affect the validity of iterators and references.
1086 //! No copy-constructors are called.
1087 template<class Iterator
>
1088 void insert_equal(Iterator b
, Iterator e
)
1091 this->insert_equal(*b
);
1094 //! <b>Requires</b>: value must be an lvalue
1096 //! <b>Effects</b>: Tries to inserts value into the unordered_set.
1098 //! <b>Returns</b>: If the value
1099 //! is not already present inserts it and returns a pair containing the
1100 //! iterator to the new value and true. If there is an equivalent value
1101 //! returns a pair containing an iterator to the already present value
1104 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1106 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
1108 //! <b>Note</b>: Does not affect the validity of iterators and references.
1109 //! No copy-constructors are called.
1110 std::pair
<iterator
, bool> insert_unique(reference value
)
1112 insert_commit_data commit_data
;
1113 std::pair
<iterator
, bool> ret
= this->insert_unique_check
1114 (value
, this->priv_hasher(), this->priv_equal(), commit_data
);
1117 return std::pair
<iterator
, bool>
1118 (this->insert_unique_commit(value
, commit_data
), true);
1121 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1122 //! of type value_type.
1124 //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
1126 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
1127 //! Worst case O(N*this->size()).
1129 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
1131 //! <b>Note</b>: Does not affect the validity of iterators and references.
1132 //! No copy-constructors are called.
1133 template<class Iterator
>
1134 void insert_unique(Iterator b
, Iterator e
)
1137 this->insert_unique(*b
);
1140 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1141 //! the same hash values as the stored hasher. The difference is that
1142 //! "hash_func" hashes the given key instead of the value_type.
1144 //! "equal_func" must be a equality function that induces
1145 //! the same equality as key_equal. The difference is that
1146 //! "equal_func" compares an arbitrary key with the contained values.
1148 //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
1149 //! a user provided key instead of the value itself.
1151 //! <b>Returns</b>: If there is an equivalent value
1152 //! returns a pair containing an iterator to the already present value
1153 //! and false. If the value can be inserted returns true in the returned
1154 //! pair boolean and fills "commit_data" that is meant to be used with
1155 //! the "insert_commit" function.
1157 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1159 //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
1161 //! <b>Notes</b>: This function is used to improve performance when constructing
1162 //! a value_type is expensive: if there is an equivalent value
1163 //! the constructed object must be discarded. Many times, the part of the
1164 //! node that is used to impose the hash or the equality is much cheaper to
1165 //! construct than the value_type and this function offers the possibility to
1166 //! use that the part to check if the insertion will be successful.
1168 //! If the check is successful, the user can construct the value_type and use
1169 //! "insert_commit" to insert the object in constant-time.
1171 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
1172 //! objects are inserted or erased from the unordered_set.
1174 //! After a successful rehashing insert_commit_data remains valid.
1175 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1176 std::pair
<iterator
, bool> insert_unique_check
1177 ( const KeyType
&key
1178 , KeyHasher hash_func
1179 , KeyValueEqual equal_func
1180 , insert_commit_data
&commit_data
)
1182 size_type bucket_num
;
1184 siterator prev_pos
=
1185 this->priv_find(key
, hash_func
, equal_func
, bucket_num
, commit_data
.hash
, prev
);
1186 bool success
= prev_pos
== priv_invalid_local_it();
1190 return std::pair
<iterator
, bool>(iterator(prev_pos
, this),success
);
1193 //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
1194 //! must have been obtained from a previous call to "insert_check".
1195 //! No objects should have been inserted or erased from the unordered_set between
1196 //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
1198 //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
1199 //! from the "commit_data" that a previous "insert_check" filled.
1201 //! <b>Returns</b>: An iterator to the newly inserted object.
1203 //! <b>Complexity</b>: Constant time.
1205 //! <b>Throws</b>: Nothing.
1207 //! <b>Notes</b>: This function has only sense if a "insert_check" has been
1208 //! previously executed to fill "commit_data". No value should be inserted or
1209 //! erased between the "insert_check" and "insert_commit" calls.
1211 //! After a successful rehashing insert_commit_data remains valid.
1212 iterator
insert_unique_commit(reference value
, const insert_commit_data
&commit_data
)
1214 size_type bucket_num
= priv_hash_to_bucket(commit_data
.hash
);
1215 bucket_type
&b
= this->priv_buckets()[bucket_num
];
1216 this->priv_size_traits().increment();
1217 node_ptr n
= node_ptr(&priv_value_to_node(value
));
1218 this->priv_store_hash(n
, commit_data
.hash
, store_hash_t());
1219 if(safemode_or_autounlink
)
1220 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n
));
1221 priv_insertion_update_cache(bucket_num
);
1222 this->priv_insert_in_group(node_ptr(0), n
, optimize_multikey_t());
1223 return iterator(b
.insert_after(b
.before_begin(), *n
), this);
1226 //! <b>Effects</b>: Erases the element pointed to by i.
1228 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1230 //! <b>Throws</b>: Nothing.
1232 //! <b>Note</b>: Invalidates the iterators (but not the references)
1233 //! to the erased element. No destructors are called.
1234 void erase(const_iterator i
)
1235 { this->erase_and_dispose(i
, detail::null_disposer()); }
1237 //! <b>Effects</b>: Erases the range pointed to by b end e.
1239 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
1240 //! worst case O(this->size()).
1242 //! <b>Throws</b>: Nothing.
1244 //! <b>Note</b>: Invalidates the iterators (but not the references)
1245 //! to the erased elements. No destructors are called.
1246 void erase(const_iterator b
, const_iterator e
)
1247 { this->erase_and_dispose(b
, e
, detail::null_disposer()); }
1249 //! <b>Effects</b>: Erases all the elements with the given value.
1251 //! <b>Returns</b>: The number of erased elements.
1253 //! <b>Complexity</b>: Average case O(this->count(value)).
1254 //! Worst case O(this->size()).
1256 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1257 //! Basic guarantee.
1259 //! <b>Note</b>: Invalidates the iterators (but not the references)
1260 //! to the erased elements. No destructors are called.
1261 size_type
erase(const_reference value
)
1262 { return this->erase(value
, this->priv_hasher(), this->priv_equal()); }
1264 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1265 //! the same hash values as the stored hasher. The difference is that
1266 //! "hash_func" hashes the given key instead of the value_type.
1268 //! "equal_func" must be a equality function that induces
1269 //! the same equality as key_equal. The difference is that
1270 //! "equal_func" compares an arbitrary key with the contained values.
1272 //! <b>Effects</b>: Erases all the elements that have the same hash and
1273 //! compare equal with the given key.
1275 //! <b>Returns</b>: The number of erased elements.
1277 //! <b>Complexity</b>: Average case O(this->count(value)).
1278 //! Worst case O(this->size()).
1280 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
1282 //! <b>Note</b>: Invalidates the iterators (but not the references)
1283 //! to the erased elements. No destructors are called.
1284 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1285 size_type
erase(const KeyType
& key
, KeyHasher hash_func
, KeyValueEqual equal_func
)
1286 { return this->erase_and_dispose(key
, hash_func
, equal_func
, detail::null_disposer()); }
1288 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1290 //! <b>Effects</b>: Erases the element pointed to by i.
1291 //! Disposer::operator()(pointer) is called for the removed element.
1293 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1295 //! <b>Throws</b>: Nothing.
1297 //! <b>Note</b>: Invalidates the iterators
1298 //! to the erased elements.
1299 template<class Disposer
>
1300 void erase_and_dispose(const_iterator i
, Disposer disposer
)
1302 priv_erase(i
, disposer
, optimize_multikey_t());
1303 this->priv_size_traits().decrement();
1304 priv_erasure_update_cache();
1307 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1308 template<class Disposer
>
1309 iterator
erase_and_dispose(iterator i
, Disposer disposer
)
1310 { return this->erase_and_dispose(const_iterator(i
), disposer
); }
1313 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1315 //! <b>Effects</b>: Erases the range pointed to by b end e.
1316 //! Disposer::operator()(pointer) is called for the removed elements.
1318 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
1319 //! worst case O(this->size()).
1321 //! <b>Throws</b>: Nothing.
1323 //! <b>Note</b>: Invalidates the iterators
1324 //! to the erased elements.
1325 template<class Disposer
>
1326 void erase_and_dispose(const_iterator b
, const_iterator e
, Disposer disposer
)
1330 //Get the bucket number and local iterator for both iterators
1331 siterator
first_local_it(b
.slist_it());
1332 size_type first_bucket_num
= this->priv_get_bucket_num(first_local_it
);
1334 siterator before_first_local_it
1335 = priv_get_previous(priv_buckets()[first_bucket_num
], first_local_it
);
1336 size_type last_bucket_num
;
1337 siterator last_local_it
;
1339 //For the end iterator, we will assign the end iterator
1340 //of the last bucket
1341 if(e
== this->end()){
1342 last_bucket_num
= this->bucket_count() - 1;
1343 last_local_it
= priv_buckets()[last_bucket_num
].end();
1346 last_local_it
= e
.slist_it();
1347 last_bucket_num
= this->priv_get_bucket_num(last_local_it
);
1349 priv_erase_range(before_first_local_it
, first_bucket_num
, last_local_it
, last_bucket_num
, disposer
);
1350 priv_erasure_update_cache(first_bucket_num
, last_bucket_num
);
1353 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1355 //! <b>Effects</b>: Erases all the elements with the given value.
1356 //! Disposer::operator()(pointer) is called for the removed elements.
1358 //! <b>Returns</b>: The number of erased elements.
1360 //! <b>Complexity</b>: Average case O(this->count(value)).
1361 //! Worst case O(this->size()).
1363 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1364 //! Basic guarantee.
1366 //! <b>Note</b>: Invalidates the iterators (but not the references)
1367 //! to the erased elements. No destructors are called.
1368 template<class Disposer
>
1369 size_type
erase_and_dispose(const_reference value
, Disposer disposer
)
1370 { return this->erase_and_dispose(value
, priv_hasher(), priv_equal(), disposer
); }
1372 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1374 //! <b>Effects</b>: Erases all the elements with the given key.
1375 //! according to the comparison functor "equal_func".
1376 //! Disposer::operator()(pointer) is called for the removed elements.
1378 //! <b>Returns</b>: The number of erased elements.
1380 //! <b>Complexity</b>: Average case O(this->count(value)).
1381 //! Worst case O(this->size()).
1383 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
1385 //! <b>Note</b>: Invalidates the iterators
1386 //! to the erased elements.
1387 template<class KeyType
, class KeyHasher
, class KeyValueEqual
, class Disposer
>
1388 size_type
erase_and_dispose(const KeyType
& key
, KeyHasher hash_func
1389 ,KeyValueEqual equal_func
, Disposer disposer
)
1391 size_type bucket_num
;
1395 this->priv_find(key
, hash_func
, equal_func
, bucket_num
, h
, prev
);
1396 bool success
= it
!= priv_invalid_local_it();
1401 else if(optimize_multikey
){
1402 siterator last
= bucket_type::s_iterator_to
1403 (*node_traits::get_next(group_functions_t::priv_get_last_in_group
1404 (dcast_bucket_ptr(it
.pointed_node()), optimize_multikey_t())));
1405 this->priv_erase_range_impl(bucket_num
, prev
, last
, disposer
, count
);
1408 //If found erase all equal values
1409 bucket_type
&b
= this->priv_buckets()[bucket_num
];
1410 for(siterator end
= b
.end(); it
!= end
; ++count
, ++it
){
1411 slist_node_ptr
n(it
.pointed_node());
1412 const value_type
&v
= priv_value_from_slist_node(n
);
1414 std::size_t vh
= this->priv_stored_or_compute_hash(v
, store_hash_t());
1415 if(h
!= vh
|| !equal_func(key
, v
)){
1419 else if(!equal_func(key
, v
)){
1422 this->priv_size_traits().decrement();
1424 b
.erase_after_and_dispose(prev
, it
, make_node_disposer(disposer
));
1426 priv_erasure_update_cache();
1430 //! <b>Effects</b>: Erases all of the elements.
1432 //! <b>Complexity</b>: Linear to the number of elements on the container.
1433 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
1435 //! <b>Throws</b>: Nothing.
1437 //! <b>Note</b>: Invalidates the iterators (but not the references)
1438 //! to the erased elements. No destructors are called.
1441 priv_clear_buckets();
1442 this->priv_size_traits().set_size(size_type(0));
1445 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1447 //! <b>Effects</b>: Erases all of the elements.
1449 //! <b>Complexity</b>: Linear to the number of elements on the container.
1450 //! Disposer::operator()(pointer) is called for the removed elements.
1452 //! <b>Throws</b>: Nothing.
1454 //! <b>Note</b>: Invalidates the iterators (but not the references)
1455 //! to the erased elements. No destructors are called.
1456 template<class Disposer
>
1457 void clear_and_dispose(Disposer disposer
)
1459 if(!constant_time_size
|| !this->empty()){
1460 size_type num_buckets
= this->bucket_count();
1461 bucket_ptr b
= this->priv_buckets();
1462 for(; num_buckets
--; ++b
){
1463 b
->clear_and_dispose(make_node_disposer(disposer
));
1465 this->priv_size_traits().set_size(size_type(0));
1467 priv_initialize_cache();
1470 //! <b>Effects</b>: Returns the number of contained elements with the given value
1472 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1474 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1475 size_type
count(const_reference value
) const
1476 { return this->count(value
, this->priv_hasher(), this->priv_equal()); }
1478 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1479 //! the same hash values as the stored hasher. The difference is that
1480 //! "hash_func" hashes the given key instead of the value_type.
1482 //! "equal_func" must be a equality function that induces
1483 //! the same equality as key_equal. The difference is that
1484 //! "equal_func" compares an arbitrary key with the contained values.
1486 //! <b>Effects</b>: Returns the number of contained elements with the given key
1488 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1490 //! <b>Throws</b>: If hash_func or equal throw.
1491 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1492 size_type
count(const KeyType
&key
, const KeyHasher
&hash_func
, const KeyValueEqual
&equal_func
) const
1494 size_type bucket_n1
, bucket_n2
, count
;
1495 this->priv_equal_range(key
, hash_func
, equal_func
, bucket_n1
, bucket_n2
, count
);
1499 //! <b>Effects</b>: Finds an iterator to the first element is equal to
1500 //! "value" or end() if that element does not exist.
1502 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1504 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1505 iterator
find(const_reference value
)
1506 { return this->find(value
, this->priv_hasher(), this->priv_equal()); }
1508 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1509 //! the same hash values as the stored hasher. The difference is that
1510 //! "hash_func" hashes the given key instead of the value_type.
1512 //! "equal_func" must be a equality function that induces
1513 //! the same equality as key_equal. The difference is that
1514 //! "equal_func" compares an arbitrary key with the contained values.
1516 //! <b>Effects</b>: Finds an iterator to the first element whose key is
1517 //! "key" according to the given hash and equality functor or end() if
1518 //! that element does not exist.
1520 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1522 //! <b>Throws</b>: If hash_func or equal_func throw.
1524 //! <b>Note</b>: This function is used when constructing a value_type
1525 //! is expensive and the value_type can be compared with a cheaper
1526 //! key type. Usually this key is part of the value_type.
1527 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1528 iterator
find(const KeyType
&key
, KeyHasher hash_func
, KeyValueEqual equal_func
)
1533 siterator local_it
= this->priv_find(key
, hash_func
, equal_func
, bucket_n
, hash
, prev
);
1534 return iterator(local_it
, this);
1537 //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
1538 //! "key" or end() if that element does not exist.
1540 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1542 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1543 const_iterator
find(const_reference value
) const
1544 { return this->find(value
, this->priv_hasher(), this->priv_equal()); }
1546 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1547 //! the same hash values as the stored hasher. The difference is that
1548 //! "hash_func" hashes the given key instead of the value_type.
1550 //! "equal_func" must be a equality function that induces
1551 //! the same equality as key_equal. The difference is that
1552 //! "equal_func" compares an arbitrary key with the contained values.
1554 //! <b>Effects</b>: Finds an iterator to the first element whose key is
1555 //! "key" according to the given hasher and equality functor or end() if
1556 //! that element does not exist.
1558 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1560 //! <b>Throws</b>: If hash_func or equal_func throw.
1562 //! <b>Note</b>: This function is used when constructing a value_type
1563 //! is expensive and the value_type can be compared with a cheaper
1564 //! key type. Usually this key is part of the value_type.
1565 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1567 (const KeyType
&key
, KeyHasher hash_func
, KeyValueEqual equal_func
) const
1570 std::size_t hash_value
;
1572 siterator sit
= this->priv_find(key
, hash_func
, equal_func
, bucket_n
, hash_value
, prev
);
1573 return const_iterator(sit
, this);
1576 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
1577 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
1580 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
1582 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1583 std::pair
<iterator
,iterator
> equal_range(const_reference value
)
1584 { return this->equal_range(value
, this->priv_hasher(), this->priv_equal()); }
1586 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1587 //! the same hash values as the stored hasher. The difference is that
1588 //! "hash_func" hashes the given key instead of the value_type.
1590 //! "equal_func" must be a equality function that induces
1591 //! the same equality as key_equal. The difference is that
1592 //! "equal_func" compares an arbitrary key with the contained values.
1594 //! <b>Effects</b>: Returns a range containing all elements with equivalent
1595 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
1598 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
1599 //! Worst case O(this->size()).
1601 //! <b>Throws</b>: If hash_func or the equal_func throw.
1603 //! <b>Note</b>: This function is used when constructing a value_type
1604 //! is expensive and the value_type can be compared with a cheaper
1605 //! key type. Usually this key is part of the value_type.
1606 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1607 std::pair
<iterator
,iterator
> equal_range
1608 (const KeyType
&key
, KeyHasher hash_func
, KeyValueEqual equal_func
)
1610 size_type bucket_n1
, bucket_n2
, count
;
1611 std::pair
<siterator
, siterator
> ret
= this->priv_equal_range
1612 (key
, hash_func
, equal_func
, bucket_n1
, bucket_n2
, count
);
1613 return std::pair
<iterator
, iterator
>
1614 (iterator(ret
.first
, this), iterator(ret
.second
, this));
1617 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
1618 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
1621 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
1623 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1624 std::pair
<const_iterator
, const_iterator
>
1625 equal_range(const_reference value
) const
1626 { return this->equal_range(value
, this->priv_hasher(), this->priv_equal()); }
1628 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1629 //! the same hash values as the stored hasher. The difference is that
1630 //! "hash_func" hashes the given key instead of the value_type.
1632 //! "equal_func" must be a equality function that induces
1633 //! the same equality as key_equal. The difference is that
1634 //! "equal_func" compares an arbitrary key with the contained values.
1636 //! <b>Effects</b>: Returns a range containing all elements with equivalent
1637 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
1640 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
1641 //! Worst case O(this->size()).
1643 //! <b>Throws</b>: If the hasher or equal_func throw.
1645 //! <b>Note</b>: This function is used when constructing a value_type
1646 //! is expensive and the value_type can be compared with a cheaper
1647 //! key type. Usually this key is part of the value_type.
1648 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
1649 std::pair
<const_iterator
,const_iterator
> equal_range
1650 (const KeyType
&key
, KeyHasher hash_func
, KeyValueEqual equal_func
) const
1652 size_type bucket_n1
, bucket_n2
, count
;
1653 std::pair
<siterator
, siterator
> ret
=
1654 this->priv_equal_range(key
, hash_func
, equal_func
, bucket_n1
, bucket_n2
, count
);
1655 return std::pair
<const_iterator
, const_iterator
>
1656 (const_iterator(ret
.first
, this), const_iterator(ret
.second
, this));
1659 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1660 //! appropriate type. Otherwise the behavior is undefined.
1662 //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
1663 //! that points to the value
1665 //! <b>Complexity</b>: Constant.
1667 //! <b>Throws</b>: If the internal hash function throws.
1668 iterator
iterator_to(reference value
)
1670 return iterator(bucket_type::s_iterator_to(priv_value_to_node(value
)), this);
1673 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1674 //! appropriate type. Otherwise the behavior is undefined.
1676 //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
1677 //! unordered_set that points to the value
1679 //! <b>Complexity</b>: Constant.
1681 //! <b>Throws</b>: If the internal hash function throws.
1682 const_iterator
iterator_to(const_reference value
) const
1684 return const_iterator(bucket_type::s_iterator_to(priv_value_to_node(const_cast<reference
>(value
))), this);
1687 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1688 //! appropriate type. Otherwise the behavior is undefined.
1690 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
1691 //! that points to the value
1693 //! <b>Complexity</b>: Constant.
1695 //! <b>Throws</b>: Nothing.
1697 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1699 static local_iterator
s_local_iterator_to(reference value
)
1701 BOOST_STATIC_ASSERT((!stateful_value_traits
));
1702 siterator sit
= bucket_type::s_iterator_to(((hashtable_impl
*)0)->priv_value_to_node(value
));
1703 return local_iterator(sit
, (hashtable_impl
*)0);
1706 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1707 //! appropriate type. Otherwise the behavior is undefined.
1709 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
1710 //! the unordered_set that points to the value
1712 //! <b>Complexity</b>: Constant.
1714 //! <b>Throws</b>: Nothing.
1716 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1718 static const_local_iterator
s_local_iterator_to(const_reference value
)
1720 BOOST_STATIC_ASSERT((!stateful_value_traits
));
1721 siterator sit
= bucket_type::s_iterator_to(((hashtable_impl
*)0)->priv_value_to_node(const_cast<value_type
&>(value
)));
1722 return const_local_iterator(sit
, (hashtable_impl
*)0);
1725 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1726 //! appropriate type. Otherwise the behavior is undefined.
1728 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
1729 //! that points to the value
1731 //! <b>Complexity</b>: Constant.
1733 //! <b>Throws</b>: Nothing.
1734 local_iterator
local_iterator_to(reference value
)
1736 siterator sit
= bucket_type::s_iterator_to(this->priv_value_to_node(value
));
1737 return local_iterator(sit
, this);
1740 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
1741 //! appropriate type. Otherwise the behavior is undefined.
1743 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
1744 //! the unordered_set that points to the value
1746 //! <b>Complexity</b>: Constant.
1748 //! <b>Throws</b>: Nothing.
1749 const_local_iterator
local_iterator_to(const_reference value
) const
1751 siterator sit
= bucket_type::s_iterator_to
1752 (const_cast<node
&>(this->priv_value_to_node(value
)));
1753 return const_local_iterator(sit
, this);
1756 //! <b>Effects</b>: Returns the number of buckets passed in the constructor
1757 //! or the last rehash function.
1759 //! <b>Complexity</b>: Constant.
1761 //! <b>Throws</b>: Nothing.
1762 size_type
bucket_count() const
1763 { return this->priv_buckets_len(); }
1765 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1767 //! <b>Effects</b>: Returns the number of elements in the nth bucket.
1769 //! <b>Complexity</b>: Constant.
1771 //! <b>Throws</b>: Nothing.
1772 size_type
bucket_size(size_type n
) const
1773 { return this->priv_buckets()[n
].size(); }
1775 //! <b>Effects</b>: Returns the index of the bucket in which elements
1776 //! with keys equivalent to k would be found, if any such element existed.
1778 //! <b>Complexity</b>: Constant.
1780 //! <b>Throws</b>: If the hash functor throws.
1782 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
1783 size_type
bucket(const key_type
& k
) const
1784 { return this->bucket(k
, this->priv_hasher()); }
1786 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1787 //! the same hash values as the stored hasher. The difference is that
1788 //! "hash_func" hashes the given key instead of the value_type.
1790 //! <b>Effects</b>: Returns the index of the bucket in which elements
1791 //! with keys equivalent to k would be found, if any such element existed.
1793 //! <b>Complexity</b>: Constant.
1795 //! <b>Throws</b>: If hash_func throws.
1797 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
1798 template<class KeyType
, class KeyHasher
>
1799 size_type
bucket(const KeyType
& k
, const KeyHasher
&hash_func
) const
1800 { return priv_hash_to_bucket(hash_func(k
)); }
1802 //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
1803 //! or the last rehash function.
1805 //! <b>Complexity</b>: Constant.
1807 //! <b>Throws</b>: Nothing.
1808 bucket_ptr
bucket_pointer() const
1809 { return this->priv_buckets(); }
1811 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1813 //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
1814 //! of the sequence stored in the bucket n.
1816 //! <b>Complexity</b>: Constant.
1818 //! <b>Throws</b>: Nothing.
1820 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1821 //! containing all of the elements in the nth bucket.
1822 local_iterator
begin(size_type n
)
1823 { return local_iterator(this->priv_buckets()[n
].begin(), this); }
1825 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1827 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
1828 //! of the sequence stored in the bucket n.
1830 //! <b>Complexity</b>: Constant.
1832 //! <b>Throws</b>: Nothing.
1834 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1835 //! containing all of the elements in the nth bucket.
1836 const_local_iterator
begin(size_type n
) const
1837 { return this->cbegin(n
); }
1839 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1841 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
1842 //! of the sequence stored in the bucket n.
1844 //! <b>Complexity</b>: Constant.
1846 //! <b>Throws</b>: Nothing.
1848 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1849 //! containing all of the elements in the nth bucket.
1850 const_local_iterator
cbegin(size_type n
) const
1852 siterator sit
= const_cast<bucket_type
&>(this->priv_buckets()[n
]).begin();
1853 return const_local_iterator(sit
, this);
1856 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1858 //! <b>Effects</b>: Returns a local_iterator pointing to the end
1859 //! of the sequence stored in the bucket n.
1861 //! <b>Complexity</b>: Constant.
1863 //! <b>Throws</b>: Nothing.
1865 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1866 //! containing all of the elements in the nth bucket.
1867 local_iterator
end(size_type n
)
1868 { return local_iterator(this->priv_buckets()[n
].end(), this); }
1870 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1872 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
1873 //! of the sequence stored in the bucket n.
1875 //! <b>Complexity</b>: Constant.
1877 //! <b>Throws</b>: Nothing.
1879 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1880 //! containing all of the elements in the nth bucket.
1881 const_local_iterator
end(size_type n
) const
1882 { return this->cend(n
); }
1884 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
1886 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
1887 //! of the sequence stored in the bucket n.
1889 //! <b>Complexity</b>: Constant.
1891 //! <b>Throws</b>: Nothing.
1893 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
1894 //! containing all of the elements in the nth bucket.
1895 const_local_iterator
cend(size_type n
) const
1896 { return const_local_iterator(const_cast<bucket_type
&>(this->priv_buckets()[n
]).end(), this); }
1898 //! <b>Requires</b>: new_buckets must be a pointer to a new bucket array
1899 //! or the same as the old bucket array. new_size is the length of the
1900 //! the array pointed by new_buckets. If new_buckets == this->bucket_pointer()
1901 //! n can be bigger or smaller than this->bucket_count().
1902 //! 'new_bucket_traits' copy constructor should not throw.
1904 //! <b>Effects</b>: Updates the internal reference with the new bucket erases
1905 //! the values from the old bucket and inserts then in the new one.
1906 //! Bucket traits hold by *this is assigned from new_bucket_traits.
1907 //! If the container is configured as incremental<>, the split bucket is set
1908 //! to the new bucket_len().
1910 //! If store_hash option is true, this method does not use the hash function.
1912 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
1914 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
1915 void rehash(const bucket_traits
&new_bucket_traits
)
1917 bucket_ptr new_buckets
= new_bucket_traits
.bucket_begin();
1918 size_type new_buckets_len
= new_bucket_traits
.bucket_count();
1919 bucket_ptr old_buckets
= this->priv_buckets();
1920 size_type old_buckets_len
= this->priv_buckets_len();
1922 //Check power of two bucket array if the option is activated
1923 BOOST_INTRUSIVE_INVARIANT_ASSERT
1924 (!power_2_buckets
|| (0 == (new_buckets_len
& (new_buckets_len
-1u))));
1926 size_type n
= priv_get_cache_bucket_num();
1927 const bool same_buffer
= old_buckets
== new_buckets
;
1928 //If the new bucket length is a common factor
1929 //of the old one we can avoid hash calculations.
1930 const bool fast_shrink
= (!incremental
) && (old_buckets_len
> new_buckets_len
) &&
1931 (power_2_buckets
||(old_buckets_len
% new_buckets_len
) == 0);
1932 //If we are shrinking the same bucket array and it's
1933 //is a fast shrink, just rehash the last nodes
1934 size_type new_first_bucket_num
= new_buckets_len
;
1935 if(same_buffer
&& fast_shrink
&& (n
< new_buckets_len
)){
1936 n
= new_buckets_len
;
1937 new_first_bucket_num
= priv_get_cache_bucket_num();
1940 //Anti-exception stuff: they destroy the elements if something goes wrong.
1941 //If the source and destination buckets are the same, the second rollback function
1942 //is harmless, because all elements have been already unlinked and destroyed
1943 typedef detail::init_disposer
<node_algorithms
> NodeDisposer
;
1944 NodeDisposer node_disp
;
1945 detail::exception_array_disposer
<bucket_type
, NodeDisposer
>
1946 rollback1(new_buckets
[0], node_disp
, new_buckets_len
);
1947 detail::exception_array_disposer
<bucket_type
, NodeDisposer
>
1948 rollback2(old_buckets
[0], node_disp
, old_buckets_len
);
1950 //Put size in a safe value for rollback exception
1951 size_type size_backup
= this->priv_size_traits().get_size();
1952 this->priv_size_traits().set_size(0);
1953 //Put cache to safe position
1954 priv_initialize_cache();
1955 priv_insertion_update_cache(size_type(0u));
1957 //Iterate through nodes
1958 for(; n
< old_buckets_len
; ++n
){
1959 bucket_type
&old_bucket
= old_buckets
[n
];
1962 siterator
before_i(old_bucket
.before_begin());
1963 siterator
end(old_bucket
.end());
1964 siterator
i(old_bucket
.begin());
1965 for(;i
!= end
; ++i
){
1966 const value_type
&v
= priv_value_from_slist_node(i
.pointed_node());
1967 const std::size_t hash_value
= this->priv_stored_or_compute_hash(v
, store_hash_t());
1968 const size_type new_n
= priv_hash_to_bucket(hash_value
, new_buckets_len
, new_buckets_len
);
1969 if(cache_begin
&& new_n
< new_first_bucket_num
)
1970 new_first_bucket_num
= new_n
;
1971 siterator last
= bucket_type::s_iterator_to
1972 (*group_functions_t::priv_get_last_in_group
1973 (dcast_bucket_ptr(i
.pointed_node()), optimize_multikey_t()));
1974 if(same_buffer
&& new_n
== n
){
1978 bucket_type
&new_b
= new_buckets
[new_n
];
1979 new_b
.splice_after(new_b
.before_begin(), old_bucket
, before_i
, last
);
1985 const size_type new_n
= priv_hash_to_bucket(n
, new_buckets_len
, new_buckets_len
);
1986 if(cache_begin
&& new_n
< new_first_bucket_num
)
1987 new_first_bucket_num
= new_n
;
1988 bucket_type
&new_b
= new_buckets
[new_n
];
1989 if(!old_bucket
.empty()){
1990 new_b
.splice_after( new_b
.before_begin()
1992 , old_bucket
.before_begin()
1993 , priv_get_last(old_bucket
));
1998 this->priv_size_traits().set_size(size_backup
);
1999 this->priv_split_traits().set_size(new_buckets_len
);
2000 this->priv_real_bucket_traits() = new_bucket_traits
;
2001 priv_initialize_cache();
2002 priv_insertion_update_cache(new_first_bucket_num
);
2003 rollback1
.release();
2004 rollback2
.release();
2007 //! <b>Requires</b>:
2011 //! <b>Complexity</b>:
2015 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2016 bool incremental_rehash(bool grow
= true)
2018 //This function is only available for containers with incremental hashing
2019 BOOST_STATIC_ASSERT(( incremental
&& power_2_buckets
));
2020 size_type split_idx
= priv_split_traits().get_size();
2021 size_type bucket_len
= priv_buckets_len();
2024 //Test if the split variable can be changed
2025 if(split_idx
>= bucket_len
)
2028 size_type bucket_len
= priv_buckets_len();
2029 size_type bucket_to_rehash
= split_idx
- bucket_len
/2;
2030 bucket_type
&old_bucket
= this->priv_buckets()[bucket_to_rehash
];
2031 siterator
before_i(old_bucket
.before_begin());
2032 siterator
end(old_bucket
.end());
2033 siterator
i(old_bucket
.begin());
2034 priv_split_traits().increment();
2036 //Anti-exception stuff: if an exception is thrown while
2037 //moving elements from old_bucket to the target bucket, all moved
2038 //elements are moved back to the original one.
2039 detail::incremental_rehash_rollback
<bucket_type
, split_traits
> rollback
2040 ( this->priv_buckets()[split_idx
], old_bucket
, priv_split_traits());
2041 for(;i
!= end
; ++i
){
2042 const value_type
&v
= priv_value_from_slist_node(i
.pointed_node());
2043 const std::size_t hash_value
= this->priv_stored_or_compute_hash(v
, store_hash_t());
2044 const size_type new_n
= priv_hash_to_bucket(hash_value
);
2045 siterator last
= bucket_type::s_iterator_to
2046 (*group_functions_t::priv_get_last_in_group
2047 (dcast_bucket_ptr(i
.pointed_node()), optimize_multikey_t()));
2048 if(new_n
== bucket_to_rehash
){
2052 bucket_type
&new_b
= this->priv_buckets()[new_n
];
2053 new_b
.splice_after(new_b
.before_begin(), old_bucket
, before_i
, last
);
2058 priv_erasure_update_cache();
2062 //Test if the split variable can be changed
2063 if(split_idx
<= bucket_len
/2)
2065 const size_type target_bucket_num
= split_idx
- 1 - bucket_len
/2;
2066 bucket_type
&target_bucket
= this->priv_buckets()[target_bucket_num
];
2067 bucket_type
&source_bucket
= this->priv_buckets()[split_idx
-1];
2068 target_bucket
.splice_after(target_bucket
.cbefore_begin(), source_bucket
);
2069 priv_split_traits().decrement();
2070 priv_insertion_update_cache(target_bucket_num
);
2075 //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
2076 //! this->bucket_count()/2 or this->bucket_count()*2, or
2077 //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
2078 //! and does nothing.
2080 //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
2081 //! and transfers all the objects from old buckets to the new ones.
2083 //! <b>Complexity</b>: Linear to size().
2085 //! <b>Throws</b>: Nothing
2087 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2088 bool incremental_rehash(const bucket_traits
&new_bucket_traits
)
2090 //This function is only available for containers with incremental hashing
2091 BOOST_STATIC_ASSERT(( incremental
&& power_2_buckets
));
2092 size_type new_bucket_traits_size
= new_bucket_traits
.bucket_count();
2093 size_type cur_bucket_traits
= this->priv_buckets_len();
2094 if(new_bucket_traits_size
/2 != cur_bucket_traits
&& new_bucket_traits_size
!= cur_bucket_traits
/2){
2098 const size_type split_idx
= this->split_count();
2100 if(new_bucket_traits_size
/2 == cur_bucket_traits
){
2101 //Test if the split variable can be changed
2102 if(!(split_idx
>= cur_bucket_traits
))
2106 //Test if the split variable can be changed
2107 if(!(split_idx
<= cur_bucket_traits
/2))
2111 const size_type ini_n
= priv_get_cache_bucket_num();
2112 const bucket_ptr old_buckets
= this->priv_buckets();
2113 this->priv_real_bucket_traits() = new_bucket_traits
;
2114 if(new_bucket_traits
.bucket_begin() != old_buckets
){
2115 for(size_type n
= ini_n
; n
< split_idx
; ++n
){
2116 bucket_type
&new_bucket
= new_bucket_traits
.bucket_begin()[n
];
2117 bucket_type
&old_bucket
= old_buckets
[n
];
2118 new_bucket
.splice_after(new_bucket
.cbefore_begin(), old_bucket
);
2120 //Put cache to safe position
2121 priv_initialize_cache();
2122 priv_insertion_update_cache(ini_n
);
2127 //! <b>Requires</b>:
2131 //! <b>Complexity</b>:
2134 size_type
split_count() const
2136 //This function is only available if incremental hashing is activated
2137 BOOST_STATIC_ASSERT(( incremental
&& power_2_buckets
));
2138 return this->priv_split_traits().get_size();
2141 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
2142 //! the container that is bigger than n. This suggestion can be used
2143 //! to create bucket arrays with a size that will usually improve
2144 //! container's performance. If such value does not exist, the
2145 //! higher possible value is returned.
2147 //! <b>Complexity</b>: Amortized constant time.
2149 //! <b>Throws</b>: Nothing.
2150 static size_type
suggested_upper_bucket_count(size_type n
)
2152 const std::size_t *primes
= &detail::prime_list_holder
<0>::prime_list
[0];
2153 const std::size_t *primes_end
= primes
+ detail::prime_list_holder
<0>::prime_list_size
;
2154 size_type
const* bound
= std::lower_bound(primes
, primes_end
, n
);
2155 if(bound
== primes_end
)
2157 return size_type(*bound
);
2160 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
2161 //! the container that is smaller than n. This suggestion can be used
2162 //! to create bucket arrays with a size that will usually improve
2163 //! container's performance. If such value does not exist, the
2164 //! lower possible value is returned.
2166 //! <b>Complexity</b>: Amortized constant time.
2168 //! <b>Throws</b>: Nothing.
2169 static size_type
suggested_lower_bucket_count(size_type n
)
2171 const std::size_t *primes
= &detail::prime_list_holder
<0>::prime_list
[0];
2172 const std::size_t *primes_end
= primes
+ detail::prime_list_holder
<0>::prime_list_size
;
2173 size_type
const* bound
= std::upper_bound(primes
, primes_end
, n
);
2174 if(bound
!= primes_end
)
2176 return size_type(*bound
);
2182 std::size_t priv_hash_to_bucket(std::size_t hash_value
) const
2183 { return priv_hash_to_bucket(hash_value
, this->priv_real_bucket_traits().bucket_count(), priv_split_traits().get_size()); }
2185 std::size_t priv_hash_to_bucket(std::size_t hash_value
, std::size_t bucket_len
, std::size_t split
) const
2187 std::size_t bucket_number
= priv_hash_to_bucket_impl(hash_value
, bucket_len
, power_2_buckets_t());
2189 if(bucket_number
>= split
)
2190 bucket_number
-= bucket_len
/2;
2191 return bucket_number
;
2194 std::size_t priv_hash_to_bucket_impl(std::size_t hash_value
, std::size_t bucket_len
, detail::bool_
<false>) const
2195 { return hash_value
% bucket_len
; }
2197 std::size_t priv_hash_to_bucket_impl(std::size_t hash_value
, std::size_t bucket_len
, detail::bool_
<true>) const
2198 { return hash_value
& (bucket_len
- 1); }
2200 const key_equal
&priv_equal() const
2201 { return static_cast<const key_equal
&>(this->data_
.internal_
.bucket_hash_equal_
.get()); }
2203 key_equal
&priv_equal()
2204 { return static_cast<key_equal
&>(this->data_
.internal_
.bucket_hash_equal_
.get()); }
2206 value_type
&priv_value_from_slist_node(slist_node_ptr n
)
2207 { return *this->get_real_value_traits().to_value_ptr(dcast_bucket_ptr(n
)); }
2209 const value_type
&priv_value_from_slist_node(slist_node_ptr n
) const
2210 { return *this->get_real_value_traits().to_value_ptr(dcast_bucket_ptr(n
)); }
2212 const real_bucket_traits
&priv_real_bucket_traits(detail::bool_
<false>) const
2213 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
.bucket_traits_
; }
2215 const real_bucket_traits
&priv_real_bucket_traits(detail::bool_
<true>) const
2216 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
.bucket_traits_
.get_bucket_traits(*this); }
2218 real_bucket_traits
&priv_real_bucket_traits(detail::bool_
<false>)
2219 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
.bucket_traits_
; }
2221 real_bucket_traits
&priv_real_bucket_traits(detail::bool_
<true>)
2222 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
.bucket_traits_
.get_bucket_traits(*this); }
2224 const real_bucket_traits
&priv_real_bucket_traits() const
2225 { return this->priv_real_bucket_traits(detail::bool_
<external_bucket_traits
>()); }
2227 real_bucket_traits
&priv_real_bucket_traits()
2228 { return this->priv_real_bucket_traits(detail::bool_
<external_bucket_traits
>()); }
2230 const hasher
&priv_hasher() const
2231 { return static_cast<const hasher
&>(this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.get()); }
2233 hasher
&priv_hasher()
2234 { return static_cast<hasher
&>(this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.get()); }
2236 bucket_ptr
priv_buckets() const
2237 { return this->priv_real_bucket_traits().bucket_begin(); }
2239 size_type
priv_buckets_len() const
2240 { return this->priv_real_bucket_traits().bucket_count(); }
2242 static node_ptr
uncast(const_node_ptr ptr
)
2243 { return node_ptr(const_cast<node
*>(detail::get_pointer(ptr
))); }
2245 node
&priv_value_to_node(value_type
&v
)
2246 { return *this->get_real_value_traits().to_node_ptr(v
); }
2248 const node
&priv_value_to_node(const value_type
&v
) const
2249 { return *this->get_real_value_traits().to_node_ptr(v
); }
2251 size_traits
&priv_size_traits()
2252 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
; }
2254 const size_traits
&priv_size_traits() const
2255 { return this->data_
.internal_
.bucket_hash_equal_
.bucket_hash
.bucket_plus_size_
; }
2257 split_traits
&priv_split_traits()
2258 { return this->data_
.internal_
; }
2260 const split_traits
&priv_split_traits() const
2261 { return this->data_
.internal_
; }
2263 template<class Disposer
>
2264 void priv_erase_range_impl
2265 (size_type bucket_num
, siterator before_first_it
, siterator end
, Disposer disposer
, size_type
&num_erased
)
2267 const bucket_ptr buckets
= priv_buckets();
2268 bucket_type
&b
= buckets
[bucket_num
];
2270 if(before_first_it
== b
.before_begin() && end
== b
.end()){
2271 priv_erase_range_impl(bucket_num
, 1, disposer
, num_erased
);
2275 siterator
to_erase(before_first_it
);
2277 slist_node_ptr end_ptr
= end
.pointed_node();
2278 while(to_erase
!= end
){
2279 group_functions_t::priv_erase_from_group(end_ptr
, dcast_bucket_ptr(to_erase
.pointed_node()), optimize_multikey_t());
2280 to_erase
= b
.erase_after_and_dispose(before_first_it
, make_node_disposer(disposer
));
2283 this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased
);
2287 template<class Disposer
>
2288 void priv_erase_range_impl
2289 (size_type first_bucket_num
, size_type num_buckets
, Disposer disposer
, size_type
&num_erased
)
2291 //Now fully clear the intermediate buckets
2292 const bucket_ptr buckets
= priv_buckets();
2294 for(size_type i
= first_bucket_num
; i
< (num_buckets
+ first_bucket_num
); ++i
){
2295 bucket_type
&b
= buckets
[i
];
2296 siterator
b_begin(b
.before_begin());
2297 siterator
nxt(b_begin
);
2299 siterator
end(b
.end());
2301 priv_init_group(nxt
.pointed_node(), optimize_multikey_t());
2302 nxt
= b
.erase_after_and_dispose
2303 (b_begin
, make_node_disposer(disposer
));
2304 this->priv_size_traits().decrement();
2310 template<class Disposer
>
2311 void priv_erase_range( siterator before_first_it
, size_type first_bucket
2312 , siterator last_it
, size_type last_bucket
2313 , Disposer disposer
)
2315 size_type num_erased
;
2316 if (first_bucket
== last_bucket
){
2317 priv_erase_range_impl(first_bucket
, before_first_it
, last_it
, disposer
, num_erased
);
2320 bucket_type
*b
= (&this->priv_buckets()[0]);
2321 priv_erase_range_impl(first_bucket
, before_first_it
, b
[first_bucket
].end(), disposer
, num_erased
);
2322 if(size_type n
= (last_bucket
- first_bucket
- 1))
2323 priv_erase_range_impl(first_bucket
+ 1, n
, disposer
, num_erased
);
2324 priv_erase_range_impl(last_bucket
, b
[last_bucket
].before_begin(), last_it
, disposer
, num_erased
);
2328 static node_ptr
dcast_bucket_ptr(typename
slist_impl::node_ptr p
)
2329 { return node_ptr(&static_cast<node
&>(*p
)); }
2331 std::size_t priv_stored_or_compute_hash(const value_type
&v
, detail::true_
) const
2332 { return node_traits::get_hash(this->get_real_value_traits().to_node_ptr(v
)); }
2334 std::size_t priv_stored_or_compute_hash(const value_type
&v
, detail::false_
) const
2335 { return priv_hasher()(v
); }
2337 std::size_t priv_stored_hash(slist_node_ptr n
, detail::true_
) const
2338 { return node_traits::get_hash(dcast_bucket_ptr(n
)); }
2340 std::size_t priv_stored_hash(slist_node_ptr
, detail::false_
) const
2342 //This code should never be reached!
2343 BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
2347 static void priv_store_hash(node_ptr p
, std::size_t h
, detail::true_
)
2348 { return node_traits::set_hash(p
, h
); }
2350 static void priv_store_hash(node_ptr
, std::size_t, detail::false_
)
2353 static void priv_clear_group_nodes(bucket_type
&b
, detail::true_
)
2355 siterator
it(b
.begin()), itend(b
.end());
2357 node_ptr
to_erase(dcast_bucket_ptr(it
.pointed_node()));
2359 group_algorithms::init(to_erase
);
2363 static void priv_clear_group_nodes(bucket_type
&, detail::false_
)
2366 std::size_t priv_get_bucket_num(siterator it
)
2367 { return priv_get_bucket_num_hash_dispatch(it
, store_hash_t()); }
2369 std::size_t priv_get_bucket_num_hash_dispatch(siterator it
, detail::true_
)
2371 return this->priv_hash_to_bucket
2372 (this->priv_stored_hash(it
.pointed_node(), store_hash_t()));
2375 std::size_t priv_get_bucket_num_hash_dispatch(siterator it
, detail::false_
)
2376 { return priv_get_bucket_num_no_hash_store(it
, optimize_multikey_t()); }
2378 std::size_t priv_get_bucket_num_no_hash_store(siterator it
, detail::true_
)
2380 bucket_ptr
f(priv_buckets()), l(f
+ priv_buckets_len() - 1);
2381 slist_node_ptr bb
= group_functions_t::priv_get_bucket_before_begin
2382 ( f
->end().pointed_node()
2383 , l
->end().pointed_node()
2384 , dcast_bucket_ptr(it
.pointed_node()));
2385 //Now get the bucket_impl from the iterator
2386 const bucket_type
&b
= static_cast<const bucket_type
&>
2387 (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb
)));
2388 //Now just calculate the index b has in the bucket array
2389 return static_cast<size_type
>(&b
- &*f
);
2392 std::size_t priv_get_bucket_num_no_hash_store(siterator it
, detail::false_
)
2394 bucket_ptr
f(priv_buckets()), l(f
+ priv_buckets_len() - 1);
2395 slist_node_ptr
first_ptr(f
->cend().pointed_node())
2396 , last_ptr(l
->cend().pointed_node());
2398 //The end node is embedded in the singly linked list:
2399 //iterate until we reach it.
2400 while(!(first_ptr
<= it
.pointed_node() && it
.pointed_node() <= last_ptr
)){
2403 //Now get the bucket_impl from the iterator
2404 const bucket_type
&b
= static_cast<const bucket_type
&>
2405 (bucket_type::container_from_end_iterator(it
));
2407 //Now just calculate the index b has in the bucket array
2408 return static_cast<std::size_t>(&b
- &*f
);
2411 void priv_init_group(slist_node_ptr n
, detail::true_
)
2412 { group_algorithms::init(dcast_bucket_ptr(n
)); }
2414 void priv_init_group(slist_node_ptr
, detail::false_
)
2417 void priv_insert_in_group(node_ptr first_in_group
, node_ptr n
, detail::true_
)
2420 if(group_algorithms::unique(first_in_group
))
2421 group_algorithms::link_after(first_in_group
, n
);
2423 group_algorithms::link_after(node_traits::get_next(first_in_group
), n
);
2427 group_algorithms::init_header(n
);
2431 void priv_insert_in_group(node_ptr
, node_ptr
, detail::false_
)
2434 siterator priv_get_previous
2435 (bucket_type
&b
, siterator i
)
2436 { return priv_get_previous(b
, i
, optimize_multikey_t()); }
2438 siterator priv_get_previous
2439 (bucket_type
&b
, siterator i
, detail::true_
)
2441 node_ptr
elem(dcast_bucket_ptr(i
.pointed_node()));
2442 node_ptr
prev_in_group(group_traits::get_next(elem
));
2443 bool first_in_group
= node_traits::get_next(prev_in_group
) != elem
;
2444 typename
bucket_type::node
&n
= first_in_group
2445 ? *group_functions_t::priv_get_prev_to_first_in_group(b
.end().pointed_node(), elem
)
2446 : *group_traits::get_next(elem
)
2448 return bucket_type::s_iterator_to(n
);
2451 siterator priv_get_previous
2452 (bucket_type
&b
, siterator i
, detail::false_
)
2453 { return b
.previous(i
); }
2455 static siterator
priv_get_last(bucket_type
&b
)
2456 { return priv_get_last(b
, optimize_multikey_t()); }
2458 static siterator
priv_get_last(bucket_type
&b
, detail::true_
)
2460 //First find the last node of p's group.
2461 //This requires checking the first node of the next group or
2463 slist_node_ptr
end_ptr(b
.end().pointed_node());
2464 node_ptr
possible_end(node_traits::get_next( dcast_bucket_ptr(end_ptr
)));
2465 node_ptr
last_node_group(possible_end
);
2467 while(end_ptr
!= possible_end
){
2468 last_node_group
= group_traits::get_next(dcast_bucket_ptr(possible_end
));
2469 possible_end
= node_traits::get_next(last_node_group
);
2471 return bucket_type::s_iterator_to(*last_node_group
);
2474 static siterator
priv_get_last(bucket_type
&b
, detail::false_
)
2475 { return b
.previous(b
.end()); }
2477 siterator priv_get_previous_and_next_in_group
2478 (siterator i
, node_ptr
&nxt_in_group
)
2481 node_ptr
elem(dcast_bucket_ptr(i
.pointed_node()));
2482 bucket_ptr
f(priv_buckets()), l(f
+ priv_buckets_len() - 1);
2484 slist_node_ptr
first_end_ptr(f
->cend().pointed_node());
2485 slist_node_ptr
last_end_ptr (l
->cend().pointed_node());
2487 node_ptr
nxt(node_traits::get_next(elem
));
2488 node_ptr
prev_in_group(group_traits::get_next(elem
));
2489 bool last_in_group
= (first_end_ptr
<= nxt
&& nxt
<= last_end_ptr
) ||
2490 (group_traits::get_next(nxt
) != elem
);
2491 bool first_in_group
= node_traits::get_next(prev_in_group
) != elem
;
2500 start_pos
= prev_in_group
;
2501 nxt_in_group
= node_traits::get_next(elem
);
2503 slist_node_ptr bucket_node
;
2505 bucket_node
= this->priv_buckets()
2506 [this->priv_hash_to_bucket
2507 (this->priv_stored_hash(elem
, store_hash_t()))
2508 ].before_begin().pointed_node();
2511 bucket_node
= group_functions_t::priv_get_bucket_before_begin
2512 (first_end_ptr
, last_end_ptr
, start_pos
);
2514 prev
= bucket_type::s_iterator_to
2515 (*group_functions_t::priv_get_prev_to_first_in_group(bucket_node
, elem
));
2519 nxt_in_group
= group_functions_t::priv_get_first_in_group_of_last_in_group(elem
);
2522 nxt_in_group
= node_traits::get_next(elem
);
2524 prev
= bucket_type::s_iterator_to(*group_traits::get_next(elem
));
2529 template<class Disposer
>
2530 void priv_erase(const_iterator i
, Disposer disposer
, detail::true_
)
2532 siterator
elem(i
.slist_it());
2533 node_ptr nxt_in_group
;
2534 siterator prev
= priv_get_previous_and_next_in_group(elem
, nxt_in_group
);
2535 bucket_type::s_erase_after_and_dispose(prev
, make_node_disposer(disposer
));
2537 group_algorithms::unlink_after(nxt_in_group
);
2538 if(safemode_or_autounlink
)
2539 group_algorithms::init(dcast_bucket_ptr(elem
.pointed_node()));
2542 template <class Disposer
>
2543 void priv_erase(const_iterator i
, Disposer disposer
, detail::false_
)
2545 siterator
to_erase(i
.slist_it());
2546 bucket_type
&b
= this->priv_buckets()[this->priv_get_bucket_num(to_erase
)];
2547 siterator
prev(priv_get_previous(b
, to_erase
));
2548 b
.erase_after_and_dispose(prev
, make_node_disposer(disposer
));
2551 bucket_ptr
priv_invalid_bucket() const
2553 const real_bucket_traits
&rbt
= this->priv_real_bucket_traits();
2554 return rbt
.bucket_begin() + rbt
.bucket_count();
2557 siterator
priv_invalid_local_it() const
2558 { return priv_invalid_bucket()->end(); }
2560 siterator
priv_begin() const
2561 { return priv_begin(cache_begin_t()); }
2563 siterator
priv_begin(detail::bool_
<false>) const
2566 size_type buckets_len
= this->priv_buckets_len();
2567 for (n
= 0; n
< buckets_len
; ++n
){
2568 bucket_type
&b
= this->priv_buckets()[n
];
2573 return priv_invalid_local_it();
2576 siterator
priv_begin(detail::bool_
<true>) const
2578 if(this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
== priv_invalid_bucket()){
2579 return priv_invalid_local_it();
2582 return this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
->begin();
2586 void priv_initialize_cache()
2587 { priv_initialize_cache(cache_begin_t()); }
2589 void priv_initialize_cache(detail::bool_
<true>)
2590 { this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
= priv_invalid_bucket(); }
2592 void priv_initialize_cache(detail::bool_
<false>)
2595 void priv_insertion_update_cache(size_type insertion_bucket
)
2596 { priv_insertion_update_cache(insertion_bucket
, cache_begin_t()); }
2598 void priv_insertion_update_cache(size_type insertion_bucket
, detail::bool_
<true>)
2600 bucket_ptr p
= priv_buckets() + insertion_bucket
;
2601 if(p
< this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
){
2602 this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
= p
;
2606 void priv_insertion_update_cache(size_type
, detail::bool_
<false>)
2609 void priv_erasure_update_cache(size_type first_bucket
, size_type last_bucket
)
2610 { priv_erasure_update_cache(first_bucket
, last_bucket
, cache_begin_t()); }
2612 void priv_erasure_update_cache(size_type first_bucket_num
, size_type last_bucket_num
, detail::bool_
<true>)
2614 //If the last bucket is the end, the cache must be updated
2615 //to the last position if all
2616 if(priv_get_cache_bucket_num() == first_bucket_num
&&
2617 priv_buckets()[first_bucket_num
].empty() ){
2618 priv_set_cache(priv_buckets() + last_bucket_num
);
2619 priv_erasure_update_cache();
2623 void priv_erasure_update_cache(size_type
, size_type
, detail::bool_
<false>)
2626 void priv_erasure_update_cache()
2627 { priv_erasure_update_cache(cache_begin_t()); }
2629 void priv_erasure_update_cache(detail::bool_
<true>)
2631 if(constant_time_size
&& !size()){
2632 priv_initialize_cache();
2635 size_type current_n
= this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
- priv_buckets();
2636 for( const size_type num_buckets
= this->priv_buckets_len()
2637 ; current_n
< num_buckets
2638 ; ++current_n
, ++this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
){
2639 if(!this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
->empty()){
2643 priv_initialize_cache();
2647 void priv_erasure_update_cache(detail::bool_
<false>)
2650 void priv_swap_cache(detail::bool_
<true>, hashtable_impl
&other
)
2652 std::swap( this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
2653 , other
.data_
.internal_
.bucket_hash_equal_
.cached_begin_
);
2656 void priv_swap_cache(detail::bool_
<false>, hashtable_impl
&)
2659 bucket_ptr
priv_get_cache()
2660 { return priv_get_cache(cache_begin_t()); }
2662 bucket_ptr
priv_get_cache(detail::bool_
<true>)
2663 { return this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
; }
2665 bucket_ptr
priv_get_cache(detail::bool_
<false>)
2666 { return this->priv_buckets(); }
2668 void priv_set_cache(bucket_ptr p
)
2669 { priv_set_cache(p
, cache_begin_t()); }
2671 void priv_set_cache(bucket_ptr p
, detail::bool_
<true>)
2672 { this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
= p
; }
2674 void priv_set_cache(bucket_ptr
, detail::bool_
<false>)
2677 size_type
priv_get_cache_bucket_num()
2678 { return priv_get_cache_bucket_num(cache_begin_t()); }
2680 size_type
priv_get_cache_bucket_num(detail::bool_
<true>)
2681 { return this->data_
.internal_
.bucket_hash_equal_
.cached_begin_
- this->priv_buckets(); }
2683 size_type
priv_get_cache_bucket_num(detail::bool_
<false>)
2686 void priv_clear_buckets()
2688 this->priv_clear_buckets
2690 , this->priv_buckets_len() - (priv_get_cache() - priv_buckets()));
2693 void priv_initialize_buckets()
2694 { this->priv_clear_buckets(priv_buckets(), this->priv_buckets_len()); }
2696 void priv_clear_buckets(bucket_ptr buckets_ptr
, size_type buckets_len
)
2698 for(; buckets_len
--; ++buckets_ptr
){
2699 if(safemode_or_autounlink
){
2700 priv_clear_group_nodes(*buckets_ptr
, optimize_multikey_t());
2701 buckets_ptr
->clear_and_dispose(detail::init_disposer
<node_algorithms
>());
2704 buckets_ptr
->clear();
2707 priv_initialize_cache();
2710 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
2712 ( const KeyType
&key
, KeyHasher hash_func
2713 , KeyValueEqual equal_func
, size_type
&bucket_number
, std::size_t &h
, siterator
&previt
) const
2716 return priv_find_with_hash(key
, equal_func
, bucket_number
, h
, previt
);
2719 template<class KeyType
, class KeyValueEqual
>
2720 siterator priv_find_with_hash
2721 ( const KeyType
&key
, KeyValueEqual equal_func
, size_type
&bucket_number
, const std::size_t h
, siterator
&previt
) const
2723 bucket_number
= priv_hash_to_bucket(h
);
2724 bucket_type
&b
= this->priv_buckets()[bucket_number
];
2725 previt
= b
.before_begin();
2726 if(constant_time_size
&& this->empty()){
2727 return priv_invalid_local_it();
2730 siterator it
= previt
;
2733 while(it
!= b
.end()){
2734 const value_type
&v
= priv_value_from_slist_node(it
.pointed_node());
2736 std::size_t vh
= this->priv_stored_or_compute_hash(v
, store_hash_t());
2737 if(h
== vh
&& equal_func(key
, v
)){
2741 else if(equal_func(key
, v
)){
2744 if(optimize_multikey
){
2745 previt
= bucket_type::s_iterator_to
2746 (*group_functions_t::priv_get_last_in_group
2747 (dcast_bucket_ptr(it
.pointed_node()), optimize_multikey_t()));
2755 previt
= b
.before_begin();
2756 return priv_invalid_local_it();
2759 iterator
priv_insert_equal_with_hash(reference value
, std::size_t hash_value
)
2761 size_type bucket_num
;
2763 siterator it
= this->priv_find_with_hash
2764 (value
, this->priv_equal(), bucket_num
, hash_value
, prev
);
2765 return priv_insert_equal_find(value
, bucket_num
, hash_value
, it
);
2768 iterator
priv_insert_equal_find(reference value
, size_type bucket_num
, std::size_t hash_value
, siterator it
)
2770 bucket_type
&b
= this->priv_buckets()[bucket_num
];
2771 bool found_equal
= it
!= priv_invalid_local_it();
2773 it
= b
.before_begin();
2775 //Now store hash if needed
2776 node_ptr n
= node_ptr(&priv_value_to_node(value
));
2777 this->priv_store_hash(n
, hash_value
, store_hash_t());
2778 //Checks for some modes
2779 if(safemode_or_autounlink
)
2780 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n
));
2781 //Shorcut for optimize_multikey cases
2782 if(optimize_multikey
){
2783 node_ptr first_in_group
= found_equal
?
2784 dcast_bucket_ptr(it
.pointed_node()) : node_ptr(0);
2785 this->priv_insert_in_group(first_in_group
, n
, optimize_multikey_t());
2787 //Update cache and increment size if needed
2788 priv_insertion_update_cache(bucket_num
);
2789 this->priv_size_traits().increment();
2790 //Insert the element in the bucket after it
2791 return iterator(b
.insert_after(it
, *n
), this);
2794 template<class KeyType
, class KeyHasher
, class KeyValueEqual
>
2795 std::pair
<siterator
, siterator
> priv_equal_range
2796 ( const KeyType
&key
2797 , KeyHasher hash_func
2798 , KeyValueEqual equal_func
2799 , size_type
&bucket_number_first
2800 , size_type
&bucket_number_second
2801 , size_type
&count
) const
2806 //Let's see if the element is present
2807 std::pair
<siterator
, siterator
> to_return
2808 ( priv_find(key
, hash_func
, equal_func
, bucket_number_first
, h
, prev
)
2809 , priv_invalid_local_it());
2810 if(to_return
.first
== to_return
.second
){
2811 bucket_number_second
= bucket_number_first
;
2814 //If it's present, find the first that it's not equal in
2816 bucket_type
&b
= this->priv_buckets()[bucket_number_first
];
2817 siterator it
= to_return
.first
;
2818 if(optimize_multikey
){
2819 to_return
.second
= bucket_type::s_iterator_to
2820 (*node_traits::get_next(group_functions_t::priv_get_last_in_group
2821 (dcast_bucket_ptr(it
.pointed_node()), optimize_multikey_t())));
2822 count
= std::distance(it
, to_return
.second
);
2823 if(to_return
.second
!= b
.end()){
2824 bucket_number_second
= bucket_number_first
;
2831 while(it
!= b
.end()){
2832 const value_type
&v
= priv_value_from_slist_node(it
.pointed_node());
2834 std::size_t hv
= this->priv_stored_or_compute_hash(v
, store_hash_t());
2835 if(hv
!= h
|| !equal_func(key
, v
)){
2836 to_return
.second
= it
;
2837 bucket_number_second
= bucket_number_first
;
2841 else if(!equal_func(key
, v
)){
2842 to_return
.second
= it
;
2843 bucket_number_second
= bucket_number_first
;
2851 //If we reached the end, find the first, non-empty bucket
2852 for(bucket_number_second
= bucket_number_first
+1
2853 ; bucket_number_second
!= this->priv_buckets_len()
2854 ; ++bucket_number_second
){
2855 bucket_type
&b
= this->priv_buckets()[bucket_number_second
];
2857 to_return
.second
= b
.begin();
2862 //Otherwise, return the end node
2863 to_return
.second
= priv_invalid_local_it();
2870 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2873 , class O1
= none
, class O2
= none
2874 , class O3
= none
, class O4
= none
2875 , class O5
= none
, class O6
= none
2876 , class O7
= none
, class O8
= none
2877 , class O9
= none
, class O10
= none
2880 template <class T
, bool UniqueKeys
, class ...Options
>
2882 struct make_hashtable_opt
2884 typedef typename pack_options
2886 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2887 O1
, O2
, O3
, O4
, O5
, O6
, O7
, O8
, O9
, O10
2891 >::type packed_options
;
2893 //Real value traits must be calculated from options
2894 typedef typename
detail::get_value_traits
2895 <T
, typename
packed_options::value_traits
>::type value_traits
;
2897 static const bool external_value_traits
=
2898 detail::external_value_traits_is_true
<value_traits
>::value
;
2899 typedef typename
detail::eval_if_c
2900 < external_value_traits
2901 , detail::eval_value_traits
<value_traits
>
2902 , detail::identity
<value_traits
>
2903 >::type real_value_traits
;
2904 typedef typename
packed_options::bucket_traits specified_bucket_traits
;
2907 //Real bucket traits must be calculated from options and calculated value_traits
2908 typedef typename
detail::get_slist_impl
2909 <typename
detail::reduced_slist_node_traits
2910 <typename
real_value_traits::node_traits
>::type
2914 detail::if_c
< detail::is_same
2915 < specified_bucket_traits
2916 , default_bucket_traits
2918 , detail::bucket_traits_impl
<slist_impl
>
2919 , specified_bucket_traits
2920 >::type real_bucket_traits
;
2922 typedef detail::usetopt
2924 , typename
packed_options::hash
2925 , typename
packed_options::equal
2926 , typename
packed_options::size_type
2927 , real_bucket_traits
2928 , (std::size_t(UniqueKeys
)*detail::hash_bool_flags::unique_keys_pos
)
2929 | (std::size_t(packed_options::constant_time_size
)*detail::hash_bool_flags::constant_time_size_pos
)
2930 | (std::size_t(packed_options::power_2_buckets
)*detail::hash_bool_flags::power_2_buckets_pos
)
2931 | (std::size_t(packed_options::cache_begin
)*detail::hash_bool_flags::cache_begin_pos
)
2932 | (std::size_t(packed_options::compare_hash
)*detail::hash_bool_flags::compare_hash_pos
)
2933 | (std::size_t(packed_options::incremental
)*detail::hash_bool_flags::incremental_pos
)
2938 //! Helper metafunction to define a \c hashtable that yields to the same type when the
2939 //! same options (either explicitly or implicitly) are used.
2940 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2941 template<class T
, class ...Options
>
2943 template<class T
, class O1
= none
, class O2
= none
2944 , class O3
= none
, class O4
= none
2945 , class O5
= none
, class O6
= none
2946 , class O7
= none
, class O8
= none
2947 , class O9
= none
, class O10
= none
2950 struct make_hashtable
2953 typedef hashtable_impl
2954 < typename make_hashtable_opt
2956 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2957 O1
, O2
, O3
, O4
, O5
, O6
, O7
, O8
, O9
, O10
2962 > implementation_defined
;
2965 typedef implementation_defined type
;
2968 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2970 #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2971 template<class T
, class ...Options
>
2973 template<class T
, class O1
, class O2
, class O3
, class O4
, class O5
, class O6
, class O7
, class O8
, class O9
, class O10
>
2976 : public make_hashtable
<T
,
2977 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2978 O1
, O2
, O3
, O4
, O5
, O6
, O7
, O8
, O9
, O10
2984 typedef typename make_hashtable
<T
,
2985 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2986 O1
, O2
, O3
, O4
, O5
, O6
, O7
, O8
, O9
, O10
2993 typedef typename
Base::value_traits value_traits
;
2994 typedef typename
Base::real_value_traits real_value_traits
;
2995 typedef typename
Base::iterator iterator
;
2996 typedef typename
Base::const_iterator const_iterator
;
2997 typedef typename
Base::bucket_ptr bucket_ptr
;
2998 typedef typename
Base::size_type size_type
;
2999 typedef typename
Base::hasher hasher
;
3000 typedef typename
Base::bucket_traits bucket_traits
;
3001 typedef typename
Base::key_equal key_equal
;
3003 //Assert if passed value traits are compatible with the type
3004 BOOST_STATIC_ASSERT((detail::is_same
<typename
real_value_traits::value_type
, T
>::value
));
3006 hashtable ( const bucket_traits
&b_traits
3007 , const hasher
& hash_func
= hasher()
3008 , const key_equal
&equal_func
= key_equal()
3009 , const value_traits
&v_traits
= value_traits())
3010 : Base(b_traits
, hash_func
, equal_func
, v_traits
)
3016 } //namespace intrusive
3019 #include <boost/intrusive/detail/config_end.hpp>
3021 #endif //BOOST_INTRUSIVE_HASHTABLE_HPP