Chris@16: // boost heap: fibonacci heap Chris@16: // Chris@16: // Copyright (C) 2010 Tim Blechmann Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_HEAP_FIBONACCI_HEAP_HPP Chris@16: #define BOOST_HEAP_FIBONACCI_HEAP_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@101: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@101: #pragma once Chris@101: #endif Chris@101: Chris@101: Chris@16: #ifndef BOOST_DOXYGEN_INVOKED Chris@16: #ifdef BOOST_HEAP_SANITYCHECKS Chris@16: #define BOOST_HEAP_ASSERT BOOST_ASSERT Chris@16: #else Chris@16: #define BOOST_HEAP_ASSERT(expression) Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace heap { Chris@16: namespace detail { Chris@16: Chris@16: typedef parameter::parameters, Chris@16: boost::parameter::optional, Chris@16: boost::parameter::optional, Chris@16: boost::parameter::optional, Chris@16: boost::parameter::optional Chris@16: > fibonacci_heap_signature; Chris@16: Chris@16: template Chris@16: struct make_fibonacci_heap_base Chris@16: { Chris@16: static const bool constant_time_size = parameter::binding::type::value; Chris@16: Chris@16: typedef typename detail::make_heap_base::type base_type; Chris@16: typedef typename detail::make_heap_base::allocator_argument allocator_argument; Chris@16: typedef typename detail::make_heap_base::compare_argument compare_argument; Chris@16: typedef marked_heap_node node_type; Chris@16: Chris@16: typedef typename allocator_argument::template rebind::other allocator_type; Chris@16: Chris@16: struct type: Chris@16: base_type, Chris@16: allocator_type Chris@16: { Chris@16: type(compare_argument const & arg): Chris@16: base_type(arg) Chris@16: {} Chris@16: Chris@101: type(type const & rhs): Chris@101: base_type(static_cast(rhs)), Chris@101: allocator_type(static_cast(rhs)) Chris@101: {} Chris@101: Chris@101: type & operator=(type const & rhs) Chris@101: { Chris@101: base_type::operator=(static_cast(rhs)); Chris@101: allocator_type::operator=(static_cast(rhs)); Chris@101: return *this; Chris@101: } Chris@101: Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: type(type && rhs): Chris@16: base_type(std::move(static_cast(rhs))), Chris@16: allocator_type(std::move(static_cast(rhs))) Chris@16: {} Chris@16: Chris@16: type & operator=(type && rhs) Chris@16: { Chris@16: base_type::operator=(std::move(static_cast(rhs))); Chris@16: allocator_type::operator=(std::move(static_cast(rhs))); Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: }; Chris@16: }; Chris@16: Chris@16: } Chris@16: Chris@16: Chris@16: Chris@16: /** Chris@16: * \class fibonacci_heap Chris@16: * \brief fibonacci heap Chris@16: * Chris@16: * The template parameter T is the type to be managed by the container. Chris@16: * The user can specify additional options and if no options are provided default options are used. Chris@16: * Chris@16: * The container supports the following options: Chris@16: * - \c boost::heap::stable<>, defaults to \c stable Chris@16: * - \c boost::heap::compare<>, defaults to \c compare > Chris@16: * - \c boost::heap::allocator<>, defaults to \c allocator > Chris@16: * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size Chris@16: * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type Chris@16: * Chris@16: */ Chris@16: #ifdef BOOST_DOXYGEN_INVOKED Chris@16: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class fibonacci_heap: Chris@16: private detail::make_fibonacci_heap_base::type Chris@16: >::type Chris@16: { Chris@16: typedef typename detail::fibonacci_heap_signature::bind::type bound_args; Chris@16: typedef detail::make_fibonacci_heap_base base_maker; Chris@16: typedef typename base_maker::type super_t; Chris@16: Chris@16: typedef typename super_t::size_holder_type size_holder; Chris@16: typedef typename super_t::internal_type internal_type; Chris@16: typedef typename base_maker::allocator_argument allocator_argument; Chris@16: Chris@16: template Chris@16: friend struct heap_merge_emulate; Chris@16: Chris@16: private: Chris@16: #ifndef BOOST_DOXYGEN_INVOKED Chris@16: struct implementation_defined: Chris@16: detail::extract_allocator_types Chris@16: { Chris@16: typedef T value_type; Chris@16: typedef typename detail::extract_allocator_types::size_type size_type; Chris@16: typedef typename detail::extract_allocator_types::reference reference; Chris@16: Chris@16: typedef typename base_maker::compare_argument value_compare; Chris@16: typedef typename base_maker::allocator_type allocator_type; Chris@16: Chris@16: typedef typename allocator_type::pointer node_pointer; Chris@16: typedef typename allocator_type::const_pointer const_node_pointer; Chris@16: Chris@16: typedef detail::heap_node_list node_list_type; Chris@16: typedef typename node_list_type::iterator node_list_iterator; Chris@16: typedef typename node_list_type::const_iterator node_list_const_iterator; Chris@16: Chris@16: typedef typename base_maker::node_type node; Chris@16: Chris@16: typedef detail::value_extractor value_extractor; Chris@16: typedef typename super_t::internal_compare internal_compare; Chris@16: typedef detail::node_handle handle_type; Chris@16: Chris@16: typedef detail::recursive_tree_iterator Chris@16: > iterator; Chris@16: typedef iterator const_iterator; Chris@16: Chris@16: typedef detail::tree_iterator, Chris@16: true, Chris@16: true, Chris@16: value_compare Chris@16: > ordered_iterator; Chris@16: }; Chris@16: Chris@16: typedef typename implementation_defined::node node; Chris@16: typedef typename implementation_defined::node_pointer node_pointer; Chris@16: typedef typename implementation_defined::node_list_type node_list_type; Chris@16: typedef typename implementation_defined::node_list_iterator node_list_iterator; Chris@16: typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator; Chris@16: typedef typename implementation_defined::internal_compare internal_compare; Chris@16: #endif Chris@16: Chris@16: public: Chris@16: typedef T value_type; Chris@16: Chris@16: typedef typename implementation_defined::size_type size_type; Chris@16: typedef typename implementation_defined::difference_type difference_type; Chris@16: typedef typename implementation_defined::value_compare value_compare; Chris@16: typedef typename implementation_defined::allocator_type allocator_type; Chris@16: typedef typename implementation_defined::reference reference; Chris@16: typedef typename implementation_defined::const_reference const_reference; Chris@16: typedef typename implementation_defined::pointer pointer; Chris@16: typedef typename implementation_defined::const_pointer const_pointer; Chris@16: /// \copydoc boost::heap::priority_queue::iterator Chris@16: typedef typename implementation_defined::iterator iterator; Chris@16: typedef typename implementation_defined::const_iterator const_iterator; Chris@16: typedef typename implementation_defined::ordered_iterator ordered_iterator; Chris@16: Chris@16: typedef typename implementation_defined::handle_type handle_type; Chris@16: Chris@16: static const bool constant_time_size = base_maker::constant_time_size; Chris@16: static const bool has_ordered_iterators = true; Chris@16: static const bool is_mergable = true; Chris@16: static const bool is_stable = detail::extract_stable::value; Chris@16: static const bool has_reserve = false; Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) Chris@16: explicit fibonacci_heap(value_compare const & cmp = value_compare()): Chris@16: super_t(cmp), top_element(0) Chris@16: {} Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) Chris@16: fibonacci_heap(fibonacci_heap const & rhs): Chris@16: super_t(rhs), top_element(0) Chris@16: { Chris@16: if (rhs.empty()) Chris@16: return; Chris@16: Chris@16: clone_forest(rhs); Chris@16: size_holder::set_size(rhs.size()); Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) Chris@16: fibonacci_heap(fibonacci_heap && rhs): Chris@16: super_t(std::move(rhs)), top_element(rhs.top_element) Chris@16: { Chris@16: roots.splice(roots.begin(), rhs.roots); Chris@16: rhs.top_element = NULL; Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) Chris@16: fibonacci_heap & operator=(fibonacci_heap && rhs) Chris@16: { Chris@16: clear(); Chris@16: Chris@16: super_t::operator=(std::move(rhs)); Chris@16: roots.splice(roots.begin(), rhs.roots); Chris@16: top_element = rhs.top_element; Chris@16: rhs.top_element = NULL; Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) Chris@16: fibonacci_heap & operator=(fibonacci_heap const & rhs) Chris@16: { Chris@16: clear(); Chris@16: size_holder::set_size(rhs.size()); Chris@16: static_cast(*this) = rhs; Chris@16: Chris@16: if (rhs.empty()) Chris@16: top_element = NULL; Chris@16: else Chris@16: clone_forest(rhs); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: ~fibonacci_heap(void) Chris@16: { Chris@16: clear(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::empty Chris@16: bool empty(void) const Chris@16: { Chris@16: if (constant_time_size) Chris@16: return size() == 0; Chris@16: else Chris@16: return roots.empty(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::size Chris@16: size_type size(void) const Chris@16: { Chris@16: if (constant_time_size) Chris@16: return size_holder::get_size(); Chris@16: Chris@16: if (empty()) Chris@16: return 0; Chris@16: else Chris@16: return detail::count_list_nodes(roots); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::max_size Chris@16: size_type max_size(void) const Chris@16: { Chris@16: return allocator_type::max_size(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::clear Chris@16: void clear(void) Chris@16: { Chris@16: typedef detail::node_disposer disposer; Chris@16: roots.clear_and_dispose(disposer(*this)); Chris@16: Chris@16: size_holder::set_size(0); Chris@16: top_element = NULL; Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::get_allocator Chris@16: allocator_type get_allocator(void) const Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::swap Chris@16: void swap(fibonacci_heap & rhs) Chris@16: { Chris@16: super_t::swap(rhs); Chris@16: std::swap(top_element, rhs.top_element); Chris@16: roots.swap(rhs.roots); Chris@16: } Chris@16: Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::top Chris@16: value_type const & top(void) const Chris@16: { Chris@16: BOOST_ASSERT(!empty()); Chris@16: Chris@16: return super_t::get_value(top_element->value); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Adds a new element to the priority queue. Returns handle to element Chris@16: * Chris@16: * \b Complexity: Constant. Chris@16: * Chris@16: * \b Note: Does not invalidate iterators. Chris@16: * Chris@16: * */ Chris@16: handle_type push(value_type const & v) Chris@16: { Chris@16: size_holder::increment(); Chris@16: Chris@16: node_pointer n = allocator_type::allocate(1); Chris@16: Chris@16: new(n) node(super_t::make_node(v)); Chris@16: roots.push_front(*n); Chris@16: Chris@16: if (!top_element || super_t::operator()(top_element->value, n->value)) Chris@16: top_element = n; Chris@16: return handle_type(n); Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: /** Chris@16: * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element. Chris@16: * Chris@16: * \b Complexity: Constant. Chris@16: * Chris@16: * \b Note: Does not invalidate iterators. Chris@16: * Chris@16: * */ Chris@16: template Chris@16: handle_type emplace(Args&&... args) Chris@16: { Chris@16: size_holder::increment(); Chris@16: Chris@16: node_pointer n = allocator_type::allocate(1); Chris@16: Chris@16: new(n) node(super_t::make_node(std::forward(args)...)); Chris@16: roots.push_front(*n); Chris@16: Chris@16: if (!top_element || super_t::operator()(top_element->value, n->value)) Chris@16: top_element = n; Chris@16: return handle_type(n); Chris@16: } Chris@16: #endif Chris@16: Chris@16: /** Chris@16: * \b Effects: Removes the top element from the priority queue. Chris@16: * Chris@16: * \b Complexity: Logarithmic (amortized). Linear (worst case). Chris@16: * Chris@16: * */ Chris@16: void pop(void) Chris@16: { Chris@16: BOOST_ASSERT(!empty()); Chris@16: Chris@16: node_pointer element = top_element; Chris@16: roots.erase(node_list_type::s_iterator_to(*element)); Chris@16: Chris@16: finish_erase_or_pop(element); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. Chris@16: * Chris@16: * \b Complexity: Logarithmic if current value < v, Constant otherwise. Chris@16: * Chris@16: * */ Chris@16: void update (handle_type handle, const_reference v) Chris@16: { Chris@16: if (super_t::operator()(super_t::get_value(handle.node_->value), v)) Chris@16: increase(handle, v); Chris@16: else Chris@16: decrease(handle, v); Chris@16: } Chris@16: Chris@16: /** \copydoc boost::heap::fibonacci_heap::update(handle_type, const_reference) Chris@16: * Chris@16: * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates Chris@16: * the iterator to the object referred to by the handle. Chris@16: * */ Chris@16: void update_lazy(handle_type handle, const_reference v) Chris@16: { Chris@16: handle.node_->value = super_t::make_node(v); Chris@16: update_lazy(handle); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Updates the heap after the element handled by \c handle has been changed. Chris@16: * Chris@16: * \b Complexity: Logarithmic. Chris@16: * Chris@16: * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! Chris@16: * */ Chris@16: void update (handle_type handle) Chris@16: { Chris@16: node_pointer n = handle.node_; Chris@16: node_pointer parent = n->get_parent(); Chris@16: Chris@16: if (parent) { Chris@16: n->parent = NULL; Chris@16: roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n)); Chris@16: } Chris@16: add_children_to_root(n); Chris@16: consolidate(); Chris@16: } Chris@16: Chris@16: /** \copydoc boost::heap::fibonacci_heap::update (handle_type handle) Chris@16: * Chris@16: * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates Chris@16: * the iterator to the object referred to by the handle. Chris@16: * */ Chris@16: void update_lazy (handle_type handle) Chris@16: { Chris@16: node_pointer n = handle.node_; Chris@16: node_pointer parent = n->get_parent(); Chris@16: Chris@16: if (parent) { Chris@16: n->parent = NULL; Chris@16: roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n)); Chris@16: } Chris@16: add_children_to_root(n); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. Chris@16: * Chris@16: * \b Complexity: Constant. Chris@16: * Chris@16: * \b Note: The new value is expected to be greater than the current one Chris@16: * */ Chris@16: void increase (handle_type handle, const_reference v) Chris@16: { Chris@16: handle.node_->value = super_t::make_node(v); Chris@16: increase(handle); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Updates the heap after the element handled by \c handle has been changed. Chris@16: * Chris@16: * \b Complexity: Constant. Chris@16: * Chris@16: * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! Chris@16: * */ Chris@16: void increase (handle_type handle) Chris@16: { Chris@16: node_pointer n = handle.node_; Chris@16: Chris@16: if (n->parent) { Chris@16: if (super_t::operator()(n->get_parent()->value, n->value)) { Chris@16: node_pointer parent = n->get_parent(); Chris@16: cut(n); Chris@16: cascading_cut(parent); Chris@16: } Chris@16: } Chris@16: Chris@16: if (super_t::operator()(top_element->value, n->value)) { Chris@16: top_element = n; Chris@16: return; Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. Chris@16: * Chris@16: * \b Complexity: Logarithmic. Chris@16: * Chris@16: * \b Note: The new value is expected to be less than the current one Chris@16: * */ Chris@16: void decrease (handle_type handle, const_reference v) Chris@16: { Chris@16: handle.node_->value = super_t::make_node(v); Chris@16: decrease(handle); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Updates the heap after the element handled by \c handle has been changed. Chris@16: * Chris@16: * \b Complexity: Logarithmic. Chris@16: * Chris@16: * \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! Chris@16: * */ Chris@16: void decrease (handle_type handle) Chris@16: { Chris@16: update(handle); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Removes the element handled by \c handle from the priority_queue. Chris@16: * Chris@16: * \b Complexity: Logarithmic. Chris@16: * */ Chris@16: void erase(handle_type const & handle) Chris@16: { Chris@16: node_pointer element = handle.node_; Chris@16: node_pointer parent = element->get_parent(); Chris@16: Chris@16: if (parent) Chris@16: parent->children.erase(node_list_type::s_iterator_to(*element)); Chris@16: else Chris@16: roots.erase(node_list_type::s_iterator_to(*element)); Chris@16: Chris@16: finish_erase_or_pop(element); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::begin Chris@16: iterator begin(void) const Chris@16: { Chris@16: return iterator(roots.begin()); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::end Chris@16: iterator end(void) const Chris@16: { Chris@16: return iterator(roots.end()); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \b Effects: Returns an ordered iterator to the first element contained in the priority queue. Chris@16: * Chris@16: * \b Note: Ordered iterators traverse the priority queue in heap order. Chris@16: * */ Chris@16: ordered_iterator ordered_begin(void) const Chris@16: { Chris@16: return ordered_iterator(roots.begin(), roots.end(), top_element, super_t::value_comp()); Chris@16: } Chris@16: Chris@16: /** Chris@101: * \b Effects: Returns an ordered iterator to the end of the priority queue. Chris@16: * Chris@16: * \b Note: Ordered iterators traverse the priority queue in heap order. Chris@16: * */ Chris@16: ordered_iterator ordered_end(void) const Chris@16: { Chris@16: return ordered_iterator(NULL, super_t::value_comp()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \b Effects: Merge with priority queue rhs. Chris@16: * Chris@16: * \b Complexity: Constant. Chris@16: * Chris@16: * */ Chris@16: void merge(fibonacci_heap & rhs) Chris@16: { Chris@16: size_holder::add(rhs.get_size()); Chris@16: Chris@16: if (!top_element || Chris@16: (rhs.top_element && super_t::operator()(top_element->value, rhs.top_element->value))) Chris@16: top_element = rhs.top_element; Chris@16: Chris@16: roots.splice(roots.end(), rhs.roots); Chris@16: Chris@101: rhs.top_element = NULL; Chris@16: rhs.set_size(0); Chris@16: Chris@16: super_t::set_stability_count((std::max)(super_t::get_stability_count(), Chris@16: rhs.get_stability_count())); Chris@16: rhs.set_stability_count(0); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator Chris@16: static handle_type s_handle_from_iterator(iterator const & it) Chris@16: { Chris@16: node * ptr = const_cast(it.get_node()); Chris@16: return handle_type(ptr); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::value_comp Chris@16: value_compare const & value_comp(void) const Chris@16: { Chris@16: return super_t::value_comp(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const Chris@16: template Chris@16: bool operator<(HeapType const & rhs) const Chris@16: { Chris@16: return detail::heap_compare(*this, rhs); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const Chris@16: template Chris@16: bool operator>(HeapType const & rhs) const Chris@16: { Chris@16: return detail::heap_compare(rhs, *this); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const Chris@16: template Chris@16: bool operator>=(HeapType const & rhs) const Chris@16: { Chris@16: return !operator<(rhs); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const Chris@16: template Chris@16: bool operator<=(HeapType const & rhs) const Chris@16: { Chris@16: return !operator>(rhs); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const Chris@16: template Chris@16: bool operator==(HeapType const & rhs) const Chris@16: { Chris@16: return detail::heap_equality(*this, rhs); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const Chris@16: template Chris@16: bool operator!=(HeapType const & rhs) const Chris@16: { Chris@16: return !(*this == rhs); Chris@16: } Chris@16: Chris@16: private: Chris@16: #if !defined(BOOST_DOXYGEN_INVOKED) Chris@16: void clone_forest(fibonacci_heap const & rhs) Chris@16: { Chris@16: BOOST_HEAP_ASSERT(roots.empty()); Chris@16: typedef typename node::template node_cloner node_cloner; Chris@16: roots.clone_from(rhs.roots, node_cloner(*this, NULL), detail::nop_disposer()); Chris@16: Chris@16: top_element = detail::find_max_child(roots, super_t::get_internal_cmp()); Chris@16: } Chris@16: Chris@16: void cut(node_pointer n) Chris@16: { Chris@16: node_pointer parent = n->get_parent(); Chris@16: roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n)); Chris@16: n->parent = 0; Chris@16: n->mark = false; Chris@16: } Chris@16: Chris@16: void cascading_cut(node_pointer n) Chris@16: { Chris@16: node_pointer parent = n->get_parent(); Chris@16: Chris@16: if (parent) { Chris@16: if (!parent->mark) Chris@16: parent->mark = true; Chris@16: else { Chris@16: cut(n); Chris@16: cascading_cut(parent); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: void add_children_to_root(node_pointer n) Chris@16: { Chris@16: for (node_list_iterator it = n->children.begin(); it != n->children.end(); ++it) { Chris@16: node_pointer child = static_cast(&*it); Chris@16: child->parent = 0; Chris@16: } Chris@16: Chris@16: roots.splice(roots.end(), n->children); Chris@16: } Chris@16: Chris@16: void consolidate(void) Chris@16: { Chris@16: if (roots.empty()) Chris@16: return; Chris@16: Chris@16: static const size_type max_log2 = sizeof(size_type) * 8; Chris@16: boost::array aux; Chris@16: aux.assign(NULL); Chris@16: Chris@16: node_list_iterator it = roots.begin(); Chris@16: top_element = static_cast(&*it); Chris@16: Chris@16: do { Chris@16: node_pointer n = static_cast(&*it); Chris@16: ++it; Chris@16: size_type node_rank = n->child_count(); Chris@16: Chris@16: if (aux[node_rank] == NULL) Chris@16: aux[node_rank] = n; Chris@16: else { Chris@16: do { Chris@16: node_pointer other = aux[node_rank]; Chris@16: if (super_t::operator()(n->value, other->value)) Chris@16: std::swap(n, other); Chris@16: Chris@16: if (other->parent) Chris@16: n->children.splice(n->children.end(), other->parent->children, node_list_type::s_iterator_to(*other)); Chris@16: else Chris@16: n->children.splice(n->children.end(), roots, node_list_type::s_iterator_to(*other)); Chris@16: Chris@16: other->parent = n; Chris@16: Chris@16: aux[node_rank] = NULL; Chris@16: node_rank = n->child_count(); Chris@16: } while (aux[node_rank] != NULL); Chris@16: aux[node_rank] = n; Chris@16: } Chris@16: Chris@16: if (super_t::operator()(top_element->value, n->value)) Chris@16: top_element = n; Chris@16: } Chris@16: while (it != roots.end()); Chris@16: } Chris@16: Chris@16: void finish_erase_or_pop(node_pointer erased_node) Chris@16: { Chris@16: add_children_to_root(erased_node); Chris@16: Chris@16: erased_node->~node(); Chris@16: allocator_type::deallocate(erased_node, 1); Chris@16: Chris@16: size_holder::decrement(); Chris@16: if (!empty()) Chris@16: consolidate(); Chris@16: else Chris@16: top_element = NULL; Chris@16: } Chris@16: Chris@16: mutable node_pointer top_element; Chris@16: node_list_type roots; Chris@16: #endif Chris@16: }; Chris@16: Chris@16: } /* namespace heap */ Chris@16: } /* namespace boost */ Chris@16: Chris@16: #undef BOOST_HEAP_ASSERT Chris@16: Chris@16: #endif /* BOOST_HEAP_FIBONACCI_HEAP_HPP */