fibonacci_heap.hpp 25 KB

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