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 typedef typename heap_base_maker::allocator_argument::template rebind<internal_type>::other internal_type_allocator;
71 typedef std::vector<internal_type, internal_type_allocator> container_type;
73 template <typename Heap1, typename Heap2>
74 friend struct detail::heap_merge_emulate;
78 #ifndef BOOST_DOXYGEN_INVOKED
79 struct implementation_defined:
80 detail::extract_allocator_types<typename heap_base_maker::allocator_argument>
82 typedef typename heap_base_maker::compare_argument value_compare;
83 typedef detail::stable_heap_iterator<T, typename container_type::const_iterator, super_t> iterator;
84 typedef iterator const_iterator;
85 typedef typename container_type::allocator_type allocator_type;
91 typedef typename implementation_defined::size_type size_type;
92 typedef typename implementation_defined::difference_type difference_type;
93 typedef typename implementation_defined::value_compare value_compare;
94 typedef typename implementation_defined::allocator_type allocator_type;
95 typedef typename implementation_defined::reference reference;
96 typedef typename implementation_defined::const_reference const_reference;
97 typedef typename implementation_defined::pointer pointer;
98 typedef typename implementation_defined::const_pointer const_pointer;
100 * \b Note: The iterator does not traverse the priority queue in order of the priorities.
102 typedef typename implementation_defined::iterator iterator;
103 typedef typename implementation_defined::const_iterator const_iterator;
105 static const bool constant_time_size = true;
106 static const bool has_ordered_iterators = false;
107 static const bool is_mergable = false;
108 static const bool is_stable = heap_base_maker::is_stable;
109 static const bool has_reserve = true;
112 * \b Effects: constructs an empty priority queue.
114 * \b Complexity: Constant.
117 explicit priority_queue(value_compare const & cmp = value_compare()):
122 * \b Effects: copy-constructs priority queue from rhs.
124 * \b Complexity: Linear.
127 priority_queue (priority_queue const & rhs):
128 super_t(rhs), q_(rhs.q_)
131 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
133 * \b Effects: C++11-style move constructor.
135 * \b Complexity: Constant.
137 * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
139 priority_queue(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value):
140 super_t(std::move(rhs)), q_(std::move(rhs.q_))
144 * \b Effects: C++11-style move assignment.
146 * \b Complexity: Constant.
148 * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined
150 priority_queue & operator=(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable<super_t>::value)
152 super_t::operator=(std::move(rhs));
153 q_ = std::move(rhs.q_);
159 * \b Effects: Assigns priority queue from rhs.
161 * \b Complexity: Linear.
164 priority_queue & operator=(priority_queue const & rhs)
166 static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
172 * \b Effects: Returns true, if the priority queue contains no elements.
174 * \b Complexity: Constant.
177 bool empty(void) const BOOST_NOEXCEPT
183 * \b Effects: Returns the number of elements contained in the priority queue.
185 * \b Complexity: Constant.
188 size_type size(void) const BOOST_NOEXCEPT
194 * \b Effects: Returns the maximum number of elements the priority queue can contain.
196 * \b Complexity: Constant.
199 size_type max_size(void) const BOOST_NOEXCEPT
201 return q_.max_size();
205 * \b Effects: Removes all elements from the priority queue.
207 * \b Complexity: Linear.
210 void clear(void) BOOST_NOEXCEPT
216 * \b Effects: Returns allocator.
218 * \b Complexity: Constant.
221 allocator_type get_allocator(void) const
223 return q_.get_allocator();
227 * \b Effects: Returns a const_reference to the maximum element.
229 * \b Complexity: Constant.
232 const_reference top(void) const
234 BOOST_ASSERT(!empty());
235 return super_t::get_value(q_.front());
239 * \b Effects: Adds a new element to the priority queue.
241 * \b Complexity: Logarithmic (amortized). Linear (worst case).
244 void push(value_type const & v)
246 q_.push_back(super_t::make_node(v));
247 std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
250 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
252 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place.
254 * \b Complexity: Logarithmic (amortized). Linear (worst case).
257 template <class... Args>
258 void emplace(Args&&... args)
260 q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
261 std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
266 * \b Effects: Removes the top element from the priority queue.
268 * \b Complexity: Logarithmic (amortized). Linear (worst case).
273 BOOST_ASSERT(!empty());
274 std::pop_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
279 * \b Effects: Swaps two priority queues.
281 * \b Complexity: Constant.
284 void swap(priority_queue & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value && boost::is_nothrow_move_assignable<super_t>::value)
291 * \b Effects: Returns an iterator to the first element contained in the priority queue.
293 * \b Complexity: Constant.
296 iterator begin(void) const BOOST_NOEXCEPT
298 return iterator(q_.begin());
302 * \b Effects: Returns an iterator to the end of the priority queue.
304 * \b Complexity: Constant.
307 iterator end(void) const BOOST_NOEXCEPT
309 return iterator(q_.end());
313 * \b Effects: Reserves memory for element_count elements
315 * \b Complexity: Linear.
317 * \b Node: Invalidates iterators
320 void reserve(size_type element_count)
322 q_.reserve(element_count);
326 * \b Effect: Returns the value_compare object used by the priority queue
329 value_compare const & value_comp(void) const
331 return super_t::value_comp();
335 * \b Returns: Element-wise comparison of heap data structures
337 * \b Requirement: the \c value_compare object of both heaps must match.
340 template <typename HeapType>
341 bool operator<(HeapType const & rhs) const
343 return detail::heap_compare(*this, rhs);
347 * \b Returns: Element-wise comparison of heap data structures
349 * \b Requirement: the \c value_compare object of both heaps must match.
352 template <typename HeapType>
353 bool operator>(HeapType const & rhs) const
355 return detail::heap_compare(rhs, *this);
359 * \b Returns: Element-wise comparison of heap data structures
361 * \b Requirement: the \c value_compare object of both heaps must match.
364 template <typename HeapType>
365 bool operator>=(HeapType const & rhs) const
367 return !operator<(rhs);
371 * \b Returns: Element-wise comparison of heap data structures
373 * \b Requirement: the \c value_compare object of both heaps must match.
376 template <typename HeapType>
377 bool operator<=(HeapType const & rhs) const
379 return !operator>(rhs);
382 /** \brief Equivalent comparison
383 * \b Returns: True, if both heap data structures are equivalent.
385 * \b Requirement: the \c value_compare object of both heaps must match.
388 template <typename HeapType>
389 bool operator==(HeapType const & rhs) const
391 return detail::heap_equality(*this, rhs);
394 /** \brief Equivalent comparison
395 * \b Returns: True, if both heap data structures are not equivalent.
397 * \b Requirement: the \c value_compare object of both heaps must match.
400 template <typename HeapType>
401 bool operator!=(HeapType const & rhs) const
403 return !(*this == rhs);
407 } /* namespace heap */
408 } /* namespace boost */
410 #endif /* BOOST_HEAP_PRIORITY_QUEUE_HPP */