fibonacci_heap.hpp 24 KB

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