priority_queue.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // boost heap: wrapper for stl 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_PRIORITY_QUEUE_HPP
  9. #define BOOST_HEAP_PRIORITY_QUEUE_HPP
  10. #include <algorithm>
  11. #include <queue>
  12. #include <utility>
  13. #include <vector>
  14. #include <boost/assert.hpp>
  15. #include <boost/heap/detail/heap_comparison.hpp>
  16. #include <boost/heap/detail/stable_heap.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. namespace boost {
  21. namespace heap {
  22. namespace detail {
  23. typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
  24. boost::parameter::optional<tag::compare>,
  25. boost::parameter::optional<tag::stable>,
  26. boost::parameter::optional<tag::stability_counter_type>
  27. > priority_queue_signature;
  28. }
  29. /**
  30. * \class priority_queue
  31. * \brief priority queue, based on stl heap functions
  32. *
  33. * The priority_queue class is a wrapper for the stl heap functions.<br>
  34. * The template parameter T is the type to be managed by the container.
  35. * The user can specify additional options and if no options are provided default options are used.
  36. *
  37. * The container supports the following options:
  38. * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
  39. * - \c boost::heap::stable<>, defaults to \c stable<false>
  40. * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
  41. * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
  42. *
  43. */
  44. #ifdef BOOST_DOXYGEN_INVOKED
  45. template<class T, class ...Options>
  46. #else
  47. template <typename T,
  48. class A0 = boost::parameter::void_,
  49. class A1 = boost::parameter::void_,
  50. class A2 = boost::parameter::void_,
  51. class A3 = boost::parameter::void_
  52. >
  53. #endif
  54. class priority_queue:
  55. private detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false>::type
  56. {
  57. typedef detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false> heap_base_maker;
  58. typedef typename heap_base_maker::type super_t;
  59. typedef typename super_t::internal_type internal_type;
  60. typedef typename boost::allocator_rebind<typename heap_base_maker::allocator_argument, internal_type>::type internal_type_allocator;
  61. typedef std::vector<internal_type, internal_type_allocator> container_type;
  62. template <typename Heap1, typename Heap2>
  63. friend struct detail::heap_merge_emulate;
  64. container_type q_;
  65. #ifndef BOOST_DOXYGEN_INVOKED
  66. struct implementation_defined:
  67. detail::extract_allocator_types<typename heap_base_maker::allocator_argument>
  68. {
  69. typedef typename heap_base_maker::compare_argument value_compare;
  70. typedef detail::stable_heap_iterator<T, typename container_type::const_iterator, super_t> iterator;
  71. typedef iterator const_iterator;
  72. typedef typename container_type::allocator_type allocator_type;
  73. };
  74. #endif
  75. public:
  76. typedef T value_type;
  77. typedef typename implementation_defined::size_type size_type;
  78. typedef typename implementation_defined::difference_type difference_type;
  79. typedef typename implementation_defined::value_compare value_compare;
  80. typedef typename implementation_defined::allocator_type allocator_type;
  81. typedef typename implementation_defined::reference reference;
  82. typedef typename implementation_defined::const_reference const_reference;
  83. typedef typename implementation_defined::pointer pointer;
  84. typedef typename implementation_defined::const_pointer const_pointer;
  85. /**
  86. * \b Note: The iterator does not traverse the priority queue in order of the priorities.
  87. * */
  88. typedef typename implementation_defined::iterator iterator;
  89. typedef typename implementation_defined::const_iterator const_iterator;
  90. static const bool constant_time_size = true;
  91. static const bool has_ordered_iterators = false;
  92. static const bool is_mergable = false;
  93. static const bool is_stable = heap_base_maker::is_stable;
  94. static const bool has_reserve = true;
  95. /**
  96. * \b Effects: constructs an empty priority queue.
  97. *
  98. * \b Complexity: Constant.
  99. *
  100. * */
  101. explicit priority_queue(value_compare const & cmp = value_compare()):
  102. super_t(cmp)
  103. {}
  104. /**
  105. * \b Effects: copy-constructs priority queue from rhs.
  106. *
  107. * \b Complexity: Linear.
  108. *
  109. * */
  110. priority_queue (priority_queue const & rhs):
  111. super_t(rhs), q_(rhs.q_)
  112. {}
  113. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  114. /**
  115. * \b Effects: C++11-style move constructor.
  116. *
  117. * \b Complexity: Constant.
  118. *
  119. * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
  120. * */
  121. priority_queue(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value):
  122. super_t(std::move(rhs)), q_(std::move(rhs.q_))
  123. {}
  124. /**
  125. * \b Effects: C++11-style move assignment.
  126. *
  127. * \b Complexity: Constant.
  128. *
  129. * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
  130. * */
  131. priority_queue & operator=(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable<super_t>::value)
  132. {
  133. super_t::operator=(std::move(rhs));
  134. q_ = std::move(rhs.q_);
  135. return *this;
  136. }
  137. #endif
  138. /**
  139. * \b Effects: Assigns priority queue from rhs.
  140. *
  141. * \b Complexity: Linear.
  142. *
  143. * */
  144. priority_queue & operator=(priority_queue const & rhs)
  145. {
  146. static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
  147. q_ = rhs.q_;
  148. return *this;
  149. }
  150. /**
  151. * \b Effects: Returns true, if the priority queue contains no elements.
  152. *
  153. * \b Complexity: Constant.
  154. *
  155. * */
  156. bool empty(void) const BOOST_NOEXCEPT
  157. {
  158. return q_.empty();
  159. }
  160. /**
  161. * \b Effects: Returns the number of elements contained in the priority queue.
  162. *
  163. * \b Complexity: Constant.
  164. *
  165. * */
  166. size_type size(void) const BOOST_NOEXCEPT
  167. {
  168. return q_.size();
  169. }
  170. /**
  171. * \b Effects: Returns the maximum number of elements the priority queue can contain.
  172. *
  173. * \b Complexity: Constant.
  174. *
  175. * */
  176. size_type max_size(void) const BOOST_NOEXCEPT
  177. {
  178. return q_.max_size();
  179. }
  180. /**
  181. * \b Effects: Removes all elements from the priority queue.
  182. *
  183. * \b Complexity: Linear.
  184. *
  185. * */
  186. void clear(void) BOOST_NOEXCEPT
  187. {
  188. q_.clear();
  189. }
  190. /**
  191. * \b Effects: Returns allocator.
  192. *
  193. * \b Complexity: Constant.
  194. *
  195. * */
  196. allocator_type get_allocator(void) const
  197. {
  198. return q_.get_allocator();
  199. }
  200. /**
  201. * \b Effects: Returns a const_reference to the maximum element.
  202. *
  203. * \b Complexity: Constant.
  204. *
  205. * */
  206. const_reference top(void) const
  207. {
  208. BOOST_ASSERT(!empty());
  209. return super_t::get_value(q_.front());
  210. }
  211. /**
  212. * \b Effects: Adds a new element to the priority queue.
  213. *
  214. * \b Complexity: Logarithmic (amortized). Linear (worst case).
  215. *
  216. * */
  217. void push(value_type const & v)
  218. {
  219. q_.push_back(super_t::make_node(v));
  220. std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
  221. }
  222. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  223. /**
  224. * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place.
  225. *
  226. * \b Complexity: Logarithmic (amortized). Linear (worst case).
  227. *
  228. * */
  229. template <class... Args>
  230. void emplace(Args&&... args)
  231. {
  232. q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
  233. std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
  234. }
  235. #endif
  236. /**
  237. * \b Effects: Removes the top element from the priority queue.
  238. *
  239. * \b Complexity: Logarithmic (amortized). Linear (worst case).
  240. *
  241. * */
  242. void pop(void)
  243. {
  244. BOOST_ASSERT(!empty());
  245. std::pop_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
  246. q_.pop_back();
  247. }
  248. /**
  249. * \b Effects: Swaps two priority queues.
  250. *
  251. * \b Complexity: Constant.
  252. *
  253. * */
  254. void swap(priority_queue & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value && boost::is_nothrow_move_assignable<super_t>::value)
  255. {
  256. super_t::swap(rhs);
  257. q_.swap(rhs.q_);
  258. }
  259. /**
  260. * \b Effects: Returns an iterator to the first element contained in the priority queue.
  261. *
  262. * \b Complexity: Constant.
  263. *
  264. * */
  265. iterator begin(void) const BOOST_NOEXCEPT
  266. {
  267. return iterator(q_.begin());
  268. }
  269. /**
  270. * \b Effects: Returns an iterator to the end of the priority queue.
  271. *
  272. * \b Complexity: Constant.
  273. *
  274. * */
  275. iterator end(void) const BOOST_NOEXCEPT
  276. {
  277. return iterator(q_.end());
  278. }
  279. /**
  280. * \b Effects: Reserves memory for element_count elements
  281. *
  282. * \b Complexity: Linear.
  283. *
  284. * \b Node: Invalidates iterators
  285. *
  286. * */
  287. void reserve(size_type element_count)
  288. {
  289. q_.reserve(element_count);
  290. }
  291. /**
  292. * \b Effect: Returns the value_compare object used by the priority queue
  293. *
  294. * */
  295. value_compare const & value_comp(void) const
  296. {
  297. return super_t::value_comp();
  298. }
  299. /**
  300. * \b Returns: Element-wise comparison of heap data structures
  301. *
  302. * \b Requirement: the \c value_compare object of both heaps must match.
  303. *
  304. * */
  305. template <typename HeapType>
  306. bool operator<(HeapType const & rhs) const
  307. {
  308. return detail::heap_compare(*this, rhs);
  309. }
  310. /**
  311. * \b Returns: Element-wise comparison of heap data structures
  312. *
  313. * \b Requirement: the \c value_compare object of both heaps must match.
  314. *
  315. * */
  316. template <typename HeapType>
  317. bool operator>(HeapType const & rhs) const
  318. {
  319. return detail::heap_compare(rhs, *this);
  320. }
  321. /**
  322. * \b Returns: Element-wise comparison of heap data structures
  323. *
  324. * \b Requirement: the \c value_compare object of both heaps must match.
  325. *
  326. * */
  327. template <typename HeapType>
  328. bool operator>=(HeapType const & rhs) const
  329. {
  330. return !operator<(rhs);
  331. }
  332. /**
  333. * \b Returns: Element-wise comparison of heap data structures
  334. *
  335. * \b Requirement: the \c value_compare object of both heaps must match.
  336. *
  337. * */
  338. template <typename HeapType>
  339. bool operator<=(HeapType const & rhs) const
  340. {
  341. return !operator>(rhs);
  342. }
  343. /** \brief Equivalent comparison
  344. * \b Returns: True, if both heap data structures are equivalent.
  345. *
  346. * \b Requirement: the \c value_compare object of both heaps must match.
  347. *
  348. * */
  349. template <typename HeapType>
  350. bool operator==(HeapType const & rhs) const
  351. {
  352. return detail::heap_equality(*this, rhs);
  353. }
  354. /** \brief Equivalent comparison
  355. * \b Returns: True, if both heap data structures are not equivalent.
  356. *
  357. * \b Requirement: the \c value_compare object of both heaps must match.
  358. *
  359. * */
  360. template <typename HeapType>
  361. bool operator!=(HeapType const & rhs) const
  362. {
  363. return !(*this == rhs);
  364. }
  365. };
  366. } /* namespace heap */
  367. } /* namespace boost */
  368. #endif /* BOOST_HEAP_PRIORITY_QUEUE_HPP */