mutable_heap.hpp 15 KB

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