skew_heap.hpp 29 KB

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