skew_heap.hpp 31 KB

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