Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2006-2013 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP Chris@16: #define BOOST_INTRUSIVE_HASHTABLE_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@101: Chris@16: //General intrusive utilities Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@16: //Implementation utilities Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: Chris@101: //boost Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@101: //std C++ Chris@101: #include //std::equal_to Chris@101: #include //std::pair Chris@101: #include //std::lower_bound, std::upper_bound Chris@101: #include //std::size_t Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: Chris@101: template Chris@101: struct prime_list_holder Chris@101: { Chris@101: static const std::size_t prime_list[]; Chris@101: static const std::size_t prime_list_size; Chris@101: }; Chris@101: Chris@101: //We only support LLP64(Win64) or LP64(most Unix) data models Chris@101: #ifdef _WIN64 //In 64 bit windows sizeof(size_t) == sizeof(unsigned long long) Chris@101: #define BOOST_INTRUSIVE_PRIME_C(NUMBER) NUMBER##ULL Chris@101: #define BOOST_INTRUSIVE_64_BIT_SIZE_T 1 Chris@101: #else //In 32 bit windows and 32/64 bit unixes sizeof(size_t) == sizeof(unsigned long) Chris@101: #define BOOST_INTRUSIVE_PRIME_C(NUMBER) NUMBER##UL Chris@101: #define BOOST_INTRUSIVE_64_BIT_SIZE_T (((((ULONG_MAX>>16)>>16)>>16)>>15) != 0) Chris@101: #endif Chris@101: Chris@101: template Chris@101: const std::size_t prime_list_holder::prime_list[] = { Chris@101: BOOST_INTRUSIVE_PRIME_C(3), BOOST_INTRUSIVE_PRIME_C(7), Chris@101: BOOST_INTRUSIVE_PRIME_C(11), BOOST_INTRUSIVE_PRIME_C(17), Chris@101: BOOST_INTRUSIVE_PRIME_C(29), BOOST_INTRUSIVE_PRIME_C(53), Chris@101: BOOST_INTRUSIVE_PRIME_C(97), BOOST_INTRUSIVE_PRIME_C(193), Chris@101: BOOST_INTRUSIVE_PRIME_C(389), BOOST_INTRUSIVE_PRIME_C(769), Chris@101: BOOST_INTRUSIVE_PRIME_C(1543), BOOST_INTRUSIVE_PRIME_C(3079), Chris@101: BOOST_INTRUSIVE_PRIME_C(6151), BOOST_INTRUSIVE_PRIME_C(12289), Chris@101: BOOST_INTRUSIVE_PRIME_C(24593), BOOST_INTRUSIVE_PRIME_C(49157), Chris@101: BOOST_INTRUSIVE_PRIME_C(98317), BOOST_INTRUSIVE_PRIME_C(196613), Chris@101: BOOST_INTRUSIVE_PRIME_C(393241), BOOST_INTRUSIVE_PRIME_C(786433), Chris@101: BOOST_INTRUSIVE_PRIME_C(1572869), BOOST_INTRUSIVE_PRIME_C(3145739), Chris@101: BOOST_INTRUSIVE_PRIME_C(6291469), BOOST_INTRUSIVE_PRIME_C(12582917), Chris@101: BOOST_INTRUSIVE_PRIME_C(25165843), BOOST_INTRUSIVE_PRIME_C(50331653), Chris@101: BOOST_INTRUSIVE_PRIME_C(100663319), BOOST_INTRUSIVE_PRIME_C(201326611), Chris@101: BOOST_INTRUSIVE_PRIME_C(402653189), BOOST_INTRUSIVE_PRIME_C(805306457), Chris@101: BOOST_INTRUSIVE_PRIME_C(1610612741), BOOST_INTRUSIVE_PRIME_C(3221225473), Chris@101: #if BOOST_INTRUSIVE_64_BIT_SIZE_T Chris@101: //Taken from Boost.MultiIndex code, thanks to Joaquin M Lopez Munoz. Chris@101: BOOST_INTRUSIVE_PRIME_C(6442450939), BOOST_INTRUSIVE_PRIME_C(12884901893), Chris@101: BOOST_INTRUSIVE_PRIME_C(25769803751), BOOST_INTRUSIVE_PRIME_C(51539607551), Chris@101: BOOST_INTRUSIVE_PRIME_C(103079215111), BOOST_INTRUSIVE_PRIME_C(206158430209), Chris@101: BOOST_INTRUSIVE_PRIME_C(412316860441), BOOST_INTRUSIVE_PRIME_C(824633720831), Chris@101: BOOST_INTRUSIVE_PRIME_C(1649267441651), BOOST_INTRUSIVE_PRIME_C(3298534883309), Chris@101: BOOST_INTRUSIVE_PRIME_C(6597069766657), BOOST_INTRUSIVE_PRIME_C(13194139533299), Chris@101: BOOST_INTRUSIVE_PRIME_C(26388279066623), BOOST_INTRUSIVE_PRIME_C(52776558133303), Chris@101: BOOST_INTRUSIVE_PRIME_C(105553116266489), BOOST_INTRUSIVE_PRIME_C(211106232532969), Chris@101: BOOST_INTRUSIVE_PRIME_C(422212465066001), BOOST_INTRUSIVE_PRIME_C(844424930131963), Chris@101: BOOST_INTRUSIVE_PRIME_C(1688849860263953), BOOST_INTRUSIVE_PRIME_C(3377699720527861), Chris@101: BOOST_INTRUSIVE_PRIME_C(6755399441055731), BOOST_INTRUSIVE_PRIME_C(13510798882111483), Chris@101: BOOST_INTRUSIVE_PRIME_C(27021597764222939), BOOST_INTRUSIVE_PRIME_C(54043195528445957), Chris@101: BOOST_INTRUSIVE_PRIME_C(108086391056891903), BOOST_INTRUSIVE_PRIME_C(216172782113783843), Chris@101: BOOST_INTRUSIVE_PRIME_C(432345564227567621), BOOST_INTRUSIVE_PRIME_C(864691128455135207), Chris@101: BOOST_INTRUSIVE_PRIME_C(1729382256910270481), BOOST_INTRUSIVE_PRIME_C(3458764513820540933), Chris@101: BOOST_INTRUSIVE_PRIME_C(6917529027641081903), BOOST_INTRUSIVE_PRIME_C(13835058055282163729), Chris@101: BOOST_INTRUSIVE_PRIME_C(18446744073709551557) Chris@101: #else Chris@101: BOOST_INTRUSIVE_PRIME_C(4294967291) Chris@101: #endif Chris@101: }; Chris@101: Chris@101: #undef BOOST_INTRUSIVE_PRIME_C Chris@101: #undef BOOST_INTRUSIVE_64_BIT_SIZE_T Chris@101: Chris@101: template Chris@101: const std::size_t prime_list_holder::prime_list_size Chris@101: = sizeof(prime_list)/sizeof(std::size_t); Chris@101: Chris@16: struct hash_bool_flags Chris@16: { Chris@16: static const std::size_t unique_keys_pos = 1u; Chris@16: static const std::size_t constant_time_size_pos = 2u; Chris@16: static const std::size_t power_2_buckets_pos = 4u; Chris@16: static const std::size_t cache_begin_pos = 8u; Chris@16: static const std::size_t compare_hash_pos = 16u; Chris@16: static const std::size_t incremental_pos = 32u; Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct get_slist_impl_from_supposed_value_traits Chris@16: { Chris@101: typedef SupposedValueTraits value_traits; Chris@16: typedef typename detail::get_node_traits Chris@101: ::type node_traits; Chris@16: typedef typename get_slist_impl Chris@16: ::type Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct unordered_bucket_impl Chris@16: { Chris@16: typedef typename Chris@16: get_slist_impl_from_supposed_value_traits Chris@16: ::type slist_impl; Chris@16: typedef detail::bucket_impl implementation_defined; Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct unordered_bucket_ptr_impl Chris@16: { Chris@16: typedef typename detail::get_node_traits Chris@16: ::type::node_ptr node_ptr; Chris@16: typedef typename unordered_bucket_impl Chris@16: ::type bucket_type; Chris@16: Chris@16: typedef typename pointer_traits Chris@16: ::template rebind_pointer Chris@16: < bucket_type >::type implementation_defined; Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct store_hash_bool Chris@16: { Chris@16: template Chris@16: struct two_or_three {one _[2 + Add];}; Chris@16: template static one test(...); Chris@16: template static two_or_three test (int); Chris@16: static const std::size_t value = sizeof(test(0)); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct store_hash_is_true Chris@16: { Chris@16: static const bool value = store_hash_bool::value > sizeof(one)*2; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct optimize_multikey_bool Chris@16: { Chris@16: template Chris@16: struct two_or_three {one _[2 + Add];}; Chris@16: template static one test(...); Chris@16: template static two_or_three test (int); Chris@16: static const std::size_t value = sizeof(test(0)); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct optimize_multikey_is_true Chris@16: { Chris@16: static const bool value = optimize_multikey_bool::value > sizeof(one)*2; Chris@16: }; Chris@16: Chris@16: struct insert_commit_data_impl Chris@16: { Chris@16: std::size_t hash; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline typename pointer_traits::template rebind_pointer::type Chris@16: dcast_bucket_ptr(const SlistNodePtr &p) Chris@16: { Chris@16: typedef typename pointer_traits::template rebind_pointer::type node_ptr; Chris@16: return pointer_traits::pointer_to(static_cast(*p)); Chris@16: } Chris@16: Chris@16: template Chris@16: struct group_functions Chris@16: { Chris@16: typedef NodeTraits node_traits; Chris@16: typedef unordered_group_adapter group_traits; Chris@16: typedef typename node_traits::node_ptr node_ptr; Chris@16: typedef typename node_traits::node node; Chris@16: typedef typename reduced_slist_node_traits Chris@16: ::type reduced_node_traits; Chris@16: typedef typename reduced_node_traits::node_ptr slist_node_ptr; Chris@16: typedef typename reduced_node_traits::node slist_node; Chris@16: typedef circular_slist_algorithms group_algorithms; Chris@16: Chris@16: static slist_node_ptr get_bucket_before_begin Chris@16: (const slist_node_ptr &bucket_beg, const slist_node_ptr &bucket_end, const node_ptr &p) Chris@16: { Chris@16: //First find the last node of p's group. Chris@16: //This requires checking the first node of the next group or Chris@16: //the bucket node. Chris@16: node_ptr prev_node = p; Chris@16: node_ptr nxt(node_traits::get_next(p)); Chris@16: while(!(bucket_beg <= nxt && nxt <= bucket_end) && Chris@16: (group_traits::get_next(nxt) == prev_node)){ Chris@16: prev_node = nxt; Chris@16: nxt = node_traits::get_next(nxt); Chris@16: } Chris@16: Chris@16: //If we've reached the bucket node just return it. Chris@16: if(bucket_beg <= nxt && nxt <= bucket_end){ Chris@16: return nxt; Chris@16: } Chris@16: Chris@16: //Otherwise, iterate using group links until the bucket node Chris@16: node_ptr first_node_of_group = nxt; Chris@16: node_ptr last_node_group = group_traits::get_next(first_node_of_group); Chris@16: slist_node_ptr possible_end = node_traits::get_next(last_node_group); Chris@16: Chris@16: while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){ Chris@16: first_node_of_group = detail::dcast_bucket_ptr(possible_end); Chris@16: last_node_group = group_traits::get_next(first_node_of_group); Chris@16: possible_end = node_traits::get_next(last_node_group); Chris@16: } Chris@16: return possible_end; Chris@16: } Chris@16: Chris@16: static node_ptr get_prev_to_first_in_group(const slist_node_ptr &bucket_node, const node_ptr &first_in_group) Chris@16: { Chris@16: //Just iterate using group links and obtain the node Chris@16: //before "first_in_group)" Chris@16: node_ptr prev_node = detail::dcast_bucket_ptr(bucket_node); Chris@16: node_ptr nxt(node_traits::get_next(prev_node)); Chris@16: while(nxt != first_in_group){ Chris@16: prev_node = group_traits::get_next(nxt); Chris@16: nxt = node_traits::get_next(prev_node); Chris@16: } Chris@16: return prev_node; Chris@16: } Chris@16: Chris@16: static node_ptr get_first_in_group_of_last_in_group(const node_ptr &last_in_group) Chris@16: { Chris@16: //Just iterate using group links and obtain the node Chris@16: //before "last_in_group" Chris@16: node_ptr possible_first = group_traits::get_next(last_in_group); Chris@16: node_ptr possible_first_prev = group_traits::get_next(possible_first); Chris@16: // The deleted node is at the end of the group, so the Chris@16: // node in the group pointing to it is at the beginning Chris@16: // of the group. Find that to change its pointer. Chris@16: while(possible_first_prev != last_in_group){ Chris@16: possible_first = possible_first_prev; Chris@16: possible_first_prev = group_traits::get_next(possible_first); Chris@16: } Chris@16: return possible_first; Chris@16: } Chris@16: Chris@16: static void erase_from_group(const slist_node_ptr &end_ptr, const node_ptr &to_erase_ptr, detail::true_) Chris@16: { Chris@16: node_ptr nxt_ptr(node_traits::get_next(to_erase_ptr)); Chris@16: node_ptr prev_in_group_ptr(group_traits::get_next(to_erase_ptr)); Chris@16: bool last_in_group = (end_ptr == nxt_ptr) || Chris@16: (group_traits::get_next(nxt_ptr) != to_erase_ptr); Chris@16: bool is_first_in_group = node_traits::get_next(prev_in_group_ptr) != to_erase_ptr; Chris@16: Chris@16: if(is_first_in_group && last_in_group){ Chris@16: group_algorithms::init(to_erase_ptr); Chris@16: } Chris@16: else if(is_first_in_group){ Chris@16: group_algorithms::unlink_after(nxt_ptr); Chris@16: } Chris@16: else if(last_in_group){ Chris@16: node_ptr first_in_group = Chris@16: get_first_in_group_of_last_in_group(to_erase_ptr); Chris@16: group_algorithms::unlink_after(first_in_group); Chris@16: } Chris@16: else{ Chris@16: group_algorithms::unlink_after(nxt_ptr); Chris@16: } Chris@16: } Chris@16: Chris@16: static void erase_from_group(const slist_node_ptr&, const node_ptr&, detail::false_) Chris@16: {} Chris@16: Chris@16: static node_ptr get_last_in_group(const node_ptr &first_in_group, detail::true_) Chris@16: { return group_traits::get_next(first_in_group); } Chris@16: Chris@16: static node_ptr get_last_in_group(const node_ptr &n, detail::false_) Chris@16: { return n; } Chris@16: Chris@16: static void init_group(const node_ptr &n, true_) Chris@16: { group_algorithms::init(n); } Chris@16: Chris@16: static void init_group(const node_ptr &, false_) Chris@16: {} Chris@16: Chris@16: static void insert_in_group(const node_ptr &first_in_group, const node_ptr &n, true_) Chris@16: { Chris@16: if(first_in_group){ Chris@16: if(group_algorithms::unique(first_in_group)) Chris@16: group_algorithms::link_after(first_in_group, n); Chris@16: else{ Chris@101: group_algorithms::link_after(node_traits::get_next(first_in_group), n); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: group_algorithms::init_header(n); Chris@16: } Chris@16: } Chris@16: Chris@16: static slist_node_ptr get_previous_and_next_in_group Chris@16: ( const slist_node_ptr &i, node_ptr &nxt_in_group Chris@16: //If first_end_ptr == last_end_ptr, then first_end_ptr is the bucket of i Chris@16: //Otherwise first_end_ptr is the first bucket and last_end_ptr the last one. Chris@16: , const slist_node_ptr &first_end_ptr, const slist_node_ptr &last_end_ptr) Chris@16: { Chris@16: slist_node_ptr prev; Chris@16: node_ptr elem(detail::dcast_bucket_ptr(i)); Chris@16: Chris@16: //It's the last in group if the next_node is a bucket Chris@16: slist_node_ptr nxt(node_traits::get_next(elem)); Chris@101: bool last_in_group = (first_end_ptr <= nxt && nxt <= last_end_ptr) || Chris@101: (group_traits::get_next(detail::dcast_bucket_ptr(nxt)) != elem); Chris@16: //It's the first in group if group_previous's next_node is not Chris@16: //itself, as group list does not link bucket Chris@16: node_ptr prev_in_group(group_traits::get_next(elem)); Chris@16: bool first_in_group = node_traits::get_next(prev_in_group) != elem; Chris@16: Chris@16: if(first_in_group){ Chris@16: node_ptr start_pos; Chris@16: if(last_in_group){ Chris@16: start_pos = elem; Chris@16: nxt_in_group = node_ptr(); Chris@16: } Chris@16: else{ Chris@16: start_pos = prev_in_group; Chris@16: nxt_in_group = node_traits::get_next(elem); Chris@16: } Chris@16: slist_node_ptr bucket_node; Chris@16: if(first_end_ptr != last_end_ptr){ Chris@16: bucket_node = group_functions::get_bucket_before_begin Chris@16: (first_end_ptr, last_end_ptr, start_pos); Chris@16: } Chris@16: else{ Chris@16: bucket_node = first_end_ptr; Chris@16: } Chris@16: prev = group_functions::get_prev_to_first_in_group(bucket_node, elem); Chris@16: } Chris@16: else{ Chris@16: if(last_in_group){ Chris@16: nxt_in_group = group_functions::get_first_in_group_of_last_in_group(elem); Chris@16: } Chris@16: else{ Chris@16: nxt_in_group = node_traits::get_next(elem); Chris@16: } Chris@16: prev = group_traits::get_next(elem); Chris@16: } Chris@16: return prev; Chris@16: } Chris@16: Chris@16: static void insert_in_group(const node_ptr&, const node_ptr&, false_) Chris@16: {} Chris@16: }; Chris@16: Chris@16: template Chris@16: class incremental_rehash_rollback Chris@16: { Chris@16: private: Chris@16: typedef BucketType bucket_type; Chris@16: typedef SplitTraits split_traits; Chris@16: Chris@16: incremental_rehash_rollback(); Chris@16: incremental_rehash_rollback & operator=(const incremental_rehash_rollback &); Chris@16: incremental_rehash_rollback (const incremental_rehash_rollback &); Chris@16: Chris@16: public: Chris@16: incremental_rehash_rollback Chris@16: (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_traits) Chris@16: : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket) Chris@16: , split_traits_(split_traits), released_(false) Chris@16: {} Chris@16: Chris@16: void release() Chris@16: { released_ = true; } Chris@16: Chris@16: ~incremental_rehash_rollback() Chris@16: { Chris@16: if(!released_){ Chris@16: //If an exception is thrown, just put all moved nodes back in the old bucket Chris@16: //and move back the split mark. Chris@16: destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_); Chris@16: split_traits_.decrement(); Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: bucket_type &source_bucket_; Chris@16: bucket_type &destiny_bucket_; Chris@16: split_traits &split_traits_; Chris@16: bool released_; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct node_functions Chris@16: { Chris@16: static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_) Chris@16: { return NodeTraits::set_hash(p, h); } Chris@16: Chris@16: static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_) Chris@16: {} Chris@16: }; Chris@16: Chris@16: inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_) Chris@16: { return hash_value % bucket_cnt; } Chris@16: Chris@16: inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_) Chris@16: { return hash_value & (bucket_cnt - 1); } Chris@16: Chris@16: template Chris@16: inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split) Chris@16: { Chris@16: std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_()); Chris@16: if(Incremental) Chris@16: if(bucket_number >= split) Chris@16: bucket_number -= bucket_cnt/2; Chris@16: return bucket_number; Chris@16: } Chris@16: Chris@16: } //namespace detail { Chris@16: Chris@16: //!This metafunction will obtain the type of a bucket Chris@16: //!from the value_traits or hook option to be used with Chris@16: //!a hash container. Chris@16: template Chris@16: struct unordered_bucket Chris@16: : public detail::unordered_bucket_impl Chris@16: ::proto_value_traits Chris@16: > Chris@16: {}; Chris@16: Chris@16: //!This metafunction will obtain the type of a bucket pointer Chris@16: //!from the value_traits or hook option to be used with Chris@16: //!a hash container. Chris@16: template Chris@16: struct unordered_bucket_ptr Chris@101: : public detail::unordered_bucket_ptr_impl Chris@101: ::proto_value_traits Chris@101: > Chris@16: {}; Chris@16: Chris@16: //!This metafunction will obtain the type of the default bucket traits Chris@16: //!(when the user does not specify the bucket_traits<> option) from the Chris@16: //!value_traits or hook option to be used with Chris@16: //!a hash container. Chris@16: template Chris@16: struct unordered_default_bucket_traits Chris@16: { Chris@16: typedef typename ValueTraitsOrHookOption:: Chris@101: template pack::proto_value_traits supposed_value_traits; Chris@16: typedef typename detail:: Chris@16: get_slist_impl_from_supposed_value_traits Chris@16: ::type slist_impl; Chris@16: typedef detail::bucket_traits_impl Chris@16: implementation_defined; Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: struct default_bucket_traits; Chris@16: Chris@101: //hashtable default hook traits Chris@101: struct default_hashtable_hook_applier Chris@101: { template struct apply{ typedef typename T::default_hashtable_hook type; }; }; Chris@101: Chris@101: template<> Chris@101: struct is_default_hook_tag Chris@101: { static const bool value = true; }; Chris@101: Chris@16: struct hashtable_defaults Chris@16: { Chris@101: typedef default_hashtable_hook_applier proto_value_traits; Chris@16: typedef std::size_t size_type; Chris@16: typedef void equal; Chris@16: typedef void hash; Chris@16: typedef default_bucket_traits bucket_traits; Chris@16: static const bool constant_time_size = true; Chris@16: static const bool power_2_buckets = false; Chris@16: static const bool cache_begin = false; Chris@16: static const bool compare_hash = false; Chris@16: static const bool incremental = false; Chris@16: }; Chris@16: Chris@101: template Chris@16: struct downcast_node_to_value_t Chris@101: : public detail::node_to_value Chris@16: { Chris@101: typedef detail::node_to_value base_t; Chris@16: typedef typename base_t::result_type result_type; Chris@101: typedef ValueTraits value_traits; Chris@16: typedef typename detail::get_slist_impl Chris@16: ::type Chris@16: >::type slist_impl; Chris@16: typedef typename detail::add_const_if_c Chris@16: ::type & first_argument_type; Chris@16: typedef typename detail::add_const_if_c Chris@101: < typename ValueTraits::node_traits::node Chris@16: , IsConst>::type & intermediate_argument_type; Chris@16: typedef typename pointer_traits Chris@101: :: Chris@16: template rebind_pointer Chris@101: ::type const_value_traits_ptr; Chris@101: Chris@101: downcast_node_to_value_t(const const_value_traits_ptr &ptr) Chris@16: : base_t(ptr) Chris@16: {} Chris@16: Chris@16: result_type operator()(first_argument_type arg) const Chris@16: { return this->base_t::operator()(static_cast(arg)); } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct node_cast_adaptor Chris@101: //Use public inheritance to avoid MSVC bugs with closures Chris@101: : public detail::ebo_functor_holder Chris@16: { Chris@16: typedef detail::ebo_functor_holder base_t; Chris@16: Chris@16: typedef typename pointer_traits::element_type slist_node; Chris@16: typedef typename pointer_traits::element_type node; Chris@16: Chris@16: template Chris@16: node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits) Chris@16: : base_t(base_t(c2f, traits)) Chris@16: {} Chris@16: Chris@16: typename base_t::node_ptr operator()(const slist_node &to_clone) Chris@16: { return base_t::operator()(static_cast(to_clone)); } Chris@16: Chris@16: void operator()(SlistNodePtr to_clone) Chris@16: { Chris@16: base_t::operator()(pointer_traits::pointer_to(static_cast(*to_clone))); Chris@16: } Chris@16: }; Chris@16: Chris@16: static const std::size_t hashtable_data_bool_flags_mask = Chris@16: ( hash_bool_flags::cache_begin_pos Chris@16: | hash_bool_flags::constant_time_size_pos Chris@16: | hash_bool_flags::incremental_pos Chris@16: ); Chris@16: Chris@101: //bucket_plus_vtraits stores ValueTraits + BucketTraits Chris@101: //this data is needed by iterators to obtain the Chris@101: //value from the iterator and detect the bucket Chris@16: template Chris@16: struct bucket_plus_vtraits : public ValueTraits Chris@16: { Chris@16: typedef BucketTraits bucket_traits; Chris@16: typedef ValueTraits value_traits; Chris@16: Chris@101: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@101: Chris@16: typedef typename Chris@16: detail::get_slist_impl_from_supposed_value_traits Chris@101: ::type slist_impl; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@101: typedef unordered_group_adapter group_traits; Chris@101: typedef typename slist_impl::iterator siterator; Chris@101: typedef typename slist_impl::size_type size_type; Chris@101: typedef detail::bucket_impl bucket_type; Chris@101: typedef detail::group_functions group_functions_t; Chris@101: typedef typename slist_impl::node_algorithms node_algorithms; Chris@101: typedef typename slist_impl::node_ptr slist_node_ptr; Chris@101: typedef typename node_traits::node_ptr node_ptr; Chris@101: typedef typename node_traits::node node; Chris@101: typedef typename value_traits::value_type value_type; Chris@101: typedef circular_slist_algorithms group_algorithms; Chris@101: typedef typename pointer_traits Chris@101: :: Chris@101: template rebind_pointer Chris@101: ::type const_value_traits_ptr; Chris@101: typedef typename pointer_traits Chris@101: :: Chris@101: template rebind_pointer Chris@101: ::type const_bucket_value_traits_ptr; Chris@101: typedef typename detail::unordered_bucket_ptr_impl Chris@101: ::type bucket_ptr; Chris@101: typedef detail::bool_::value> optimize_multikey_t; Chris@16: Chris@16: template Chris@16: bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits) Chris@16: : ValueTraits(val_traits), bucket_traits_(::boost::forward(b_traits)) Chris@16: {} Chris@16: Chris@16: bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x) Chris@101: { bucket_traits_ = x.bucket_traits_; return *this; } Chris@101: Chris@101: const_value_traits_ptr priv_value_traits_ptr() const Chris@101: { return pointer_traits::pointer_to(this->priv_value_traits()); } Chris@16: Chris@16: //bucket_value_traits Chris@16: // Chris@16: const bucket_plus_vtraits &get_bucket_value_traits() const Chris@16: { return *this; } Chris@16: Chris@16: bucket_plus_vtraits &get_bucket_value_traits() Chris@16: { return *this; } Chris@16: Chris@16: const_bucket_value_traits_ptr bucket_value_traits_ptr() const Chris@16: { return pointer_traits::pointer_to(this->get_bucket_value_traits()); } Chris@16: Chris@16: //value traits Chris@16: // Chris@16: const value_traits &priv_value_traits() const Chris@16: { return *this; } Chris@16: Chris@16: value_traits &priv_value_traits() Chris@16: { return *this; } Chris@16: Chris@16: //bucket_traits Chris@16: // Chris@16: const bucket_traits &priv_bucket_traits() const Chris@16: { return this->bucket_traits_; } Chris@16: Chris@16: bucket_traits &priv_bucket_traits() Chris@16: { return this->bucket_traits_; } Chris@16: Chris@101: //bucket operations Chris@16: bucket_ptr priv_bucket_pointer() const Chris@101: { return this->priv_bucket_traits().bucket_begin(); } Chris@16: Chris@16: typename slist_impl::size_type priv_bucket_count() const Chris@101: { return this->priv_bucket_traits().bucket_count(); } Chris@16: Chris@16: bucket_ptr priv_invalid_bucket() const Chris@16: { Chris@101: const bucket_traits &rbt = this->priv_bucket_traits(); Chris@16: return rbt.bucket_begin() + rbt.bucket_count(); Chris@16: } Chris@16: siterator priv_invalid_local_it() const Chris@101: { return this->priv_bucket_traits().bucket_begin()->before_begin(); } Chris@101: Chris@16: static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey Chris@16: { Chris@16: //First find the last node of p's group. Chris@16: //This requires checking the first node of the next group or Chris@16: //the bucket node. Chris@16: slist_node_ptr end_ptr(b.end().pointed_node()); Chris@16: node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr(end_ptr))); Chris@16: node_ptr last_node_group(possible_end); Chris@16: Chris@16: while(end_ptr != possible_end){ Chris@16: last_node_group = group_traits::get_next(detail::dcast_bucket_ptr(possible_end)); Chris@16: possible_end = node_traits::get_next(last_node_group); Chris@16: } Chris@16: return bucket_type::s_iterator_to(*last_node_group); Chris@16: } Chris@16: Chris@16: static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey Chris@16: { return b.previous(b.end()); } Chris@16: Chris@16: static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey Chris@16: { Chris@16: node_ptr elem(detail::dcast_bucket_ptr(i.pointed_node())); Chris@16: node_ptr prev_in_group(group_traits::get_next(elem)); Chris@16: bool first_in_group = node_traits::get_next(prev_in_group) != elem; Chris@16: typename bucket_type::node &n = first_in_group Chris@16: ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem) Chris@16: : *group_traits::get_next(elem) Chris@16: ; Chris@16: return bucket_type::s_iterator_to(n); Chris@16: } Chris@16: Chris@16: static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey Chris@16: { return b.previous(i); } Chris@16: Chris@16: static void priv_clear_group_nodes(bucket_type &b, detail::true_) //optimize multikey Chris@16: { Chris@16: siterator it(b.begin()), itend(b.end()); Chris@16: while(it != itend){ Chris@16: node_ptr to_erase(detail::dcast_bucket_ptr(it.pointed_node())); Chris@16: ++it; Chris@16: group_algorithms::init(to_erase); Chris@16: } Chris@16: } Chris@16: Chris@16: static void priv_clear_group_nodes(bucket_type &, detail::false_) //NOT optimize multikey Chris@16: {} Chris@16: Chris@16: std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey Chris@16: { Chris@16: const bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1); Chris@16: slist_node_ptr bb = group_functions_t::get_bucket_before_begin Chris@16: ( f->end().pointed_node() Chris@16: , l->end().pointed_node() Chris@16: , detail::dcast_bucket_ptr(it.pointed_node())); Chris@16: //Now get the bucket_impl from the iterator Chris@16: const bucket_type &b = static_cast Chris@16: (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb))); Chris@16: //Now just calculate the index b has in the bucket array Chris@16: return static_cast(&b - &*f); Chris@16: } Chris@16: Chris@16: std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey Chris@16: { Chris@16: bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1); Chris@16: slist_node_ptr first_ptr(f->cend().pointed_node()) Chris@16: , last_ptr(l->cend().pointed_node()); Chris@16: Chris@16: //The end node is embedded in the singly linked list: Chris@16: //iterate until we reach it. Chris@16: while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){ Chris@16: ++it; Chris@16: } Chris@16: //Now get the bucket_impl from the iterator Chris@16: const bucket_type &b = static_cast Chris@16: (bucket_type::container_from_end_iterator(it)); Chris@16: Chris@16: //Now just calculate the index b has in the bucket array Chris@16: return static_cast(&b - &*f); Chris@16: } Chris@16: Chris@16: static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash Chris@16: { return node_traits::get_hash(detail::dcast_bucket_ptr(n)); } Chris@16: Chris@101: static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash (This should never be called) Chris@101: { BOOST_INTRUSIVE_INVARIANT_ASSERT(0); return 0; } Chris@101: Chris@101: node &priv_value_to_node(value_type &v) Chris@101: { return *this->priv_value_traits().to_node_ptr(v); } Chris@101: Chris@101: const node &priv_value_to_node(const value_type &v) const Chris@101: { return *this->priv_value_traits().to_node_ptr(v); } Chris@101: Chris@101: value_type &priv_value_from_slist_node(slist_node_ptr n) Chris@101: { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr(n)); } Chris@101: Chris@101: const value_type &priv_value_from_slist_node(slist_node_ptr n) const Chris@101: { return *this->priv_value_traits().to_value_ptr(detail::dcast_bucket_ptr(n)); } Chris@101: Chris@101: void priv_clear_buckets(const bucket_ptr buckets_ptr, const size_type bucket_cnt) Chris@16: { Chris@101: bucket_ptr buckets_it = buckets_ptr; Chris@101: for(size_type bucket_i = 0; bucket_i != bucket_cnt; ++buckets_it, ++bucket_i){ Chris@101: if(safemode_or_autounlink){ Chris@101: bucket_plus_vtraits::priv_clear_group_nodes(*buckets_it, optimize_multikey_t()); Chris@101: buckets_it->clear_and_dispose(detail::init_disposer()); Chris@101: } Chris@101: else{ Chris@101: buckets_it->clear(); Chris@101: } Chris@101: } Chris@16: } Chris@16: Chris@16: bucket_traits bucket_traits_; Chris@16: }; Chris@16: Chris@101: template Chris@101: struct get_hash Chris@101: { Chris@101: typedef Hash type; Chris@101: }; Chris@101: Chris@101: template Chris@101: struct get_hash Chris@101: { Chris@101: typedef ::boost::hash type; Chris@101: }; Chris@101: Chris@101: //bucket_hash_t Chris@101: //Stores bucket_plus_vtraits plust the hash function Chris@16: template Chris@16: struct bucket_hash_t Chris@101: //Use public inheritance to avoid MSVC bugs with closures Chris@16: : public detail::ebo_functor_holder Chris@16: ::value_traits::value_type Chris@16: >::type Chris@16: > Chris@16: { Chris@101: typedef typename bucket_plus_vtraits::value_traits value_traits; Chris@101: typedef typename value_traits::value_type value_type; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@101: typedef typename get_hash< VoidOrKeyHash, value_type>::type hasher; Chris@16: typedef BucketTraits bucket_traits; Chris@16: typedef bucket_plus_vtraits bucket_plus_vtraits_t; Chris@16: Chris@16: template Chris@16: bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h) Chris@101: : detail::ebo_functor_holder(h), internal(val_traits, ::boost::forward(b_traits)) Chris@16: {} Chris@16: Chris@16: const hasher &priv_hasher() const Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@16: Chris@16: hasher &priv_hasher() Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@16: Chris@16: std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true Chris@101: { return node_traits::get_hash(this->internal.priv_value_traits().to_node_ptr(v)); } Chris@16: Chris@16: std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false Chris@16: { return this->priv_hasher()(v); } Chris@101: Chris@101: bucket_plus_vtraits_t internal; //4 Chris@16: }; Chris@16: Chris@101: Chris@101: template Chris@101: struct get_equal_to Chris@101: { Chris@101: typedef EqualTo type; Chris@101: }; Chris@101: Chris@101: template Chris@101: struct get_equal_to Chris@101: { Chris@101: typedef ::std::equal_to type; Chris@101: }; Chris@101: Chris@101: Chris@101: //bucket_hash_equal_t Chris@101: //Stores bucket_hash_t and the equality function when the first Chris@101: //non-empty bucket shall not be cached. Chris@16: template Chris@16: struct bucket_hash_equal_t Chris@101: //Use public inheritance to avoid MSVC bugs with closures Chris@16: : public detail::ebo_functor_holder //equal Chris@16: ::value_traits::value_type Chris@16: >::type Chris@16: > Chris@16: { Chris@101: typedef bucket_hash_t bucket_hash_type; Chris@101: typedef bucket_plus_vtraits bucket_plus_vtraits_t; Chris@101: typedef typename bucket_plus_vtraits_t::value_traits value_traits; Chris@16: typedef typename get_equal_to< VoidOrKeyEqual Chris@101: , typename value_traits::value_type Chris@16: >::type value_equal; Chris@16: typedef typename bucket_hash_type::hasher hasher; Chris@16: typedef BucketTraits bucket_traits; Chris@101: typedef typename bucket_plus_vtraits_t::slist_impl slist_impl; Chris@101: typedef typename slist_impl::size_type size_type; Chris@101: typedef typename slist_impl::iterator siterator; Chris@101: typedef detail::bucket_impl bucket_type; Chris@101: typedef typename detail::unordered_bucket_ptr_impl::type bucket_ptr; Chris@16: Chris@16: template Chris@16: bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e) Chris@16: : detail::ebo_functor_holder(e) Chris@101: , internal(val_traits, ::boost::forward(b_traits), h) Chris@16: {} Chris@16: Chris@16: bucket_ptr priv_get_cache() Chris@101: { return this->internal.internal.priv_bucket_pointer(); } Chris@16: Chris@16: void priv_set_cache(const bucket_ptr &) Chris@16: {} Chris@16: Chris@16: size_type priv_get_cache_bucket_num() Chris@16: { return 0u; } Chris@16: Chris@16: void priv_initialize_cache() Chris@16: {} Chris@16: Chris@16: void priv_swap_cache(bucket_hash_equal_t &) Chris@16: {} Chris@16: Chris@16: siterator priv_begin() const Chris@16: { Chris@16: size_type n = 0; Chris@101: size_type bucket_cnt = this->internal.internal.priv_bucket_count(); Chris@16: for (n = 0; n < bucket_cnt; ++n){ Chris@101: bucket_type &b = this->internal.internal.priv_bucket_pointer()[n]; Chris@16: if(!b.empty()){ Chris@16: return b.begin(); Chris@16: } Chris@16: } Chris@101: return this->internal.internal.priv_invalid_local_it(); Chris@16: } Chris@16: Chris@16: void priv_insertion_update_cache(size_type) Chris@16: {} Chris@16: Chris@16: void priv_erasure_update_cache_range(size_type, size_type) Chris@16: {} Chris@16: Chris@16: void priv_erasure_update_cache() Chris@16: {} Chris@16: Chris@16: const value_equal &priv_equal() const Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@16: Chris@16: value_equal &priv_equal() Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@101: Chris@101: bucket_hash_t internal; //3 Chris@16: }; Chris@16: Chris@101: //bucket_hash_equal_t Chris@101: //Stores bucket_hash_t and the equality function when the first Chris@101: //non-empty bucket shall be cached. Chris@16: template //cache_begin == true version Chris@16: struct bucket_hash_equal_t Chris@101: //Use public inheritance to avoid MSVC bugs with closures Chris@16: : public detail::ebo_functor_holder //equal Chris@101: ::value_traits::value_type Chris@16: >::type Chris@101: > Chris@16: { Chris@101: typedef bucket_plus_vtraits bucket_plus_vtraits_t; Chris@101: typedef bucket_hash_t bucket_hash_type; Chris@101: typedef typename bucket_plus_vtraits Chris@101: ::value_traits value_traits; Chris@101: typedef typename get_equal_to Chris@101: < VoidOrKeyEqual Chris@101: , typename value_traits::value_type>::type value_equal; Chris@101: typedef typename bucket_hash_type::hasher hasher; Chris@101: typedef BucketTraits bucket_traits; Chris@101: typedef typename bucket_plus_vtraits_t::slist_impl::size_type size_type; Chris@101: typedef typename bucket_plus_vtraits_t::slist_impl::iterator siterator; Chris@16: Chris@16: template Chris@16: bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e) Chris@16: : detail::ebo_functor_holder(e) Chris@101: , internal(val_traits, ::boost::forward(b_traits), h) Chris@16: {} Chris@16: Chris@16: typedef typename detail::unordered_bucket_ptr_impl Chris@101: ::type bucket_ptr; Chris@16: Chris@16: bucket_ptr &priv_get_cache() Chris@16: { return cached_begin_; } Chris@16: Chris@16: const bucket_ptr &priv_get_cache() const Chris@16: { return cached_begin_; } Chris@16: Chris@16: void priv_set_cache(const bucket_ptr &p) Chris@16: { cached_begin_ = p; } Chris@16: Chris@16: std::size_t priv_get_cache_bucket_num() Chris@101: { return this->cached_begin_ - this->internal.internal.priv_bucket_pointer(); } Chris@16: Chris@16: void priv_initialize_cache() Chris@101: { this->cached_begin_ = this->internal.internal.priv_invalid_bucket(); } Chris@16: Chris@16: void priv_swap_cache(bucket_hash_equal_t &other) Chris@16: { Chris@101: ::boost::adl_move_swap(this->cached_begin_, other.cached_begin_); Chris@16: } Chris@16: Chris@16: siterator priv_begin() const Chris@16: { Chris@101: if(this->cached_begin_ == this->internal.internal.priv_invalid_bucket()){ Chris@101: return this->internal.internal.priv_invalid_local_it(); Chris@16: } Chris@16: else{ Chris@16: return this->cached_begin_->begin(); Chris@16: } Chris@16: } Chris@16: Chris@16: void priv_insertion_update_cache(size_type insertion_bucket) Chris@16: { Chris@101: bucket_ptr p = this->internal.internal.priv_bucket_pointer() + insertion_bucket; Chris@16: if(p < this->cached_begin_){ Chris@16: this->cached_begin_ = p; Chris@16: } Chris@16: } Chris@16: Chris@16: const value_equal &priv_equal() const Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@16: Chris@16: value_equal &priv_equal() Chris@16: { return this->detail::ebo_functor_holder::get(); } Chris@16: Chris@16: void priv_erasure_update_cache_range(size_type first_bucket_num, size_type last_bucket_num) Chris@16: { Chris@16: //If the last bucket is the end, the cache must be updated Chris@16: //to the last position if all Chris@16: if(this->priv_get_cache_bucket_num() == first_bucket_num && Chris@101: this->internal.internal.priv_bucket_pointer()[first_bucket_num].empty() ){ Chris@101: this->priv_set_cache(this->internal.internal.priv_bucket_pointer() + last_bucket_num); Chris@16: this->priv_erasure_update_cache(); Chris@16: } Chris@16: } Chris@16: Chris@16: void priv_erasure_update_cache() Chris@16: { Chris@101: if(this->cached_begin_ != this->internal.internal.priv_invalid_bucket()){ Chris@101: size_type current_n = this->priv_get_cache() - this->internal.internal.priv_bucket_pointer(); Chris@101: for( const size_type num_buckets = this->internal.internal.priv_bucket_count() Chris@16: ; current_n < num_buckets Chris@16: ; ++current_n, ++this->priv_get_cache()){ Chris@16: if(!this->priv_get_cache()->empty()){ Chris@16: return; Chris@16: } Chris@16: } Chris@16: this->priv_initialize_cache(); Chris@16: } Chris@16: } Chris@16: Chris@16: bucket_ptr cached_begin_; Chris@101: bucket_hash_t internal; //2 Chris@16: }; Chris@16: Chris@101: //hashdata_internal Chris@101: //Stores bucket_hash_equal_t and split_traits Chris@16: template Chris@16: struct hashdata_internal Chris@16: : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> //split_traits Chris@16: { Chris@16: typedef bucket_hash_equal_t Chris@101: < VoidOrKeyHash, VoidOrKeyEqual Chris@101: , ValueTraits, BucketTraits Chris@16: , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos) Chris@101: > internal_type; Chris@101: typedef typename internal_type::value_equal value_equal; Chris@101: typedef typename internal_type::hasher hasher; Chris@101: typedef bucket_plus_vtraits bucket_plus_vtraits_t; Chris@101: typedef typename bucket_plus_vtraits_t::size_type size_type; Chris@101: typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr; Chris@101: typedef detail::size_holder Chris@101: <0 != (BoolFlags & hash_bool_flags::incremental_pos) Chris@101: , SizeType, int> split_traits; Chris@101: typedef typename bucket_plus_vtraits_t:: Chris@101: value_traits::node_traits node_traits; Chris@101: typedef detail::bool_::value> optimize_multikey_t; Chris@16: Chris@16: template Chris@101: hashdata_internal( const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits Chris@101: , const hasher & h, const value_equal &e) Chris@101: : internal(val_traits, ::boost::forward(b_traits), h, e) Chris@16: {} Chris@16: Chris@16: split_traits &priv_split_traits() Chris@16: { return *this; } Chris@16: Chris@16: const split_traits &priv_split_traits() const Chris@16: { return *this; } Chris@101: Chris@101: ~hashdata_internal() Chris@101: { this->priv_clear_buckets(); } Chris@101: Chris@101: void priv_clear_buckets() Chris@101: { Chris@101: this->internal.internal.internal.priv_clear_buckets Chris@101: ( this->internal.priv_get_cache() Chris@101: , this->internal.internal.internal.priv_bucket_count() Chris@101: - (this->internal.priv_get_cache() Chris@101: - this->internal.internal.internal.priv_bucket_pointer())); Chris@101: } Chris@101: Chris@101: void priv_clear_buckets_and_cache() Chris@101: { Chris@101: this->priv_clear_buckets(); Chris@101: this->internal.priv_initialize_cache(); Chris@101: } Chris@101: Chris@101: void priv_initialize_buckets_and_cache() Chris@101: { Chris@101: this->internal.internal.internal.priv_clear_buckets Chris@101: ( this->internal.internal.internal.priv_bucket_pointer() Chris@101: , this->internal.internal.internal.priv_bucket_count()); Chris@101: this->internal.priv_initialize_cache(); Chris@101: } Chris@101: Chris@101: internal_type internal; //2 Chris@16: }; Chris@16: Chris@101: //hashtable_data_t Chris@101: //Stores hashdata_internal and size_traits Chris@16: template Chris@16: struct hashtable_data_t Chris@101: : public detail::size_holder Chris@101: < 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos), SizeType> //size_traits Chris@16: { Chris@16: typedef detail::size_holder Chris@16: < 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos) Chris@101: , SizeType> size_traits; Chris@16: typedef hashdata_internal Chris@101: < SizeType Chris@101: , BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos) Chris@101: , VoidOrKeyHash, VoidOrKeyEqual Chris@101: , ValueTraits, BucketTraits> internal_type; Chris@101: typedef ValueTraits value_traits; Chris@101: typedef typename internal_type::value_equal value_equal; Chris@101: typedef typename internal_type::hasher hasher; Chris@101: typedef BucketTraits bucket_traits; Chris@16: typedef bucket_plus_vtraits Chris@101: bucket_plus_vtraits_t; Chris@16: Chris@16: template Chris@16: hashtable_data_t( BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h Chris@16: , const value_equal &e, const value_traits &val_traits) Chris@101: : internal(val_traits, ::boost::forward(b_traits), h, e) Chris@16: {} Chris@101: Chris@101: internal_type internal; //1 Chris@16: }; Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: //! The class template hashtable is an intrusive hash table container, that Chris@16: //! is used to construct intrusive unordered_set and unordered_multiset containers. The Chris@16: //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw. Chris@16: //! Chris@16: //! hashtable is a semi-intrusive container: each object to be stored in the Chris@16: //! container must contain a proper hook, but the container also needs Chris@16: //! additional auxiliary memory to work: hashtable needs a pointer to an array Chris@16: //! of type `bucket_type` to be passed in the constructor. This bucket array must Chris@16: //! have at least the same lifetime as the container. This makes the use of Chris@16: //! hashtable more complicated than purely intrusive containers. Chris@16: //! `bucket_type` is default-constructible, copyable and assignable Chris@16: //! Chris@16: //! The template parameter \c T is the type to be managed by the container. Chris@16: //! The user can specify additional options and if no options are provided Chris@16: //! default options are used. Chris@16: //! Chris@16: //! The container supports the following options: Chris@16: //! \c base_hook<>/member_hook<>/value_traits<>, Chris@16: //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> Chris@16: //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>. Chris@16: //! Chris@16: //! hashtable only provides forward iterators but it provides 4 iterator types: Chris@16: //! iterator and const_iterator to navigate through the whole container and Chris@16: //! local_iterator and const_local_iterator to navigate through the values Chris@16: //! stored in a single bucket. Local iterators are faster and smaller. Chris@16: //! Chris@16: //! It's not recommended to use non constant-time size hashtables because several Chris@16: //! key functions, like "empty()", become non-constant time functions. Non Chris@16: //! constant_time size hashtables are mainly provided to support auto-unlink hooks. Chris@16: //! Chris@16: //! hashtables, does not make automatic rehashings nor Chris@16: //! offers functions related to a load factor. Rehashing can be explicitly requested Chris@16: //! and the user must provide a new bucket array that will be used from that moment. Chris@16: //! Chris@16: //! Since no automatic rehashing is done, iterators are never invalidated when Chris@16: //! inserting or erasing elements. Iterators are only invalidated when rehashing. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class hashtable_impl Chris@101: : private hashtable_data_t Chris@16: < SizeType Chris@16: , BoolFlags & hashtable_data_bool_flags_mask Chris@16: , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> Chris@16: { Chris@16: typedef hashtable_data_t Chris@16: < SizeType Chris@16: , BoolFlags & hashtable_data_bool_flags_mask Chris@101: , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> data_type; Chris@101: Chris@101: public: Chris@101: typedef ValueTraits value_traits; Chris@16: Chris@16: /// @cond Chris@16: typedef BucketTraits bucket_traits; Chris@16: Chris@16: typedef typename detail::get_slist_impl Chris@16: ::type Chris@101: >::type slist_impl; Chris@101: typedef bucket_plus_vtraits bucket_plus_vtraits_t; Chris@101: typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr; Chris@16: Chris@16: Chris@16: /// @endcond Chris@16: Chris@101: typedef typename value_traits::pointer pointer; Chris@101: typedef typename value_traits::const_pointer const_pointer; Chris@101: typedef typename value_traits::value_type value_type; Chris@16: typedef typename pointer_traits::reference reference; Chris@16: typedef typename pointer_traits::reference const_reference; Chris@16: typedef typename pointer_traits::difference_type difference_type; Chris@16: typedef SizeType size_type; Chris@16: typedef value_type key_type; Chris@16: typedef typename data_type::value_equal key_equal; Chris@101: typedef typename data_type::value_equal value_equal; Chris@16: typedef typename data_type::hasher hasher; Chris@16: typedef detail::bucket_impl bucket_type; Chris@16: typedef typename pointer_traits Chris@16: ::template rebind_pointer Chris@16: < bucket_type >::type bucket_ptr; Chris@101: typedef typename pointer_traits Chris@101: ::template rebind_pointer Chris@101: < const bucket_type >::type const_bucket_ptr; Chris@101: typedef typename pointer_traits Chris@101: ::reference bucket_reference; Chris@101: typedef typename pointer_traits Chris@101: ::reference const_bucket_reference; Chris@16: typedef typename slist_impl::iterator siterator; Chris@16: typedef typename slist_impl::const_iterator const_siterator; Chris@16: typedef hashtable_iterator iterator; Chris@16: typedef hashtable_iterator const_iterator; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@16: typedef typename node_traits::node node; Chris@16: typedef typename pointer_traits Chris@16: ::template rebind_pointer Chris@16: < node >::type node_ptr; Chris@16: typedef typename pointer_traits Chris@16: ::template rebind_pointer Chris@16: < const node >::type const_node_ptr; Chris@101: typedef typename pointer_traits Chris@101: ::reference node_reference; Chris@101: typedef typename pointer_traits Chris@101: ::reference const_node_reference; Chris@16: typedef typename slist_impl::node_algorithms node_algorithms; Chris@16: Chris@101: static const bool stateful_value_traits = detail::is_stateful_value_traits::value; Chris@16: static const bool store_hash = detail::store_hash_is_true::value; Chris@16: Chris@16: static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos); Chris@16: static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos); Chris@16: static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos); Chris@16: static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos); Chris@16: static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos); Chris@16: static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos)); Chris@16: Chris@16: static const bool optimize_multikey Chris@16: = detail::optimize_multikey_is_true::value && !unique_keys; Chris@16: Chris@16: /// @cond Chris@16: private: Chris@16: Chris@16: //Configuration error: compare_hash<> can't be specified without store_hash<> Chris@16: //See documentation for more explanations Chris@16: BOOST_STATIC_ASSERT((!compare_hash || store_hash)); Chris@16: Chris@16: typedef typename slist_impl::node_ptr slist_node_ptr; Chris@16: typedef typename pointer_traits Chris@16: ::template rebind_pointer Chris@16: < void >::type void_pointer; Chris@16: //We'll define group traits, but these won't be instantiated if Chris@16: //optimize_multikey is not true Chris@16: typedef unordered_group_adapter group_traits; Chris@16: typedef circular_slist_algorithms group_algorithms; Chris@16: typedef detail::bool_ store_hash_t; Chris@16: typedef detail::bool_ optimize_multikey_t; Chris@16: typedef detail::bool_ cache_begin_t; Chris@16: typedef detail::bool_ power_2_buckets_t; Chris@16: typedef detail::size_holder size_traits; Chris@16: typedef detail::size_holder split_traits; Chris@16: typedef detail::group_functions group_functions_t; Chris@16: typedef detail::node_functions node_functions_t; Chris@16: Chris@16: private: Chris@16: //noncopyable, movable Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl) Chris@16: Chris@101: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@16: Chris@16: //Constant-time size is incompatible with auto-unlink hooks! Chris@101: BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink))); Chris@16: //Cache begin is incompatible with auto-unlink hooks! Chris@101: BOOST_STATIC_ASSERT(!(cache_begin && ((int)value_traits::link_mode == (int)auto_unlink))); Chris@16: Chris@16: template Chris@101: node_cast_adaptor< detail::node_disposer Chris@16: , slist_node_ptr, node_ptr > Chris@16: make_node_disposer(const Disposer &disposer) const Chris@16: { Chris@16: return node_cast_adaptor Chris@101: < detail::node_disposer Chris@16: , slist_node_ptr, node_ptr > Chris@101: (disposer, &this->priv_value_traits()); Chris@16: } Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: public: Chris@16: typedef detail::insert_commit_data_impl insert_commit_data; Chris@16: Chris@16: typedef detail::transform_iterator Chris@16: < typename slist_impl::iterator Chris@16: , downcast_node_to_value_t Chris@101: < value_traits Chris@16: , false> > local_iterator; Chris@16: Chris@16: typedef detail::transform_iterator Chris@16: < typename slist_impl::iterator Chris@101: , downcast_node_to_value_t Chris@101: < value_traits Chris@16: , true> > const_local_iterator; Chris@16: Chris@16: public: Chris@16: Chris@16: //! Requires: buckets must not be being used by any other resource. Chris@16: //! Chris@16: //! Effects: Constructs an empty unordered_set, storing a reference Chris@16: //! to the bucket array and copies of the key_hasher and equal_func functors. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) Chris@16: //! or the copy constructor or invocation of hash_func or equal_func throws. Chris@16: //! Chris@16: //! Notes: buckets array must be disposed only after Chris@16: //! *this is disposed. Chris@16: explicit hashtable_impl ( const bucket_traits &b_traits Chris@16: , const hasher & hash_func = hasher() Chris@16: , const key_equal &equal_func = key_equal() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : data_type(b_traits, hash_func, equal_func, v_traits) Chris@16: { Chris@101: this->data_type::internal.priv_initialize_buckets_and_cache(); Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: size_type bucket_sz = this->priv_bucket_count(); Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0); Chris@16: //Check power of two bucket array if the option is activated Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT Chris@16: (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1)))); Chris@16: this->priv_split_traits().set_size(bucket_sz>>1); Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: hashtable_impl(BOOST_RV_REF(hashtable_impl) x) Chris@16: : data_type( ::boost::move(x.priv_bucket_traits()) Chris@16: , ::boost::move(x.priv_hasher()) Chris@16: , ::boost::move(x.priv_equal()) Chris@16: , ::boost::move(x.priv_value_traits()) Chris@16: ) Chris@16: { Chris@16: this->priv_swap_cache(x); Chris@16: x.priv_initialize_cache(); Chris@16: if(constant_time_size){ Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: this->priv_size_traits().set_size(x.priv_size_traits().get_size()); Chris@16: x.priv_size_traits().set_size(size_type(0)); Chris@16: } Chris@16: if(incremental){ Chris@16: this->priv_split_traits().set_size(x.priv_split_traits().get_size()); Chris@16: x.priv_split_traits().set_size(size_type(0)); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x) Chris@16: { this->swap(x); return *this; } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: //! Effects: Detaches all elements from this. The objects in the unordered_set Chris@16: //! are not deleted (i.e. no destructors are called). Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements in the unordered_set, if Chris@16: //! it's a safe-mode or auto-unlink value. Otherwise constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: ~hashtable_impl(); Chris@16: #endif Chris@16: Chris@16: //! Effects: Returns an iterator pointing to the beginning of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Worst case (empty unordered_set): O(this->bucket_count()) Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: iterator begin() Chris@16: { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); } Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the beginning Chris@16: //! of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Worst case (empty unordered_set): O(this->bucket_count()) Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator begin() const Chris@16: { return this->cbegin(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the beginning Chris@16: //! of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Worst case (empty unordered_set): O(this->bucket_count()) Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator cbegin() const Chris@16: { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); } Chris@16: Chris@16: //! Effects: Returns an iterator pointing to the end of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: iterator end() Chris@16: { return iterator(this->priv_invalid_local_it(), 0); } Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the end of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator end() const Chris@16: { return this->cend(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the end of the unordered_set. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator cend() const Chris@16: { return const_iterator(this->priv_invalid_local_it(), 0); } Chris@16: Chris@16: //! Effects: Returns the hasher object used by the unordered_set. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If hasher copy-constructor throws. Chris@16: hasher hash_function() const Chris@16: { return this->priv_hasher(); } Chris@16: Chris@16: //! Effects: Returns the key_equal object used by the unordered_set. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If key_equal copy-constructor throws. Chris@16: key_equal key_eq() const Chris@16: { return this->priv_equal(); } Chris@16: Chris@16: //! Effects: Returns true if the container is empty. Chris@16: //! Chris@16: //! Complexity: if constant-time size and cache_begin options are disabled, Chris@16: //! average constant time (worst case, with empty() == true: O(this->bucket_count()). Chris@16: //! Otherwise constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: bool empty() const Chris@16: { Chris@16: if(constant_time_size){ Chris@16: return !this->size(); Chris@16: } Chris@16: else if(cache_begin){ Chris@16: return this->begin() == this->end(); Chris@16: } Chris@16: else{ Chris@16: size_type bucket_cnt = this->priv_bucket_count(); Chris@16: const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer()); Chris@16: for (size_type n = 0; n < bucket_cnt; ++n, ++b){ Chris@16: if(!b->empty()){ Chris@16: return false; Chris@16: } Chris@16: } Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Returns the number of elements stored in the unordered_set. Chris@16: //! Chris@16: //! Complexity: Linear to elements contained in *this if Chris@16: //! constant_time_size is false. Constant-time otherwise. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: size_type size() const Chris@16: { Chris@16: if(constant_time_size) Chris@16: return this->priv_size_traits().get_size(); Chris@16: else{ Chris@16: size_type len = 0; Chris@16: size_type bucket_cnt = this->priv_bucket_count(); Chris@16: const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer()); Chris@16: for (size_type n = 0; n < bucket_cnt; ++n, ++b){ Chris@16: len += b->size(); Chris@16: } Chris@16: return len; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: the hasher and the equality function unqualified swap Chris@16: //! call should not throw. Chris@16: //! Chris@16: //! Effects: Swaps the contents of two unordered_sets. Chris@16: //! Swaps also the contained bucket array and equality and hasher functors. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If the swap() call for the comparison or hash functors Chris@16: //! found using ADL throw. Basic guarantee. Chris@16: void swap(hashtable_impl& other) Chris@16: { Chris@16: //These can throw Chris@101: ::boost::adl_move_swap(this->priv_equal(), other.priv_equal()); Chris@101: ::boost::adl_move_swap(this->priv_hasher(), other.priv_hasher()); Chris@16: //These can't throw Chris@101: ::boost::adl_move_swap(this->priv_bucket_traits(), other.priv_bucket_traits()); Chris@101: ::boost::adl_move_swap(this->priv_value_traits(), other.priv_value_traits()); Chris@16: this->priv_swap_cache(other); Chris@16: if(constant_time_size){ Chris@16: size_type backup = this->priv_size_traits().get_size(); Chris@16: this->priv_size_traits().set_size(other.priv_size_traits().get_size()); Chris@16: other.priv_size_traits().set_size(backup); Chris@16: } Chris@16: if(incremental){ Chris@16: size_type backup = this->priv_split_traits().get_size(); Chris@16: this->priv_split_traits().set_size(other.priv_split_traits().get_size()); Chris@16: other.priv_split_traits().set_size(backup); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw Chris@16: //! Cloner should yield to nodes that compare equal and produce the same Chris@16: //! hash than the original node. Chris@16: //! Chris@16: //! Effects: Erases all the elements from *this Chris@16: //! calling Disposer::operator()(pointer), clones all the Chris@16: //! elements from src calling Cloner::operator()(const_reference ) Chris@16: //! and inserts them on *this. The hash function and the equality Chris@16: //! predicate are copied from the source. Chris@16: //! Chris@16: //! If store_hash option is true, this method does not use the hash function. Chris@16: //! Chris@16: //! If any operation throws, all cloned elements are unlinked and disposed Chris@16: //! calling Disposer::operator()(pointer). Chris@16: //! Chris@16: //! Complexity: Linear to erased plus inserted elements. Chris@16: //! Chris@16: //! Throws: If cloner or hasher throw or hash or equality predicate copying Chris@16: //! throws. Basic guarantee. Chris@16: template Chris@16: void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer) Chris@16: { Chris@16: this->clear_and_dispose(disposer); Chris@16: if(!constant_time_size || !src.empty()){ Chris@16: const size_type src_bucket_count = src.bucket_count(); Chris@16: const size_type dst_bucket_count = this->bucket_count(); Chris@16: //Check power of two bucket array if the option is activated Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT Chris@16: (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1)))); Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT Chris@16: (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1)))); Chris@16: Chris@16: //If src bucket count is bigger or equal, structural copy is possible Chris@16: if(!incremental && (src_bucket_count >= dst_bucket_count)){ Chris@16: //First clone the first ones Chris@16: const bucket_ptr src_buckets = src.priv_bucket_pointer(); Chris@16: const bucket_ptr dst_buckets = this->priv_bucket_pointer(); Chris@16: size_type constructed; Chris@101: Chris@101: typedef node_cast_adaptor< detail::node_disposer Chris@16: , slist_node_ptr, node_ptr > NodeDisposer; Chris@101: typedef node_cast_adaptor< detail::node_cloner Chris@16: , slist_node_ptr, node_ptr > NodeCloner; Chris@101: NodeDisposer node_disp(disposer, &this->priv_value_traits()); Chris@16: Chris@16: detail::exception_array_disposer Chris@16: rollback(dst_buckets[0], node_disp, constructed); Chris@16: for( constructed = 0 Chris@16: ; constructed < dst_bucket_count Chris@16: ; ++constructed){ Chris@16: dst_buckets[constructed].clone_from Chris@16: ( src_buckets[constructed] Chris@101: , NodeCloner(cloner, &this->priv_value_traits()), node_disp); Chris@16: } Chris@16: if(src_bucket_count != dst_bucket_count){ Chris@16: //Now insert the remaining ones using the modulo trick Chris@16: for(//"constructed" comes from the previous loop Chris@16: ; constructed < src_bucket_count Chris@16: ; ++constructed){ Chris@16: bucket_type &dst_b = Chris@16: dst_buckets[detail::hash_to_bucket_split(constructed, dst_bucket_count, dst_bucket_count)]; Chris@16: bucket_type &src_b = src_buckets[constructed]; Chris@16: for( siterator b(src_b.begin()), e(src_b.end()) Chris@16: ; b != e Chris@16: ; ++b){ Chris@101: dst_b.push_front(*(NodeCloner(cloner, &this->priv_value_traits())(*b.pointed_node()))); Chris@16: } Chris@16: } Chris@16: } Chris@16: this->priv_hasher() = src.priv_hasher(); Chris@16: this->priv_equal() = src.priv_equal(); Chris@16: rollback.release(); Chris@16: this->priv_size_traits().set_size(src.priv_size_traits().get_size()); Chris@16: this->priv_split_traits().set_size(dst_bucket_count); Chris@16: this->priv_insertion_update_cache(0u); Chris@16: this->priv_erasure_update_cache(); Chris@16: } Chris@16: else if(store_hash){ Chris@16: //Unlike previous cloning algorithm, this can throw Chris@16: //if cloner, hasher or comparison functor throw Chris@101: const_iterator b(src.cbegin()), e(src.cend()); Chris@16: detail::exception_disposer Chris@16: rollback(*this, disposer); Chris@16: for(; b != e; ++b){ Chris@16: std::size_t hash_value = this->priv_stored_or_compute_hash(*b, store_hash_t());; Chris@16: this->priv_insert_equal_with_hash(*cloner(*b), hash_value); Chris@16: } Chris@16: rollback.release(); Chris@16: } Chris@16: else{ Chris@16: //Unlike previous cloning algorithm, this can throw Chris@16: //if cloner, hasher or comparison functor throw Chris@101: const_iterator b(src.cbegin()), e(src.cend()); Chris@16: detail::exception_disposer Chris@16: rollback(*this, disposer); Chris@16: for(; b != e; ++b){ Chris@16: this->insert_equal(*cloner(*b)); Chris@16: } Chris@16: rollback.release(); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue Chris@16: //! Chris@16: //! Effects: Inserts the value into the unordered_set. Chris@16: //! Chris@16: //! Returns: An iterator to the inserted value. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Strong guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: iterator insert_equal(reference value) Chris@16: { Chris@16: size_type bucket_num; Chris@16: std::size_t hash_value; Chris@16: siterator prev; Chris@16: siterator it = this->priv_find Chris@16: (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev); Chris@16: return this->priv_insert_equal_find(value, bucket_num, hash_value, it); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue Chris@16: //! of type value_type. Chris@16: //! Chris@16: //! Effects: Equivalent to this->insert_equal(t) for each element in [b, e). Chris@16: //! Chris@101: //! Complexity: Average case O(N), where N is distance(b, e). Chris@16: //! Worst case O(N*this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Basic guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: template Chris@16: void insert_equal(Iterator b, Iterator e) Chris@16: { Chris@16: for (; b != e; ++b) Chris@16: this->insert_equal(*b); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue Chris@16: //! Chris@16: //! Effects: Tries to inserts value into the unordered_set. Chris@16: //! Chris@16: //! Returns: If the value Chris@16: //! is not already present inserts it and returns a pair containing the Chris@16: //! iterator to the new value and true. If there is an equivalent value Chris@16: //! returns a pair containing an iterator to the already present value Chris@16: //! and false. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Strong guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: std::pair insert_unique(reference value) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: std::pair ret = this->insert_unique_check Chris@16: (value, this->priv_hasher(), this->priv_equal(), commit_data); Chris@16: if(!ret.second) Chris@16: return ret; Chris@16: return std::pair Chris@16: (this->insert_unique_commit(value, commit_data), true); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue Chris@16: //! of type value_type. Chris@16: //! Chris@16: //! Effects: Equivalent to this->insert_unique(t) for each element in [b, e). Chris@16: //! Chris@101: //! Complexity: Average case O(N), where N is distance(b, e). Chris@16: //! Worst case O(N*this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Basic guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: template Chris@16: void insert_unique(Iterator b, Iterator e) Chris@16: { Chris@16: for (; b != e; ++b) Chris@16: this->insert_unique(*b); Chris@16: } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Checks if a value can be inserted in the unordered_set, using Chris@16: //! a user provided key instead of the value itself. Chris@16: //! Chris@16: //! Returns: If there is an equivalent value Chris@16: //! returns a pair containing an iterator to the already present value Chris@16: //! and false. If the value can be inserted returns true in the returned Chris@16: //! pair boolean and fills "commit_data" that is meant to be used with Chris@16: //! the "insert_commit" function. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal_func throw. Strong guarantee. Chris@16: //! Chris@16: //! Notes: This function is used to improve performance when constructing Chris@16: //! a value_type is expensive: if there is an equivalent value Chris@16: //! the constructed object must be discarded. Many times, the part of the Chris@16: //! node that is used to impose the hash or the equality is much cheaper to Chris@16: //! construct than the value_type and this function offers the possibility to Chris@16: //! use that the part to check if the insertion will be successful. Chris@16: //! Chris@16: //! If the check is successful, the user can construct the value_type and use Chris@16: //! "insert_commit" to insert the object in constant-time. Chris@16: //! Chris@16: //! "commit_data" remains valid for a subsequent "insert_commit" only if no more Chris@16: //! objects are inserted or erased from the unordered_set. Chris@16: //! Chris@16: //! After a successful rehashing insert_commit_data remains valid. Chris@16: template Chris@16: std::pair insert_unique_check Chris@16: ( const KeyType &key Chris@16: , KeyHasher hash_func Chris@16: , KeyValueEqual equal_func Chris@16: , insert_commit_data &commit_data) Chris@16: { Chris@16: size_type bucket_num; Chris@16: siterator prev; Chris@16: siterator prev_pos = Chris@16: this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev); Chris@16: bool success = prev_pos == this->priv_invalid_local_it(); Chris@16: if(success){ Chris@16: prev_pos = prev; Chris@16: } Chris@16: return std::pair(iterator(prev_pos, &this->get_bucket_value_traits()),success); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue of type value_type. commit_data Chris@16: //! must have been obtained from a previous call to "insert_check". Chris@16: //! No objects should have been inserted or erased from the unordered_set between Chris@16: //! the "insert_check" that filled "commit_data" and the call to "insert_commit". Chris@16: //! Chris@16: //! Effects: Inserts the value in the unordered_set using the information obtained Chris@16: //! from the "commit_data" that a previous "insert_check" filled. Chris@16: //! Chris@16: //! Returns: An iterator to the newly inserted object. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Notes: This function has only sense if a "insert_check" has been Chris@16: //! previously executed to fill "commit_data". No value should be inserted or Chris@16: //! erased between the "insert_check" and "insert_commit" calls. Chris@16: //! Chris@16: //! After a successful rehashing insert_commit_data remains valid. Chris@16: iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) Chris@16: { Chris@16: size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash); Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_num]; Chris@16: this->priv_size_traits().increment(); Chris@16: node_ptr n = pointer_traits::pointer_to(this->priv_value_to_node(value)); Chris@16: node_functions_t::store_hash(n, commit_data.hash, store_hash_t()); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); Chris@16: this->priv_insertion_update_cache(bucket_num); Chris@16: group_functions_t::insert_in_group(node_ptr(), n, optimize_multikey_t()); Chris@16: return iterator(b.insert_after(b.before_begin(), *n), &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: //! Effects: Erases the element pointed to by i. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased element. No destructors are called. Chris@16: void erase(const_iterator i) Chris@16: { this->erase_and_dispose(i, detail::null_disposer()); } Chris@16: Chris@16: //! Effects: Erases the range pointed to by b end e. Chris@16: //! Chris@101: //! Complexity: Average case O(distance(b, e)), Chris@16: //! worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: void erase(const_iterator b, const_iterator e) Chris@16: { this->erase_and_dispose(b, e, detail::null_disposer()); } Chris@16: Chris@16: //! Effects: Erases all the elements with the given value. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: //! Basic guarantee. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: size_type erase(const_reference value) Chris@16: { return this->erase(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Erases all the elements that have the same hash and Chris@16: //! compare equal with the given key. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal_func throw. Basic guarantee. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: template Chris@16: size_type erase(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func) Chris@16: { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the element pointed to by i. Chris@16: //! Disposer::operator()(pointer) is called for the removed element. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: void erase_and_dispose(const_iterator i, Disposer disposer Chris@16: /// @cond Chris@16: , typename detail::enable_if_c::value >::type * = 0 Chris@16: /// @endcond Chris@16: ) Chris@16: { Chris@16: this->priv_erase(i, disposer, optimize_multikey_t()); Chris@16: this->priv_size_traits().decrement(); Chris@16: this->priv_erasure_update_cache(); Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the range pointed to by b end e. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@101: //! Complexity: Average case O(distance(b, e)), Chris@16: //! worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) Chris@16: { Chris@16: if(b != e){ Chris@16: //Get the bucket number and local iterator for both iterators Chris@16: siterator first_local_it(b.slist_it()); Chris@16: size_type first_bucket_num = this->priv_get_bucket_num(first_local_it); Chris@16: Chris@16: const bucket_ptr buck_ptr = this->priv_bucket_pointer(); Chris@16: siterator before_first_local_it Chris@16: = this->priv_get_previous(buck_ptr[first_bucket_num], first_local_it); Chris@16: size_type last_bucket_num; Chris@16: siterator last_local_it; Chris@16: Chris@16: //For the end iterator, we will assign the end iterator Chris@16: //of the last bucket Chris@16: if(e == this->end()){ Chris@16: last_bucket_num = this->bucket_count() - 1; Chris@16: last_local_it = buck_ptr[last_bucket_num].end(); Chris@16: } Chris@16: else{ Chris@16: last_local_it = e.slist_it(); Chris@16: last_bucket_num = this->priv_get_bucket_num(last_local_it); Chris@16: } Chris@16: this->priv_erase_range(before_first_local_it, first_bucket_num, last_local_it, last_bucket_num, disposer); Chris@16: this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all the elements with the given value. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: //! Basic guarantee. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: template Chris@16: size_type erase_and_dispose(const_reference value, Disposer disposer) Chris@16: { return this->erase_and_dispose(value, this->priv_hasher(), this->priv_equal(), disposer); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all the elements with the given key. Chris@16: //! according to the comparison functor "equal_func". Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal_func throw. Basic guarantee. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func Chris@16: ,KeyValueEqual equal_func, Disposer disposer) Chris@16: { Chris@16: size_type bucket_num; Chris@16: std::size_t h; Chris@16: siterator prev; Chris@16: siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev); Chris@16: bool success = it != this->priv_invalid_local_it(); Chris@16: size_type cnt(0); Chris@16: if(!success){ Chris@16: return 0; Chris@16: } Chris@16: else if(optimize_multikey){ Chris@16: siterator last = bucket_type::s_iterator_to Chris@16: (*node_traits::get_next(group_functions_t::get_last_in_group Chris@16: (detail::dcast_bucket_ptr(it.pointed_node()), optimize_multikey_t()))); Chris@16: this->priv_erase_range_impl(bucket_num, prev, last, disposer, cnt); Chris@16: } Chris@16: else{ Chris@16: //If found erase all equal values Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_num]; Chris@16: for(siterator end_sit = b.end(); it != end_sit; ++cnt, ++it){ Chris@16: slist_node_ptr n(it.pointed_node()); Chris@16: const value_type &v = this->priv_value_from_slist_node(n); Chris@16: if(compare_hash){ Chris@16: std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t()); Chris@16: if(h != vh || !equal_func(key, v)){ Chris@16: break; Chris@16: } Chris@16: } Chris@16: else if(!equal_func(key, v)){ Chris@16: break; Chris@16: } Chris@16: this->priv_size_traits().decrement(); Chris@16: } Chris@16: b.erase_after_and_dispose(prev, it, make_node_disposer(disposer)); Chris@16: } Chris@16: this->priv_erasure_update_cache(); Chris@16: return cnt; Chris@16: } Chris@16: Chris@16: //! Effects: Erases all of the elements. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements on the container. Chris@16: //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: void clear() Chris@16: { Chris@101: this->data_type::internal.priv_clear_buckets_and_cache(); Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all of the elements. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements on the container. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: template Chris@16: void clear_and_dispose(Disposer disposer) Chris@16: { Chris@16: if(!constant_time_size || !this->empty()){ Chris@16: size_type num_buckets = this->bucket_count(); Chris@16: bucket_ptr b = this->priv_bucket_pointer(); Chris@16: for(; num_buckets--; ++b){ Chris@16: b->clear_and_dispose(make_node_disposer(disposer)); Chris@16: } Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: } Chris@16: this->priv_initialize_cache(); Chris@16: } Chris@16: Chris@16: //! Effects: Returns the number of contained elements with the given value Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: size_type count(const_reference value) const Chris@16: { return this->count(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Returns the number of contained elements with the given key Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal throw. Chris@16: template Chris@16: size_type count(const KeyType &key, const KeyHasher &hash_func, const KeyValueEqual &equal_func) const Chris@16: { Chris@16: size_type bucket_n1, bucket_n2, cnt; Chris@16: this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt); Chris@16: return cnt; Chris@16: } Chris@16: Chris@16: //! Effects: Finds an iterator to the first element is equal to Chris@16: //! "value" or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: iterator find(const_reference value) Chris@16: { return this->find(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Finds an iterator to the first element whose key is Chris@16: //! "key" according to the given hash and equality functor or end() if Chris@16: //! that element does not exist. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal_func throw. Chris@16: //! Chris@16: //! Note: This function is used when constructing a value_type Chris@16: //! is expensive and the value_type can be compared with a cheaper Chris@16: //! key type. Usually this key is part of the value_type. Chris@16: template Chris@16: iterator find(const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) Chris@16: { Chris@16: size_type bucket_n; Chris@16: std::size_t hash; Chris@16: siterator prev; Chris@16: siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev); Chris@16: return iterator(local_it, &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: //! Effects: Finds a const_iterator to the first element whose key is Chris@16: //! "key" or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: const_iterator find(const_reference value) const Chris@16: { return this->find(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Finds an iterator to the first element whose key is Chris@16: //! "key" according to the given hasher and equality functor or end() if Chris@16: //! that element does not exist. Chris@16: //! Chris@16: //! Complexity: Average case O(1), worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or equal_func throw. Chris@16: //! Chris@16: //! Note: This function is used when constructing a value_type Chris@16: //! is expensive and the value_type can be compared with a cheaper Chris@16: //! key type. Usually this key is part of the value_type. Chris@16: template Chris@16: const_iterator find Chris@16: (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const Chris@16: { Chris@16: size_type bucket_n; Chris@16: std::size_t hash_value; Chris@16: siterator prev; Chris@16: siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev); Chris@16: return const_iterator(sit, &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: //! Effects: Returns a range containing all elements with values equivalent Chris@16: //! to value. Returns std::make_pair(this->end(), this->end()) if no such Chris@16: //! elements exist. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: std::pair equal_range(const_reference value) Chris@16: { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Returns a range containing all elements with equivalent Chris@16: //! keys. Returns std::make_pair(this->end(), this->end()) if no such Chris@16: //! elements exist. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(key, hash_func, equal_func)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If hash_func or the equal_func throw. Chris@16: //! Chris@16: //! Note: This function is used when constructing a value_type Chris@16: //! is expensive and the value_type can be compared with a cheaper Chris@16: //! key type. Usually this key is part of the value_type. Chris@16: template Chris@16: std::pair equal_range Chris@16: (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) Chris@16: { Chris@16: size_type bucket_n1, bucket_n2, cnt; Chris@16: std::pair ret = this->priv_equal_range Chris@16: (key, hash_func, equal_func, bucket_n1, bucket_n2, cnt); Chris@16: return std::pair Chris@16: (iterator(ret.first, &this->get_bucket_value_traits()), iterator(ret.second, &this->get_bucket_value_traits())); Chris@16: } Chris@16: Chris@16: //! Effects: Returns a range containing all elements with values equivalent Chris@16: //! to value. Returns std::make_pair(this->end(), this->end()) if no such Chris@16: //! elements exist. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(value)). Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the internal hasher or the equality functor throws. Chris@16: std::pair Chris@16: equal_range(const_reference value) const Chris@16: { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! "equal_func" must be a equality function that induces Chris@16: //! the same equality as key_equal. The difference is that Chris@16: //! "equal_func" compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Returns a range containing all elements with equivalent Chris@16: //! keys. Returns std::make_pair(this->end(), this->end()) if no such Chris@16: //! elements exist. Chris@16: //! Chris@16: //! Complexity: Average case O(this->count(key, hash_func, equal_func)). Chris@16: //! Worst case O(this->size()). Chris@16: //! Chris@16: //! Throws: If the hasher or equal_func throw. Chris@16: //! Chris@16: //! Note: This function is used when constructing a value_type Chris@16: //! is expensive and the value_type can be compared with a cheaper Chris@16: //! key type. Usually this key is part of the value_type. Chris@16: template Chris@16: std::pair equal_range Chris@16: (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const Chris@16: { Chris@16: size_type bucket_n1, bucket_n2, cnt; Chris@16: std::pair ret = Chris@16: this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt); Chris@16: return std::pair Chris@101: ( const_iterator(ret.first, &this->get_bucket_value_traits()) Chris@101: , const_iterator(ret.second, &this->get_bucket_value_traits())); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid iterator belonging to the unordered_set Chris@16: //! that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If the internal hash function throws. Chris@16: iterator iterator_to(reference value) Chris@16: { Chris@101: return iterator(bucket_type::s_iterator_to Chris@101: (this->priv_value_to_node(value)), &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid const_iterator belonging to the Chris@16: //! unordered_set that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If the internal hash function throws. Chris@16: const_iterator iterator_to(const_reference value) const Chris@16: { Chris@101: node_reference r = *pointer_traits::const_cast_from Chris@101: (pointer_traits::pointer_to(this->priv_value_to_node(value))); Chris@101: siterator sit = bucket_type::s_iterator_to(r); Chris@16: return const_iterator(sit, &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid local_iterator belonging to the unordered_set Chris@16: //! that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static local_iterator s_local_iterator_to(reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: siterator sit = bucket_type::s_iterator_to(*value_traits::to_node_ptr(value)); Chris@101: return local_iterator(sit, const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid const_local_iterator belonging to Chris@16: //! the unordered_set that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static const_local_iterator s_local_iterator_to(const_reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: node_reference r = *pointer_traits::const_cast_from Chris@101: (value_traits::to_node_ptr(value)); Chris@101: siterator sit = bucket_type::s_iterator_to(r); Chris@101: return const_local_iterator(sit, const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid local_iterator belonging to the unordered_set Chris@16: //! that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: local_iterator local_iterator_to(reference value) Chris@16: { Chris@16: siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value)); Chris@101: return local_iterator(sit, this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a unordered_set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid const_local_iterator belonging to Chris@16: //! the unordered_set that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_local_iterator local_iterator_to(const_reference value) const Chris@16: { Chris@101: node_reference r = *pointer_traits::const_cast_from Chris@101: (pointer_traits::pointer_to(this->priv_value_to_node(value))); Chris@101: siterator sit = bucket_type::s_iterator_to(r); Chris@101: return const_local_iterator(sit, this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Effects: Returns the number of buckets passed in the constructor Chris@16: //! or the last rehash function. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: size_type bucket_count() const Chris@16: { return this->priv_bucket_count(); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns the number of elements in the nth bucket. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: size_type bucket_size(size_type n) const Chris@16: { return this->priv_bucket_pointer()[n].size(); } Chris@16: Chris@16: //! Effects: Returns the index of the bucket in which elements Chris@16: //! with keys equivalent to k would be found, if any such element existed. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If the hash functor throws. Chris@16: //! Chris@16: //! Note: the return value is in the range [0, this->bucket_count()). Chris@16: size_type bucket(const key_type& k) const Chris@16: { return this->bucket(k, this->priv_hasher()); } Chris@16: Chris@16: //! Requires: "hash_func" must be a hash function that induces Chris@16: //! the same hash values as the stored hasher. The difference is that Chris@16: //! "hash_func" hashes the given key instead of the value_type. Chris@16: //! Chris@16: //! Effects: Returns the index of the bucket in which elements Chris@16: //! with keys equivalent to k would be found, if any such element existed. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If hash_func throws. Chris@16: //! Chris@16: //! Note: the return value is in the range [0, this->bucket_count()). Chris@16: template Chris@16: size_type bucket(const KeyType& k, const KeyHasher &hash_func) const Chris@16: { return this->priv_hash_to_bucket(hash_func(k)); } Chris@16: Chris@16: //! Effects: Returns the bucket array pointer passed in the constructor Chris@16: //! or the last rehash function. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: bucket_ptr bucket_pointer() const Chris@16: { return this->priv_bucket_pointer(); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a local_iterator pointing to the beginning Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: local_iterator begin(size_type n) Chris@101: { return local_iterator(this->priv_bucket_pointer()[n].begin(), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a const_local_iterator pointing to the beginning Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: const_local_iterator begin(size_type n) const Chris@16: { return this->cbegin(n); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a const_local_iterator pointing to the beginning Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: const_local_iterator cbegin(size_type n) const Chris@16: { Chris@101: bucket_reference br = pointer_traits::const_cast_from(this->priv_bucket_pointer())[n]; Chris@101: return const_local_iterator(br.begin(), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a local_iterator pointing to the end Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: local_iterator end(size_type n) Chris@101: { return local_iterator(this->priv_bucket_pointer()[n].end(), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a const_local_iterator pointing to the end Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: const_local_iterator end(size_type n) const Chris@16: { return this->cend(n); } Chris@16: Chris@16: //! Requires: n is in the range [0, this->bucket_count()). Chris@16: //! Chris@16: //! Effects: Returns a const_local_iterator pointing to the end Chris@16: //! of the sequence stored in the bucket n. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: [this->begin(n), this->end(n)) is a valid range Chris@16: //! containing all of the elements in the nth bucket. Chris@16: const_local_iterator cend(size_type n) const Chris@16: { Chris@101: bucket_reference br = pointer_traits::const_cast_from(this->priv_bucket_pointer())[n]; Chris@101: return const_local_iterator ( br.end(), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: new_bucket_traits can hold a pointer to a new bucket array Chris@16: //! or the same as the old bucket array with a different length. new_size is the length of the Chris@16: //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer() Chris@16: //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count(). Chris@16: //! 'new_bucket_traits' copy constructor should not throw. Chris@16: //! Chris@16: //! Effects: Updates the internal reference with the new bucket, erases Chris@16: //! the values from the old bucket and inserts then in the new one. Chris@16: //! Bucket traits hold by *this is assigned from new_bucket_traits. Chris@16: //! If the container is configured as incremental<>, the split bucket is set Chris@16: //! to the new bucket_count(). Chris@16: //! Chris@16: //! If store_hash option is true, this method does not use the hash function. Chris@16: //! Chris@16: //! Complexity: Average case linear in this->size(), worst case quadratic. Chris@16: //! Chris@16: //! Throws: If the hasher functor throws. Basic guarantee. Chris@16: void rehash(const bucket_traits &new_bucket_traits) Chris@16: { Chris@16: const bucket_ptr new_buckets = new_bucket_traits.bucket_begin(); Chris@16: size_type new_bucket_count = new_bucket_traits.bucket_count(); Chris@16: const bucket_ptr old_buckets = this->priv_bucket_pointer(); Chris@16: size_type old_bucket_count = this->priv_bucket_count(); Chris@16: Chris@16: //Check power of two bucket array if the option is activated Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT Chris@16: (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u)))); Chris@16: Chris@16: size_type n = this->priv_get_cache_bucket_num(); Chris@16: const bool same_buffer = old_buckets == new_buckets; Chris@16: //If the new bucket length is a common factor Chris@16: //of the old one we can avoid hash calculations. Chris@16: const bool fast_shrink = (!incremental) && (old_bucket_count > new_bucket_count) && Chris@16: (power_2_buckets ||(old_bucket_count % new_bucket_count) == 0); Chris@16: //If we are shrinking the same bucket array and it's Chris@16: //is a fast shrink, just rehash the last nodes Chris@16: size_type new_first_bucket_num = new_bucket_count; Chris@16: if(same_buffer && fast_shrink && (n < new_bucket_count)){ Chris@16: n = new_bucket_count; Chris@16: new_first_bucket_num = this->priv_get_cache_bucket_num(); Chris@16: } Chris@16: Chris@16: //Anti-exception stuff: they destroy the elements if something goes wrong. Chris@16: //If the source and destination buckets are the same, the second rollback function Chris@16: //is harmless, because all elements have been already unlinked and destroyed Chris@16: typedef detail::init_disposer NodeDisposer; Chris@16: NodeDisposer node_disp; Chris@16: bucket_type & newbuck = new_buckets[0]; Chris@16: bucket_type & oldbuck = old_buckets[0]; Chris@16: detail::exception_array_disposer Chris@16: rollback1(newbuck, node_disp, new_bucket_count); Chris@16: detail::exception_array_disposer Chris@16: rollback2(oldbuck, node_disp, old_bucket_count); Chris@16: Chris@16: //Put size in a safe value for rollback exception Chris@16: size_type size_backup = this->priv_size_traits().get_size(); Chris@16: this->priv_size_traits().set_size(0); Chris@16: //Put cache to safe position Chris@16: this->priv_initialize_cache(); Chris@16: this->priv_insertion_update_cache(size_type(0u)); Chris@16: Chris@16: //Iterate through nodes Chris@16: for(; n < old_bucket_count; ++n){ Chris@16: bucket_type &old_bucket = old_buckets[n]; Chris@16: Chris@16: if(!fast_shrink){ Chris@16: siterator before_i(old_bucket.before_begin()); Chris@16: siterator end_sit(old_bucket.end()); Chris@16: siterator i(old_bucket.begin()); Chris@16: for(;i != end_sit; ++i){ Chris@16: const value_type &v = this->priv_value_from_slist_node(i.pointed_node()); Chris@16: const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t()); Chris@16: const size_type new_n = detail::hash_to_bucket_split(hash_value, new_bucket_count, new_bucket_count); Chris@16: if(cache_begin && new_n < new_first_bucket_num) Chris@16: new_first_bucket_num = new_n; Chris@16: siterator last = bucket_type::s_iterator_to Chris@16: (*group_functions_t::get_last_in_group Chris@16: (detail::dcast_bucket_ptr(i.pointed_node()), optimize_multikey_t())); Chris@16: if(same_buffer && new_n == n){ Chris@16: before_i = last; Chris@16: } Chris@16: else{ Chris@16: bucket_type &new_b = new_buckets[new_n]; Chris@16: new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last); Chris@16: } Chris@16: i = before_i; Chris@16: } Chris@16: } Chris@16: else{ Chris@16: const size_type new_n = detail::hash_to_bucket_split(n, new_bucket_count, new_bucket_count); Chris@16: if(cache_begin && new_n < new_first_bucket_num) Chris@16: new_first_bucket_num = new_n; Chris@16: bucket_type &new_b = new_buckets[new_n]; Chris@16: if(!old_bucket.empty()){ Chris@16: new_b.splice_after( new_b.before_begin() Chris@16: , old_bucket Chris@16: , old_bucket.before_begin() Chris@16: , hashtable_impl::priv_get_last(old_bucket)); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: this->priv_size_traits().set_size(size_backup); Chris@16: this->priv_split_traits().set_size(new_bucket_count); Chris@101: this->priv_bucket_traits() = new_bucket_traits; Chris@16: this->priv_initialize_cache(); Chris@16: this->priv_insertion_update_cache(new_first_bucket_num); Chris@16: rollback1.release(); Chris@16: rollback2.release(); Chris@16: } Chris@16: Chris@16: //! Requires: Chris@16: //! Chris@16: //! Effects: Chris@16: //! Chris@16: //! Complexity: Chris@16: //! Chris@16: //! Throws: Chris@16: //! Chris@16: //! Note: this method is only available if incremental option is activated. Chris@16: bool incremental_rehash(bool grow = true) Chris@16: { Chris@16: //This function is only available for containers with incremental hashing Chris@16: BOOST_STATIC_ASSERT(( incremental && power_2_buckets )); Chris@16: const size_type split_idx = this->priv_split_traits().get_size(); Chris@16: const size_type bucket_cnt = this->priv_bucket_count(); Chris@16: const bucket_ptr buck_ptr = this->priv_bucket_pointer(); Chris@16: Chris@16: if(grow){ Chris@16: //Test if the split variable can be changed Chris@16: if(split_idx >= bucket_cnt) Chris@16: return false; Chris@16: Chris@16: const size_type bucket_to_rehash = split_idx - bucket_cnt/2; Chris@16: bucket_type &old_bucket = buck_ptr[bucket_to_rehash]; Chris@16: siterator before_i(old_bucket.before_begin()); Chris@16: const siterator end_sit(old_bucket.end()); Chris@16: siterator i(old_bucket.begin()); Chris@16: this->priv_split_traits().increment(); Chris@16: Chris@16: //Anti-exception stuff: if an exception is thrown while Chris@16: //moving elements from old_bucket to the target bucket, all moved Chris@16: //elements are moved back to the original one. Chris@16: detail::incremental_rehash_rollback rollback Chris@16: ( buck_ptr[split_idx], old_bucket, this->priv_split_traits()); Chris@16: for(;i != end_sit; ++i){ Chris@16: const value_type &v = this->priv_value_from_slist_node(i.pointed_node()); Chris@16: const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t()); Chris@16: const size_type new_n = this->priv_hash_to_bucket(hash_value); Chris@16: siterator last = bucket_type::s_iterator_to Chris@16: (*group_functions_t::get_last_in_group Chris@16: (detail::dcast_bucket_ptr(i.pointed_node()), optimize_multikey_t())); Chris@16: if(new_n == bucket_to_rehash){ Chris@16: before_i = last; Chris@16: } Chris@16: else{ Chris@16: bucket_type &new_b = buck_ptr[new_n]; Chris@16: new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last); Chris@16: } Chris@16: i = before_i; Chris@16: } Chris@16: rollback.release(); Chris@16: this->priv_erasure_update_cache(); Chris@16: return true; Chris@16: } Chris@16: else{ Chris@16: //Test if the split variable can be changed Chris@16: if(split_idx <= bucket_cnt/2) Chris@16: return false; Chris@16: const size_type target_bucket_num = split_idx - 1 - bucket_cnt/2; Chris@16: bucket_type &target_bucket = buck_ptr[target_bucket_num]; Chris@16: bucket_type &source_bucket = buck_ptr[split_idx-1]; Chris@16: target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket); Chris@16: this->priv_split_traits().decrement(); Chris@16: this->priv_insertion_update_cache(target_bucket_num); Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: If new_bucket_traits.bucket_count() is not Chris@16: //! this->bucket_count()/2 or this->bucket_count()*2, or Chris@16: //! this->split_bucket() != new_bucket_traits.bucket_count() returns false Chris@16: //! and does nothing. Chris@16: //! Chris@16: //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits Chris@16: //! and transfers all the objects from old buckets to the new ones. Chris@16: //! Chris@16: //! Complexity: Linear to size(). Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Note: this method is only available if incremental option is activated. Chris@16: bool incremental_rehash(const bucket_traits &new_bucket_traits) Chris@16: { Chris@16: //This function is only available for containers with incremental hashing Chris@16: BOOST_STATIC_ASSERT(( incremental && power_2_buckets )); Chris@16: size_type new_bucket_traits_size = new_bucket_traits.bucket_count(); Chris@16: size_type cur_bucket_traits = this->priv_bucket_count(); Chris@16: if(new_bucket_traits_size/2 != cur_bucket_traits && new_bucket_traits_size != cur_bucket_traits/2){ Chris@16: return false; Chris@16: } Chris@16: Chris@16: const size_type split_idx = this->split_count(); Chris@16: Chris@16: if(new_bucket_traits_size/2 == cur_bucket_traits){ Chris@16: //Test if the split variable can be changed Chris@16: if(!(split_idx >= cur_bucket_traits)) Chris@16: return false; Chris@16: } Chris@16: else{ Chris@16: //Test if the split variable can be changed Chris@16: if(!(split_idx <= cur_bucket_traits/2)) Chris@16: return false; Chris@16: } Chris@16: Chris@16: const size_type ini_n = this->priv_get_cache_bucket_num(); Chris@16: const bucket_ptr old_buckets = this->priv_bucket_pointer(); Chris@101: this->priv_bucket_traits() = new_bucket_traits; Chris@16: if(new_bucket_traits.bucket_begin() != old_buckets){ Chris@16: for(size_type n = ini_n; n < split_idx; ++n){ Chris@16: bucket_type &new_bucket = new_bucket_traits.bucket_begin()[n]; Chris@16: bucket_type &old_bucket = old_buckets[n]; Chris@16: new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket); Chris@16: } Chris@16: //Put cache to safe position Chris@16: this->priv_initialize_cache(); Chris@16: this->priv_insertion_update_cache(ini_n); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: //! Requires: Chris@16: //! Chris@16: //! Effects: Chris@16: //! Chris@16: //! Complexity: Chris@16: //! Chris@16: //! Throws: Chris@16: size_type split_count() const Chris@16: { Chris@16: //This function is only available if incremental hashing is activated Chris@16: BOOST_STATIC_ASSERT(( incremental && power_2_buckets )); Chris@16: return this->priv_split_traits().get_size(); Chris@16: } Chris@16: Chris@16: //! Effects: Returns the nearest new bucket count optimized for Chris@16: //! the container that is bigger or equal than n. This suggestion can be Chris@16: //! used to create bucket arrays with a size that will usually improve Chris@16: //! container's performance. If such value does not exist, the Chris@16: //! higher possible value is returned. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static size_type suggested_upper_bucket_count(size_type n) Chris@16: { Chris@101: const std::size_t *primes = &prime_list_holder<0>::prime_list[0]; Chris@101: const std::size_t *primes_end = primes + prime_list_holder<0>::prime_list_size; Chris@16: std::size_t const* bound = std::lower_bound(primes, primes_end, n); Chris@101: bound -= (bound == primes_end); Chris@16: return size_type(*bound); Chris@16: } Chris@16: Chris@16: //! Effects: Returns the nearest new bucket count optimized for Chris@16: //! the container that is smaller or equal than n. This suggestion can be Chris@16: //! used to create bucket arrays with a size that will usually improve Chris@16: //! container's performance. If such value does not exist, the Chris@16: //! lowest possible value is returned. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static size_type suggested_lower_bucket_count(size_type n) Chris@16: { Chris@101: const std::size_t *primes = &prime_list_holder<0>::prime_list[0]; Chris@101: const std::size_t *primes_end = primes + prime_list_holder<0>::prime_list_size; Chris@16: size_type const* bound = std::upper_bound(primes, primes_end, n); Chris@101: bound -= (bound != primes); Chris@16: return size_type(*bound); Chris@16: } Chris@16: /// @cond Chris@101: void check() const {} Chris@16: private: Chris@101: size_traits &priv_size_traits() Chris@101: { return static_cast(static_cast(*this)); } Chris@101: Chris@101: const size_traits &priv_size_traits() const Chris@101: { return static_cast(static_cast(*this)); } Chris@101: Chris@101: bucket_ptr priv_bucket_pointer() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_bucket_pointer(); } Chris@101: Chris@101: SizeType priv_bucket_count() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_bucket_count(); } Chris@101: Chris@101: const bucket_plus_vtraits &get_bucket_value_traits() const Chris@101: { return this->data_type::internal.internal.internal.internal.get_bucket_value_traits(); } Chris@101: Chris@101: bucket_plus_vtraits &get_bucket_value_traits() Chris@101: { return this->data_type::internal.internal.internal.internal.get_bucket_value_traits(); } Chris@101: Chris@101: bucket_traits &priv_bucket_traits() Chris@101: { return this->data_type::internal.internal.internal.internal.priv_bucket_traits(); } Chris@101: Chris@101: const bucket_traits &priv_bucket_traits() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_bucket_traits(); } Chris@101: Chris@101: value_traits &priv_value_traits() Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_traits(); } Chris@101: Chris@101: const value_traits &priv_value_traits() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_traits(); } Chris@101: Chris@101: const_value_traits_ptr priv_value_traits_ptr() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_traits_ptr(); } Chris@101: Chris@101: siterator priv_invalid_local_it() const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_invalid_local_it(); } Chris@101: Chris@101: split_traits &priv_split_traits() Chris@101: { return this->data_type::internal.priv_split_traits(); } Chris@101: Chris@101: const split_traits &priv_split_traits() const Chris@101: { return this->data_type::internal.priv_split_traits(); } Chris@101: Chris@101: bucket_ptr priv_get_cache() Chris@101: { return this->data_type::internal.internal.priv_get_cache(); } Chris@101: Chris@101: void priv_initialize_cache() Chris@101: { return this->data_type::internal.internal.priv_initialize_cache(); } Chris@101: Chris@101: siterator priv_begin() const Chris@101: { return this->data_type::internal.internal.priv_begin(); } Chris@101: Chris@101: const value_equal &priv_equal() const Chris@101: { return this->data_type::internal.internal.priv_equal(); } Chris@101: Chris@101: value_equal &priv_equal() Chris@101: { return this->data_type::internal.internal.priv_equal(); } Chris@101: Chris@101: const hasher &priv_hasher() const Chris@101: { return this->data_type::internal.internal.internal.priv_hasher(); } Chris@101: Chris@101: hasher &priv_hasher() Chris@101: { return this->data_type::internal.internal.internal.priv_hasher(); } Chris@101: Chris@101: void priv_swap_cache(hashtable_impl &h) Chris@101: { this->data_type::internal.internal.priv_swap_cache(h.data_type::internal.internal); } Chris@101: Chris@101: node &priv_value_to_node(value_type &v) Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_to_node(v); } Chris@101: Chris@101: const node &priv_value_to_node(const value_type &v) const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_to_node(v); } Chris@101: Chris@101: SizeType priv_get_cache_bucket_num() Chris@101: { return this->data_type::internal.internal.priv_get_cache_bucket_num(); } Chris@101: Chris@101: void priv_insertion_update_cache(SizeType n) Chris@101: { return this->data_type::internal.internal.priv_insertion_update_cache(n); } Chris@101: Chris@101: template Chris@101: std::size_t priv_stored_or_compute_hash(const value_type &v, detail::bool_ b) const Chris@101: { return this->data_type::internal.internal.internal.priv_stored_or_compute_hash(v, b); } Chris@101: Chris@101: value_type &priv_value_from_slist_node(slist_node_ptr n) Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_from_slist_node(n); } Chris@101: Chris@101: const value_type &priv_value_from_slist_node(slist_node_ptr n) const Chris@101: { return this->data_type::internal.internal.internal.internal.priv_value_from_slist_node(n); } Chris@101: Chris@101: void priv_erasure_update_cache_range(SizeType first_bucket_num, SizeType last_bucket_num) Chris@101: { return this->data_type::internal.internal.priv_erasure_update_cache_range(first_bucket_num, last_bucket_num); } Chris@101: Chris@101: void priv_erasure_update_cache() Chris@101: { return this->data_type::internal.internal.priv_erasure_update_cache(); } Chris@101: Chris@101: static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_ true_value) Chris@101: { return bucket_plus_vtraits::priv_stored_hash(n, true_value); } Chris@101: Chris@101: static std::size_t priv_stored_hash(slist_node_ptr n, detail::false_ false_value) Chris@101: { return bucket_plus_vtraits::priv_stored_hash(n, false_value); } Chris@16: Chris@16: std::size_t priv_hash_to_bucket(std::size_t hash_value) const Chris@16: { Chris@16: return detail::hash_to_bucket_split Chris@101: (hash_value, this->priv_bucket_traits().bucket_count(), this->priv_split_traits().get_size()); Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_erase_range_impl Chris@16: (size_type bucket_num, siterator before_first_it, siterator end_sit, Disposer disposer, size_type &num_erased) Chris@16: { Chris@16: const bucket_ptr buckets = this->priv_bucket_pointer(); Chris@16: bucket_type &b = buckets[bucket_num]; Chris@16: Chris@16: if(before_first_it == b.before_begin() && end_sit == b.end()){ Chris@16: this->priv_erase_range_impl(bucket_num, 1, disposer, num_erased); Chris@16: } Chris@16: else{ Chris@16: num_erased = 0; Chris@16: siterator to_erase(before_first_it); Chris@16: ++to_erase; Chris@16: slist_node_ptr end_ptr = end_sit.pointed_node(); Chris@16: while(to_erase != end_sit){ Chris@16: group_functions_t::erase_from_group(end_ptr, detail::dcast_bucket_ptr(to_erase.pointed_node()), optimize_multikey_t()); Chris@16: to_erase = b.erase_after_and_dispose(before_first_it, make_node_disposer(disposer)); Chris@16: ++num_erased; Chris@16: } Chris@16: this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_erase_range_impl Chris@16: (size_type first_bucket_num, size_type num_buckets, Disposer disposer, size_type &num_erased) Chris@16: { Chris@16: //Now fully clear the intermediate buckets Chris@16: const bucket_ptr buckets = this->priv_bucket_pointer(); Chris@16: num_erased = 0; Chris@16: for(size_type i = first_bucket_num; i < (num_buckets + first_bucket_num); ++i){ Chris@16: bucket_type &b = buckets[i]; Chris@16: siterator b_begin(b.before_begin()); Chris@16: siterator nxt(b_begin); Chris@16: ++nxt; Chris@16: siterator end_sit(b.end()); Chris@16: while(nxt != end_sit){ Chris@16: group_functions_t::init_group(detail::dcast_bucket_ptr(nxt.pointed_node()), optimize_multikey_t()); Chris@16: nxt = b.erase_after_and_dispose Chris@16: (b_begin, make_node_disposer(disposer)); Chris@16: this->priv_size_traits().decrement(); Chris@16: ++num_erased; Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_erase_range( siterator before_first_it, size_type first_bucket Chris@16: , siterator last_it, size_type last_bucket Chris@16: , Disposer disposer) Chris@16: { Chris@16: size_type num_erased; Chris@16: if (first_bucket == last_bucket){ Chris@16: this->priv_erase_range_impl(first_bucket, before_first_it, last_it, disposer, num_erased); Chris@16: } Chris@16: else { Chris@16: bucket_type *b = (&this->priv_bucket_pointer()[0]); Chris@16: this->priv_erase_range_impl(first_bucket, before_first_it, b[first_bucket].end(), disposer, num_erased); Chris@16: if(size_type n = (last_bucket - first_bucket - 1)) Chris@16: this->priv_erase_range_impl(first_bucket + 1, n, disposer, num_erased); Chris@16: this->priv_erase_range_impl(last_bucket, b[last_bucket].before_begin(), last_it, disposer, num_erased); Chris@16: } Chris@16: } Chris@16: Chris@16: std::size_t priv_get_bucket_num(siterator it) Chris@16: { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); } Chris@16: Chris@16: std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) //store_hash Chris@16: { Chris@16: return this->priv_hash_to_bucket Chris@16: (this->priv_stored_hash(it.pointed_node(), store_hash_t())); Chris@16: } Chris@16: Chris@16: std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) //NO store_hash Chris@101: { return this->data_type::internal.internal.internal.internal.priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); } Chris@16: Chris@16: static siterator priv_get_previous(bucket_type &b, siterator i) Chris@16: { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); } Chris@16: Chris@16: static siterator priv_get_last(bucket_type &b) Chris@16: { return bucket_plus_vtraits_t::priv_get_last(b, optimize_multikey_t()); } Chris@16: Chris@16: template Chris@16: void priv_erase(const_iterator i, Disposer disposer, detail::true_) Chris@16: { Chris@16: slist_node_ptr elem(i.slist_it().pointed_node()); Chris@16: slist_node_ptr f_bucket_end, l_bucket_end; Chris@16: if(store_hash){ Chris@16: f_bucket_end = l_bucket_end = Chris@16: (this->priv_bucket_pointer() Chris@16: [this->priv_hash_to_bucket Chris@16: (this->priv_stored_hash(elem, store_hash_t())) Chris@16: ]).before_begin().pointed_node(); Chris@16: } Chris@16: else{ Chris@16: f_bucket_end = this->priv_bucket_pointer()->cend().pointed_node(); Chris@16: l_bucket_end = f_bucket_end + this->priv_bucket_count() - 1; Chris@16: } Chris@16: node_ptr nxt_in_group; Chris@16: siterator prev = bucket_type::s_iterator_to Chris@16: (*group_functions_t::get_previous_and_next_in_group Chris@16: ( elem, nxt_in_group, f_bucket_end, l_bucket_end) Chris@16: ); Chris@16: bucket_type::s_erase_after_and_dispose(prev, make_node_disposer(disposer)); Chris@16: if(nxt_in_group) Chris@16: group_algorithms::unlink_after(nxt_in_group); Chris@16: if(safemode_or_autounlink) Chris@16: group_algorithms::init(detail::dcast_bucket_ptr(elem)); Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_erase(const_iterator i, Disposer disposer, detail::false_) Chris@16: { Chris@16: siterator to_erase(i.slist_it()); Chris@16: bucket_type &b = this->priv_bucket_pointer()[this->priv_get_bucket_num(to_erase)]; Chris@16: siterator prev(this->priv_get_previous(b, to_erase)); Chris@16: b.erase_after_and_dispose(prev, make_node_disposer(disposer)); Chris@16: } Chris@16: Chris@16: template Chris@16: siterator priv_find Chris@16: ( const KeyType &key, KeyHasher hash_func Chris@16: , KeyValueEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const Chris@16: { Chris@16: h = hash_func(key); Chris@16: return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt); Chris@16: } Chris@16: Chris@16: template Chris@16: siterator priv_find_with_hash Chris@16: ( const KeyType &key, KeyValueEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const Chris@16: { Chris@16: bucket_number = this->priv_hash_to_bucket(h); Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_number]; Chris@16: previt = b.before_begin(); Chris@16: if(constant_time_size && this->empty()){ Chris@16: return this->priv_invalid_local_it(); Chris@16: } Chris@16: Chris@16: siterator it = previt; Chris@16: ++it; Chris@16: Chris@16: while(it != b.end()){ Chris@16: const value_type &v = this->priv_value_from_slist_node(it.pointed_node()); Chris@16: if(compare_hash){ Chris@16: std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t()); Chris@16: if(h == vh && equal_func(key, v)){ Chris@16: return it; Chris@16: } Chris@16: } Chris@16: else if(equal_func(key, v)){ Chris@16: return it; Chris@16: } Chris@16: if(optimize_multikey){ Chris@16: previt = bucket_type::s_iterator_to Chris@16: (*group_functions_t::get_last_in_group Chris@16: (detail::dcast_bucket_ptr(it.pointed_node()), optimize_multikey_t())); Chris@16: it = previt; Chris@16: } Chris@16: else{ Chris@16: previt = it; Chris@16: } Chris@16: ++it; Chris@16: } Chris@16: previt = b.before_begin(); Chris@16: return this->priv_invalid_local_it(); Chris@16: } Chris@16: Chris@16: iterator priv_insert_equal_with_hash(reference value, std::size_t hash_value) Chris@16: { Chris@16: size_type bucket_num; Chris@16: siterator prev; Chris@16: siterator it = this->priv_find_with_hash Chris@16: (value, this->priv_equal(), bucket_num, hash_value, prev); Chris@16: return this->priv_insert_equal_find(value, bucket_num, hash_value, it); Chris@16: } Chris@16: Chris@16: iterator priv_insert_equal_find(reference value, size_type bucket_num, std::size_t hash_value, siterator it) Chris@16: { Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_num]; Chris@16: bool found_equal = it != this->priv_invalid_local_it(); Chris@16: if(!found_equal){ Chris@16: it = b.before_begin(); Chris@16: } Chris@16: //Now store hash if needed Chris@16: node_ptr n = pointer_traits::pointer_to(this->priv_value_to_node(value)); Chris@16: node_functions_t::store_hash(n, hash_value, store_hash_t()); Chris@16: //Checks for some modes Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); Chris@16: //Shortcut for optimize_multikey cases Chris@16: if(optimize_multikey){ Chris@16: node_ptr first_in_group = found_equal ? Chris@16: detail::dcast_bucket_ptr(it.pointed_node()) : node_ptr(); Chris@16: group_functions_t::insert_in_group(first_in_group, n, optimize_multikey_t()); Chris@16: } Chris@16: //Update cache and increment size if needed Chris@16: this->priv_insertion_update_cache(bucket_num); Chris@16: this->priv_size_traits().increment(); Chris@16: //Insert the element in the bucket after it Chris@16: return iterator(b.insert_after(it, *n), &this->get_bucket_value_traits()); Chris@16: } Chris@16: Chris@16: template Chris@16: std::pair priv_equal_range Chris@16: ( const KeyType &key Chris@16: , KeyHasher hash_func Chris@16: , KeyValueEqual equal_func Chris@16: , size_type &bucket_number_first Chris@16: , size_type &bucket_number_second Chris@16: , size_type &cnt) const Chris@16: { Chris@16: std::size_t h; Chris@16: cnt = 0; Chris@16: siterator prev; Chris@16: //Let's see if the element is present Chris@16: std::pair to_return Chris@16: ( this->priv_find(key, hash_func, equal_func, bucket_number_first, h, prev) Chris@16: , this->priv_invalid_local_it()); Chris@16: if(to_return.first == to_return.second){ Chris@16: bucket_number_second = bucket_number_first; Chris@16: return to_return; Chris@16: } Chris@16: { Chris@16: //If it's present, find the first that it's not equal in Chris@16: //the same bucket Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_number_first]; Chris@16: siterator it = to_return.first; Chris@16: if(optimize_multikey){ Chris@16: to_return.second = bucket_type::s_iterator_to Chris@16: (*node_traits::get_next(group_functions_t::get_last_in_group Chris@16: (detail::dcast_bucket_ptr(it.pointed_node()), optimize_multikey_t()))); Chris@101: Chris@101: cnt = 0; Chris@101: for(; it != to_return.second; ++it){ ++cnt; } Chris@16: if(to_return.second != b.end()){ Chris@16: bucket_number_second = bucket_number_first; Chris@16: return to_return; Chris@16: } Chris@16: } Chris@16: else{ Chris@16: ++cnt; Chris@16: ++it; Chris@16: while(it != b.end()){ Chris@16: const value_type &v = this->priv_value_from_slist_node(it.pointed_node()); Chris@16: if(compare_hash){ Chris@16: std::size_t hv = this->priv_stored_or_compute_hash(v, store_hash_t()); Chris@16: if(hv != h || !equal_func(key, v)){ Chris@16: to_return.second = it; Chris@16: bucket_number_second = bucket_number_first; Chris@16: return to_return; Chris@16: } Chris@16: } Chris@16: else if(!equal_func(key, v)){ Chris@16: to_return.second = it; Chris@16: bucket_number_second = bucket_number_first; Chris@16: return to_return; Chris@16: } Chris@16: ++it; Chris@16: ++cnt; Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //If we reached the end, find the first, non-empty bucket Chris@16: for(bucket_number_second = bucket_number_first+1 Chris@16: ; bucket_number_second != this->priv_bucket_count() Chris@16: ; ++bucket_number_second){ Chris@16: bucket_type &b = this->priv_bucket_pointer()[bucket_number_second]; Chris@16: if(!b.empty()){ Chris@16: to_return.second = b.begin(); Chris@16: return to_return; Chris@16: } Chris@16: } Chris@16: Chris@16: //Otherwise, return the end node Chris@16: to_return.second = this->priv_invalid_local_it(); Chris@16: return to_return; Chris@16: } Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: /// @cond Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: template < class T Chris@16: , bool UniqueKeys Chris@16: , class PackedOptions Chris@16: > Chris@16: #else Chris@16: template Chris@16: #endif Chris@101: struct make_bucket_traits Chris@16: { Chris@16: //Real value traits must be calculated from options Chris@16: typedef typename detail::get_value_traits Chris@16: ::type value_traits; Chris@101: Chris@16: typedef typename PackedOptions::bucket_traits specified_bucket_traits; Chris@16: Chris@16: //Real bucket traits must be calculated from options and calculated value_traits Chris@16: typedef typename detail::get_slist_impl Chris@16: ::type Chris@16: >::type slist_impl; Chris@16: Chris@16: typedef typename Chris@16: detail::if_c< detail::is_same Chris@16: < specified_bucket_traits Chris@16: , default_bucket_traits Chris@16: >::value Chris@16: , detail::bucket_traits_impl Chris@16: , specified_bucket_traits Chris@16: >::type type; Chris@16: }; Chris@16: /// @endcond Chris@16: Chris@16: //! Helper metafunction to define a \c hashtable that yields to the same type when the Chris@16: //! same options (either explicitly or implicitly) are used. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: struct make_hashtable Chris@16: { Chris@16: /// @cond Chris@16: typedef typename pack_options Chris@16: < hashtable_defaults, Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: O1, O2, O3, O4, O5, O6, O7, O8, O9, O10 Chris@16: #else Chris@16: Options... Chris@16: #endif Chris@16: >::type packed_options; Chris@16: Chris@16: typedef typename detail::get_value_traits Chris@16: ::type value_traits; Chris@16: Chris@101: typedef typename make_bucket_traits Chris@101: ::type bucket_traits; Chris@16: Chris@16: typedef hashtable_impl Chris@16: < value_traits Chris@16: , typename packed_options::hash Chris@16: , typename packed_options::equal Chris@16: , typename packed_options::size_type Chris@101: , bucket_traits Chris@16: , (std::size_t(false)*hash_bool_flags::unique_keys_pos) Chris@16: | (std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos) Chris@16: | (std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos) Chris@16: | (std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos) Chris@16: | (std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos) Chris@16: | (std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos) Chris@16: > implementation_defined; Chris@16: Chris@16: /// @endcond Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class hashtable Chris@16: : public make_hashtable::type Chris@16: { Chris@16: typedef typename make_hashtable::type Base; Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable) Chris@16: Chris@16: public: Chris@16: typedef typename Base::value_traits value_traits; Chris@16: typedef typename Base::iterator iterator; Chris@16: typedef typename Base::const_iterator const_iterator; Chris@16: typedef typename Base::bucket_ptr bucket_ptr; Chris@16: typedef typename Base::size_type size_type; Chris@16: typedef typename Base::hasher hasher; Chris@16: typedef typename Base::bucket_traits bucket_traits; Chris@16: typedef typename Base::key_equal key_equal; Chris@16: Chris@16: //Assert if passed value traits are compatible with the type Chris@101: BOOST_STATIC_ASSERT((detail::is_same::value)); Chris@16: Chris@16: explicit hashtable ( const bucket_traits &b_traits Chris@16: , const hasher & hash_func = hasher() Chris@16: , const key_equal &equal_func = key_equal() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : Base(b_traits, hash_func, equal_func, v_traits) Chris@16: {} Chris@16: Chris@16: hashtable(BOOST_RV_REF(hashtable) x) Chris@101: : Base(BOOST_MOVE_BASE(Base, x)) Chris@16: {} Chris@16: Chris@16: hashtable& operator=(BOOST_RV_REF(hashtable) x) Chris@101: { return static_cast(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); } Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_HASHTABLE_HPP