fibonacci_heap.hpp 24 KB

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