d_ary_heap.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // // boost heap: d-ary heap as containter adaptor
  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_D_ARY_HEAP_HPP
  9. #define BOOST_HEAP_D_ARY_HEAP_HPP
  10. #include <algorithm>
  11. #include <utility>
  12. #include <vector>
  13. #include <boost/assert.hpp>
  14. #include <boost/mem_fn.hpp>
  15. #include <boost/heap/detail/heap_comparison.hpp>
  16. #include <boost/heap/detail/ordered_adaptor_iterator.hpp>
  17. #include <boost/heap/detail/stable_heap.hpp>
  18. #include <boost/heap/detail/mutable_heap.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. struct nop_index_updater
  30. {
  31. template <typename T>
  32. static void run(T &, std::size_t)
  33. {}
  34. };
  35. typedef parameter::parameters<boost::parameter::required<tag::arity>,
  36. boost::parameter::optional<tag::allocator>,
  37. boost::parameter::optional<tag::compare>,
  38. boost::parameter::optional<tag::stable>,
  39. boost::parameter::optional<tag::stability_counter_type>,
  40. boost::parameter::optional<tag::constant_time_size>
  41. > d_ary_heap_signature;
  42. /* base class for d-ary heap */
  43. template <typename T,
  44. class BoundArgs,
  45. class IndexUpdater>
  46. class d_ary_heap:
  47. private make_heap_base<T, BoundArgs, false>::type
  48. {
  49. typedef make_heap_base<T, BoundArgs, false> heap_base_maker;
  50. typedef typename heap_base_maker::type super_t;
  51. typedef typename super_t::internal_type internal_type;
  52. typedef typename heap_base_maker::allocator_argument::template rebind<internal_type>::other internal_type_allocator;
  53. typedef std::vector<internal_type, internal_type_allocator> container_type;
  54. typedef typename container_type::const_iterator container_iterator;
  55. typedef IndexUpdater index_updater;
  56. container_type q_;
  57. static const unsigned int D = parameter::binding<BoundArgs, tag::arity>::type::value;
  58. template <typename Heap1, typename Heap2>
  59. friend struct heap_merge_emulate;
  60. struct implementation_defined:
  61. extract_allocator_types<typename heap_base_maker::allocator_argument>
  62. {
  63. typedef T value_type;
  64. typedef typename detail::extract_allocator_types<typename heap_base_maker::allocator_argument>::size_type size_type;
  65. typedef typename heap_base_maker::compare_argument value_compare;
  66. typedef typename heap_base_maker::allocator_argument allocator_type;
  67. struct ordered_iterator_dispatcher
  68. {
  69. static size_type max_index(const d_ary_heap * heap)
  70. {
  71. return heap->q_.size() - 1;
  72. }
  73. static bool is_leaf(const d_ary_heap * heap, size_type index)
  74. {
  75. return !heap->not_leaf(index);
  76. }
  77. static std::pair<size_type, size_type> get_child_nodes(const d_ary_heap * heap, size_type index)
  78. {
  79. BOOST_HEAP_ASSERT(!is_leaf(heap, index));
  80. return std::make_pair(d_ary_heap::first_child_index(index),
  81. heap->last_child_index(index));
  82. }
  83. static internal_type const & get_internal_value(const d_ary_heap * heap, size_type index)
  84. {
  85. return heap->q_[index];
  86. }
  87. static value_type const & get_value(internal_type const & arg)
  88. {
  89. return super_t::get_value(arg);
  90. }
  91. };
  92. typedef detail::ordered_adaptor_iterator<const value_type,
  93. internal_type,
  94. d_ary_heap,
  95. allocator_type,
  96. typename super_t::internal_compare,
  97. ordered_iterator_dispatcher
  98. > ordered_iterator;
  99. typedef detail::stable_heap_iterator<const value_type, container_iterator, super_t> iterator;
  100. typedef iterator const_iterator;
  101. typedef void * handle_type;
  102. };
  103. typedef typename implementation_defined::ordered_iterator_dispatcher ordered_iterator_dispatcher;
  104. public:
  105. typedef T value_type;
  106. typedef typename implementation_defined::size_type size_type;
  107. typedef typename implementation_defined::difference_type difference_type;
  108. typedef typename implementation_defined::value_compare value_compare;
  109. typedef typename implementation_defined::allocator_type allocator_type;
  110. typedef typename implementation_defined::reference reference;
  111. typedef typename implementation_defined::const_reference const_reference;
  112. typedef typename implementation_defined::pointer pointer;
  113. typedef typename implementation_defined::const_pointer const_pointer;
  114. typedef typename implementation_defined::iterator iterator;
  115. typedef typename implementation_defined::const_iterator const_iterator;
  116. typedef typename implementation_defined::ordered_iterator ordered_iterator;
  117. typedef typename implementation_defined::handle_type handle_type;
  118. static const bool is_stable = extract_stable<BoundArgs>::value;
  119. explicit d_ary_heap(value_compare const & cmp = value_compare()):
  120. super_t(cmp)
  121. {}
  122. d_ary_heap(d_ary_heap const & rhs):
  123. super_t(rhs), q_(rhs.q_)
  124. {}
  125. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  126. d_ary_heap(d_ary_heap && rhs):
  127. super_t(std::move(rhs)), q_(std::move(rhs.q_))
  128. {}
  129. d_ary_heap & operator=(d_ary_heap && rhs)
  130. {
  131. super_t::operator=(std::move(rhs));
  132. q_ = std::move(rhs.q_);
  133. return *this;
  134. }
  135. #endif
  136. d_ary_heap & operator=(d_ary_heap const & rhs)
  137. {
  138. static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
  139. q_ = rhs.q_;
  140. return *this;
  141. }
  142. bool empty(void) const
  143. {
  144. return q_.empty();
  145. }
  146. size_type size(void) const
  147. {
  148. return q_.size();
  149. }
  150. size_type max_size(void) const
  151. {
  152. return q_.max_size();
  153. }
  154. void clear(void)
  155. {
  156. q_.clear();
  157. }
  158. allocator_type get_allocator(void) const
  159. {
  160. return q_.get_allocator();
  161. }
  162. value_type const & top(void) const
  163. {
  164. BOOST_ASSERT(!empty());
  165. return super_t::get_value(q_.front());
  166. }
  167. void push(value_type const & v)
  168. {
  169. q_.push_back(super_t::make_node(v));
  170. reset_index(size() - 1, size() - 1);
  171. siftup(q_.size() - 1);
  172. }
  173. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  174. template <class... Args>
  175. void emplace(Args&&... args)
  176. {
  177. q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
  178. reset_index(size() - 1, size() - 1);
  179. siftup(q_.size() - 1);
  180. }
  181. #endif
  182. void pop(void)
  183. {
  184. BOOST_ASSERT(!empty());
  185. std::swap(q_.front(), q_.back());
  186. q_.pop_back();
  187. if (q_.empty())
  188. return;
  189. reset_index(0, 0);
  190. siftdown(0);
  191. }
  192. void swap(d_ary_heap & rhs)
  193. {
  194. super_t::swap(rhs);
  195. q_.swap(rhs.q_);
  196. }
  197. iterator begin(void) const
  198. {
  199. return iterator(q_.begin());
  200. }
  201. iterator end(void) const
  202. {
  203. return iterator(q_.end());
  204. }
  205. ordered_iterator ordered_begin(void) const
  206. {
  207. return ordered_iterator(0, this, super_t::get_internal_cmp());
  208. }
  209. ordered_iterator ordered_end(void) const
  210. {
  211. return ordered_iterator(size(), this, super_t::get_internal_cmp());
  212. }
  213. void reserve (size_type element_count)
  214. {
  215. q_.reserve(element_count);
  216. }
  217. value_compare const & value_comp(void) const
  218. {
  219. return super_t::value_comp();
  220. }
  221. private:
  222. void reset_index(size_type index, size_type new_index)
  223. {
  224. BOOST_HEAP_ASSERT(index < q_.size());
  225. index_updater::run(q_[index], new_index);
  226. }
  227. void siftdown(size_type index)
  228. {
  229. while (not_leaf(index)) {
  230. size_type max_child_index = top_child_index(index);
  231. if (!super_t::operator()(q_[max_child_index], q_[index])) {
  232. reset_index(index, max_child_index);
  233. reset_index(max_child_index, index);
  234. std::swap(q_[max_child_index], q_[index]);
  235. index = max_child_index;
  236. }
  237. else
  238. return;
  239. }
  240. }
  241. /* returns new index */
  242. void siftup(size_type index)
  243. {
  244. while (index != 0) {
  245. size_type parent = parent_index(index);
  246. if (super_t::operator()(q_[parent], q_[index])) {
  247. reset_index(index, parent);
  248. reset_index(parent, index);
  249. std::swap(q_[parent], q_[index]);
  250. index = parent;
  251. }
  252. else
  253. return;
  254. }
  255. }
  256. bool not_leaf(size_type index) const
  257. {
  258. const size_t first_child = first_child_index(index);
  259. return first_child < q_.size();
  260. }
  261. size_type top_child_index(size_type index) const
  262. {
  263. // invariant: index is not a leaf, so the iterator range is not empty
  264. const size_t first_index = first_child_index(index);
  265. typedef typename container_type::const_iterator container_iterator;
  266. const container_iterator first_child = q_.begin() + first_index;
  267. const container_iterator end = q_.end();
  268. const size_type max_elements = std::distance(first_child, end);
  269. const container_iterator last_child = (max_elements > D) ? first_child + D
  270. : end;
  271. const container_iterator min_element = std::max_element(first_child, last_child, static_cast<super_t const &>(*this));
  272. return min_element - q_.begin();
  273. }
  274. static size_type parent_index(size_type index)
  275. {
  276. return (index - 1) / D;
  277. }
  278. static size_type first_child_index(size_type index)
  279. {
  280. return index * D + 1;
  281. }
  282. size_type last_child_index(size_type index) const
  283. {
  284. const size_t first_index = first_child_index(index);
  285. const size_type last_index = (std::min)(first_index + D - 1, size() - 1);
  286. return last_index;
  287. }
  288. template<typename U,
  289. typename V,
  290. typename W,
  291. typename X>
  292. struct rebind {
  293. typedef d_ary_heap<U, typename d_ary_heap_signature::bind<boost::heap::stable<heap_base_maker::is_stable>,
  294. boost::heap::stability_counter_type<typename heap_base_maker::stability_counter_type>,
  295. boost::heap::arity<D>,
  296. boost::heap::compare<V>,
  297. boost::heap::allocator<W>
  298. >::type,
  299. X
  300. > other;
  301. };
  302. template <class U> friend class priority_queue_mutable_wrapper;
  303. void update(size_type index)
  304. {
  305. if (index == 0) {
  306. siftdown(index);
  307. return;
  308. }
  309. size_type parent = parent_index(index);
  310. if (super_t::operator()(q_[parent], q_[index]))
  311. siftup(index);
  312. else
  313. siftdown(index);
  314. }
  315. void erase(size_type index)
  316. {
  317. while (index != 0)
  318. {
  319. size_type parent = parent_index(index);
  320. reset_index(index, parent);
  321. reset_index(parent, index);
  322. std::swap(q_[parent], q_[index]);
  323. index = parent;
  324. }
  325. pop();
  326. }
  327. void increase(size_type index)
  328. {
  329. siftup(index);
  330. }
  331. void decrease(size_type index)
  332. {
  333. siftdown(index);
  334. }
  335. };
  336. template <typename T, typename BoundArgs>
  337. struct select_dary_heap
  338. {
  339. static const bool is_mutable = extract_mutable<BoundArgs>::value;
  340. typedef typename mpl::if_c< is_mutable,
  341. priority_queue_mutable_wrapper<d_ary_heap<T, BoundArgs, nop_index_updater > >,
  342. d_ary_heap<T, BoundArgs, nop_index_updater >
  343. >::type type;
  344. };
  345. } /* namespace detail */
  346. /**
  347. * \class d_ary_heap
  348. * \brief d-ary heap class
  349. *
  350. * This class implements an immutable priority queue. Internally, the d-ary heap is represented
  351. * as dynamically sized array (std::vector), that directly stores the values.
  352. *
  353. * The template parameter T is the type to be managed by the container.
  354. * The user can specify additional options and if no options are provided default options are used.
  355. *
  356. * The container supports the following options:
  357. * - \c boost::heap::arity<>, required
  358. * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
  359. * - \c boost::heap::stable<>, defaults to \c stable<false>
  360. * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
  361. * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
  362. * - \c boost::heap::mutable_<>, defaults to \c mutable_<false>
  363. *
  364. */
  365. #ifdef BOOST_DOXYGEN_INVOKED
  366. template<class T, class ...Options>
  367. #else
  368. template <typename T,
  369. class A0 = boost::parameter::void_,
  370. class A1 = boost::parameter::void_,
  371. class A2 = boost::parameter::void_,
  372. class A3 = boost::parameter::void_,
  373. class A4 = boost::parameter::void_,
  374. class A5 = boost::parameter::void_
  375. >
  376. #endif
  377. class d_ary_heap:
  378. public detail::select_dary_heap<T, typename detail::d_ary_heap_signature::bind<A0, A1, A2, A3, A4, A5>::type>::type
  379. {
  380. typedef typename detail::d_ary_heap_signature::bind<A0, A1, A2, A3, A4, A5>::type bound_args;
  381. typedef typename detail::select_dary_heap<T, bound_args>::type super_t;
  382. template <typename Heap1, typename Heap2>
  383. friend struct heap_merge_emulate;
  384. #ifndef BOOST_DOXYGEN_INVOKED
  385. static const bool is_mutable = detail::extract_mutable<bound_args>::value;
  386. #define BOOST_HEAP_TYPEDEF_FROM_SUPER_T(NAME) \
  387. typedef typename super_t::NAME NAME;
  388. struct implementation_defined
  389. {
  390. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(size_type)
  391. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(difference_type)
  392. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(value_compare)
  393. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(allocator_type)
  394. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(reference)
  395. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_reference)
  396. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(pointer)
  397. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_pointer)
  398. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(iterator)
  399. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_iterator)
  400. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(ordered_iterator)
  401. BOOST_HEAP_TYPEDEF_FROM_SUPER_T(handle_type)
  402. };
  403. #undef BOOST_HEAP_TYPEDEF_FROM_SUPER_T
  404. #endif
  405. public:
  406. static const bool constant_time_size = true;
  407. static const bool has_ordered_iterators = true;
  408. static const bool is_mergable = false;
  409. static const bool has_reserve = true;
  410. static const bool is_stable = super_t::is_stable;
  411. typedef T value_type;
  412. typedef typename implementation_defined::size_type size_type;
  413. typedef typename implementation_defined::difference_type difference_type;
  414. typedef typename implementation_defined::value_compare value_compare;
  415. typedef typename implementation_defined::allocator_type allocator_type;
  416. typedef typename implementation_defined::reference reference;
  417. typedef typename implementation_defined::const_reference const_reference;
  418. typedef typename implementation_defined::pointer pointer;
  419. typedef typename implementation_defined::const_pointer const_pointer;
  420. /// \copydoc boost::heap::priority_queue::iterator
  421. typedef typename implementation_defined::iterator iterator;
  422. typedef typename implementation_defined::const_iterator const_iterator;
  423. typedef typename implementation_defined::ordered_iterator ordered_iterator;
  424. typedef typename implementation_defined::handle_type handle_type;
  425. /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
  426. explicit d_ary_heap(value_compare const & cmp = value_compare()):
  427. super_t(cmp)
  428. {}
  429. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
  430. d_ary_heap(d_ary_heap const & rhs):
  431. super_t(rhs)
  432. {}
  433. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  434. /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
  435. d_ary_heap(d_ary_heap && rhs):
  436. super_t(std::move(rhs))
  437. {}
  438. /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
  439. d_ary_heap & operator=(d_ary_heap && rhs)
  440. {
  441. super_t::operator=(std::move(rhs));
  442. return *this;
  443. }
  444. #endif
  445. /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &)
  446. d_ary_heap & operator=(d_ary_heap const & rhs)
  447. {
  448. super_t::operator=(rhs);
  449. return *this;
  450. }
  451. /// \copydoc boost::heap::priority_queue::empty
  452. bool empty(void) const
  453. {
  454. return super_t::empty();
  455. }
  456. /// \copydoc boost::heap::priority_queue::size
  457. size_type size(void) const
  458. {
  459. return super_t::size();
  460. }
  461. /// \copydoc boost::heap::priority_queue::max_size
  462. size_type max_size(void) const
  463. {
  464. return super_t::max_size();
  465. }
  466. /// \copydoc boost::heap::priority_queue::clear
  467. void clear(void)
  468. {
  469. super_t::clear();
  470. }
  471. /// \copydoc boost::heap::priority_queue::get_allocator
  472. allocator_type get_allocator(void) const
  473. {
  474. return super_t::get_allocator();
  475. }
  476. /// \copydoc boost::heap::priority_queue::top
  477. value_type const & top(void) const
  478. {
  479. return super_t::top();
  480. }
  481. /// \copydoc boost::heap::priority_queue::push
  482. typename mpl::if_c<is_mutable, handle_type, void>::type push(value_type const & v)
  483. {
  484. return super_t::push(v);
  485. }
  486. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  487. /// \copydoc boost::heap::priority_queue::emplace
  488. template <class... Args>
  489. typename mpl::if_c<is_mutable, handle_type, void>::type emplace(Args&&... args)
  490. {
  491. return super_t::emplace(std::forward<Args>(args)...);
  492. }
  493. #endif
  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 detail::heap_compare(*this, 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 detail::heap_compare(rhs, *this);
  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 !operator<(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 !operator>(rhs);
  517. }
  518. /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
  519. template <typename HeapType>
  520. bool operator==(HeapType const & rhs) const
  521. {
  522. return detail::heap_equality(*this, rhs);
  523. }
  524. /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
  525. template <typename HeapType>
  526. bool operator!=(HeapType const & rhs) const
  527. {
  528. return !(*this == rhs);
  529. }
  530. /**
  531. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  532. *
  533. * \b Complexity: Logarithmic.
  534. *
  535. * \b Requirement: data structure must be configured as mutable
  536. * */
  537. void update(handle_type handle, const_reference v)
  538. {
  539. BOOST_STATIC_ASSERT(is_mutable);
  540. super_t::update(handle, v);
  541. }
  542. /**
  543. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  544. *
  545. * \b Complexity: Logarithmic.
  546. *
  547. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  548. *
  549. * \b Requirement: data structure must be configured as mutable
  550. * */
  551. void update(handle_type handle)
  552. {
  553. BOOST_STATIC_ASSERT(is_mutable);
  554. super_t::update(handle);
  555. }
  556. /**
  557. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  558. *
  559. * \b Complexity: Logarithmic.
  560. *
  561. * \b Note: The new value is expected to be greater than the current one
  562. *
  563. * \b Requirement: data structure must be configured as mutable
  564. * */
  565. void increase(handle_type handle, const_reference v)
  566. {
  567. BOOST_STATIC_ASSERT(is_mutable);
  568. super_t::increase(handle, v);
  569. }
  570. /**
  571. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  572. *
  573. * \b Complexity: Logarithmic.
  574. *
  575. * \b Note: The new value is expected to be greater than the current one. If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  576. *
  577. * \b Requirement: data structure must be configured as mutable
  578. * */
  579. void increase(handle_type handle)
  580. {
  581. BOOST_STATIC_ASSERT(is_mutable);
  582. super_t::increase(handle);
  583. }
  584. /**
  585. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  586. *
  587. * \b Complexity: Logarithmic.
  588. *
  589. * \b Note: The new value is expected to be less than the current one
  590. *
  591. * \b Requirement: data structure must be configured as mutable
  592. * */
  593. void decrease(handle_type handle, const_reference v)
  594. {
  595. BOOST_STATIC_ASSERT(is_mutable);
  596. super_t::decrease(handle, v);
  597. }
  598. /**
  599. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  600. *
  601. * \b Complexity: Logarithmic.
  602. *
  603. * \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!
  604. *
  605. * \b Requirement: data structure must be configured as mutable
  606. * */
  607. void decrease(handle_type handle)
  608. {
  609. BOOST_STATIC_ASSERT(is_mutable);
  610. super_t::decrease(handle);
  611. }
  612. /**
  613. * \b Effects: Removes the element handled by \c handle from the priority_queue.
  614. *
  615. * \b Complexity: Logarithmic.
  616. *
  617. * \b Requirement: data structure must be configured as mutable
  618. * */
  619. void erase(handle_type handle)
  620. {
  621. BOOST_STATIC_ASSERT(is_mutable);
  622. super_t::erase(handle);
  623. }
  624. /**
  625. * \b Effects: Casts an iterator to a node handle.
  626. *
  627. * \b Complexity: Constant.
  628. *
  629. * \b Requirement: data structure must be configured as mutable
  630. * */
  631. static handle_type s_handle_from_iterator(iterator const & it)
  632. {
  633. BOOST_STATIC_ASSERT(is_mutable);
  634. return super_t::s_handle_from_iterator(it);
  635. }
  636. /// \copydoc boost::heap::priority_queue::pop
  637. void pop(void)
  638. {
  639. super_t::pop();
  640. }
  641. /// \copydoc boost::heap::priority_queue::swap
  642. void swap(d_ary_heap & rhs)
  643. {
  644. super_t::swap(rhs);
  645. }
  646. /// \copydoc boost::heap::priority_queue::begin
  647. const_iterator begin(void) const
  648. {
  649. return super_t::begin();
  650. }
  651. /// \copydoc boost::heap::priority_queue::begin
  652. iterator begin(void)
  653. {
  654. return super_t::begin();
  655. }
  656. /// \copydoc boost::heap::priority_queue::end
  657. iterator end(void)
  658. {
  659. return super_t::end();
  660. }
  661. /// \copydoc boost::heap::priority_queue::end
  662. const_iterator end(void) const
  663. {
  664. return super_t::end();
  665. }
  666. /// \copydoc boost::heap::fibonacci_heap::ordered_begin
  667. ordered_iterator ordered_begin(void) const
  668. {
  669. return super_t::ordered_begin();
  670. }
  671. /// \copydoc boost::heap::fibonacci_heap::ordered_end
  672. ordered_iterator ordered_end(void) const
  673. {
  674. return super_t::ordered_end();
  675. }
  676. /// \copydoc boost::heap::priority_queue::reserve
  677. void reserve (size_type element_count)
  678. {
  679. super_t::reserve(element_count);
  680. }
  681. /// \copydoc boost::heap::priority_queue::value_comp
  682. value_compare const & value_comp(void) const
  683. {
  684. return super_t::value_comp();
  685. }
  686. };
  687. } /* namespace heap */
  688. } /* namespace boost */
  689. #undef BOOST_HEAP_ASSERT
  690. #endif /* BOOST_HEAP_D_ARY_HEAP_HPP */