comparison DEPENDENCIES/generic/include/boost/heap/priority_queue.hpp @ 16:2665513ce2d3

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