2 // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
3 // Copyright (C) 2005-2011 Daniel James
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED
8 #define BOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED
10 #include <boost/config.hpp>
11 #if defined(BOOST_HAS_PRAGMA_ONCE)
15 #include <boost/unordered/detail/buckets.hpp>
17 #if defined(BOOST_MSVC)
19 #pragma warning(disable:4127) // conditional expression is constant
22 namespace boost { namespace unordered { namespace detail {
24 ////////////////////////////////////////////////////////////////////////////
25 // convert double to std::size_t
27 inline std::size_t double_to_size(double f)
29 return f >= static_cast<double>(
30 (std::numeric_limits<std::size_t>::max)()) ?
31 (std::numeric_limits<std::size_t>::max)() :
32 static_cast<std::size_t>(f);
35 // The space used to store values in a node.
37 template <typename ValueType>
40 typedef ValueType value_type;
42 typename boost::aligned_storage<
44 boost::alignment_of<value_type>::value>::type data_;
55 return *(ValueType*) this;
58 value_type* value_ptr() {
59 return (ValueType*) this;
64 value_base& operator=(value_base const&);
67 template <typename Types>
69 boost::unordered::detail::functions<
70 typename Types::hasher,
71 typename Types::key_equal>
75 table& operator=(table const&);
77 typedef typename Types::node node;
78 typedef typename Types::bucket bucket;
79 typedef typename Types::hasher hasher;
80 typedef typename Types::key_equal key_equal;
81 typedef typename Types::key_type key_type;
82 typedef typename Types::extractor extractor;
83 typedef typename Types::value_type value_type;
84 typedef typename Types::table table_impl;
85 typedef typename Types::link_pointer link_pointer;
86 typedef typename Types::policy policy;
87 typedef typename Types::iterator iterator;
88 typedef typename Types::c_iterator c_iterator;
89 typedef typename Types::l_iterator l_iterator;
90 typedef typename Types::cl_iterator cl_iterator;
92 typedef boost::unordered::detail::functions<
93 typename Types::hasher,
94 typename Types::key_equal> functions;
95 typedef typename functions::set_hash_functions set_hash_functions;
97 typedef typename Types::allocator allocator;
98 typedef typename boost::unordered::detail::
99 rebind_wrap<allocator, node>::type node_allocator;
100 typedef typename boost::unordered::detail::
101 rebind_wrap<allocator, bucket>::type bucket_allocator;
102 typedef boost::unordered::detail::allocator_traits<node_allocator>
103 node_allocator_traits;
104 typedef boost::unordered::detail::allocator_traits<bucket_allocator>
105 bucket_allocator_traits;
106 typedef typename node_allocator_traits::pointer
108 typedef typename node_allocator_traits::const_pointer
110 typedef typename bucket_allocator_traits::pointer
112 typedef boost::unordered::detail::node_constructor<node_allocator>
114 typedef boost::unordered::detail::node_tmp<node_allocator>
117 ////////////////////////////////////////////////////////////////////////
120 boost::unordered::detail::compressed<bucket_allocator, node_allocator>
122 std::size_t bucket_count_;
125 std::size_t max_load_;
126 bucket_pointer buckets_;
128 ////////////////////////////////////////////////////////////////////////
131 static inline node_pointer next_node(link_pointer n) {
132 return static_cast<node_pointer>(n->next_);
135 ////////////////////////////////////////////////////////////////////////
138 bucket_allocator const& bucket_alloc() const
140 return allocators_.first();
143 node_allocator const& node_alloc() const
145 return allocators_.second();
148 bucket_allocator& bucket_alloc()
150 return allocators_.first();
153 node_allocator& node_alloc()
155 return allocators_.second();
158 std::size_t max_bucket_count() const
160 // -1 to account for the start bucket.
161 return policy::prev_bucket_count(
162 bucket_allocator_traits::max_size(bucket_alloc()) - 1);
165 bucket_pointer get_bucket(std::size_t bucket_index) const
167 BOOST_ASSERT(buckets_);
168 return buckets_ + static_cast<std::ptrdiff_t>(bucket_index);
171 link_pointer get_previous_start() const
173 return get_bucket(bucket_count_)->first_from_start();
176 link_pointer get_previous_start(std::size_t bucket_index) const
178 return get_bucket(bucket_index)->next_;
181 node_pointer begin() const
183 return size_ ? next_node(get_previous_start()) : node_pointer();
186 node_pointer begin(std::size_t bucket_index) const
188 if (!size_) return node_pointer();
189 link_pointer prev = get_previous_start(bucket_index);
190 return prev ? next_node(prev) : node_pointer();
193 std::size_t hash_to_bucket(std::size_t hash_value) const
195 return policy::to_bucket(bucket_count_, hash_value);
198 float load_factor() const
200 BOOST_ASSERT(bucket_count_ != 0);
201 return static_cast<float>(size_)
202 / static_cast<float>(bucket_count_);
205 std::size_t bucket_size(std::size_t index) const
207 node_pointer n = begin(index);
210 std::size_t count = 0;
211 while(n && hash_to_bucket(n->hash_) == index)
220 ////////////////////////////////////////////////////////////////////////
223 std::size_t max_size() const
227 // size < mlf_ * count
228 return boost::unordered::detail::double_to_size(ceil(
229 static_cast<double>(mlf_) *
230 static_cast<double>(max_bucket_count())
234 void recalculate_max_load()
239 // Only resize when size >= mlf_ * count
240 max_load_ = buckets_ ? boost::unordered::detail::double_to_size(ceil(
241 static_cast<double>(mlf_) *
242 static_cast<double>(bucket_count_)
247 void max_load_factor(float z)
250 mlf_ = (std::max)(z, minimum_max_load_factor);
251 recalculate_max_load();
254 std::size_t min_buckets_for_size(std::size_t size) const
256 BOOST_ASSERT(mlf_ >= minimum_max_load_factor);
261 // size < mlf_ * count
262 // => count > size / mlf_
264 // Or from rehash post-condition:
265 // count > size / mlf_
267 return policy::new_bucket_count(
268 boost::unordered::detail::double_to_size(floor(
269 static_cast<double>(size) /
270 static_cast<double>(mlf_)) + 1));
273 ////////////////////////////////////////////////////////////////////////
276 table(std::size_t num_buckets,
279 node_allocator const& a) :
282 bucket_count_(policy::new_bucket_count(num_buckets)),
289 table(table const& x, node_allocator const& a) :
292 bucket_count_(x.min_buckets_for_size(x.size_)),
299 table(table& x, boost::unordered::detail::move_tag m) :
301 allocators_(x.allocators_, m),
302 bucket_count_(x.bucket_count_),
305 max_load_(x.max_load_),
308 x.buckets_ = bucket_pointer();
313 table(table& x, node_allocator const& a,
314 boost::unordered::detail::move_tag m) :
317 bucket_count_(x.bucket_count_),
320 max_load_(x.max_load_),
324 ////////////////////////////////////////////////////////////////////////
327 void init(table const& x)
330 static_cast<table_impl*>(this)->copy_buckets(x);
334 void move_init(table& x)
336 if(node_alloc() == x.node_alloc()) {
337 move_buckets_from(x);
340 // TODO: Could pick new bucket size?
341 static_cast<table_impl*>(this)->move_buckets(x);
345 ////////////////////////////////////////////////////////////////////////
348 void create_buckets(std::size_t new_count)
350 std::size_t length = new_count + 1;
351 bucket_pointer new_buckets = bucket_allocator_traits::allocate(
352 bucket_alloc(), length);
353 bucket_pointer constructed = new_buckets;
356 bucket_pointer end = new_buckets
357 + static_cast<std::ptrdiff_t>(length);
358 for(; constructed != end; ++constructed) {
359 new ((void*) boost::addressof(*constructed)) bucket();
364 // Copy the nodes to the new buckets, including the dummy
365 // node if there is one.
367 static_cast<std::ptrdiff_t>(new_count))->next_ =
368 (buckets_ + static_cast<std::ptrdiff_t>(
369 bucket_count_))->next_;
372 else if (bucket::extra_node)
374 node_constructor a(node_alloc());
378 static_cast<std::ptrdiff_t>(new_count))->next_ =
383 for(bucket_pointer p = new_buckets; p != constructed; ++p) {
384 boost::unordered::detail::func::destroy(
385 boost::addressof(*p));
388 bucket_allocator_traits::deallocate(bucket_alloc(),
389 new_buckets, length);
395 bucket_count_ = new_count;
396 buckets_ = new_buckets;
397 recalculate_max_load();
400 ////////////////////////////////////////////////////////////////////////
403 void swap_allocators(table& other, false_type)
405 boost::unordered::detail::func::ignore_unused_variable_warning(other);
407 // According to 23.2.1.8, if propagate_on_container_swap is
408 // false the behaviour is undefined unless the allocators
410 BOOST_ASSERT(node_alloc() == other.node_alloc());
413 void swap_allocators(table& other, true_type)
415 allocators_.swap(other.allocators_);
418 // Only swaps the allocators if propagate_on_container_swap
421 set_hash_functions op1(*this, x);
422 set_hash_functions op2(x, *this);
424 // I think swap can throw if Propagate::value,
425 // since the allocators' swap can throw. Not sure though.
427 boost::unordered::detail::integral_constant<bool,
428 allocator_traits<node_allocator>::
429 propagate_on_container_swap::value>());
431 boost::swap(buckets_, x.buckets_);
432 boost::swap(bucket_count_, x.bucket_count_);
433 boost::swap(size_, x.size_);
434 std::swap(mlf_, x.mlf_);
435 std::swap(max_load_, x.max_load_);
440 // Only call with nodes allocated with the currect allocator, or
441 // one that is equal to it. (Can't assert because other's
442 // allocators might have already been moved).
443 void move_buckets_from(table& other)
445 BOOST_ASSERT(!buckets_);
446 buckets_ = other.buckets_;
447 bucket_count_ = other.bucket_count_;
449 other.buckets_ = bucket_pointer();
454 ////////////////////////////////////////////////////////////////////////
462 void delete_node(link_pointer prev)
464 node_pointer n = static_cast<node_pointer>(prev->next_);
465 prev->next_ = n->next_;
467 boost::unordered::detail::func::call_destroy(node_alloc(),
469 boost::unordered::detail::func::destroy(boost::addressof(*n));
470 node_allocator_traits::deallocate(node_alloc(), n, 1);
474 std::size_t delete_nodes(link_pointer prev, link_pointer end)
476 BOOST_ASSERT(prev->next_ != end);
478 std::size_t count = 0;
483 } while (prev->next_ != end);
488 void delete_buckets()
491 if (size_) delete_nodes(get_previous_start(), link_pointer());
493 if (bucket::extra_node) {
494 node_pointer n = static_cast<node_pointer>(
495 get_bucket(bucket_count_)->next_);
496 boost::unordered::detail::func::destroy(
497 boost::addressof(*n));
498 node_allocator_traits::deallocate(node_alloc(), n, 1);
502 buckets_ = bucket_pointer();
506 BOOST_ASSERT(!size_);
513 delete_nodes(get_previous_start(), link_pointer());
516 BOOST_ASSERT(!size_);
521 bucket_pointer end = get_bucket(bucket_count_);
522 for(bucket_pointer it = buckets_; it != end; ++it)
524 it->next_ = node_pointer();
528 void destroy_buckets()
530 bucket_pointer end = get_bucket(bucket_count_ + 1);
531 for(bucket_pointer it = buckets_; it != end; ++it)
533 boost::unordered::detail::func::destroy(
534 boost::addressof(*it));
537 bucket_allocator_traits::deallocate(bucket_alloc(),
538 buckets_, bucket_count_ + 1);
541 ////////////////////////////////////////////////////////////////////////
542 // Fix buckets after delete
545 std::size_t fix_bucket(std::size_t bucket_index, link_pointer prev)
547 link_pointer end = prev->next_;
548 std::size_t bucket_index2 = bucket_index;
552 bucket_index2 = hash_to_bucket(
553 static_cast<node_pointer>(end)->hash_);
555 // If begin and end are in the same bucket, then
556 // there's nothing to do.
557 if (bucket_index == bucket_index2) return bucket_index2;
559 // Update the bucket containing end.
560 get_bucket(bucket_index2)->next_ = prev;
563 // Check if this bucket is now empty.
564 bucket_pointer this_bucket = get_bucket(bucket_index);
565 if (this_bucket->next_ == prev)
566 this_bucket->next_ = link_pointer();
568 return bucket_index2;
571 ////////////////////////////////////////////////////////////////////////
574 void assign(table const& x)
576 if (this != boost::addressof(x))
579 boost::unordered::detail::integral_constant<bool,
580 allocator_traits<node_allocator>::
581 propagate_on_container_copy_assignment::value>());
585 void assign(table const& x, false_type)
587 // Strong exception safety.
588 set_hash_functions new_func_this(*this, x);
590 recalculate_max_load();
592 if (!size_ && !x.size_) {
593 new_func_this.commit();
597 if (x.size_ >= max_load_) {
598 create_buckets(min_buckets_for_size(x.size_));
604 new_func_this.commit();
605 static_cast<table_impl*>(this)->assign_buckets(x);
608 void assign(table const& x, true_type)
610 if (node_alloc() == x.node_alloc()) {
611 allocators_.assign(x.allocators_);
612 assign(x, false_type());
615 set_hash_functions new_func_this(*this, x);
617 // Delete everything with current allocators before assigning
620 allocators_.assign(x.allocators_);
622 // Copy over other data, all no throw.
623 new_func_this.commit();
625 bucket_count_ = min_buckets_for_size(x.size_);
628 // Finally copy the elements.
630 static_cast<table_impl*>(this)->copy_buckets(x);
635 void move_assign(table& x)
637 if (this != boost::addressof(x))
640 boost::unordered::detail::integral_constant<bool,
641 allocator_traits<node_allocator>::
642 propagate_on_container_move_assignment::value>());
646 void move_assign(table& x, true_type)
649 set_hash_functions new_func_this(*this, x);
650 allocators_.move_assign(x.allocators_);
651 // No throw from here.
653 max_load_ = x.max_load_;
654 move_buckets_from(x);
655 new_func_this.commit();
658 void move_assign(table& x, false_type)
660 if (node_alloc() == x.node_alloc()) {
662 set_hash_functions new_func_this(*this, x);
663 // No throw from here.
665 max_load_ = x.max_load_;
666 move_buckets_from(x);
667 new_func_this.commit();
670 set_hash_functions new_func_this(*this, x);
672 recalculate_max_load();
674 if (!size_ && !x.size_) {
675 new_func_this.commit();
679 if (x.size_ >= max_load_) {
680 create_buckets(min_buckets_for_size(x.size_));
686 new_func_this.commit();
687 static_cast<table_impl*>(this)->move_assign_buckets(x);
693 key_type const& get_key(value_type const& x) const
695 return extractor::extract(x);
698 std::size_t hash(key_type const& k) const
700 return policy::apply_hash(this->hash_function(), k);
705 template <typename Key, typename Hash, typename Pred>
706 node_pointer generic_find_node(
709 Pred const& eq) const
711 return static_cast<table_impl const*>(this)->
712 find_node_impl(policy::apply_hash(hf, k), k, eq);
715 node_pointer find_node(
716 std::size_t key_hash,
717 key_type const& k) const
719 return static_cast<table_impl const*>(this)->
720 find_node_impl(key_hash, k, this->key_eq());
723 node_pointer find_node(key_type const& k) const
725 return static_cast<table_impl const*>(this)->
726 find_node_impl(hash(k), k, this->key_eq());
729 // Reserve and rehash
731 void reserve_for_insert(std::size_t);
732 void rehash(std::size_t);
733 void reserve(std::size_t);
736 ////////////////////////////////////////////////////////////////////////////
739 // basic exception safety
740 template <typename Types>
741 inline void table<Types>::reserve_for_insert(std::size_t size)
744 create_buckets((std::max)(bucket_count_,
745 min_buckets_for_size(size)));
747 // According to the standard this should be 'size >= max_load_',
748 // but I think this is better, defect report filed.
749 else if(size > max_load_) {
750 std::size_t num_buckets
751 = min_buckets_for_size((std::max)(size,
752 size_ + (size_ >> 1)));
754 if (num_buckets != bucket_count_)
755 static_cast<table_impl*>(this)->rehash_impl(num_buckets);
759 // if hash function throws, basic exception safety
762 template <typename Types>
763 inline void table<Types>::rehash(std::size_t min_buckets)
769 bucket_count_ = policy::new_bucket_count(min_buckets);
772 min_buckets = policy::new_bucket_count((std::max)(min_buckets,
773 boost::unordered::detail::double_to_size(floor(
774 static_cast<double>(size_) /
775 static_cast<double>(mlf_))) + 1));
777 if(min_buckets != bucket_count_)
778 static_cast<table_impl*>(this)->rehash_impl(min_buckets);
782 template <typename Types>
783 inline void table<Types>::reserve(std::size_t num_elements)
785 rehash(static_cast<std::size_t>(
786 std::ceil(static_cast<double>(num_elements) / mlf_)));
790 #if defined(BOOST_MSVC)