skew_heap.hpp 29 KB

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