annotate DEPENDENCIES/generic/include/boost/heap/pairing_heap.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: pairing 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_PAIRING_HEAP_HPP
Chris@16 10 #define BOOST_HEAP_PAIRING_HEAP_HPP
Chris@16 11
Chris@16 12 #include <algorithm>
Chris@16 13 #include <utility>
Chris@16 14 #include <vector>
Chris@16 15
Chris@16 16 #include <boost/assert.hpp>
Chris@16 17
Chris@16 18 #include <boost/heap/detail/heap_comparison.hpp>
Chris@16 19 #include <boost/heap/detail/heap_node.hpp>
Chris@16 20 #include <boost/heap/policies.hpp>
Chris@16 21 #include <boost/heap/detail/stable_heap.hpp>
Chris@16 22 #include <boost/heap/detail/tree_iterator.hpp>
Chris@16 23
Chris@16 24 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 25 #ifdef BOOST_HEAP_SANITYCHECKS
Chris@16 26 #define BOOST_HEAP_ASSERT BOOST_ASSERT
Chris@16 27 #else
Chris@16 28 #define BOOST_HEAP_ASSERT(expression)
Chris@16 29 #endif
Chris@16 30 #endif
Chris@16 31
Chris@16 32 namespace boost {
Chris@16 33 namespace heap {
Chris@16 34 namespace detail {
Chris@16 35
Chris@16 36 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
Chris@16 37 boost::parameter::optional<tag::compare>,
Chris@16 38 boost::parameter::optional<tag::stable>,
Chris@16 39 boost::parameter::optional<tag::constant_time_size>,
Chris@16 40 boost::parameter::optional<tag::stability_counter_type>
Chris@16 41 > pairing_heap_signature;
Chris@16 42
Chris@16 43 template <typename T, typename Parspec>
Chris@16 44 struct make_pairing_heap_base
Chris@16 45 {
Chris@16 46 static const bool constant_time_size = parameter::binding<Parspec,
Chris@16 47 tag::constant_time_size,
Chris@16 48 boost::mpl::true_
Chris@16 49 >::type::value;
Chris@16 50 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::type base_type;
Chris@16 51 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::allocator_argument allocator_argument;
Chris@16 52 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::compare_argument compare_argument;
Chris@16 53
Chris@16 54 typedef heap_node<typename base_type::internal_type, false> node_type;
Chris@16 55
Chris@16 56 typedef typename allocator_argument::template rebind<node_type>::other allocator_type;
Chris@16 57
Chris@16 58 struct type:
Chris@16 59 base_type,
Chris@16 60 allocator_type
Chris@16 61 {
Chris@16 62 type(compare_argument const & arg):
Chris@16 63 base_type(arg)
Chris@16 64 {}
Chris@16 65
Chris@16 66 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 67 type(type const & rhs):
Chris@16 68 base_type(rhs), allocator_type(rhs)
Chris@16 69 {}
Chris@16 70
Chris@16 71 type(type && rhs):
Chris@16 72 base_type(std::move(static_cast<base_type&>(rhs))),
Chris@16 73 allocator_type(std::move(static_cast<allocator_type&>(rhs)))
Chris@16 74 {}
Chris@16 75
Chris@16 76 type & operator=(type && rhs)
Chris@16 77 {
Chris@16 78 base_type::operator=(std::move(static_cast<base_type&>(rhs)));
Chris@16 79 allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
Chris@16 80 return *this;
Chris@16 81 }
Chris@16 82
Chris@16 83 type & operator=(type const & rhs)
Chris@16 84 {
Chris@16 85 base_type::operator=(static_cast<base_type const &>(rhs));
Chris@16 86 allocator_type::operator=(static_cast<const allocator_type&>(rhs));
Chris@16 87 return *this;
Chris@16 88 }
Chris@16 89 #endif
Chris@16 90 };
Chris@16 91 };
Chris@16 92
Chris@16 93 }
Chris@16 94
Chris@16 95 /**
Chris@16 96 * \class pairing_heap
Chris@16 97 * \brief pairing heap
Chris@16 98 *
Chris@16 99 * Pairing heaps are self-adjusting binary heaps. Although design and implementation are rather simple,
Chris@16 100 * the complexity analysis is yet unsolved. For details, consult:
Chris@16 101 *
Chris@16 102 * Pettie, Seth (2005), "Towards a final analysis of pairing heaps",
Chris@16 103 * Proc. 46th Annual IEEE Symposium on Foundations of Computer Science, pp. 174-183
Chris@16 104 *
Chris@16 105 * The template parameter T is the type to be managed by the container.
Chris@16 106 * The user can specify additional options and if no options are provided default options are used.
Chris@16 107 *
Chris@16 108 * The container supports the following options:
Chris@16 109 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
Chris@16 110 * - \c boost::heap::stable<>, defaults to \c stable<false>
Chris@16 111 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
Chris@16 112 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
Chris@16 113 * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
Chris@16 114 *
Chris@16 115 *
Chris@16 116 */
Chris@16 117 #ifdef BOOST_DOXYGEN_INVOKED
Chris@16 118 template<class T, class ...Options>
Chris@16 119 #else
Chris@16 120 template <typename T,
Chris@16 121 class A0 = boost::parameter::void_,
Chris@16 122 class A1 = boost::parameter::void_,
Chris@16 123 class A2 = boost::parameter::void_,
Chris@16 124 class A3 = boost::parameter::void_,
Chris@16 125 class A4 = boost::parameter::void_
Chris@16 126 >
Chris@16 127 #endif
Chris@16 128 class pairing_heap:
Chris@16 129 private detail::make_pairing_heap_base<T,
Chris@16 130 typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type
Chris@16 131 >::type
Chris@16 132 {
Chris@16 133 typedef typename detail::pairing_heap_signature::bind<A0, A1, A2, A3, A4>::type bound_args;
Chris@16 134 typedef detail::make_pairing_heap_base<T, bound_args> base_maker;
Chris@16 135 typedef typename base_maker::type super_t;
Chris@16 136
Chris@16 137 typedef typename super_t::internal_type internal_type;
Chris@16 138 typedef typename super_t::size_holder_type size_holder;
Chris@16 139 typedef typename base_maker::allocator_argument allocator_argument;
Chris@16 140
Chris@16 141 private:
Chris@16 142 template <typename Heap1, typename Heap2>
Chris@16 143 friend struct heap_merge_emulate;
Chris@16 144
Chris@16 145 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 146 struct implementation_defined:
Chris@16 147 detail::extract_allocator_types<typename base_maker::allocator_argument>
Chris@16 148 {
Chris@16 149 typedef T value_type;
Chris@16 150 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::size_type size_type;
Chris@16 151 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
Chris@16 152
Chris@16 153 typedef typename base_maker::compare_argument value_compare;
Chris@16 154 typedef typename base_maker::allocator_type allocator_type;
Chris@16 155
Chris@16 156 typedef typename allocator_type::pointer node_pointer;
Chris@16 157 typedef typename allocator_type::const_pointer const_node_pointer;
Chris@16 158
Chris@16 159 typedef detail::heap_node_list node_list_type;
Chris@16 160 typedef typename node_list_type::iterator node_list_iterator;
Chris@16 161 typedef typename node_list_type::const_iterator node_list_const_iterator;
Chris@16 162
Chris@16 163 typedef typename base_maker::node_type node;
Chris@16 164
Chris@16 165 typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
Chris@16 166 typedef typename super_t::internal_compare internal_compare;
Chris@16 167 typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
Chris@16 168
Chris@16 169 typedef detail::tree_iterator<node,
Chris@16 170 const value_type,
Chris@16 171 allocator_type,
Chris@16 172 value_extractor,
Chris@16 173 detail::pointer_to_reference<node>,
Chris@16 174 false,
Chris@16 175 false,
Chris@16 176 value_compare
Chris@16 177 > iterator;
Chris@16 178
Chris@16 179 typedef iterator const_iterator;
Chris@16 180
Chris@16 181 typedef detail::tree_iterator<node,
Chris@16 182 const value_type,
Chris@16 183 allocator_type,
Chris@16 184 value_extractor,
Chris@16 185 detail::pointer_to_reference<node>,
Chris@16 186 false,
Chris@16 187 true,
Chris@16 188 value_compare
Chris@16 189 > ordered_iterator;
Chris@16 190 };
Chris@16 191
Chris@16 192 typedef typename implementation_defined::node node;
Chris@16 193 typedef typename implementation_defined::node_pointer node_pointer;
Chris@16 194 typedef typename implementation_defined::node_list_type node_list_type;
Chris@16 195 typedef typename implementation_defined::node_list_iterator node_list_iterator;
Chris@16 196 typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator;
Chris@16 197 typedef typename implementation_defined::internal_compare internal_compare;
Chris@16 198
Chris@16 199 typedef boost::intrusive::list<detail::heap_node_base<true>,
Chris@16 200 boost::intrusive::constant_time_size<false>
Chris@16 201 > node_child_list;
Chris@16 202 #endif
Chris@16 203
Chris@16 204 public:
Chris@16 205 typedef T value_type;
Chris@16 206
Chris@16 207 typedef typename implementation_defined::size_type size_type;
Chris@16 208 typedef typename implementation_defined::difference_type difference_type;
Chris@16 209 typedef typename implementation_defined::value_compare value_compare;
Chris@16 210 typedef typename implementation_defined::allocator_type allocator_type;
Chris@16 211 typedef typename implementation_defined::reference reference;
Chris@16 212 typedef typename implementation_defined::const_reference const_reference;
Chris@16 213 typedef typename implementation_defined::pointer pointer;
Chris@16 214 typedef typename implementation_defined::const_pointer const_pointer;
Chris@16 215 /// \copydoc boost::heap::priority_queue::iterator
Chris@16 216 typedef typename implementation_defined::iterator iterator;
Chris@16 217 typedef typename implementation_defined::const_iterator const_iterator;
Chris@16 218 typedef typename implementation_defined::ordered_iterator ordered_iterator;
Chris@16 219
Chris@16 220 typedef typename implementation_defined::handle_type handle_type;
Chris@16 221
Chris@16 222 static const bool constant_time_size = super_t::constant_time_size;
Chris@16 223 static const bool has_ordered_iterators = true;
Chris@16 224 static const bool is_mergable = true;
Chris@16 225 static const bool is_stable = detail::extract_stable<bound_args>::value;
Chris@16 226 static const bool has_reserve = false;
Chris@16 227
Chris@16 228 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
Chris@16 229 explicit pairing_heap(value_compare const & cmp = value_compare()):
Chris@16 230 super_t(cmp), root(NULL)
Chris@16 231 {}
Chris@16 232
Chris@16 233 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
Chris@16 234 pairing_heap(pairing_heap const & rhs):
Chris@16 235 super_t(rhs), root(NULL)
Chris@16 236 {
Chris@16 237 if (rhs.empty())
Chris@16 238 return;
Chris@16 239
Chris@16 240 clone_tree(rhs);
Chris@16 241 size_holder::set_size(rhs.get_size());
Chris@16 242 }
Chris@16 243
Chris@16 244 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 245 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
Chris@16 246 pairing_heap(pairing_heap && rhs):
Chris@16 247 super_t(std::move(rhs)), root(rhs.root)
Chris@16 248 {
Chris@16 249 rhs.root = NULL;
Chris@16 250 }
Chris@16 251
Chris@16 252 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
Chris@16 253 pairing_heap & operator=(pairing_heap && rhs)
Chris@16 254 {
Chris@16 255 super_t::operator=(std::move(rhs));
Chris@16 256 root = rhs.root;
Chris@16 257 rhs.root = NULL;
Chris@16 258 return *this;
Chris@16 259 }
Chris@16 260 #endif
Chris@16 261
Chris@16 262 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs)
Chris@16 263 pairing_heap & operator=(pairing_heap const & rhs)
Chris@16 264 {
Chris@16 265 clear();
Chris@16 266 size_holder::set_size(rhs.get_size());
Chris@16 267 static_cast<super_t&>(*this) = rhs;
Chris@16 268
Chris@16 269 clone_tree(rhs);
Chris@16 270 return *this;
Chris@16 271 }
Chris@16 272
Chris@16 273 ~pairing_heap(void)
Chris@16 274 {
Chris@16 275 while (!empty())
Chris@16 276 pop();
Chris@16 277 }
Chris@16 278
Chris@16 279 /// \copydoc boost::heap::priority_queue::empty
Chris@16 280 bool empty(void) const
Chris@16 281 {
Chris@16 282 return root == NULL;
Chris@16 283 }
Chris@16 284
Chris@16 285 /// \copydoc boost::heap::binomial_heap::size
Chris@16 286 size_type size(void) const
Chris@16 287 {
Chris@16 288 if (constant_time_size)
Chris@16 289 return size_holder::get_size();
Chris@16 290
Chris@16 291 if (root == NULL)
Chris@16 292 return 0;
Chris@16 293 else
Chris@16 294 return detail::count_nodes(root);
Chris@16 295 }
Chris@16 296
Chris@16 297 /// \copydoc boost::heap::priority_queue::max_size
Chris@16 298 size_type max_size(void) const
Chris@16 299 {
Chris@16 300 return allocator_type::max_size();
Chris@16 301 }
Chris@16 302
Chris@16 303 /// \copydoc boost::heap::priority_queue::clear
Chris@16 304 void clear(void)
Chris@16 305 {
Chris@16 306 if (empty())
Chris@16 307 return;
Chris@16 308
Chris@16 309 root->template clear_subtree<allocator_type>(*this);
Chris@16 310 root->~node();
Chris@16 311 allocator_type::deallocate(root, 1);
Chris@16 312 root = NULL;
Chris@16 313 size_holder::set_size(0);
Chris@16 314 }
Chris@16 315
Chris@16 316 /// \copydoc boost::heap::priority_queue::get_allocator
Chris@16 317 allocator_type get_allocator(void) const
Chris@16 318 {
Chris@16 319 return *this;
Chris@16 320 }
Chris@16 321
Chris@16 322 /// \copydoc boost::heap::priority_queue::swap
Chris@16 323 void swap(pairing_heap & rhs)
Chris@16 324 {
Chris@16 325 super_t::swap(rhs);
Chris@16 326 std::swap(root, rhs.root);
Chris@16 327 }
Chris@16 328
Chris@16 329
Chris@16 330 /// \copydoc boost::heap::priority_queue::top
Chris@16 331 const_reference top(void) const
Chris@16 332 {
Chris@16 333 BOOST_ASSERT(!empty());
Chris@16 334
Chris@16 335 return super_t::get_value(root->value);
Chris@16 336 }
Chris@16 337
Chris@16 338 /**
Chris@16 339 * \b Effects: Adds a new element to the priority queue. Returns handle to element
Chris@16 340 *
Chris@16 341 * \cond
Chris@16 342 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 343 * \endcond
Chris@16 344 *
Chris@16 345 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 346 *
Chris@16 347 * */
Chris@16 348 handle_type push(value_type const & v)
Chris@16 349 {
Chris@16 350 size_holder::increment();
Chris@16 351
Chris@16 352 node_pointer n = allocator_type::allocate(1);
Chris@16 353
Chris@16 354 new(n) node(super_t::make_node(v));
Chris@16 355
Chris@16 356 merge_node(n);
Chris@16 357 return handle_type(n);
Chris@16 358 }
Chris@16 359
Chris@16 360 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
Chris@16 361 /**
Chris@16 362 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element.
Chris@16 363 *
Chris@16 364 * \cond
Chris@16 365 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 366 * \endcond
Chris@16 367 *
Chris@16 368 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 369 *
Chris@16 370 * */
Chris@16 371 template <class... Args>
Chris@16 372 handle_type emplace(Args&&... args)
Chris@16 373 {
Chris@16 374 size_holder::increment();
Chris@16 375
Chris@16 376 node_pointer n = allocator_type::allocate(1);
Chris@16 377
Chris@16 378 new(n) node(super_t::make_node(std::forward<Args>(args)...));
Chris@16 379
Chris@16 380 merge_node(n);
Chris@16 381 return handle_type(n);
Chris@16 382 }
Chris@16 383 #endif
Chris@16 384
Chris@16 385 /**
Chris@16 386 * \b Effects: Removes the top element from the priority queue.
Chris@16 387 *
Chris@16 388 * \b Complexity: Logarithmic (amortized).
Chris@16 389 *
Chris@16 390 * */
Chris@16 391 void pop(void)
Chris@16 392 {
Chris@16 393 BOOST_ASSERT(!empty());
Chris@16 394
Chris@16 395 erase(handle_type(root));
Chris@16 396 }
Chris@16 397
Chris@16 398 /**
Chris@16 399 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 400 *
Chris@16 401 * \cond
Chris@16 402 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 403 * \endcond
Chris@16 404 *
Chris@16 405 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 406 *
Chris@16 407 * */
Chris@16 408 void update (handle_type handle, const_reference v)
Chris@16 409 {
Chris@16 410 handle.node_->value = super_t::make_node(v);
Chris@16 411 update(handle);
Chris@16 412 }
Chris@16 413
Chris@16 414 /**
Chris@16 415 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 416 *
Chris@16 417 * \cond
Chris@16 418 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 419 * \endcond
Chris@16 420 *
Chris@16 421 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 422 *
Chris@16 423 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 424 * */
Chris@16 425 void update (handle_type handle)
Chris@16 426 {
Chris@16 427 node_pointer n = handle.node_;
Chris@16 428
Chris@16 429 n->unlink();
Chris@16 430 if (!n->children.empty())
Chris@16 431 n = merge_nodes(n, merge_node_list(n->children));
Chris@16 432
Chris@16 433 if (n != root)
Chris@16 434 merge_node(n);
Chris@16 435 }
Chris@16 436
Chris@16 437 /**
Chris@16 438 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 439 *
Chris@16 440 * \cond
Chris@16 441 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 442 * \endcond
Chris@16 443 *
Chris@16 444 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 445 *
Chris@16 446 * \b Note: The new value is expected to be greater than the current one
Chris@16 447 * */
Chris@16 448 void increase (handle_type handle, const_reference v)
Chris@16 449 {
Chris@16 450 update(handle, v);
Chris@16 451 }
Chris@16 452
Chris@16 453 /**
Chris@16 454 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 455 *
Chris@16 456 * \cond
Chris@16 457 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 458 * \endcond
Chris@16 459 *
Chris@16 460 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 461 *
Chris@16 462 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 463 * */
Chris@16 464 void increase (handle_type handle)
Chris@16 465 {
Chris@16 466 update(handle);
Chris@16 467 }
Chris@16 468
Chris@16 469 /**
Chris@16 470 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 471 *
Chris@16 472 * \cond
Chris@16 473 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 474 * \endcond
Chris@16 475 *
Chris@16 476 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 477 *
Chris@16 478 * \b Note: The new value is expected to be less than the current one
Chris@16 479 * */
Chris@16 480 void decrease (handle_type handle, const_reference v)
Chris@16 481 {
Chris@16 482 update(handle, v);
Chris@16 483 }
Chris@16 484
Chris@16 485 /**
Chris@16 486 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 487 *
Chris@16 488 * \cond
Chris@16 489 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 490 * \endcond
Chris@16 491 *
Chris@16 492 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 493 *
Chris@16 494 * \b Note: The new value is expected to be less than the current one. If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 495 * */
Chris@16 496 void decrease (handle_type handle)
Chris@16 497 {
Chris@16 498 update(handle);
Chris@16 499 }
Chris@16 500
Chris@16 501 /**
Chris@16 502 * \b Effects: Removes the element handled by \c handle from the priority_queue.
Chris@16 503 *
Chris@16 504 * \cond
Chris@16 505 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 506 * \endcond
Chris@16 507 *
Chris@16 508 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 509 * */
Chris@16 510 void erase(handle_type handle)
Chris@16 511 {
Chris@16 512 node_pointer n = handle.node_;
Chris@16 513 if (n != root) {
Chris@16 514 n->unlink();
Chris@16 515 if (!n->children.empty())
Chris@16 516 merge_node(merge_node_list(n->children));
Chris@16 517 } else {
Chris@16 518 if (!n->children.empty())
Chris@16 519 root = merge_node_list(n->children);
Chris@16 520 else
Chris@16 521 root = NULL;
Chris@16 522 }
Chris@16 523
Chris@16 524 size_holder::decrement();
Chris@16 525 n->~node();
Chris@16 526 allocator_type::deallocate(n, 1);
Chris@16 527 }
Chris@16 528
Chris@16 529 /// \copydoc boost::heap::priority_queue::begin
Chris@16 530 iterator begin(void) const
Chris@16 531 {
Chris@16 532 return iterator(root, super_t::value_comp());
Chris@16 533 }
Chris@16 534
Chris@16 535 /// \copydoc boost::heap::priority_queue::end
Chris@16 536 iterator end(void) const
Chris@16 537 {
Chris@16 538 return iterator();
Chris@16 539 }
Chris@16 540
Chris@16 541 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
Chris@16 542 ordered_iterator ordered_begin(void) const
Chris@16 543 {
Chris@16 544 return ordered_iterator(root, super_t::value_comp());
Chris@16 545 }
Chris@16 546
Chris@16 547 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
Chris@16 548 ordered_iterator ordered_end(void) const
Chris@16 549 {
Chris@16 550 return ordered_iterator(NULL, super_t::value_comp());
Chris@16 551 }
Chris@16 552
Chris@16 553
Chris@16 554 /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator
Chris@16 555 static handle_type s_handle_from_iterator(iterator const & it)
Chris@16 556 {
Chris@16 557 node * ptr = const_cast<node *>(it.get_node());
Chris@16 558 return handle_type(ptr);
Chris@16 559 }
Chris@16 560
Chris@16 561 /**
Chris@16 562 * \b Effects: Merge all elements from rhs into this
Chris@16 563 *
Chris@16 564 * \cond
Chris@16 565 * \b Complexity: \f$2^2log(log(N))\f$ (amortized).
Chris@16 566 * \endcond
Chris@16 567 *
Chris@16 568 * \b Complexity: 2**2*log(log(N)) (amortized).
Chris@16 569 *
Chris@16 570 * */
Chris@16 571 void merge(pairing_heap & rhs)
Chris@16 572 {
Chris@16 573 if (rhs.empty())
Chris@16 574 return;
Chris@16 575
Chris@16 576 merge_node(rhs.root);
Chris@16 577
Chris@16 578 size_holder::add(rhs.get_size());
Chris@16 579 rhs.set_size(0);
Chris@16 580 rhs.root = NULL;
Chris@16 581
Chris@16 582 super_t::set_stability_count((std::max)(super_t::get_stability_count(),
Chris@16 583 rhs.get_stability_count()));
Chris@16 584 rhs.set_stability_count(0);
Chris@16 585 }
Chris@16 586
Chris@16 587 /// \copydoc boost::heap::priority_queue::value_comp
Chris@16 588 value_compare const & value_comp(void) const
Chris@16 589 {
Chris@16 590 return super_t::value_comp();
Chris@16 591 }
Chris@16 592
Chris@16 593 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
Chris@16 594 template <typename HeapType>
Chris@16 595 bool operator<(HeapType const & rhs) const
Chris@16 596 {
Chris@16 597 return detail::heap_compare(*this, rhs);
Chris@16 598 }
Chris@16 599
Chris@16 600 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
Chris@16 601 template <typename HeapType>
Chris@16 602 bool operator>(HeapType const & rhs) const
Chris@16 603 {
Chris@16 604 return detail::heap_compare(rhs, *this);
Chris@16 605 }
Chris@16 606
Chris@16 607 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
Chris@16 608 template <typename HeapType>
Chris@16 609 bool operator>=(HeapType const & rhs) const
Chris@16 610 {
Chris@16 611 return !operator<(rhs);
Chris@16 612 }
Chris@16 613
Chris@16 614 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
Chris@16 615 template <typename HeapType>
Chris@16 616 bool operator<=(HeapType const & rhs) const
Chris@16 617 {
Chris@16 618 return !operator>(rhs);
Chris@16 619 }
Chris@16 620
Chris@16 621 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
Chris@16 622 template <typename HeapType>
Chris@16 623 bool operator==(HeapType const & rhs) const
Chris@16 624 {
Chris@16 625 return detail::heap_equality(*this, rhs);
Chris@16 626 }
Chris@16 627
Chris@16 628 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
Chris@16 629 template <typename HeapType>
Chris@16 630 bool operator!=(HeapType const & rhs) const
Chris@16 631 {
Chris@16 632 return !(*this == rhs);
Chris@16 633 }
Chris@16 634
Chris@16 635 private:
Chris@16 636 #if !defined(BOOST_DOXYGEN_INVOKED)
Chris@16 637 void clone_tree(pairing_heap const & rhs)
Chris@16 638 {
Chris@16 639 BOOST_HEAP_ASSERT(root == NULL);
Chris@16 640 if (rhs.empty())
Chris@16 641 return;
Chris@16 642
Chris@16 643 root = allocator_type::allocate(1);
Chris@16 644
Chris@16 645 new(root) node(static_cast<node const &>(*rhs.root), static_cast<allocator_type&>(*this));
Chris@16 646 }
Chris@16 647
Chris@16 648 void merge_node(node_pointer other)
Chris@16 649 {
Chris@16 650 BOOST_HEAP_ASSERT(other);
Chris@16 651 if (root != NULL)
Chris@16 652 root = merge_nodes(root, other);
Chris@16 653 else
Chris@16 654 root = other;
Chris@16 655 }
Chris@16 656
Chris@16 657 node_pointer merge_node_list(node_child_list & children)
Chris@16 658 {
Chris@16 659 BOOST_HEAP_ASSERT(!children.empty());
Chris@16 660 node_pointer merged = merge_first_pair(children);
Chris@16 661 if (children.empty())
Chris@16 662 return merged;
Chris@16 663
Chris@16 664 node_child_list node_list;
Chris@16 665 node_list.push_back(*merged);
Chris@16 666
Chris@16 667 do {
Chris@16 668 node_pointer next_merged = merge_first_pair(children);
Chris@16 669 node_list.push_back(*next_merged);
Chris@16 670 } while (!children.empty());
Chris@16 671
Chris@16 672 return merge_node_list(node_list);
Chris@16 673 }
Chris@16 674
Chris@16 675 node_pointer merge_first_pair(node_child_list & children)
Chris@16 676 {
Chris@16 677 BOOST_HEAP_ASSERT(!children.empty());
Chris@16 678 node_pointer first_child = static_cast<node_pointer>(&children.front());
Chris@16 679 children.pop_front();
Chris@16 680 if (children.empty())
Chris@16 681 return first_child;
Chris@16 682
Chris@16 683 node_pointer second_child = static_cast<node_pointer>(&children.front());
Chris@16 684 children.pop_front();
Chris@16 685
Chris@16 686 return merge_nodes(first_child, second_child);
Chris@16 687 }
Chris@16 688
Chris@16 689 node_pointer merge_nodes(node_pointer node1, node_pointer node2)
Chris@16 690 {
Chris@16 691 if (super_t::operator()(node1->value, node2->value))
Chris@16 692 std::swap(node1, node2);
Chris@16 693
Chris@16 694 node2->unlink();
Chris@16 695 node1->children.push_front(*node2);
Chris@16 696 return node1;
Chris@16 697 }
Chris@16 698
Chris@16 699 node_pointer root;
Chris@16 700 #endif
Chris@16 701 };
Chris@16 702
Chris@16 703
Chris@16 704 } /* namespace heap */
Chris@16 705 } /* namespace boost */
Chris@16 706
Chris@16 707 #undef BOOST_HEAP_ASSERT
Chris@16 708 #endif /* BOOST_HEAP_PAIRING_HEAP_HPP */