diff --git a/include/boost/unordered/detail/buckets.hpp b/include/boost/unordered/detail/buckets.hpp index fed14388..8a2163c6 100644 --- a/include/boost/unordered/detail/buckets.hpp +++ b/include/boost/unordered/detail/buckets.hpp @@ -19,9 +19,9 @@ namespace boost { namespace unordered_detail { template inline BOOST_DEDUCED_TYPENAME hash_buckets::bucket_ptr - hash_buckets::get_bucket(std::size_t n) const + hash_buckets::get_bucket(std::size_t num) const { - return buckets_ + static_cast(n); + return buckets_ + static_cast(num); } template @@ -46,9 +46,9 @@ namespace boost { namespace unordered_detail { template inline BOOST_DEDUCED_TYPENAME hash_buckets::node_ptr - hash_buckets::bucket_begin(std::size_t n) const + hash_buckets::bucket_begin(std::size_t num) const { - return buckets_ ? get_bucket(n)->next_ : node_ptr(); + return buckets_ ? get_bucket(num)->next_ : node_ptr(); } //////////////////////////////////////////////////////////////////////////// @@ -103,9 +103,9 @@ namespace boost { namespace unordered_detail { { std::size_t count = 0; while(begin != end) { - node_ptr node = begin; + node_ptr n = begin; begin = begin->next_; - delete_node(node); + delete_node(n); ++count; } return count; diff --git a/include/boost/unordered/detail/equivalent.hpp b/include/boost/unordered/detail/equivalent.hpp index efb3a688..fa05019d 100644 --- a/include/boost/unordered/detail/equivalent.hpp +++ b/include/boost/unordered/detail/equivalent.hpp @@ -177,27 +177,27 @@ namespace boost { namespace unordered_detail { #else -#define BOOST_UNORDERED_INSERT_IMPL(z, n, _) \ +#define BOOST_UNORDERED_INSERT_IMPL(z, num_params, _) \ template \ - template \ + template \ BOOST_DEDUCED_TYPENAME hash_equivalent_table::iterator_base \ hash_equivalent_table \ - ::emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ + ::emplace(BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ { \ node_constructor a(*this); \ - a.construct(BOOST_UNORDERED_CALL_PARAMS(z, n)); \ + a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ return emplace_impl(a); \ } \ \ template \ - template \ + template \ BOOST_DEDUCED_TYPENAME hash_equivalent_table::iterator_base \ hash_equivalent_table \ ::emplace_hint(iterator_base const& it, \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ { \ node_constructor a(*this); \ - a.construct(BOOST_UNORDERED_CALL_PARAMS(z, n)); \ + a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ return emplace_hint_impl(it, a); \ } diff --git a/include/boost/unordered/detail/fwd.hpp b/include/boost/unordered/detail/fwd.hpp index 15924676..c8a4abc7 100644 --- a/include/boost/unordered/detail/fwd.hpp +++ b/include/boost/unordered/detail/fwd.hpp @@ -50,12 +50,12 @@ #include #include -#define BOOST_UNORDERED_TEMPLATE_ARGS(z, n) \ - BOOST_PP_ENUM_PARAMS_Z(z, n, class Arg) -#define BOOST_UNORDERED_FUNCTION_PARAMS(z, n) \ - BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, Arg, const& arg) -#define BOOST_UNORDERED_CALL_PARAMS(z, n) \ - BOOST_PP_ENUM_PARAMS_Z(z, n, arg) +#define BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params) \ + BOOST_PP_ENUM_PARAMS_Z(z, num_params, class Arg) +#define BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params) \ + BOOST_PP_ENUM_BINARY_PARAMS_Z(z, num_params, Arg, const& arg) +#define BOOST_UNORDERED_CALL_PARAMS(z, num_params) \ + BOOST_PP_ENUM_PARAMS_Z(z, num_params, arg) #endif @@ -126,7 +126,7 @@ namespace boost { namespace unordered_detail { static inline std::size_t group_count(node_ptr ptr); static inline void add_to_bucket(node_ptr n, bucket& b); static inline void add_after_node(node_ptr n, node_ptr position); - static void unlink_node(bucket& b, node_ptr node); + static void unlink_node(bucket& b, node_ptr n); static void unlink_nodes(bucket& b, node_ptr begin, node_ptr end); static void unlink_nodes(bucket& b, node_ptr end); }; @@ -146,7 +146,7 @@ namespace boost { namespace unordered_detail { static inline std::size_t group_count(node_ptr ptr); static inline void add_to_bucket(node_ptr n, bucket& b); static inline void add_after_node(node_ptr n, node_ptr position); - static void unlink_node(bucket& b, node_ptr node); + static void unlink_node(bucket& b, node_ptr n); static void unlink_nodes(bucket& b, node_ptr begin, node_ptr end); static void unlink_nodes(bucket& b, node_ptr end); @@ -237,12 +237,12 @@ namespace boost { namespace unordered_detail { return node::get_value(node_); } - void increment_bucket(node_ptr node) { - while(!node) { + void increment_bucket(node_ptr n) { + while(!n) { ++bucket_; - node = bucket_->next_; + n = bucket_->next_; } - node_ = bucket_ == node ? node_ptr() : node; + node_ = bucket_ == n ? node_ptr() : n; } void increment() { diff --git a/include/boost/unordered/detail/node.hpp b/include/boost/unordered/detail/node.hpp index 21645105..85a31410 100644 --- a/include/boost/unordered/detail/node.hpp +++ b/include/boost/unordered/detail/node.hpp @@ -71,9 +71,9 @@ namespace boost { namespace unordered_detail { } template - inline void ungrouped_node_base::unlink_node(bucket& b, node_ptr node) + inline void ungrouped_node_base::unlink_node(bucket& b, node_ptr n) { - unlink_nodes(b, node, node->next_); + unlink_nodes(b, n, n->next_); } //////////////////////////////////////////////////////////////////////////// @@ -144,41 +144,41 @@ namespace boost { namespace unordered_detail { } template - void grouped_node_base::unlink_node(bucket& b, node_ptr node) + void grouped_node_base::unlink_node(bucket& b, node_ptr n) { - node_ptr next = node->next_; - node_ptr* pos = &next_group(node); + node_ptr next = n->next_; + node_ptr* pos = &next_group(n); - if(*pos != node) { + if(*pos != n) { // The node is at the beginning of a group. // Find the previous node pointer: pos = &b.next_; - while(*pos != node) pos = &next_group(*pos); + while(*pos != n) pos = &next_group(*pos); // Remove from group if(BOOST_UNORDERED_BORLAND_BOOL(next) && - get(next).group_prev_ == node) + get(next).group_prev_ == n) { - get(next).group_prev_ = get(node).group_prev_; + get(next).group_prev_ = get(n).group_prev_; } } else if(BOOST_UNORDERED_BORLAND_BOOL(next) && - get(next).group_prev_ == node) + get(next).group_prev_ == n) { // The deleted node is not at the end of the group, so // change the link from the next node. - get(next).group_prev_ = get(node).group_prev_; + get(next).group_prev_ = get(n).group_prev_; } else { // The deleted node is at the end of the group, so the // first node in the group is pointing to it. // Find that to change its pointer. - node_ptr x = get(node).group_prev_; - while(get(x).group_prev_ != node) { + node_ptr x = get(n).group_prev_; + while(get(x).group_prev_ != n) { x = get(x).group_prev_; } - get(x).group_prev_ = get(node).group_prev_; + get(x).group_prev_ = get(n).group_prev_; } *pos = next; } diff --git a/include/boost/unordered/detail/table.hpp b/include/boost/unordered/detail/table.hpp index eb18926f..edbc3773 100644 --- a/include/boost/unordered/detail/table.hpp +++ b/include/boost/unordered/detail/table.hpp @@ -116,7 +116,7 @@ namespace boost { namespace unordered_detail { // no throw template inline std::size_t hash_table::min_buckets_for_size( - std::size_t n) const + std::size_t size) const { BOOST_ASSERT(this->mlf_ != 0); @@ -128,7 +128,7 @@ namespace boost { namespace unordered_detail { // // Or from rehash post-condition: // count > size / mlf_ - return next_prime(double_to_size_t(floor(n / (double) mlf_)) + 1); + return next_prime(double_to_size_t(floor(size / (double) mlf_)) + 1); } //////////////////////////////////////////////////////////////////////////// @@ -199,9 +199,9 @@ namespace boost { namespace unordered_detail { // Constructors template - hash_table::hash_table(std::size_t n, + hash_table::hash_table(std::size_t num_buckets, hasher const& hf, key_equal const& eq, node_allocator const& a) - : buckets(a, next_prime(n)), + : buckets(a, next_prime(num_buckets)), base(hf, eq), size_(), mlf_(1.0f), @@ -404,24 +404,26 @@ namespace boost { namespace unordered_detail { // basic exception safety template - inline void hash_table::create_for_insert(std::size_t n) + inline void hash_table::create_for_insert(std::size_t size) { - if(n > this->bucket_count_) - this->bucket_count_ = this->min_buckets_for_size(n); + // TODO: Write a test to detect this bug: + if(size > this->bucket_count_) + this->bucket_count_ = this->min_buckets_for_size(size); this->create_buckets(); this->init_buckets(); } // basic exception safety template - inline bool hash_table::reserve_for_insert(std::size_t n) + inline bool hash_table::reserve_for_insert(std::size_t size) { - if(n >= max_load_) { + if(size >= max_load_) { std::size_t s = this->size_; s = s + (s >> 1); - n = this->min_buckets_for_size(s > n ? s : n); - if(n != this->bucket_count_) { - rehash_impl(n); + std::size_t num_buckets + = this->min_buckets_for_size(s > size ? s : size); + if(num_buckets != this->bucket_count_) { + rehash_impl(num_buckets); return true; } } @@ -433,24 +435,24 @@ namespace boost { namespace unordered_detail { // strong otherwise. // TODO: Should this always create buckets? template - inline void hash_table - ::rehash(std::size_t n) + inline void hash_table::rehash(std::size_t min_buckets) { using namespace std; if(!this->buckets_) { - this->bucket_count_ = next_prime(n); + this->bucket_count_ = next_prime(min_buckets); this->create_buckets(); this->init_buckets(); } - else if(n != this->bucket_count_) { + // TODO: Another bug: + else if(min_buckets != this->bucket_count_) { // no throw: // TODO: Needlessly calling next_prime twice. std::size_t min_size = this->min_buckets_for_size(this->size_); - n = next_prime(n); - n = min_size > n ? min_size : n; + min_buckets = next_prime(min_buckets); + min_buckets = min_size > min_buckets ? min_size : min_buckets; - rehash_impl(n); + rehash_impl(min_buckets); } } @@ -461,13 +463,13 @@ namespace boost { namespace unordered_detail { template void hash_table - ::rehash_impl(std::size_t n) + ::rehash_impl(std::size_t num_buckets) { hasher const& hf = this->hash_function(); std::size_t size = this->size_; bucket_ptr end = this->get_bucket(this->bucket_count_); - buckets dst(this->node_alloc(), n); + buckets dst(this->node_alloc(), num_buckets); dst.create_buckets(); buckets src(this->node_alloc(), this->bucket_count_); @@ -726,18 +728,18 @@ namespace boost { namespace unordered_detail { template BOOST_DEDUCED_TYPENAME hash_table::iterator_base hash_table::emplace_empty_impl_with_node( - node_constructor& a, std::size_t n) + node_constructor& a, std::size_t size) { key_type const& k = get_key(a.value()); std::size_t hash_value = this->hash_function()(k); - if(this->buckets_) this->reserve_for_insert(n); - else this->create_for_insert(n); + if(this->buckets_) this->reserve_for_insert(size); + else this->create_for_insert(size); bucket_ptr bucket = this->bucket_ptr_from_hash(hash_value); - node_ptr node = a.release(); - node::add_to_bucket(node, *bucket); + node_ptr n = a.release(); + node::add_to_bucket(n, *bucket); ++this->size_; this->cached_begin_bucket_ = bucket; - return iterator_base(bucket, node); + return iterator_base(bucket, n); } }} diff --git a/include/boost/unordered/detail/unique.hpp b/include/boost/unordered/detail/unique.hpp index 0f5cf88f..14565bca 100644 --- a/include/boost/unordered/detail/unique.hpp +++ b/include/boost/unordered/detail/unique.hpp @@ -191,56 +191,58 @@ namespace boost { namespace unordered_detail { #else -#define BOOST_UNORDERED_INSERT_IMPL(z, n, _) \ - template \ - template \ - inline BOOST_DEDUCED_TYPENAME \ - hash_unique_table::emplace_return \ - hash_unique_table::emplace_impl( \ - key_type const& k, \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ - { \ - std::size_t hash_value = this->hash_function()(k); \ - bucket_ptr bucket \ - = this->bucket_ptr_from_hash(hash_value); \ - node_ptr pos = find_iterator(bucket, k); \ - \ - if (BOOST_UNORDERED_BORLAND_BOOL(pos)) { \ - return emplace_return(iterator_base(bucket, pos), false); \ - } else { \ - node_constructor a(*this); \ - a.construct(BOOST_UNORDERED_CALL_PARAMS(z, n)); \ - \ - if(this->reserve_for_insert(this->size_ + 1)) \ - bucket = this->bucket_ptr_from_hash(hash_value); \ - \ - return emplace_return(iterator_base(bucket, \ - add_node(a, bucket)), true); \ - } \ - } \ - \ - template \ - template \ - inline BOOST_DEDUCED_TYPENAME \ - hash_unique_table::emplace_return \ - hash_unique_table:: \ - emplace_impl(no_key, BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ - { \ - node_constructor a(*this); \ - a.construct(BOOST_UNORDERED_CALL_PARAMS(z, n)); \ - return emplace_impl_with_node(a); \ - } \ - \ - template \ - template \ - inline BOOST_DEDUCED_TYPENAME \ - hash_unique_table::emplace_return \ - hash_unique_table:: \ - emplace_empty_impl(BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ - { \ - node_constructor a(*this); \ - a.construct(BOOST_UNORDERED_CALL_PARAMS(z, n)); \ - return emplace_return(this->emplace_empty_impl_with_node(a, 1), true); \ +#define BOOST_UNORDERED_INSERT_IMPL(z, num_params, _) \ + template \ + template \ + inline BOOST_DEDUCED_TYPENAME \ + hash_unique_table::emplace_return \ + hash_unique_table::emplace_impl( \ + key_type const& k, \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ + { \ + std::size_t hash_value = this->hash_function()(k); \ + bucket_ptr bucket \ + = this->bucket_ptr_from_hash(hash_value); \ + node_ptr pos = find_iterator(bucket, k); \ + \ + if (BOOST_UNORDERED_BORLAND_BOOL(pos)) { \ + return emplace_return(iterator_base(bucket, pos), false); \ + } else { \ + node_constructor a(*this); \ + a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ + \ + if(this->reserve_for_insert(this->size_ + 1)) \ + bucket = this->bucket_ptr_from_hash(hash_value); \ + \ + return emplace_return(iterator_base(bucket, \ + add_node(a, bucket)), true); \ + } \ + } \ + \ + template \ + template \ + inline BOOST_DEDUCED_TYPENAME \ + hash_unique_table::emplace_return \ + hash_unique_table:: \ + emplace_impl(no_key, \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ + { \ + node_constructor a(*this); \ + a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ + return emplace_impl_with_node(a); \ + } \ + \ + template \ + template \ + inline BOOST_DEDUCED_TYPENAME \ + hash_unique_table::emplace_return \ + hash_unique_table:: \ + emplace_empty_impl( \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ + { \ + node_constructor a(*this); \ + a.construct(BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ + return emplace_return(this->emplace_empty_impl_with_node(a, 1), true); \ } BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT, @@ -312,33 +314,33 @@ namespace boost { namespace unordered_detail { emplace_empty_impl(arg0).first; } -#define BOOST_UNORDERED_INSERT_IMPL(z, n, _) \ - template \ - template \ - BOOST_DEDUCED_TYPENAME hash_unique_table::emplace_return \ - hash_unique_table::emplace( \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ - { \ - return this->size_ ? \ - emplace_impl(extractor::extract(arg0, arg1), \ - BOOST_UNORDERED_CALL_PARAMS(z, n)) : \ - emplace_empty_impl( \ - BOOST_UNORDERED_CALL_PARAMS(z, n)); \ - } \ - \ - template \ - template \ - BOOST_DEDUCED_TYPENAME hash_unique_table::iterator_base \ - hash_unique_table:: \ - emplace_hint(iterator_base const& it, \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ - { \ - return this->size_ ? \ - emplace_impl(extractor::extract(arg0, arg1), \ - BOOST_UNORDERED_CALL_PARAMS(z, n)) : \ - emplace_empty_impl( \ - BOOST_UNORDERED_CALL_PARAMS(z, n)); \ - } \ +#define BOOST_UNORDERED_INSERT_IMPL(z, num_params, _) \ + template \ + template \ + BOOST_DEDUCED_TYPENAME hash_unique_table::emplace_return \ + hash_unique_table::emplace( \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ + { \ + return this->size_ ? \ + emplace_impl(extractor::extract(arg0, arg1), \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params)) : \ + emplace_empty_impl( \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ + } \ + \ + template \ + template \ + BOOST_DEDUCED_TYPENAME hash_unique_table::iterator_base \ + hash_unique_table:: \ + emplace_hint(iterator_base const& it, \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ + { \ + return this->size_ ? \ + emplace_impl(extractor::extract(arg0, arg1), \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params)) : \ + emplace_empty_impl( \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ + } \ BOOST_PP_REPEAT_FROM_TO(2, BOOST_UNORDERED_EMPLACE_LIMIT, BOOST_UNORDERED_INSERT_IMPL, _) diff --git a/include/boost/unordered/detail/util.hpp b/include/boost/unordered/detail/util.hpp index 9a70185e..1f592dd3 100644 --- a/include/boost/unordered/detail/util.hpp +++ b/include/boost/unordered/detail/util.hpp @@ -61,24 +61,24 @@ namespace boost { namespace unordered_detail { typedef prime_list_template prime_list; // no throw - inline std::size_t next_prime(std::size_t n) { + inline std::size_t next_prime(std::size_t num) { std::size_t const* const prime_list_begin = prime_list::value; std::size_t const* const prime_list_end = prime_list_begin + prime_list::length; std::size_t const* bound = - std::lower_bound(prime_list_begin, prime_list_end, n); + std::lower_bound(prime_list_begin, prime_list_end, num); if(bound == prime_list_end) bound--; return *bound; } // no throw - inline std::size_t prev_prime(std::size_t n) { + inline std::size_t prev_prime(std::size_t num) { std::size_t const* const prime_list_begin = prime_list::value; std::size_t const* const prime_list_end = prime_list_begin + prime_list::length; std::size_t const* bound = - std::upper_bound(prime_list_begin,prime_list_end, n); + std::upper_bound(prime_list_begin,prime_list_end, num); if(bound != prime_list_begin) bound--; return *bound; @@ -129,9 +129,10 @@ namespace boost { namespace unordered_detail { template inline std::size_t initial_size(I i, I j, - std::size_t n = boost::unordered_detail::default_bucket_count) + std::size_t num_buckets = boost::unordered_detail::default_bucket_count) { - return (std::max)(static_cast(insert_size(i, j)) + 1, n); + return (std::max)(static_cast(insert_size(i, j)) + 1, + num_buckets); } //////////////////////////////////////////////////////////////////////////// @@ -158,29 +159,29 @@ namespace boost { namespace unordered_detail { #else -#define BOOST_UNORDERED_CONSTRUCT_IMPL(z, n, _) \ +#define BOOST_UNORDERED_CONSTRUCT_IMPL(z, num_params, _) \ template < \ class T, \ - BOOST_UNORDERED_TEMPLATE_ARGS(z, n) \ + BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params) \ > \ inline void construct_impl( \ T*, void* address, \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n) \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params) \ ) \ { \ new(address) T( \ - BOOST_UNORDERED_CALL_PARAMS(z, n)); \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params)); \ } \ \ template \ inline void construct_impl( \ std::pair*, void* address, \ - Key const& k, BOOST_UNORDERED_FUNCTION_PARAMS(z, n)) \ + Key const& k, BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params)) \ { \ new(address) std::pair(k, \ - Second(BOOST_UNORDERED_CALL_PARAMS(z, n))); \ + Second(BOOST_UNORDERED_CALL_PARAMS(z, num_params))); \ } BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT, @@ -230,18 +231,18 @@ namespace boost { namespace unordered_detail { } #else -#define BOOST_UNORDERED_CONSTRUCT(z, n, _) \ +#define BOOST_UNORDERED_CONSTRUCT(z, num_params, _) \ template < \ - BOOST_UNORDERED_TEMPLATE_ARGS(z, n) \ + BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params) \ > \ void construct( \ - BOOST_UNORDERED_FUNCTION_PARAMS(z, n) \ + BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params) \ ) \ { \ construct_preamble(); \ construct_impl( \ (value_type*) 0, node_->address(), \ - BOOST_UNORDERED_CALL_PARAMS(z, n) \ + BOOST_UNORDERED_CALL_PARAMS(z, num_params) \ ); \ value_constructed_ = true; \ }