Chris@16: // boost heap: pairing 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_PAIRING_HEAP_HPP Chris@16: #define BOOST_HEAP_PAIRING_HEAP_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #include 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: > pairing_heap_signature; Chris@16: Chris@16: template Chris@16: struct make_pairing_heap_base Chris@16: { Chris@16: static const bool constant_time_size = parameter::binding::type::value; 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: Chris@16: typedef 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@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: type(type const & rhs): Chris@16: base_type(rhs), allocator_type(rhs) Chris@16: {} Chris@16: 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: Chris@16: type & operator=(type const & rhs) Chris@16: { Chris@16: base_type::operator=(static_cast(rhs)); Chris@16: allocator_type::operator=(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: * \class pairing_heap Chris@16: * \brief pairing heap Chris@16: * Chris@16: * Pairing heaps are self-adjusting binary heaps. Although design and implementation are rather simple, Chris@16: * the complexity analysis is yet unsolved. For details, consult: Chris@16: * Chris@16: * Pettie, Seth (2005), "Towards a final analysis of pairing heaps", Chris@16: * Proc. 46th Annual IEEE Symposium on Foundations of Computer Science, pp. 174-183 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::compare<>, defaults to \c compare > Chris@16: * - \c boost::heap::stable<>, defaults to \c stable Chris@16: * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type 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: * 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 pairing_heap: Chris@16: private detail::make_pairing_heap_base::type Chris@16: >::type Chris@16: { Chris@16: typedef typename detail::pairing_heap_signature::bind::type bound_args; Chris@16: typedef detail::make_pairing_heap_base base_maker; Chris@16: typedef typename base_maker::type super_t; Chris@16: Chris@16: typedef typename super_t::internal_type internal_type; Chris@16: typedef typename super_t::size_holder_type size_holder; Chris@16: typedef typename base_maker::allocator_argument allocator_argument; Chris@16: Chris@16: private: Chris@16: template Chris@16: friend struct heap_merge_emulate; Chris@16: 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::tree_iterator, Chris@16: false, Chris@16: false, Chris@16: value_compare Chris@16: > iterator; Chris@16: Chris@16: typedef iterator const_iterator; Chris@16: Chris@16: typedef detail::tree_iterator, Chris@16: false, 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: Chris@16: typedef boost::intrusive::list, Chris@16: boost::intrusive::constant_time_size Chris@16: > node_child_list; 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 = super_t::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 pairing_heap(value_compare const & cmp = value_compare()): Chris@16: super_t(cmp), root(NULL) Chris@16: {} Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) Chris@16: pairing_heap(pairing_heap const & rhs): Chris@16: super_t(rhs), root(NULL) Chris@16: { Chris@16: if (rhs.empty()) Chris@16: return; Chris@16: Chris@16: clone_tree(rhs); Chris@16: size_holder::set_size(rhs.get_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: pairing_heap(pairing_heap && rhs): Chris@16: super_t(std::move(rhs)), root(rhs.root) Chris@16: { Chris@16: rhs.root = NULL; Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) Chris@16: pairing_heap & operator=(pairing_heap && rhs) Chris@16: { Chris@16: super_t::operator=(std::move(rhs)); Chris@16: root = rhs.root; Chris@16: rhs.root = NULL; Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs) Chris@16: pairing_heap & operator=(pairing_heap const & rhs) Chris@16: { Chris@16: clear(); Chris@16: size_holder::set_size(rhs.get_size()); Chris@16: static_cast(*this) = rhs; Chris@16: Chris@16: clone_tree(rhs); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: ~pairing_heap(void) Chris@16: { Chris@16: while (!empty()) Chris@16: pop(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::empty Chris@16: bool empty(void) const Chris@16: { Chris@16: return root == NULL; Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::binomial_heap::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 (root == NULL) Chris@16: return 0; Chris@16: else Chris@16: return detail::count_nodes(root); 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: if (empty()) Chris@16: return; Chris@16: Chris@16: root->template clear_subtree(*this); Chris@16: root->~node(); Chris@16: allocator_type::deallocate(root, 1); Chris@16: root = NULL; Chris@16: size_holder::set_size(0); 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(pairing_heap & rhs) Chris@16: { Chris@16: super_t::swap(rhs); Chris@16: std::swap(root, rhs.root); Chris@16: } Chris@16: Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::top Chris@16: const_reference top(void) const Chris@16: { Chris@16: BOOST_ASSERT(!empty()); Chris@16: Chris@16: return super_t::get_value(root->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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: Chris@16: merge_node(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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: Chris@16: merge_node(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). Chris@16: * Chris@16: * */ Chris@16: void pop(void) Chris@16: { Chris@16: BOOST_ASSERT(!empty()); Chris@16: Chris@16: erase(handle_type(root)); 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). Chris@16: * Chris@16: * */ Chris@16: void update (handle_type handle, const_reference v) Chris@16: { Chris@16: handle.node_->value = super_t::make_node(v); Chris@16: update(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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: Chris@16: n->unlink(); Chris@16: if (!n->children.empty()) Chris@16: n = merge_nodes(n, merge_node_list(n->children)); Chris@16: Chris@16: if (n != root) Chris@16: merge_node(n); 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: update(handle, v); 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: update(handle); 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: update(handle, v); 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). 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: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). Chris@16: * */ Chris@16: void erase(handle_type handle) Chris@16: { Chris@16: node_pointer n = handle.node_; Chris@16: if (n != root) { Chris@16: n->unlink(); Chris@16: if (!n->children.empty()) Chris@16: merge_node(merge_node_list(n->children)); Chris@16: } else { Chris@16: if (!n->children.empty()) Chris@16: root = merge_node_list(n->children); Chris@16: else Chris@16: root = NULL; Chris@16: } Chris@16: Chris@16: size_holder::decrement(); Chris@16: n->~node(); Chris@16: allocator_type::deallocate(n, 1); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::begin Chris@16: iterator begin(void) const Chris@16: { Chris@16: return iterator(root, super_t::value_comp()); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::priority_queue::end Chris@16: iterator end(void) const Chris@16: { Chris@16: return iterator(); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::fibonacci_heap::ordered_begin Chris@16: ordered_iterator ordered_begin(void) const Chris@16: { Chris@16: return ordered_iterator(root, super_t::value_comp()); Chris@16: } Chris@16: Chris@16: /// \copydoc boost::heap::fibonacci_heap::ordered_begin 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: /// \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: /** Chris@16: * \b Effects: Merge all elements from rhs into this Chris@16: * Chris@16: * \cond Chris@16: * \b Complexity: \f$2^2log(log(N))\f$ (amortized). Chris@16: * \endcond Chris@16: * Chris@16: * \b Complexity: 2**2*log(log(N)) (amortized). Chris@16: * Chris@16: * */ Chris@16: void merge(pairing_heap & rhs) Chris@16: { Chris@16: if (rhs.empty()) Chris@16: return; Chris@16: Chris@16: merge_node(rhs.root); Chris@16: Chris@16: size_holder::add(rhs.get_size()); Chris@16: rhs.set_size(0); Chris@16: rhs.root = NULL; 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::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_tree(pairing_heap const & rhs) Chris@16: { Chris@16: BOOST_HEAP_ASSERT(root == NULL); Chris@16: if (rhs.empty()) Chris@16: return; Chris@16: Chris@16: root = allocator_type::allocate(1); Chris@16: Chris@16: new(root) node(static_cast(*rhs.root), static_cast(*this)); Chris@16: } Chris@16: Chris@16: void merge_node(node_pointer other) Chris@16: { Chris@16: BOOST_HEAP_ASSERT(other); Chris@16: if (root != NULL) Chris@16: root = merge_nodes(root, other); Chris@16: else Chris@16: root = other; Chris@16: } Chris@16: Chris@16: node_pointer merge_node_list(node_child_list & children) Chris@16: { Chris@16: BOOST_HEAP_ASSERT(!children.empty()); Chris@16: node_pointer merged = merge_first_pair(children); Chris@16: if (children.empty()) Chris@16: return merged; Chris@16: Chris@16: node_child_list node_list; Chris@16: node_list.push_back(*merged); Chris@16: Chris@16: do { Chris@16: node_pointer next_merged = merge_first_pair(children); Chris@16: node_list.push_back(*next_merged); Chris@16: } while (!children.empty()); Chris@16: Chris@16: return merge_node_list(node_list); Chris@16: } Chris@16: Chris@16: node_pointer merge_first_pair(node_child_list & children) Chris@16: { Chris@16: BOOST_HEAP_ASSERT(!children.empty()); Chris@16: node_pointer first_child = static_cast(&children.front()); Chris@16: children.pop_front(); Chris@16: if (children.empty()) Chris@16: return first_child; Chris@16: Chris@16: node_pointer second_child = static_cast(&children.front()); Chris@16: children.pop_front(); Chris@16: Chris@16: return merge_nodes(first_child, second_child); Chris@16: } Chris@16: Chris@16: node_pointer merge_nodes(node_pointer node1, node_pointer node2) Chris@16: { Chris@16: if (super_t::operator()(node1->value, node2->value)) Chris@16: std::swap(node1, node2); Chris@16: Chris@16: node2->unlink(); Chris@16: node1->children.push_front(*node2); Chris@16: return node1; Chris@16: } Chris@16: Chris@16: node_pointer root; Chris@16: #endif Chris@16: }; Chris@16: Chris@16: Chris@16: } /* namespace heap */ Chris@16: } /* namespace boost */ Chris@16: Chris@16: #undef BOOST_HEAP_ASSERT Chris@16: #endif /* BOOST_HEAP_PAIRING_HEAP_HPP */