skew_heap.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. // boost heap: skew 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_SKEW_HEAP_HPP
  9. #define BOOST_HEAP_SKEW_HEAP_HPP
  10. #include <algorithm>
  11. #include <utility>
  12. #include <vector>
  13. #include <boost/assert.hpp>
  14. #include <boost/array.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. template <typename node_pointer, bool store_parent_pointer>
  33. struct parent_holder
  34. {
  35. parent_holder(void):
  36. parent_(NULL)
  37. {}
  38. void set_parent(node_pointer parent)
  39. {
  40. BOOST_HEAP_ASSERT(static_cast<node_pointer>(this) != parent);
  41. parent_ = parent;
  42. }
  43. node_pointer get_parent(void) const
  44. {
  45. return parent_;
  46. }
  47. node_pointer parent_;
  48. };
  49. template <typename node_pointer>
  50. struct parent_holder<node_pointer, false>
  51. {
  52. void set_parent(node_pointer parent)
  53. {}
  54. node_pointer get_parent(void) const
  55. {
  56. return NULL;
  57. }
  58. };
  59. template <typename value_type, bool store_parent_pointer>
  60. struct skew_heap_node:
  61. parent_holder<skew_heap_node<value_type, store_parent_pointer>*, store_parent_pointer>
  62. {
  63. typedef parent_holder<skew_heap_node<value_type, store_parent_pointer>*, store_parent_pointer> super_t;
  64. typedef boost::array<skew_heap_node*, 2> child_list_type;
  65. typedef typename child_list_type::iterator child_iterator;
  66. typedef typename child_list_type::const_iterator const_child_iterator;
  67. skew_heap_node(value_type const & v):
  68. value(v)
  69. {
  70. children.assign(0);
  71. }
  72. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  73. skew_heap_node(value_type && v):
  74. value(v)
  75. {
  76. children.assign(0);
  77. }
  78. #endif
  79. template <typename Alloc>
  80. skew_heap_node (skew_heap_node const & rhs, Alloc & allocator, skew_heap_node * parent):
  81. value(rhs.value)
  82. {
  83. super_t::set_parent(parent);
  84. node_cloner<skew_heap_node, skew_heap_node, Alloc> cloner(allocator);
  85. clone_child(0, rhs, cloner);
  86. clone_child(1, rhs, cloner);
  87. }
  88. template <typename Cloner>
  89. void clone_child(int index, skew_heap_node const & rhs, Cloner & cloner)
  90. {
  91. if (rhs.children[index])
  92. children[index] = cloner(*rhs.children[index], this);
  93. else
  94. children[index] = NULL;
  95. }
  96. template <typename Alloc>
  97. void clear_subtree(Alloc & alloc)
  98. {
  99. node_disposer<skew_heap_node, skew_heap_node, Alloc> disposer(alloc);
  100. dispose_child(children[0], disposer);
  101. dispose_child(children[1], disposer);
  102. }
  103. template <typename Disposer>
  104. void dispose_child(skew_heap_node * node, Disposer & disposer)
  105. {
  106. if (node)
  107. disposer(node);
  108. }
  109. std::size_t count_children(void) const
  110. {
  111. size_t ret = 1;
  112. if (children[0])
  113. ret += children[0]->count_children();
  114. if (children[1])
  115. ret += children[1]->count_children();
  116. return ret;
  117. }
  118. template <typename HeapBase>
  119. bool is_heap(typename HeapBase::value_compare const & cmp) const
  120. {
  121. for (const_child_iterator it = children.begin(); it != children.end(); ++it) {
  122. const skew_heap_node * child = *it;
  123. if (child == NULL)
  124. continue;
  125. if (store_parent_pointer)
  126. BOOST_HEAP_ASSERT(child->get_parent() == this);
  127. if (cmp(HeapBase::get_value(value), HeapBase::get_value(child->value)) ||
  128. !child->is_heap<HeapBase>(cmp))
  129. return false;
  130. }
  131. return true;
  132. }
  133. value_type value;
  134. boost::array<skew_heap_node*, 2> children;
  135. };
  136. typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
  137. boost::parameter::optional<tag::compare>,
  138. boost::parameter::optional<tag::stable>,
  139. boost::parameter::optional<tag::store_parent_pointer>,
  140. boost::parameter::optional<tag::stability_counter_type>,
  141. boost::parameter::optional<tag::constant_time_size>,
  142. boost::parameter::optional<tag::mutable_>
  143. > skew_heap_signature;
  144. template <typename T, typename BoundArgs>
  145. struct make_skew_heap_base
  146. {
  147. static const bool constant_time_size = parameter::binding<BoundArgs,
  148. tag::constant_time_size,
  149. boost::mpl::true_
  150. >::type::value;
  151. typedef typename make_heap_base<T, BoundArgs, constant_time_size>::type base_type;
  152. typedef typename make_heap_base<T, BoundArgs, constant_time_size>::allocator_argument allocator_argument;
  153. typedef typename make_heap_base<T, BoundArgs, constant_time_size>::compare_argument compare_argument;
  154. static const bool is_mutable = extract_mutable<BoundArgs>::value;
  155. static const bool store_parent_pointer = parameter::binding<BoundArgs,
  156. tag::store_parent_pointer,
  157. boost::mpl::false_>::type::value || is_mutable;
  158. typedef skew_heap_node<typename base_type::internal_type, store_parent_pointer> node_type;
  159. typedef typename allocator_argument::template rebind<node_type>::other allocator_type;
  160. struct type:
  161. base_type,
  162. allocator_type
  163. {
  164. type(compare_argument const & arg):
  165. base_type(arg)
  166. {}
  167. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  168. type(type && rhs):
  169. base_type(std::move(static_cast<base_type&>(rhs))),
  170. allocator_type(std::move(static_cast<allocator_type&>(rhs)))
  171. {}
  172. type(type const & rhs):
  173. base_type(rhs),
  174. allocator_type(rhs)
  175. {}
  176. type & operator=(type && rhs)
  177. {
  178. base_type::operator=(std::move(static_cast<base_type&>(rhs)));
  179. allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
  180. return *this;
  181. }
  182. type & operator=(type const & rhs)
  183. {
  184. base_type::operator=(static_cast<base_type const &>(rhs));
  185. allocator_type::operator=(static_cast<allocator_type const &>(rhs));
  186. return *this;
  187. }
  188. #endif
  189. };
  190. };
  191. } /* namespace detail */
  192. /**
  193. * \class skew_heap
  194. * \brief skew heap
  195. *
  196. *
  197. * The template parameter T is the type to be managed by the container.
  198. * The user can specify additional options and if no options are provided default options are used.
  199. *
  200. * The container supports the following options:
  201. * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
  202. * - \c boost::heap::stable<>, defaults to \c stable<false>
  203. * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
  204. * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
  205. * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
  206. * - \c boost::heap::store_parent_pointer<>, defaults to \c store_parent_pointer<true>. Maintaining a parent pointer adds some
  207. * maintenance and size overhead, but iterating a heap is more efficient.
  208. * - \c boost::heap::mutable<>, defaults to \c mutable<false>.
  209. *
  210. */
  211. #ifdef BOOST_DOXYGEN_INVOKED
  212. template<class T, class ...Options>
  213. #else
  214. template <typename T,
  215. class A0 = boost::parameter::void_,
  216. class A1 = boost::parameter::void_,
  217. class A2 = boost::parameter::void_,
  218. class A3 = boost::parameter::void_,
  219. class A4 = boost::parameter::void_,
  220. class A5 = boost::parameter::void_,
  221. class A6 = boost::parameter::void_
  222. >
  223. #endif
  224. class skew_heap:
  225. private detail::make_skew_heap_base<T,
  226. typename detail::skew_heap_signature::bind<A0, A1, A2, A3, A4, A5, A6>::type
  227. >::type
  228. {
  229. typedef typename detail::skew_heap_signature::bind<A0, A1, A2, A3, A4, A5, A6>::type bound_args;
  230. typedef detail::make_skew_heap_base<T, bound_args> base_maker;
  231. typedef typename base_maker::type super_t;
  232. typedef typename super_t::internal_type internal_type;
  233. typedef typename super_t::size_holder_type size_holder;
  234. typedef typename base_maker::allocator_argument allocator_argument;
  235. static const bool store_parent_pointer = base_maker::store_parent_pointer;
  236. template <typename Heap1, typename Heap2>
  237. friend struct heap_merge_emulate;
  238. struct implementation_defined:
  239. detail::extract_allocator_types<typename base_maker::allocator_argument>
  240. {
  241. typedef T value_type;
  242. typedef typename base_maker::compare_argument value_compare;
  243. typedef typename base_maker::allocator_type allocator_type;
  244. typedef typename base_maker::node_type node;
  245. typedef typename allocator_type::pointer node_pointer;
  246. typedef typename allocator_type::const_pointer const_node_pointer;
  247. typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
  248. typedef boost::array<node_pointer, 2> child_list_type;
  249. typedef typename child_list_type::iterator child_list_iterator;
  250. typedef typename boost::mpl::if_c<false,
  251. detail::recursive_tree_iterator<node,
  252. child_list_iterator,
  253. const value_type,
  254. value_extractor,
  255. detail::list_iterator_converter<node,
  256. child_list_type
  257. >
  258. >,
  259. detail::tree_iterator<node,
  260. const value_type,
  261. allocator_type,
  262. value_extractor,
  263. detail::dereferencer<node>,
  264. true,
  265. false,
  266. value_compare
  267. >
  268. >::type iterator;
  269. typedef iterator const_iterator;
  270. typedef detail::tree_iterator<node,
  271. const value_type,
  272. allocator_type,
  273. value_extractor,
  274. detail::dereferencer<node>,
  275. true,
  276. true,
  277. value_compare
  278. > ordered_iterator;
  279. typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
  280. typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
  281. };
  282. typedef typename implementation_defined::value_extractor value_extractor;
  283. typedef typename implementation_defined::node node;
  284. typedef typename implementation_defined::node_pointer node_pointer;
  285. public:
  286. typedef T value_type;
  287. typedef typename implementation_defined::size_type size_type;
  288. typedef typename implementation_defined::difference_type difference_type;
  289. typedef typename implementation_defined::value_compare value_compare;
  290. typedef typename implementation_defined::allocator_type allocator_type;
  291. typedef typename implementation_defined::reference reference;
  292. typedef typename implementation_defined::const_reference const_reference;
  293. typedef typename implementation_defined::pointer pointer;
  294. typedef typename implementation_defined::const_pointer const_pointer;
  295. /// \copydoc boost::heap::priority_queue::iterator
  296. typedef typename implementation_defined::iterator iterator;
  297. typedef typename implementation_defined::const_iterator const_iterator;
  298. typedef typename implementation_defined::ordered_iterator ordered_iterator;
  299. static const bool constant_time_size = super_t::constant_time_size;
  300. static const bool has_ordered_iterators = true;
  301. static const bool is_mergable = true;
  302. static const bool is_stable = detail::extract_stable<bound_args>::value;
  303. static const bool has_reserve = false;
  304. static const bool is_mutable = detail::extract_mutable<bound_args>::value;
  305. typedef typename mpl::if_c<is_mutable, typename implementation_defined::handle_type, void*>::type handle_type;
  306. /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
  307. explicit skew_heap(value_compare const & cmp = value_compare()):
  308. super_t(cmp), root(NULL)
  309. {}
  310. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
  311. skew_heap(skew_heap const & rhs):
  312. super_t(rhs), root(0)
  313. {
  314. if (rhs.empty())
  315. return;
  316. clone_tree(rhs);
  317. size_holder::set_size(rhs.get_size());
  318. }
  319. /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs)
  320. skew_heap & operator=(skew_heap const & rhs)
  321. {
  322. clear();
  323. size_holder::set_size(rhs.get_size());
  324. static_cast<super_t&>(*this) = rhs;
  325. clone_tree(rhs);
  326. return *this;
  327. }
  328. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  329. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
  330. skew_heap(skew_heap && rhs):
  331. super_t(std::move(rhs)), root(rhs.root)
  332. {
  333. rhs.root = NULL;
  334. }
  335. /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
  336. skew_heap & operator=(skew_heap && rhs)
  337. {
  338. super_t::operator=(std::move(rhs));
  339. root = rhs.root;
  340. rhs.root = NULL;
  341. return *this;
  342. }
  343. #endif
  344. ~skew_heap(void)
  345. {
  346. clear();
  347. }
  348. /**
  349. * \b Effects: Adds a new element to the priority queue.
  350. *
  351. * \b Complexity: Logarithmic (amortized).
  352. *
  353. * */
  354. typename mpl::if_c<is_mutable, handle_type, void>::type push(value_type const & v)
  355. {
  356. typedef typename mpl::if_c<is_mutable, push_handle, push_void>::type push_helper;
  357. return push_helper::push(this, v);
  358. }
  359. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  360. /**
  361. * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place.
  362. *
  363. * \b Complexity: Logarithmic (amortized).
  364. *
  365. * */
  366. template <typename... Args>
  367. typename mpl::if_c<is_mutable, handle_type, void>::type emplace(Args&&... args)
  368. {
  369. typedef typename mpl::if_c<is_mutable, push_handle, push_void>::type push_helper;
  370. return push_helper::emplace(this, std::forward<Args>(args)...);
  371. }
  372. #endif
  373. /// \copydoc boost::heap::priority_queue::empty
  374. bool empty(void) const
  375. {
  376. return root == NULL;
  377. }
  378. /// \copydoc boost::heap::binomial_heap::size
  379. size_type size(void) const
  380. {
  381. if (constant_time_size)
  382. return size_holder::get_size();
  383. if (root == NULL)
  384. return 0;
  385. else
  386. return root->count_children();
  387. }
  388. /// \copydoc boost::heap::priority_queue::max_size
  389. size_type max_size(void) const
  390. {
  391. return allocator_type::max_size();
  392. }
  393. /// \copydoc boost::heap::priority_queue::clear
  394. void clear(void)
  395. {
  396. if (empty())
  397. return;
  398. root->template clear_subtree<allocator_type>(*this);
  399. root->~node();
  400. allocator_type::deallocate(root, 1);
  401. root = NULL;
  402. size_holder::set_size(0);
  403. }
  404. /// \copydoc boost::heap::priority_queue::get_allocator
  405. allocator_type get_allocator(void) const
  406. {
  407. return *this;
  408. }
  409. /// \copydoc boost::heap::priority_queue::swap
  410. void swap(skew_heap & rhs)
  411. {
  412. super_t::swap(rhs);
  413. std::swap(root, rhs.root);
  414. }
  415. /// \copydoc boost::heap::priority_queue::top
  416. const_reference top(void) const
  417. {
  418. BOOST_ASSERT(!empty());
  419. return super_t::get_value(root->value);
  420. }
  421. /**
  422. * \b Effects: Removes the top element from the priority queue.
  423. *
  424. * \b Complexity: Logarithmic (amortized).
  425. *
  426. * */
  427. void pop(void)
  428. {
  429. BOOST_ASSERT(!empty());
  430. node_pointer top = root;
  431. root = merge_children(root);
  432. size_holder::decrement();
  433. if (root)
  434. BOOST_HEAP_ASSERT(root->get_parent() == NULL);
  435. else
  436. BOOST_HEAP_ASSERT(size_holder::get_size() == 0);
  437. top->~node();
  438. allocator_type::deallocate(top, 1);
  439. sanity_check();
  440. }
  441. /// \copydoc boost::heap::priority_queue::begin
  442. iterator begin(void) const
  443. {
  444. return iterator(root, super_t::value_comp());
  445. }
  446. /// \copydoc boost::heap::priority_queue::end
  447. iterator end(void) const
  448. {
  449. return iterator();
  450. }
  451. /// \copydoc boost::heap::fibonacci_heap::ordered_begin
  452. ordered_iterator ordered_begin(void) const
  453. {
  454. return ordered_iterator(root, super_t::value_comp());
  455. }
  456. /// \copydoc boost::heap::fibonacci_heap::ordered_begin
  457. ordered_iterator ordered_end(void) const
  458. {
  459. return ordered_iterator(0, super_t::value_comp());
  460. }
  461. /**
  462. * \b Effects: Merge all elements from rhs into this
  463. *
  464. * \b Complexity: Logarithmic (amortized).
  465. *
  466. * */
  467. void merge(skew_heap & rhs)
  468. {
  469. if (rhs.empty())
  470. return;
  471. merge_node(rhs.root);
  472. size_holder::add(rhs.get_size());
  473. rhs.set_size(0);
  474. rhs.root = NULL;
  475. sanity_check();
  476. super_t::set_stability_count((std::max)(super_t::get_stability_count(),
  477. rhs.get_stability_count()));
  478. rhs.set_stability_count(0);
  479. }
  480. /// \copydoc boost::heap::priority_queue::value_comp
  481. value_compare const & value_comp(void) const
  482. {
  483. return super_t::value_comp();
  484. }
  485. /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
  486. template <typename HeapType>
  487. bool operator<(HeapType const & rhs) const
  488. {
  489. return detail::heap_compare(*this, rhs);
  490. }
  491. /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
  492. template <typename HeapType>
  493. bool operator>(HeapType const & rhs) const
  494. {
  495. return detail::heap_compare(rhs, *this);
  496. }
  497. /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
  498. template <typename HeapType>
  499. bool operator>=(HeapType const & rhs) const
  500. {
  501. return !operator<(rhs);
  502. }
  503. /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
  504. template <typename HeapType>
  505. bool operator<=(HeapType const & rhs) const
  506. {
  507. return !operator>(rhs);
  508. }
  509. /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
  510. template <typename HeapType>
  511. bool operator==(HeapType const & rhs) const
  512. {
  513. return detail::heap_equality(*this, rhs);
  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 !(*this == rhs);
  520. }
  521. /// \copydoc boost::heap::d_ary_heap::s_handle_from_iterator
  522. static handle_type s_handle_from_iterator(iterator const & it)
  523. {
  524. node * ptr = const_cast<node *>(it.get_node());
  525. return handle_type(ptr);
  526. }
  527. /**
  528. * \b Effects: Removes the element handled by \c handle from the priority_queue.
  529. *
  530. * \b Complexity: Logarithmic (amortized).
  531. * */
  532. void erase (handle_type object)
  533. {
  534. BOOST_STATIC_ASSERT(is_mutable);
  535. node_pointer this_node = object.node_;
  536. unlink_node(this_node);
  537. size_holder::decrement();
  538. sanity_check();
  539. this_node->~node();
  540. allocator_type::deallocate(this_node, 1);
  541. }
  542. /**
  543. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  544. *
  545. * \b Complexity: Logarithmic (amortized).
  546. *
  547. * */
  548. void update (handle_type handle, const_reference v)
  549. {
  550. BOOST_STATIC_ASSERT(is_mutable);
  551. if (super_t::operator()(super_t::get_value(handle.node_->value), v))
  552. increase(handle, v);
  553. else
  554. decrease(handle, v);
  555. }
  556. /**
  557. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  558. *
  559. * \b Complexity: Logarithmic (amortized).
  560. *
  561. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  562. * */
  563. void update (handle_type handle)
  564. {
  565. BOOST_STATIC_ASSERT(is_mutable);
  566. node_pointer this_node = handle.node_;
  567. if (this_node->get_parent()) {
  568. if (super_t::operator()(super_t::get_value(this_node->get_parent()->value),
  569. super_t::get_value(this_node->value)))
  570. increase(handle);
  571. else
  572. decrease(handle);
  573. }
  574. else
  575. decrease(handle);
  576. }
  577. /**
  578. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  579. *
  580. * \b Complexity: Logarithmic (amortized).
  581. *
  582. * \b Note: The new value is expected to be greater than the current one
  583. * */
  584. void increase (handle_type handle, const_reference v)
  585. {
  586. BOOST_STATIC_ASSERT(is_mutable);
  587. handle.node_->value = super_t::make_node(v);
  588. increase(handle);
  589. }
  590. /**
  591. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  592. *
  593. * \b Complexity: Logarithmic (amortized).
  594. *
  595. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  596. * */
  597. void increase (handle_type handle)
  598. {
  599. BOOST_STATIC_ASSERT(is_mutable);
  600. node_pointer this_node = handle.node_;
  601. if (this_node == root)
  602. return;
  603. node_pointer parent = this_node->get_parent();
  604. if (this_node == parent->children[0])
  605. parent->children[0] = NULL;
  606. else
  607. parent->children[1] = NULL;
  608. this_node->set_parent(NULL);
  609. merge_node(this_node);
  610. }
  611. /**
  612. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  613. *
  614. * \b Complexity: Logarithmic (amortized).
  615. *
  616. * \b Note: The new value is expected to be less than the current one
  617. * */
  618. void decrease (handle_type handle, const_reference v)
  619. {
  620. BOOST_STATIC_ASSERT(is_mutable);
  621. handle.node_->value = super_t::make_node(v);
  622. decrease(handle);
  623. }
  624. /**
  625. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  626. *
  627. * \b Complexity: Logarithmic (amortized).
  628. *
  629. * \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!
  630. * */
  631. void decrease (handle_type handle)
  632. {
  633. BOOST_STATIC_ASSERT(is_mutable);
  634. node_pointer this_node = handle.node_;
  635. unlink_node(this_node);
  636. this_node->children.assign(0);
  637. this_node->set_parent(NULL);
  638. merge_node(this_node);
  639. }
  640. private:
  641. #if !defined(BOOST_DOXYGEN_INVOKED)
  642. struct push_void
  643. {
  644. static void push(skew_heap * self, const_reference v)
  645. {
  646. self->push_internal(v);
  647. }
  648. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  649. template <class... Args>
  650. static void emplace(skew_heap * self, Args&&... args)
  651. {
  652. self->emplace_internal(std::forward<Args>(args)...);
  653. }
  654. #endif
  655. };
  656. struct push_handle
  657. {
  658. static handle_type push(skew_heap * self, const_reference v)
  659. {
  660. return handle_type(self->push_internal(v));
  661. }
  662. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  663. template <class... Args>
  664. static handle_type emplace(skew_heap * self, Args&&... args)
  665. {
  666. return handle_type(self->emplace_internal(std::forward<Args>(args)...));
  667. }
  668. #endif
  669. };
  670. node_pointer push_internal(const_reference v)
  671. {
  672. size_holder::increment();
  673. node_pointer n = super_t::allocate(1);
  674. new(n) node(super_t::make_node(v));
  675. merge_node(n);
  676. return n;
  677. }
  678. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  679. template <class... Args>
  680. node_pointer emplace_internal(Args&&... args)
  681. {
  682. size_holder::increment();
  683. node_pointer n = super_t::allocate(1);
  684. new(n) node(super_t::make_node(std::forward<Args>(args)...));
  685. merge_node(n);
  686. return n;
  687. }
  688. #endif
  689. void unlink_node(node_pointer node)
  690. {
  691. node_pointer parent = node->get_parent();
  692. node_pointer merged_children = merge_children(node);
  693. if (parent) {
  694. if (node == parent->children[0])
  695. parent->children[0] = merged_children;
  696. else
  697. parent->children[1] = merged_children;
  698. }
  699. else
  700. root = merged_children;
  701. }
  702. void clone_tree(skew_heap const & rhs)
  703. {
  704. BOOST_HEAP_ASSERT(root == NULL);
  705. if (rhs.empty())
  706. return;
  707. root = allocator_type::allocate(1);
  708. new(root) node(*rhs.root, static_cast<allocator_type&>(*this), NULL);
  709. }
  710. void merge_node(node_pointer other)
  711. {
  712. BOOST_HEAP_ASSERT(other);
  713. if (root != NULL)
  714. root = merge_nodes(root, other, NULL);
  715. else
  716. root = other;
  717. }
  718. node_pointer merge_nodes(node_pointer node1, node_pointer node2, node_pointer new_parent)
  719. {
  720. if (node1 == NULL) {
  721. if (node2)
  722. node2->set_parent(new_parent);
  723. return node2;
  724. }
  725. if (node2 == NULL) {
  726. node1->set_parent(new_parent);
  727. return node1;
  728. }
  729. node_pointer merged = merge_nodes_recursive(node1, node2, new_parent);
  730. return merged;
  731. }
  732. node_pointer merge_children(node_pointer node)
  733. {
  734. node_pointer parent = node->get_parent();
  735. node_pointer merged_children = merge_nodes(node->children[0], node->children[1], parent);
  736. return merged_children;
  737. }
  738. node_pointer merge_nodes_recursive(node_pointer node1, node_pointer node2, node_pointer new_parent)
  739. {
  740. if (super_t::operator()(node1->value, node2->value))
  741. std::swap(node1, node2);
  742. node * parent = node1;
  743. node * child = node2;
  744. if (parent->children[1]) {
  745. node * merged = merge_nodes(parent->children[1], child, parent);
  746. parent->children[1] = merged;
  747. merged->set_parent(parent);
  748. } else {
  749. parent->children[1] = child;
  750. child->set_parent(parent);
  751. }
  752. std::swap(parent->children[0], parent->children[1]);
  753. parent->set_parent(new_parent);
  754. return parent;
  755. }
  756. void sanity_check(void)
  757. {
  758. #ifdef BOOST_HEAP_SANITYCHECKS
  759. if (root)
  760. BOOST_HEAP_ASSERT( root->template is_heap<super_t>(super_t::value_comp()) );
  761. if (constant_time_size) {
  762. size_type stored_size = size_holder::get_size();
  763. size_type counted_size;
  764. if (root == NULL)
  765. counted_size = 0;
  766. else
  767. counted_size = root->count_children();
  768. BOOST_HEAP_ASSERT(counted_size == stored_size);
  769. }
  770. #endif
  771. }
  772. node_pointer root;
  773. #endif
  774. };
  775. } /* namespace heap */
  776. } /* namespace boost */
  777. #undef BOOST_HEAP_ASSERT
  778. #endif /* BOOST_HEAP_SKEW_HEAP_HPP */