priority_queue.hpp 11 KB

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