1 // boost heap: wrapper for stl heap
3 // Copyright (C) 2010 Tim Blechmann
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)
9 #ifndef BOOST_HEAP_PRIORITY_QUEUE_HPP
10 #define BOOST_HEAP_PRIORITY_QUEUE_HPP
17 #include <boost/assert.hpp>
19 #include <boost/heap/detail/heap_comparison.hpp>
20 #include <boost/heap/detail/stable_heap.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
31 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
32 boost::parameter::optional<tag::compare>,
33 boost::parameter::optional<tag::stable>,
34 boost::parameter::optional<tag::stability_counter_type>
35 > priority_queue_signature;
39 * \class priority_queue
40 * \brief priority queue, based on stl heap functions
42 * The priority_queue class is a wrapper for the stl heap functions.<br>
43 * The template parameter T is the type to be managed by the container.
44 * The user can specify additional options and if no options are provided default options are used.
46 * The container supports the following options:
47 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
48 * - \c boost::heap::stable<>, defaults to \c stable<false>
49 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
50 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
53 #ifdef BOOST_DOXYGEN_INVOKED
54 template<class T, class ...Options>
57 class A0 = boost::parameter::void_,
58 class A1 = boost::parameter::void_,
59 class A2 = boost::parameter::void_,
60 class A3 = boost::parameter::void_
64 private detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false>::type
66 typedef detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false> heap_base_maker;
68 typedef typename heap_base_maker::type super_t;
69 typedef typename super_t::internal_type internal_type;
70 #ifdef BOOST_NO_CXX11_ALLOCATOR
71 typedef typename heap_base_maker::allocator_argument::template rebind<internal_type>::other internal_type_allocator;
73 typedef typename std::allocator_traits<typename heap_base_maker::allocator_argument>::template rebind_alloc<internal_type> internal_type_allocator;
75 typedef std::vector<internal_type, internal_type_allocator> container_type;
77 template <typename Heap1, typename Heap2>
78 friend struct detail::heap_merge_emulate;
82 #ifndef BOOST_DOXYGEN_INVOKED
83 struct implementation_defined:
84 detail::extract_allocator_types<typename heap_base_maker::allocator_argument>
86 typedef typename heap_base_maker::compare_argument value_compare;
87 typedef detail::stable_heap_iterator<T, typename container_type::const_iterator, super_t> iterator;
88 typedef iterator const_iterator;
89 typedef typename container_type::allocator_type allocator_type;
90 #ifndef BOOST_NO_CXX11_ALLOCATOR
91 typedef typename std::allocator_traits<allocator_type> allocator_traits;
98 typedef typename implementation_defined::size_type size_type;
99 typedef typename implementation_defined::difference_type difference_type;
100 typedef typename implementation_defined::value_compare value_compare;
101 typedef typename implementation_defined::allocator_type allocator_type;
102 #ifndef BOOST_NO_CXX11_ALLOCATOR
103 typedef typename implementation_defined::allocator_traits allocator_traits;
105 typedef typename implementation_defined::reference reference;
106 typedef typename implementation_defined::const_reference const_reference;
107 typedef typename implementation_defined::pointer pointer;
108 typedef typename implementation_defined::const_pointer const_pointer;
110 * \b Note: The iterator does not traverse the priority queue in order of the priorities.
112 typedef typename implementation_defined::iterator iterator;
113 typedef typename implementation_defined::const_iterator const_iterator;
115 static const bool constant_time_size = true;
116 static const bool has_ordered_iterators = false;
117 static const bool is_mergable = false;
118 static const bool is_stable = heap_base_maker::is_stable;
119 static const bool has_reserve = true;
122 * \b Effects: constructs an empty priority queue.
124 * \b Complexity: Constant.
127 explicit priority_queue(value_compare const & cmp = value_compare()):
132 * \b Effects: copy-constructs priority queue from rhs.
134 * \b Complexity: Linear.
137 priority_queue (priority_queue const & rhs):
138 super_t(rhs), q_(rhs.q_)
141 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
143 * \b Effects: C++11-style move constructor.
145 * \b Complexity: Constant.
147 * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
149 priority_queue(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value):
150 super_t(std::move(rhs)), q_(std::move(rhs.q_))
154 * \b Effects: C++11-style move assignment.
156 * \b Complexity: Constant.
158 * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
160 priority_queue & operator=(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable<super_t>::value)
162 super_t::operator=(std::move(rhs));
163 q_ = std::move(rhs.q_);
169 * \b Effects: Assigns priority queue from rhs.
171 * \b Complexity: Linear.
174 priority_queue & operator=(priority_queue const & rhs)
176 static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
182 * \b Effects: Returns true, if the priority queue contains no elements.
184 * \b Complexity: Constant.
187 bool empty(void) const BOOST_NOEXCEPT
193 * \b Effects: Returns the number of elements contained in the priority queue.
195 * \b Complexity: Constant.
198 size_type size(void) const BOOST_NOEXCEPT
204 * \b Effects: Returns the maximum number of elements the priority queue can contain.
206 * \b Complexity: Constant.
209 size_type max_size(void) const BOOST_NOEXCEPT
211 return q_.max_size();
215 * \b Effects: Removes all elements from the priority queue.
217 * \b Complexity: Linear.
220 void clear(void) BOOST_NOEXCEPT
226 * \b Effects: Returns allocator.
228 * \b Complexity: Constant.
231 allocator_type get_allocator(void) const
233 return q_.get_allocator();
237 * \b Effects: Returns a const_reference to the maximum element.
239 * \b Complexity: Constant.
242 const_reference top(void) const
244 BOOST_ASSERT(!empty());
245 return super_t::get_value(q_.front());
249 * \b Effects: Adds a new element to the priority queue.
251 * \b Complexity: Logarithmic (amortized). Linear (worst case).
254 void push(value_type const & v)
256 q_.push_back(super_t::make_node(v));
257 std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
260 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
262 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place.
264 * \b Complexity: Logarithmic (amortized). Linear (worst case).
267 template <class... Args>
268 void emplace(Args&&... args)
270 q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
271 std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
276 * \b Effects: Removes the top element from the priority queue.
278 * \b Complexity: Logarithmic (amortized). Linear (worst case).
283 BOOST_ASSERT(!empty());
284 std::pop_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
289 * \b Effects: Swaps two priority queues.
291 * \b Complexity: Constant.
294 void swap(priority_queue & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value && boost::is_nothrow_move_assignable<super_t>::value)
301 * \b Effects: Returns an iterator to the first element contained in the priority queue.
303 * \b Complexity: Constant.
306 iterator begin(void) const BOOST_NOEXCEPT
308 return iterator(q_.begin());
312 * \b Effects: Returns an iterator to the end of the priority queue.
314 * \b Complexity: Constant.
317 iterator end(void) const BOOST_NOEXCEPT
319 return iterator(q_.end());
323 * \b Effects: Reserves memory for element_count elements
325 * \b Complexity: Linear.
327 * \b Node: Invalidates iterators
330 void reserve(size_type element_count)
332 q_.reserve(element_count);
336 * \b Effect: Returns the value_compare object used by the priority queue
339 value_compare const & value_comp(void) const
341 return super_t::value_comp();
345 * \b Returns: Element-wise comparison of heap data structures
347 * \b Requirement: the \c value_compare object of both heaps must match.
350 template <typename HeapType>
351 bool operator<(HeapType const & rhs) const
353 return detail::heap_compare(*this, rhs);
357 * \b Returns: Element-wise comparison of heap data structures
359 * \b Requirement: the \c value_compare object of both heaps must match.
362 template <typename HeapType>
363 bool operator>(HeapType const & rhs) const
365 return detail::heap_compare(rhs, *this);
369 * \b Returns: Element-wise comparison of heap data structures
371 * \b Requirement: the \c value_compare object of both heaps must match.
374 template <typename HeapType>
375 bool operator>=(HeapType const & rhs) const
377 return !operator<(rhs);
381 * \b Returns: Element-wise comparison of heap data structures
383 * \b Requirement: the \c value_compare object of both heaps must match.
386 template <typename HeapType>
387 bool operator<=(HeapType const & rhs) const
389 return !operator>(rhs);
392 /** \brief Equivalent comparison
393 * \b Returns: True, if both heap data structures are equivalent.
395 * \b Requirement: the \c value_compare object of both heaps must match.
398 template <typename HeapType>
399 bool operator==(HeapType const & rhs) const
401 return detail::heap_equality(*this, rhs);
404 /** \brief Equivalent comparison
405 * \b Returns: True, if both heap data structures are not equivalent.
407 * \b Requirement: the \c value_compare object of both heaps must match.
410 template <typename HeapType>
411 bool operator!=(HeapType const & rhs) const
413 return !(*this == rhs);
417 } /* namespace heap */
418 } /* namespace boost */
420 #endif /* BOOST_HEAP_PRIORITY_QUEUE_HPP */