1 // boost heap: binomial heap
3 // Copyright (C) 2010 Tim Blechmann
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_HEAP_BINOMIAL_HEAP_HPP
10 #define BOOST_HEAP_BINOMIAL_HEAP_HPP
16 #include <boost/assert.hpp>
18 #include <boost/heap/detail/heap_comparison.hpp>
19 #include <boost/heap/detail/heap_node.hpp>
20 #include <boost/heap/detail/stable_heap.hpp>
21 #include <boost/heap/detail/tree_iterator.hpp>
22 #include <boost/type_traits/integral_constant.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #ifndef BOOST_DOXYGEN_INVOKED
29 #ifdef BOOST_HEAP_SANITYCHECKS
30 #define BOOST_HEAP_ASSERT BOOST_ASSERT
32 #define BOOST_HEAP_ASSERT(expression)
40 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
41 boost::parameter::optional<tag::compare>,
42 boost::parameter::optional<tag::stable>,
43 boost::parameter::optional<tag::constant_time_size>,
44 boost::parameter::optional<tag::stability_counter_type>
45 > binomial_heap_signature;
47 template <typename T, typename Parspec>
48 struct make_binomial_heap_base
50 static const bool constant_time_size = parameter::binding<Parspec,
51 tag::constant_time_size,
54 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::type base_type;
55 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::allocator_argument allocator_argument;
56 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::compare_argument compare_argument;
58 typedef parent_pointing_heap_node<typename base_type::internal_type> node_type;
60 typedef typename boost::allocator_rebind<allocator_argument, node_type>::type allocator_type;
66 type(compare_argument const & arg):
70 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
71 type(type const & rhs):
72 base_type(rhs), allocator_type(rhs)
76 base_type(std::move(static_cast<base_type&>(rhs))),
77 allocator_type(std::move(static_cast<allocator_type&>(rhs)))
80 type & operator=(type && rhs)
82 base_type::operator=(std::move(static_cast<base_type&>(rhs)));
83 allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
87 type & operator=(type const & rhs)
89 base_type::operator=(static_cast<base_type const &>(rhs));
90 allocator_type::operator=(static_cast<allocator_type const &>(rhs));
100 * \class binomial_heap
101 * \brief binomial heap
103 * The template parameter T is the type to be managed by the container.
104 * The user can specify additional options and if no options are provided default options are used.
106 * The container supports the following options:
107 * - \c boost::heap::stable<>, defaults to \c stable<false>
108 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
109 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
110 * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
111 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
114 #ifdef BOOST_DOXYGEN_INVOKED
115 template<class T, class ...Options>
117 template <typename T,
118 class A0 = boost::parameter::void_,
119 class A1 = boost::parameter::void_,
120 class A2 = boost::parameter::void_,
121 class A3 = boost::parameter::void_
125 private detail::make_binomial_heap_base<T,
126 typename detail::binomial_heap_signature::bind<A0, A1, A2, A3>::type
129 typedef typename detail::binomial_heap_signature::bind<A0, A1, A2, A3>::type bound_args;
130 typedef detail::make_binomial_heap_base<T, bound_args> base_maker;
131 typedef typename base_maker::type super_t;
133 typedef typename super_t::internal_type internal_type;
134 typedef typename super_t::size_holder_type size_holder;
135 typedef typename super_t::stability_counter_type stability_counter_type;
136 typedef typename base_maker::allocator_argument allocator_argument;
138 template <typename Heap1, typename Heap2>
139 friend struct heap_merge_emulate;
142 static const bool constant_time_size = super_t::constant_time_size;
143 static const bool has_ordered_iterators = true;
144 static const bool is_mergable = true;
145 static const bool is_stable = detail::extract_stable<bound_args>::value;
146 static const bool has_reserve = false;
149 #ifndef BOOST_DOXYGEN_INVOKED
150 struct implementation_defined:
151 detail::extract_allocator_types<typename base_maker::allocator_argument>
153 typedef T value_type;
154 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::size_type size_type;
155 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
157 typedef typename base_maker::compare_argument value_compare;
158 typedef typename base_maker::allocator_type allocator_type;
159 typedef typename base_maker::node_type node;
161 typedef typename boost::allocator_pointer<allocator_type>::type node_pointer;
162 typedef typename boost::allocator_const_pointer<allocator_type>::type const_node_pointer;
164 typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
166 typedef typename base_maker::node_type node_type;
168 typedef boost::intrusive::list<detail::heap_node_base<false>,
169 boost::intrusive::constant_time_size<true>
172 typedef typename node_list_type::iterator node_list_iterator;
173 typedef typename node_list_type::const_iterator node_list_const_iterator;
174 typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
176 typedef detail::recursive_tree_iterator<node_type,
177 node_list_const_iterator,
180 detail::list_iterator_converter<node_type, node_list_type>
182 typedef iterator const_iterator;
184 typedef detail::tree_iterator<node_type,
188 detail::list_iterator_converter<node_type, node_list_type>,
197 typedef T value_type;
199 typedef typename implementation_defined::size_type size_type;
200 typedef typename implementation_defined::difference_type difference_type;
201 typedef typename implementation_defined::value_compare value_compare;
202 typedef typename implementation_defined::allocator_type allocator_type;
203 typedef typename implementation_defined::reference reference;
204 typedef typename implementation_defined::const_reference const_reference;
205 typedef typename implementation_defined::pointer pointer;
206 typedef typename implementation_defined::const_pointer const_pointer;
207 /// \copydoc boost::heap::priority_queue::iterator
208 typedef typename implementation_defined::iterator iterator;
209 typedef typename implementation_defined::const_iterator const_iterator;
210 typedef typename implementation_defined::ordered_iterator ordered_iterator;
212 typedef typename implementation_defined::handle_type handle_type;
215 typedef typename implementation_defined::node_type node_type;
216 typedef typename implementation_defined::node_list_type node_list_type;
217 typedef typename implementation_defined::node_pointer node_pointer;
218 typedef typename implementation_defined::const_node_pointer const_node_pointer;
219 typedef typename implementation_defined::node_list_iterator node_list_iterator;
220 typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator;
222 typedef typename super_t::internal_compare internal_compare;
225 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
226 explicit binomial_heap(value_compare const & cmp = value_compare()):
227 super_t(cmp), top_element(0)
230 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
231 binomial_heap(binomial_heap const & rhs):
232 super_t(rhs), top_element(0)
238 size_holder::set_size(rhs.get_size());
241 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &)
242 binomial_heap & operator=(binomial_heap const & rhs)
245 size_holder::set_size(rhs.get_size());
246 static_cast<super_t&>(*this) = rhs;
255 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
256 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
257 binomial_heap(binomial_heap && rhs):
258 super_t(std::move(rhs)), top_element(rhs.top_element)
260 trees.splice(trees.begin(), rhs.trees);
261 rhs.top_element = NULL;
264 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
265 binomial_heap & operator=(binomial_heap && rhs)
268 super_t::operator=(std::move(rhs));
269 trees.splice(trees.begin(), rhs.trees);
270 top_element = rhs.top_element;
271 rhs.top_element = NULL;
281 /// \copydoc boost::heap::priority_queue::empty
282 bool empty(void) const
284 return top_element == NULL;
288 * \b Effects: Returns the number of elements contained in the priority queue.
290 * \b Complexity: Constant, if configured with constant_time_size<true>, otherwise linear.
293 size_type size(void) const
295 if (constant_time_size)
296 return size_holder::get_size();
301 return detail::count_list_nodes<node_type, node_list_type>(trees);
304 /// \copydoc boost::heap::priority_queue::max_size
305 size_type max_size(void) const
307 const allocator_type& alloc = *this;
308 return boost::allocator_max_size(alloc);
311 /// \copydoc boost::heap::priority_queue::clear
314 typedef detail::node_disposer<node_type, typename node_list_type::value_type, allocator_type> disposer;
315 trees.clear_and_dispose(disposer(*this));
317 size_holder::set_size(0);
321 /// \copydoc boost::heap::priority_queue::get_allocator
322 allocator_type get_allocator(void) const
327 /// \copydoc boost::heap::priority_queue::swap
328 void swap(binomial_heap & rhs)
331 std::swap(top_element, rhs.top_element);
332 trees.swap(rhs.trees);
335 /// \copydoc boost::heap::priority_queue::top
336 const_reference top(void) const
338 BOOST_ASSERT(!empty());
340 return super_t::get_value(top_element->value);
344 * \b Effects: Adds a new element to the priority queue. Returns handle to element
346 * \b Complexity: Logarithmic.
349 handle_type push(value_type const & v)
351 allocator_type& alloc = *this;
352 node_pointer n = alloc.allocate(1);
353 new(n) node_type(super_t::make_node(v));
354 insert_node(trees.begin(), n);
356 if (!top_element || super_t::operator()(top_element->value, n->value))
359 size_holder::increment();
361 return handle_type(n);
364 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
366 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element.
368 * \b Complexity: Logarithmic.
371 template <class... Args>
372 handle_type emplace(Args&&... args)
374 allocator_type& alloc = *this;
375 node_pointer n = alloc.allocate(1);
376 new(n) node_type(super_t::make_node(std::forward<Args>(args)...));
377 insert_node(trees.begin(), n);
379 if (!top_element || super_t::operator()(top_element->value, n->value))
382 size_holder::increment();
384 return handle_type(n);
389 * \b Effects: Removes the top element from the priority queue.
391 * \b Complexity: Logarithmic.
396 BOOST_ASSERT(!empty());
398 node_pointer element = top_element;
400 trees.erase(node_list_type::s_iterator_to(*element));
401 size_holder::decrement();
403 if (element->child_count()) {
404 size_type sz = (1 << element->child_count()) - 1;
406 binomial_heap children(value_comp(), element->children, sz);
408 stability_counter_type stability_count = super_t::get_stability_count();
409 size_t size = constant_time_size ? size_holder::get_size()
412 super_t::set_stability_count(stability_count);
414 if (constant_time_size)
415 size_holder::set_size( size );
417 merge_and_clear_nodes(children);
424 update_top_element();
426 element->~node_type();
427 allocator_type& alloc = *this;
428 alloc.deallocate(element, 1);
433 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
435 * \b Complexity: Logarithmic.
438 void update (handle_type handle, const_reference v)
440 if (super_t::operator()(super_t::get_value(handle.node_->value), v))
447 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
449 * \b Complexity: Logarithmic.
451 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
453 void update (handle_type handle)
455 node_pointer this_node = handle.node_;
457 if (this_node->parent) {
458 if (super_t::operator()(super_t::get_value(this_node->parent->value), super_t::get_value(this_node->value)))
468 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
470 * \b Complexity: Logarithmic.
472 * \b Note: The new value is expected to be greater than the current one
474 void increase (handle_type handle, const_reference v)
476 handle.node_->value = super_t::make_node(v);
481 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
483 * \b Complexity: Logarithmic.
485 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
487 void increase (handle_type handle)
489 node_pointer n = handle.node_;
492 update_top_element();
497 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
499 * \b Complexity: Logarithmic.
501 * \b Note: The new value is expected to be less than the current one
503 void decrease (handle_type handle, const_reference v)
505 handle.node_->value = super_t::make_node(v);
510 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
512 * \b Complexity: Logarithmic.
514 * \b Note: The new value is expected to be less than the current one. If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
516 void decrease (handle_type handle)
518 node_pointer n = handle.node_;
522 update_top_element();
526 * \b Effects: Merge with priority queue rhs.
528 * \b Complexity: Logarithmic.
531 void merge(binomial_heap & rhs)
541 size_type new_size = size_holder::get_size() + rhs.get_size();
542 merge_and_clear_nodes(rhs);
544 size_holder::set_size(new_size);
546 rhs.top_element = NULL;
548 super_t::set_stability_count((std::max)(super_t::get_stability_count(),
549 rhs.get_stability_count()));
550 rhs.set_stability_count(0);
554 /// \copydoc boost::heap::priority_queue::begin
555 iterator begin(void) const
557 return iterator(trees.begin());
560 /// \copydoc boost::heap::priority_queue::end
561 iterator end(void) const
563 return iterator(trees.end());
566 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
567 ordered_iterator ordered_begin(void) const
569 return ordered_iterator(trees.begin(), trees.end(), top_element, super_t::value_comp());
572 /// \copydoc boost::heap::fibonacci_heap::ordered_end
573 ordered_iterator ordered_end(void) const
575 return ordered_iterator(NULL, super_t::value_comp());
579 * \b Effects: Removes the element handled by \c handle from the priority_queue.
581 * \b Complexity: Logarithmic.
583 void erase(handle_type handle)
585 node_pointer n = handle.node_;
586 siftup(n, force_inf());
591 /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator
592 static handle_type s_handle_from_iterator(iterator const & it)
594 node_type * ptr = const_cast<node_type *>(it.get_node());
595 return handle_type(ptr);
598 /// \copydoc boost::heap::priority_queue::value_comp
599 value_compare const & value_comp(void) const
601 return super_t::value_comp();
604 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
605 template <typename HeapType>
606 bool operator<(HeapType const & rhs) const
608 return detail::heap_compare(*this, rhs);
611 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
612 template <typename HeapType>
613 bool operator>(HeapType const & rhs) const
615 return detail::heap_compare(rhs, *this);
618 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
619 template <typename HeapType>
620 bool operator>=(HeapType const & rhs) const
622 return !operator<(rhs);
625 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
626 template <typename HeapType>
627 bool operator<=(HeapType const & rhs) const
629 return !operator>(rhs);
632 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
633 template <typename HeapType>
634 bool operator==(HeapType const & rhs) const
636 return detail::heap_equality(*this, rhs);
639 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
640 template <typename HeapType>
641 bool operator!=(HeapType const & rhs) const
643 return !(*this == rhs);
647 #if !defined(BOOST_DOXYGEN_INVOKED)
648 void merge_and_clear_nodes(binomial_heap & rhs)
650 BOOST_HEAP_ASSERT (!empty());
651 BOOST_HEAP_ASSERT (!rhs.empty());
653 node_list_iterator this_iterator = trees.begin();
654 node_pointer carry_node = NULL;
656 while (!rhs.trees.empty()) {
657 node_pointer rhs_node = static_cast<node_pointer>(&rhs.trees.front());
658 size_type rhs_degree = rhs_node->child_count();
660 if (super_t::operator()(top_element->value, rhs_node->value))
661 top_element = rhs_node;
664 node_pointer this_node = static_cast<node_pointer>(&*this_iterator);
665 size_type this_degree = this_node->child_count();
667 rhs.sorted_by_degree();
669 if (this_degree == rhs_degree) {
671 if (carry_node->child_count() < this_degree) {
672 trees.insert(this_iterator, *carry_node);
675 rhs.trees.pop_front();
676 carry_node = merge_trees(carry_node, rhs_node);
680 this_iterator = trees.erase(this_iterator);
681 rhs.trees.pop_front();
682 carry_node = merge_trees(this_node, rhs_node);
685 if (this_iterator == trees.end())
691 if (this_degree < rhs_degree) {
693 if (carry_node->child_count() < this_degree) {
694 trees.insert(this_iterator, *carry_node);
697 } else if (carry_node->child_count() == rhs_degree) {
698 rhs.trees.pop_front();
699 carry_node = merge_trees(carry_node, rhs_node);
702 this_iterator = trees.erase(this_iterator);
703 carry_node = merge_trees(this_node, carry_node);
708 if (this_iterator == trees.end())
713 if (this_iterator == trees.end())
719 if (this_degree > rhs_degree) {
720 rhs.trees.pop_front();
722 if (carry_node->child_count() < rhs_degree) {
723 trees.insert(this_iterator, *carry_node);
724 trees.insert(this_iterator, *rhs_node);
727 carry_node = merge_trees(rhs_node, carry_node);
729 trees.insert(this_iterator, *rhs_node);
733 if (!rhs.trees.empty()) {
735 node_list_iterator rhs_it = rhs.trees.begin();
736 while (static_cast<node_pointer>(&*rhs_it)->child_count() < carry_node->child_count())
738 rhs.insert_node(rhs_it, carry_node);
741 rhs.sorted_by_degree();
743 trees.splice(trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end());
744 update_top_element();
746 merge_and_clear_nodes(rhs);
748 trees.splice(trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end());
753 insert_node(this_iterator, carry_node);
756 void clone_forest(binomial_heap const & rhs)
758 BOOST_HEAP_ASSERT(trees.empty());
759 typedef typename node_type::template node_cloner<allocator_type> node_cloner;
760 trees.clone_from(rhs.trees, node_cloner(*this, NULL), detail::nop_disposer());
762 update_top_element();
767 template <typename X>
768 bool operator()(X const &, X const &) const
774 template <typename Compare>
775 void siftup(node_pointer n, Compare const & cmp)
778 node_pointer parent = n->parent;
779 node_pointer grand_parent = parent->parent;
780 if (cmp(n->value, parent->value))
783 n->remove_from_parent();
785 n->swap_children(parent);
786 n->update_children();
787 parent->update_children();
790 parent->remove_from_parent();
791 grand_parent->add_child(n);
793 node_list_iterator it = trees.erase(node_list_type::s_iterator_to(*parent));
794 trees.insert(it, *n);
796 n->add_child(parent);
800 void siftdown(node_pointer n)
802 while (n->child_count()) {
803 node_pointer max_child = detail::find_max_child<node_list_type, node_type, internal_compare>(n->children, super_t::get_internal_cmp());
805 if (super_t::operator()(max_child->value, n->value))
808 max_child->remove_from_parent();
810 n->swap_children(max_child);
811 n->update_children();
812 max_child->update_children();
814 node_pointer parent = n->parent;
816 n->remove_from_parent();
817 max_child->add_child(n);
818 parent->add_child(max_child);
820 node_list_iterator position = trees.erase(node_list_type::s_iterator_to(*n));
821 max_child->add_child(n);
822 trees.insert(position, *max_child);
827 void insert_node(node_list_iterator it, node_pointer n)
829 if (it != trees.end())
830 BOOST_HEAP_ASSERT(static_cast<node_pointer>(&*it)->child_count() >= n->child_count());
833 BOOST_HEAP_ASSERT(!n->is_linked());
834 if (it == trees.end())
837 node_pointer this_node = static_cast<node_pointer>(&*it);
838 size_type this_degree = this_node->child_count();
839 size_type n_degree = n->child_count();
840 if (this_degree == n_degree) {
841 BOOST_HEAP_ASSERT(it->is_linked());
842 it = trees.erase(it);
844 n = merge_trees(n, this_node);
848 trees.insert(it, *n);
851 // private constructor, just used in pop()
852 explicit binomial_heap(value_compare const & cmp, node_list_type & child_list, size_type size):
855 size_holder::set_size(size);
857 top_element = static_cast<node_pointer>(&*child_list.begin()); // not correct, but we will reset it later
861 for (node_list_iterator it = child_list.begin(); it != child_list.end(); ++it) {
862 node_pointer n = static_cast<node_pointer>(&*it);
866 trees.splice(trees.end(), child_list, child_list.begin(), child_list.end());
868 trees.sort(detail::cmp_by_degree<node_type>());
871 node_pointer merge_trees (node_pointer node1, node_pointer node2)
873 BOOST_HEAP_ASSERT(node1->child_count() == node2->child_count());
875 if (super_t::operator()(node1->value, node2->value))
876 std::swap(node1, node2);
879 node2->remove_from_parent();
881 node1->add_child(node2);
885 void update_top_element(void)
887 top_element = detail::find_max_child<node_list_type, node_type, internal_compare>(trees, super_t::get_internal_cmp());
890 void sorted_by_degree(void) const
892 #ifdef BOOST_HEAP_SANITYCHECKS
895 for (node_list_const_iterator it = trees.begin(); it != trees.end(); ++it) {
896 const_node_pointer n = static_cast<const_node_pointer>(&*it);
897 BOOST_HEAP_ASSERT(int(n->child_count()) > degree);
898 degree = n->child_count();
900 BOOST_HEAP_ASSERT((detail::is_heap<node_type, super_t>(n, *this)));
902 size_type child_nodes = detail::count_nodes<node_type>(n);
903 BOOST_HEAP_ASSERT(child_nodes == size_type(1 << static_cast<const_node_pointer>(&*it)->child_count()));
908 void sanity_check(void)
910 #ifdef BOOST_HEAP_SANITYCHECKS
914 node_pointer found_top = detail::find_max_child<node_list_type, node_type, internal_compare>(trees, super_t::get_internal_cmp());
915 BOOST_HEAP_ASSERT(top_element == found_top);
918 if (constant_time_size) {
919 size_t counted = detail::count_list_nodes<node_type, node_list_type>(trees);
920 size_t stored = size_holder::get_size();
921 BOOST_HEAP_ASSERT(counted == stored);
926 node_pointer top_element;
927 node_list_type trees;
928 #endif // BOOST_DOXYGEN_INVOKED
932 } /* namespace heap */
933 } /* namespace boost */
935 #undef BOOST_HEAP_ASSERT
937 #endif /* BOOST_HEAP_D_ARY_HEAP_HPP */