fibonacci_heap.hpp 24 KB

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