pairing_heap.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // boost heap: pairing heap
  2. //
  3. // Copyright (C) 2010 Tim Blechmann
  4. //
  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)
  8. #ifndef BOOST_HEAP_PAIRING_HEAP_HPP
  9. #define BOOST_HEAP_PAIRING_HEAP_HPP
  10. #include <algorithm>
  11. #include <utility>
  12. #include <vector>
  13. #include <boost/assert.hpp>
  14. #include <boost/heap/detail/heap_comparison.hpp>
  15. #include <boost/heap/detail/heap_node.hpp>
  16. #include <boost/heap/policies.hpp>
  17. #include <boost/heap/detail/stable_heap.hpp>
  18. #include <boost/heap/detail/tree_iterator.hpp>
  19. #include <boost/type_traits/integral_constant.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #ifndef BOOST_DOXYGEN_INVOKED
  24. #ifdef BOOST_HEAP_SANITYCHECKS
  25. #define BOOST_HEAP_ASSERT BOOST_ASSERT
  26. #else
  27. #define BOOST_HEAP_ASSERT(expression)
  28. #endif
  29. #endif
  30. namespace boost {
  31. namespace heap {
  32. namespace detail {
  33. typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
  34. boost::parameter::optional<tag::compare>,
  35. boost::parameter::optional<tag::stable>,
  36. boost::parameter::optional<tag::constant_time_size>,
  37. boost::parameter::optional<tag::stability_counter_type>
  38. > pairing_heap_signature;
  39. template <typename T, typename Parspec>
  40. struct make_pairing_heap_base
  41. {
  42. static const bool constant_time_size = parameter::binding<Parspec,
  43. tag::constant_time_size,
  44. boost::true_type
  45. >::type::value;
  46. typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::type base_type;
  47. typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::allocator_argument allocator_argument;
  48. typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::compare_argument compare_argument;
  49. typedef heap_node<typename base_type::internal_type, false> node_type;
  50. typedef typename boost::allocator_rebind<allocator_argument, node_type>::type allocator_type;
  51. struct type:
  52. base_type,
  53. allocator_type
  54. {
  55. type(compare_argument const & arg):
  56. base_type(arg)
  57. {}
  58. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  59. type(type const & rhs):
  60. base_type(rhs), allocator_type(rhs)
  61. {}
  62. type(type && rhs):
  63. base_type(std::move(static_cast<base_type&>(rhs))),
  64. allocator_type(std::move(static_cast<allocator_type&>(rhs)))
  65. {}
  66. type & operator=(type && rhs)
  67. {
  68. base_type::operator=(std::move(static_cast<base_type&>(rhs)));
  69. allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
  70. return *this;
  71. }
  72. type & operator=(type const & rhs)
  73. {
  74. base_type::operator=(static_cast<base_type const &>(rhs));
  75. allocator_type::operator=(static_cast<const allocator_type&>(rhs));
  76. return *this;
  77. }
  78. #endif
  79. };
  80. };
  81. }
  82. /**
  83. * \class pairing_heap
  84. * \brief pairing heap
  85. *
  86. * Pairing heaps are self-adjusting binary heaps. Although design and implementation are rather simple,
  87. * the complexity analysis is yet unsolved. For details, consult:
  88. *
  89. * Pettie, Seth (2005), "Towards a final analysis of pairing heaps",
  90. * Proc. 46th Annual IEEE Symposium on Foundations of Computer Science, pp. 174-183
  91. *
  92. * The template parameter T is the type to be managed by the container.
  93. * The user can specify additional options and if no options are provided default options are used.
  94. *
  95. * The container supports the following options:
  96. * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
  97. * - \c boost::heap::stable<>, defaults to \c stable<false>
  98. * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
  99. * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
  100. * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
  101. *
  102. *
  103. */
  104. #ifdef BOOST_DOXYGEN_INVOKED
  105. template<class T, class ...Options>
  106. #else
  107. template <typename T,
  108. class A0 = boost::parameter::void_,
  109. class A1 = boost::parameter::void_,
  110. class A2 = boost::parameter::void_,
  111. class A3 = boost::parameter::void_,
  112. class A4 = boost::parameter::void_
  113. >
  114. #endif
  115. class pairing_heap:
  116. private detail::make_pairing_heap_base<T,
  117. typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type
  118. >::type
  119. {
  120. typedef typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type bound_args;
  121. typedef detail::make_pairing_heap_base<T, bound_args> base_maker;
  122. typedef typename base_maker::type super_t;
  123. typedef typename super_t::internal_type internal_type;
  124. typedef typename super_t::size_holder_type size_holder;
  125. typedef typename base_maker::allocator_argument allocator_argument;
  126. private:
  127. template <typename Heap1, typename Heap2>
  128. friend struct heap_merge_emulate;
  129. #ifndef BOOST_DOXYGEN_INVOKED
  130. struct implementation_defined:
  131. detail::extract_allocator_types<typename base_maker::allocator_argument>
  132. {
  133. typedef T value_type;
  134. typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::size_type size_type;
  135. typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
  136. typedef typename base_maker::compare_argument value_compare;
  137. typedef typename base_maker::allocator_type allocator_type;
  138. typedef typename boost::allocator_pointer<allocator_type>::type node_pointer;
  139. typedef typename boost::allocator_const_pointer<allocator_type>::type const_node_pointer;
  140. typedef detail::heap_node_list node_list_type;
  141. typedef typename node_list_type::iterator node_list_iterator;
  142. typedef typename node_list_type::const_iterator node_list_const_iterator;
  143. typedef typename base_maker::node_type node;
  144. typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
  145. typedef typename super_t::internal_compare internal_compare;
  146. typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
  147. typedef detail::tree_iterator<node,
  148. const value_type,
  149. allocator_type,
  150. value_extractor,
  151. detail::pointer_to_reference<node>,
  152. false,
  153. false,
  154. value_compare
  155. > iterator;
  156. typedef iterator const_iterator;
  157. typedef detail::tree_iterator<node,
  158. const value_type,
  159. allocator_type,
  160. value_extractor,
  161. detail::pointer_to_reference<node>,
  162. false,
  163. true,
  164. value_compare
  165. > ordered_iterator;
  166. };
  167. typedef typename implementation_defined::node node;
  168. typedef typename implementation_defined::node_pointer node_pointer;
  169. typedef typename implementation_defined::node_list_type node_list_type;
  170. typedef typename implementation_defined::node_list_iterator node_list_iterator;
  171. typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator;
  172. typedef typename implementation_defined::internal_compare internal_compare;
  173. typedef boost::intrusive::list<detail::heap_node_base<true>,
  174. boost::intrusive::constant_time_size<false>
  175. > node_child_list;
  176. #endif
  177. public:
  178. typedef T value_type;
  179. typedef typename implementation_defined::size_type size_type;
  180. typedef typename implementation_defined::difference_type difference_type;
  181. typedef typename implementation_defined::value_compare value_compare;
  182. typedef typename implementation_defined::allocator_type allocator_type;
  183. typedef typename implementation_defined::reference reference;
  184. typedef typename implementation_defined::const_reference const_reference;
  185. typedef typename implementation_defined::pointer pointer;
  186. typedef typename implementation_defined::const_pointer const_pointer;
  187. /// \copydoc boost::heap::priority_queue::iterator
  188. typedef typename implementation_defined::iterator iterator;
  189. typedef typename implementation_defined::const_iterator const_iterator;
  190. typedef typename implementation_defined::ordered_iterator ordered_iterator;
  191. typedef typename implementation_defined::handle_type handle_type;
  192. static const bool constant_time_size = super_t::constant_time_size;
  193. static const bool has_ordered_iterators = true;
  194. static const bool is_mergable = true;
  195. static const bool is_stable = detail::extract_stable<bound_args>::value;
  196. static const bool has_reserve = false;
  197. /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
  198. explicit pairing_heap(value_compare const & cmp = value_compare()):
  199. super_t(cmp), root(NULL)
  200. {}
  201. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
  202. pairing_heap(pairing_heap const & rhs):
  203. super_t(rhs), root(NULL)
  204. {
  205. if (rhs.empty())
  206. return;
  207. clone_tree(rhs);
  208. size_holder::set_size(rhs.get_size());
  209. }
  210. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  211. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
  212. pairing_heap(pairing_heap && rhs):
  213. super_t(std::move(rhs)), root(rhs.root)
  214. {
  215. rhs.root = NULL;
  216. }
  217. /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
  218. pairing_heap & operator=(pairing_heap && rhs)
  219. {
  220. super_t::operator=(std::move(rhs));
  221. root = rhs.root;
  222. rhs.root = NULL;
  223. return *this;
  224. }
  225. #endif
  226. /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs)
  227. pairing_heap & operator=(pairing_heap const & rhs)
  228. {
  229. clear();
  230. size_holder::set_size(rhs.get_size());
  231. static_cast<super_t&>(*this) = rhs;
  232. clone_tree(rhs);
  233. return *this;
  234. }
  235. ~pairing_heap(void)
  236. {
  237. while (!empty())
  238. pop();
  239. }
  240. /// \copydoc boost::heap::priority_queue::empty
  241. bool empty(void) const
  242. {
  243. return root == NULL;
  244. }
  245. /// \copydoc boost::heap::binomial_heap::size
  246. size_type size(void) const
  247. {
  248. if (constant_time_size)
  249. return size_holder::get_size();
  250. if (root == NULL)
  251. return 0;
  252. else
  253. return detail::count_nodes(root);
  254. }
  255. /// \copydoc boost::heap::priority_queue::max_size
  256. size_type max_size(void) const
  257. {
  258. const allocator_type& alloc = *this;
  259. return boost::allocator_max_size(alloc);
  260. }
  261. /// \copydoc boost::heap::priority_queue::clear
  262. void clear(void)
  263. {
  264. if (empty())
  265. return;
  266. root->template clear_subtree<allocator_type>(*this);
  267. root->~node();
  268. allocator_type& alloc = *this;
  269. alloc.deallocate(root, 1);
  270. root = NULL;
  271. size_holder::set_size(0);
  272. }
  273. /// \copydoc boost::heap::priority_queue::get_allocator
  274. allocator_type get_allocator(void) const
  275. {
  276. return *this;
  277. }
  278. /// \copydoc boost::heap::priority_queue::swap
  279. void swap(pairing_heap & rhs)
  280. {
  281. super_t::swap(rhs);
  282. std::swap(root, rhs.root);
  283. }
  284. /// \copydoc boost::heap::priority_queue::top
  285. const_reference top(void) const
  286. {
  287. BOOST_ASSERT(!empty());
  288. return super_t::get_value(root->value);
  289. }
  290. /**
  291. * \b Effects: Adds a new element to the priority queue. Returns handle to element
  292. *
  293. * \cond
  294. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  295. * \endcond
  296. *
  297. * \b Complexity: 2**2*log(log(N)) (amortized).
  298. *
  299. * */
  300. handle_type push(value_type const & v)
  301. {
  302. size_holder::increment();
  303. allocator_type& alloc = *this;
  304. node_pointer n = alloc.allocate(1);
  305. new(n) node(super_t::make_node(v));
  306. merge_node(n);
  307. return handle_type(n);
  308. }
  309. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  310. /**
  311. * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element.
  312. *
  313. * \cond
  314. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  315. * \endcond
  316. *
  317. * \b Complexity: 2**2*log(log(N)) (amortized).
  318. *
  319. * */
  320. template <class... Args>
  321. handle_type emplace(Args&&... args)
  322. {
  323. size_holder::increment();
  324. allocator_type& alloc = *this;
  325. node_pointer n = alloc.allocate(1);
  326. new(n) node(super_t::make_node(std::forward<Args>(args)...));
  327. merge_node(n);
  328. return handle_type(n);
  329. }
  330. #endif
  331. /**
  332. * \b Effects: Removes the top element from the priority queue.
  333. *
  334. * \b Complexity: Logarithmic (amortized).
  335. *
  336. * */
  337. void pop(void)
  338. {
  339. BOOST_ASSERT(!empty());
  340. erase(handle_type(root));
  341. }
  342. /**
  343. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  344. *
  345. * \cond
  346. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  347. * \endcond
  348. *
  349. * \b Complexity: 2**2*log(log(N)) (amortized).
  350. *
  351. * */
  352. void update (handle_type handle, const_reference v)
  353. {
  354. handle.node_->value = super_t::make_node(v);
  355. update(handle);
  356. }
  357. /**
  358. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  359. *
  360. * \cond
  361. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  362. * \endcond
  363. *
  364. * \b Complexity: 2**2*log(log(N)) (amortized).
  365. *
  366. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  367. * */
  368. void update (handle_type handle)
  369. {
  370. node_pointer n = handle.node_;
  371. n->unlink();
  372. if (!n->children.empty())
  373. n = merge_nodes(n, merge_node_list(n->children));
  374. if (n != root)
  375. merge_node(n);
  376. }
  377. /**
  378. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  379. *
  380. * \cond
  381. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  382. * \endcond
  383. *
  384. * \b Complexity: 2**2*log(log(N)) (amortized).
  385. *
  386. * \b Note: The new value is expected to be greater than the current one
  387. * */
  388. void increase (handle_type handle, const_reference v)
  389. {
  390. update(handle, v);
  391. }
  392. /**
  393. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  394. *
  395. * \cond
  396. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  397. * \endcond
  398. *
  399. * \b Complexity: 2**2*log(log(N)) (amortized).
  400. *
  401. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  402. * */
  403. void increase (handle_type handle)
  404. {
  405. update(handle);
  406. }
  407. /**
  408. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  409. *
  410. * \cond
  411. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  412. * \endcond
  413. *
  414. * \b Complexity: 2**2*log(log(N)) (amortized).
  415. *
  416. * \b Note: The new value is expected to be less than the current one
  417. * */
  418. void decrease (handle_type handle, const_reference v)
  419. {
  420. update(handle, v);
  421. }
  422. /**
  423. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  424. *
  425. * \cond
  426. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  427. * \endcond
  428. *
  429. * \b Complexity: 2**2*log(log(N)) (amortized).
  430. *
  431. * \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!
  432. * */
  433. void decrease (handle_type handle)
  434. {
  435. update(handle);
  436. }
  437. /**
  438. * \b Effects: Removes the element handled by \c handle from the priority_queue.
  439. *
  440. * \cond
  441. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  442. * \endcond
  443. *
  444. * \b Complexity: 2**2*log(log(N)) (amortized).
  445. * */
  446. void erase(handle_type handle)
  447. {
  448. node_pointer n = handle.node_;
  449. if (n != root) {
  450. n->unlink();
  451. if (!n->children.empty())
  452. merge_node(merge_node_list(n->children));
  453. } else {
  454. if (!n->children.empty())
  455. root = merge_node_list(n->children);
  456. else
  457. root = NULL;
  458. }
  459. size_holder::decrement();
  460. n->~node();
  461. allocator_type& alloc = *this;
  462. alloc.deallocate(n, 1);
  463. }
  464. /// \copydoc boost::heap::priority_queue::begin
  465. iterator begin(void) const
  466. {
  467. return iterator(root, super_t::value_comp());
  468. }
  469. /// \copydoc boost::heap::priority_queue::end
  470. iterator end(void) const
  471. {
  472. return iterator(super_t::value_comp());
  473. }
  474. /// \copydoc boost::heap::fibonacci_heap::ordered_begin
  475. ordered_iterator ordered_begin(void) const
  476. {
  477. return ordered_iterator(root, super_t::value_comp());
  478. }
  479. /// \copydoc boost::heap::fibonacci_heap::ordered_begin
  480. ordered_iterator ordered_end(void) const
  481. {
  482. return ordered_iterator(NULL, super_t::value_comp());
  483. }
  484. /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator
  485. static handle_type s_handle_from_iterator(iterator const & it)
  486. {
  487. node * ptr = const_cast<node *>(it.get_node());
  488. return handle_type(ptr);
  489. }
  490. /**
  491. * \b Effects: Merge all elements from rhs into this
  492. *
  493. * \cond
  494. * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
  495. * \endcond
  496. *
  497. * \b Complexity: 2**2*log(log(N)) (amortized).
  498. *
  499. * */
  500. void merge(pairing_heap & rhs)
  501. {
  502. if (rhs.empty())
  503. return;
  504. merge_node(rhs.root);
  505. size_holder::add(rhs.get_size());
  506. rhs.set_size(0);
  507. rhs.root = NULL;
  508. super_t::set_stability_count((std::max)(super_t::get_stability_count(),
  509. rhs.get_stability_count()));
  510. rhs.set_stability_count(0);
  511. }
  512. /// \copydoc boost::heap::priority_queue::value_comp
  513. value_compare const & value_comp(void) const
  514. {
  515. return super_t::value_comp();
  516. }
  517. /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
  518. template <typename HeapType>
  519. bool operator<(HeapType const & rhs) const
  520. {
  521. return detail::heap_compare(*this, rhs);
  522. }
  523. /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
  524. template <typename HeapType>
  525. bool operator>(HeapType const & rhs) const
  526. {
  527. return detail::heap_compare(rhs, *this);
  528. }
  529. /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
  530. template <typename HeapType>
  531. bool operator>=(HeapType const & rhs) const
  532. {
  533. return !operator<(rhs);
  534. }
  535. /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
  536. template <typename HeapType>
  537. bool operator<=(HeapType const & rhs) const
  538. {
  539. return !operator>(rhs);
  540. }
  541. /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
  542. template <typename HeapType>
  543. bool operator==(HeapType const & rhs) const
  544. {
  545. return detail::heap_equality(*this, rhs);
  546. }
  547. /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
  548. template <typename HeapType>
  549. bool operator!=(HeapType const & rhs) const
  550. {
  551. return !(*this == rhs);
  552. }
  553. private:
  554. #if !defined(BOOST_DOXYGEN_INVOKED)
  555. void clone_tree(pairing_heap const & rhs)
  556. {
  557. BOOST_HEAP_ASSERT(root == NULL);
  558. if (rhs.empty())
  559. return;
  560. root = allocator_type::allocate(1);
  561. new(root) node(static_cast<node const &>(*rhs.root), static_cast<allocator_type&>(*this));
  562. }
  563. void merge_node(node_pointer other)
  564. {
  565. BOOST_HEAP_ASSERT(other);
  566. if (root != NULL)
  567. root = merge_nodes(root, other);
  568. else
  569. root = other;
  570. }
  571. node_pointer merge_node_list(node_child_list & children)
  572. {
  573. BOOST_HEAP_ASSERT(!children.empty());
  574. node_pointer merged = merge_first_pair(children);
  575. if (children.empty())
  576. return merged;
  577. node_child_list node_list;
  578. node_list.push_back(*merged);
  579. do {
  580. node_pointer next_merged = merge_first_pair(children);
  581. node_list.push_back(*next_merged);
  582. } while (!children.empty());
  583. return merge_node_list(node_list);
  584. }
  585. node_pointer merge_first_pair(node_child_list & children)
  586. {
  587. BOOST_HEAP_ASSERT(!children.empty());
  588. node_pointer first_child = static_cast<node_pointer>(&children.front());
  589. children.pop_front();
  590. if (children.empty())
  591. return first_child;
  592. node_pointer second_child = static_cast<node_pointer>(&children.front());
  593. children.pop_front();
  594. return merge_nodes(first_child, second_child);
  595. }
  596. node_pointer merge_nodes(node_pointer node1, node_pointer node2)
  597. {
  598. if (super_t::operator()(node1->value, node2->value))
  599. std::swap(node1, node2);
  600. node2->unlink();
  601. node1->children.push_front(*node2);
  602. return node1;
  603. }
  604. node_pointer root;
  605. #endif
  606. };
  607. } /* namespace heap */
  608. } /* namespace boost */
  609. #undef BOOST_HEAP_ASSERT
  610. #endif /* BOOST_HEAP_PAIRING_HEAP_HPP */