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