priority_queue.hpp 12 KB

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