mutable_heap.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // boost 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_DETAIL_MUTABLE_HEAP_HPP
  9. #define BOOST_HEAP_DETAIL_MUTABLE_HEAP_HPP
  10. /*! \file
  11. * INTERNAL ONLY
  12. */
  13. #include <list>
  14. #include <utility>
  15. #include <boost/iterator/iterator_adaptor.hpp>
  16. #include <boost/heap/detail/ordered_adaptor_iterator.hpp>
  17. namespace boost {
  18. namespace heap {
  19. namespace detail {
  20. /* wrapper for a mutable heap container adaptors
  21. *
  22. * this wrapper introduces an additional indirection. the heap is not constructed from objects,
  23. * but instead from std::list iterators. this way, the mutability is achieved
  24. *
  25. */
  26. template <typename PriorityQueueType>
  27. class priority_queue_mutable_wrapper
  28. {
  29. public:
  30. typedef typename PriorityQueueType::value_type value_type;
  31. typedef typename PriorityQueueType::size_type size_type;
  32. typedef typename PriorityQueueType::value_compare value_compare;
  33. typedef typename PriorityQueueType::allocator_type allocator_type;
  34. typedef typename PriorityQueueType::reference reference;
  35. typedef typename PriorityQueueType::const_reference const_reference;
  36. typedef typename PriorityQueueType::pointer pointer;
  37. typedef typename PriorityQueueType::const_pointer const_pointer;
  38. static const bool is_stable = PriorityQueueType::is_stable;
  39. private:
  40. typedef std::pair<value_type, size_type> node_type;
  41. #ifdef BOOST_NO_CXX11_ALLOCATOR
  42. typedef std::list<node_type, typename allocator_type::template rebind<node_type>::other> object_list;
  43. #else
  44. typedef std::list<node_type, typename std::allocator_traits<allocator_type>::template rebind_alloc<node_type>> object_list;
  45. #endif
  46. typedef typename object_list::iterator list_iterator;
  47. typedef typename object_list::const_iterator const_list_iterator;
  48. template <typename Heap1, typename Heap2>
  49. friend struct heap_merge_emulate;
  50. typedef typename PriorityQueueType::super_t::stability_counter_type stability_counter_type;
  51. stability_counter_type get_stability_count(void) const
  52. {
  53. return q_.get_stability_count();
  54. }
  55. void set_stability_count(stability_counter_type new_count)
  56. {
  57. q_.set_stability_count(new_count);
  58. }
  59. struct index_updater
  60. {
  61. template <typename It>
  62. static void run(It & it, size_type new_index)
  63. {
  64. q_type::get_value(it)->second = new_index;
  65. }
  66. };
  67. public:
  68. struct handle_type
  69. {
  70. value_type & operator*() const
  71. {
  72. return iterator->first;
  73. }
  74. handle_type (void)
  75. {}
  76. handle_type(handle_type const & rhs):
  77. iterator(rhs.iterator)
  78. {}
  79. bool operator==(handle_type const & rhs) const
  80. {
  81. return iterator == rhs.iterator;
  82. }
  83. bool operator!=(handle_type const & rhs) const
  84. {
  85. return iterator != rhs.iterator;
  86. }
  87. private:
  88. explicit handle_type(list_iterator const & it):
  89. iterator(it)
  90. {}
  91. list_iterator iterator;
  92. friend class priority_queue_mutable_wrapper;
  93. };
  94. private:
  95. struct indirect_cmp:
  96. public value_compare
  97. {
  98. indirect_cmp(value_compare const & cmp = value_compare()):
  99. value_compare(cmp)
  100. {}
  101. bool operator()(const_list_iterator const & lhs, const_list_iterator const & rhs) const
  102. {
  103. return value_compare::operator()(lhs->first, rhs->first);
  104. }
  105. };
  106. typedef typename PriorityQueueType::template rebind<list_iterator,
  107. indirect_cmp,
  108. allocator_type, index_updater >::other q_type;
  109. protected:
  110. q_type q_;
  111. object_list objects;
  112. protected:
  113. priority_queue_mutable_wrapper(value_compare const & cmp = value_compare()):
  114. q_(cmp)
  115. {}
  116. priority_queue_mutable_wrapper(priority_queue_mutable_wrapper const & rhs):
  117. q_(rhs.q_), objects(rhs.objects)
  118. {
  119. for (typename object_list::iterator it = objects.begin(); it != objects.end(); ++it)
  120. q_.push(it);
  121. }
  122. priority_queue_mutable_wrapper & operator=(priority_queue_mutable_wrapper const & rhs)
  123. {
  124. q_ = rhs.q_;
  125. objects = rhs.objects;
  126. q_.clear();
  127. for (typename object_list::iterator it = objects.begin(); it != objects.end(); ++it)
  128. q_.push(it);
  129. return *this;
  130. }
  131. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  132. priority_queue_mutable_wrapper (priority_queue_mutable_wrapper && rhs):
  133. q_(std::move(rhs.q_))
  134. {
  135. /// FIXME: msvc seems to invalidate iterators when moving std::list
  136. std::swap(objects, rhs.objects);
  137. }
  138. priority_queue_mutable_wrapper & operator=(priority_queue_mutable_wrapper && rhs)
  139. {
  140. q_ = std::move(rhs.q_);
  141. objects.clear();
  142. std::swap(objects, rhs.objects);
  143. return *this;
  144. }
  145. #endif
  146. public:
  147. template <typename iterator_type>
  148. class iterator_base:
  149. public boost::iterator_adaptor<iterator_base<iterator_type>,
  150. iterator_type,
  151. value_type const,
  152. boost::bidirectional_traversal_tag>
  153. {
  154. typedef boost::iterator_adaptor<iterator_base<iterator_type>,
  155. iterator_type,
  156. value_type const,
  157. boost::bidirectional_traversal_tag> super_t;
  158. friend class boost::iterator_core_access;
  159. friend class priority_queue_mutable_wrapper;
  160. iterator_base(void):
  161. super_t(0)
  162. {}
  163. template <typename T>
  164. explicit iterator_base(T const & it):
  165. super_t(it)
  166. {}
  167. value_type const & dereference() const
  168. {
  169. return super_t::base()->first;
  170. }
  171. iterator_type get_list_iterator() const
  172. {
  173. return super_t::base_reference();
  174. }
  175. };
  176. typedef iterator_base<list_iterator> iterator;
  177. typedef iterator_base<const_list_iterator> const_iterator;
  178. typedef typename object_list::difference_type difference_type;
  179. class ordered_iterator:
  180. public boost::iterator_adaptor<ordered_iterator,
  181. const_list_iterator,
  182. value_type const,
  183. boost::forward_traversal_tag
  184. >,
  185. q_type::ordered_iterator_dispatcher
  186. {
  187. typedef boost::iterator_adaptor<ordered_iterator,
  188. const_list_iterator,
  189. value_type const,
  190. boost::forward_traversal_tag
  191. > adaptor_type;
  192. typedef const_list_iterator iterator;
  193. typedef typename q_type::ordered_iterator_dispatcher ordered_iterator_dispatcher;
  194. friend class boost::iterator_core_access;
  195. public:
  196. ordered_iterator(void):
  197. adaptor_type(0), unvisited_nodes(indirect_cmp()), q_(NULL)
  198. {}
  199. ordered_iterator(const priority_queue_mutable_wrapper * q, indirect_cmp const & cmp):
  200. adaptor_type(0), unvisited_nodes(cmp), q_(q)
  201. {}
  202. ordered_iterator(const_list_iterator it, const priority_queue_mutable_wrapper * q, indirect_cmp const & cmp):
  203. adaptor_type(it), unvisited_nodes(cmp), q_(q)
  204. {
  205. if (it != q->objects.end())
  206. discover_nodes(it);
  207. }
  208. bool operator!=(ordered_iterator const & rhs) const
  209. {
  210. return adaptor_type::base() != rhs.base();
  211. }
  212. bool operator==(ordered_iterator const & rhs) const
  213. {
  214. return !operator!=(rhs);
  215. }
  216. private:
  217. void increment(void)
  218. {
  219. if (unvisited_nodes.empty())
  220. adaptor_type::base_reference() = q_->objects.end();
  221. else {
  222. iterator next = unvisited_nodes.top();
  223. unvisited_nodes.pop();
  224. discover_nodes(next);
  225. adaptor_type::base_reference() = next;
  226. }
  227. }
  228. value_type const & dereference() const
  229. {
  230. return adaptor_type::base()->first;
  231. }
  232. void discover_nodes(iterator current)
  233. {
  234. size_type current_index = current->second;
  235. const q_type * q = &(q_->q_);
  236. if (ordered_iterator_dispatcher::is_leaf(q, current_index))
  237. return;
  238. std::pair<size_type, size_type> child_range = ordered_iterator_dispatcher::get_child_nodes(q, current_index);
  239. for (size_type i = child_range.first; i <= child_range.second; ++i) {
  240. typename q_type::internal_type const & internal_value_at_index = ordered_iterator_dispatcher::get_internal_value(q, i);
  241. typename q_type::value_type const & value_at_index = q_->q_.get_value(internal_value_at_index);
  242. unvisited_nodes.push(value_at_index);
  243. }
  244. }
  245. std::priority_queue<iterator,
  246. #ifdef BOOST_NO_CXX11_ALLOCATOR
  247. std::vector<iterator, typename allocator_type::template rebind<iterator>::other >,
  248. #else
  249. std::vector<iterator, typename std::allocator_traits<allocator_type>::template rebind_alloc<iterator> >,
  250. #endif
  251. indirect_cmp
  252. > unvisited_nodes;
  253. const priority_queue_mutable_wrapper * q_;
  254. };
  255. bool empty(void) const
  256. {
  257. return q_.empty();
  258. }
  259. size_type size(void) const
  260. {
  261. return q_.size();
  262. }
  263. size_type max_size(void) const
  264. {
  265. return objects.max_size();
  266. }
  267. void clear(void)
  268. {
  269. q_.clear();
  270. objects.clear();
  271. }
  272. allocator_type get_allocator(void) const
  273. {
  274. return q_.get_allocator();
  275. }
  276. void swap(priority_queue_mutable_wrapper & rhs)
  277. {
  278. objects.swap(rhs.objects);
  279. q_.swap(rhs.q_);
  280. }
  281. const_reference top(void) const
  282. {
  283. BOOST_ASSERT(!empty());
  284. return q_.top()->first;
  285. }
  286. handle_type push(value_type const & v)
  287. {
  288. objects.push_front(std::make_pair(v, 0));
  289. list_iterator ret = objects.begin();
  290. q_.push(ret);
  291. return handle_type(ret);
  292. }
  293. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  294. template <class... Args>
  295. handle_type emplace(Args&&... args)
  296. {
  297. objects.push_front(std::make_pair(std::forward<Args>(args)..., 0));
  298. list_iterator ret = objects.begin();
  299. q_.push(ret);
  300. return handle_type(ret);
  301. }
  302. #endif
  303. void pop(void)
  304. {
  305. BOOST_ASSERT(!empty());
  306. list_iterator q_top = q_.top();
  307. q_.pop();
  308. objects.erase(q_top);
  309. }
  310. /**
  311. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  312. *
  313. * \b Complexity: Logarithmic.
  314. *
  315. * */
  316. void update(handle_type handle, const_reference v)
  317. {
  318. list_iterator it = handle.iterator;
  319. value_type const & current_value = it->first;
  320. value_compare const & cmp = q_.value_comp();
  321. if (cmp(v, current_value))
  322. decrease(handle, v);
  323. else
  324. increase(handle, v);
  325. }
  326. /**
  327. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  328. *
  329. * \b Complexity: Logarithmic.
  330. *
  331. * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
  332. * */
  333. void update(handle_type handle)
  334. {
  335. list_iterator it = handle.iterator;
  336. size_type index = it->second;
  337. q_.update(index);
  338. }
  339. /**
  340. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  341. *
  342. * \b Complexity: Logarithmic.
  343. *
  344. * \b Note: The new value is expected to be greater than the current one
  345. * */
  346. void increase(handle_type handle, const_reference v)
  347. {
  348. BOOST_ASSERT(!value_compare()(v, handle.iterator->first));
  349. handle.iterator->first = v;
  350. increase(handle);
  351. }
  352. /**
  353. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  354. *
  355. * \b Complexity: Logarithmic.
  356. *
  357. * \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!
  358. * */
  359. void increase(handle_type handle)
  360. {
  361. list_iterator it = handle.iterator;
  362. size_type index = it->second;
  363. q_.increase(index);
  364. }
  365. /**
  366. * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
  367. *
  368. * \b Complexity: Logarithmic.
  369. *
  370. * \b Note: The new value is expected to be less than the current one
  371. * */
  372. void decrease(handle_type handle, const_reference v)
  373. {
  374. BOOST_ASSERT(!value_compare()(handle.iterator->first, v));
  375. handle.iterator->first = v;
  376. decrease(handle);
  377. }
  378. /**
  379. * \b Effects: Updates the heap after the element handled by \c handle has been changed.
  380. *
  381. * \b Complexity: Logarithmic.
  382. *
  383. * \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!
  384. * */
  385. void decrease(handle_type handle)
  386. {
  387. list_iterator it = handle.iterator;
  388. size_type index = it->second;
  389. q_.decrease(index);
  390. }
  391. /**
  392. * \b Effects: Removes the element handled by \c handle from the priority_queue.
  393. *
  394. * \b Complexity: Logarithmic.
  395. * */
  396. void erase(handle_type handle)
  397. {
  398. list_iterator it = handle.iterator;
  399. size_type index = it->second;
  400. q_.erase(index);
  401. objects.erase(it);
  402. }
  403. const_iterator begin(void) const
  404. {
  405. return const_iterator(objects.begin());
  406. }
  407. const_iterator end(void) const
  408. {
  409. return const_iterator(objects.end());
  410. }
  411. iterator begin(void)
  412. {
  413. return iterator(objects.begin());
  414. }
  415. iterator end(void)
  416. {
  417. return iterator(objects.end());
  418. }
  419. ordered_iterator ordered_begin(void) const
  420. {
  421. if (!empty())
  422. return ordered_iterator(q_.top(), this, indirect_cmp(q_.value_comp()));
  423. else
  424. return ordered_end();
  425. }
  426. ordered_iterator ordered_end(void) const
  427. {
  428. return ordered_iterator(objects.end(), this, indirect_cmp(q_.value_comp()));
  429. }
  430. static handle_type s_handle_from_iterator(iterator const & it)
  431. {
  432. return handle_type(it.get_list_iterator());
  433. }
  434. value_compare const & value_comp(void) const
  435. {
  436. return q_.value_comp();
  437. }
  438. void reserve (size_type element_count)
  439. {
  440. q_.reserve(element_count);
  441. }
  442. };
  443. } /* namespace detail */
  444. } /* namespace heap */
  445. } /* namespace boost */
  446. #endif /* BOOST_HEAP_DETAIL_MUTABLE_HEAP_HPP */