fibonacci_heap.hpp 25 KB

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