annotate DEPENDENCIES/generic/include/boost/heap/fibonacci_heap.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // boost heap: fibonacci 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_FIBONACCI_HEAP_HPP
Chris@16 10 #define BOOST_HEAP_FIBONACCI_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/array.hpp>
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/heap_node.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@101 24 #ifdef BOOST_HAS_PRAGMA_ONCE
Chris@101 25 #pragma once
Chris@101 26 #endif
Chris@101 27
Chris@101 28
Chris@16 29 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 30 #ifdef BOOST_HEAP_SANITYCHECKS
Chris@16 31 #define BOOST_HEAP_ASSERT BOOST_ASSERT
Chris@16 32 #else
Chris@16 33 #define BOOST_HEAP_ASSERT(expression)
Chris@16 34 #endif
Chris@16 35 #endif
Chris@16 36
Chris@16 37 namespace boost {
Chris@16 38 namespace heap {
Chris@16 39 namespace detail {
Chris@16 40
Chris@16 41 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
Chris@16 42 boost::parameter::optional<tag::compare>,
Chris@16 43 boost::parameter::optional<tag::stable>,
Chris@16 44 boost::parameter::optional<tag::constant_time_size>,
Chris@16 45 boost::parameter::optional<tag::stability_counter_type>
Chris@16 46 > fibonacci_heap_signature;
Chris@16 47
Chris@16 48 template <typename T, typename Parspec>
Chris@16 49 struct make_fibonacci_heap_base
Chris@16 50 {
Chris@16 51 static const bool constant_time_size = parameter::binding<Parspec,
Chris@16 52 tag::constant_time_size,
Chris@16 53 boost::mpl::true_
Chris@16 54 >::type::value;
Chris@16 55
Chris@16 56 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::type base_type;
Chris@16 57 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::allocator_argument allocator_argument;
Chris@16 58 typedef typename detail::make_heap_base<T, Parspec, constant_time_size>::compare_argument compare_argument;
Chris@16 59 typedef marked_heap_node<typename base_type::internal_type> node_type;
Chris@16 60
Chris@16 61 typedef typename allocator_argument::template rebind<node_type>::other allocator_type;
Chris@16 62
Chris@16 63 struct type:
Chris@16 64 base_type,
Chris@16 65 allocator_type
Chris@16 66 {
Chris@16 67 type(compare_argument const & arg):
Chris@16 68 base_type(arg)
Chris@16 69 {}
Chris@16 70
Chris@101 71 type(type const & rhs):
Chris@101 72 base_type(static_cast<base_type const &>(rhs)),
Chris@101 73 allocator_type(static_cast<allocator_type const &>(rhs))
Chris@101 74 {}
Chris@101 75
Chris@101 76 type & operator=(type const & rhs)
Chris@101 77 {
Chris@101 78 base_type::operator=(static_cast<base_type const &>(rhs));
Chris@101 79 allocator_type::operator=(static_cast<allocator_type const &>(rhs));
Chris@101 80 return *this;
Chris@101 81 }
Chris@101 82
Chris@16 83 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 84 type(type && rhs):
Chris@16 85 base_type(std::move(static_cast<base_type&>(rhs))),
Chris@16 86 allocator_type(std::move(static_cast<allocator_type&>(rhs)))
Chris@16 87 {}
Chris@16 88
Chris@16 89 type & operator=(type && rhs)
Chris@16 90 {
Chris@16 91 base_type::operator=(std::move(static_cast<base_type&>(rhs)));
Chris@16 92 allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
Chris@16 93 return *this;
Chris@16 94 }
Chris@16 95 #endif
Chris@16 96 };
Chris@16 97 };
Chris@16 98
Chris@16 99 }
Chris@16 100
Chris@16 101
Chris@16 102
Chris@16 103 /**
Chris@16 104 * \class fibonacci_heap
Chris@16 105 * \brief fibonacci heap
Chris@16 106 *
Chris@16 107 * The template parameter T is the type to be managed by the container.
Chris@16 108 * The user can specify additional options and if no options are provided default options are used.
Chris@16 109 *
Chris@16 110 * The container supports the following options:
Chris@16 111 * - \c boost::heap::stable<>, defaults to \c stable<false>
Chris@16 112 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
Chris@16 113 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
Chris@16 114 * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
Chris@16 115 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
Chris@16 116 *
Chris@16 117 */
Chris@16 118 #ifdef BOOST_DOXYGEN_INVOKED
Chris@16 119 template<class T, class ...Options>
Chris@16 120 #else
Chris@16 121 template <typename T,
Chris@16 122 class A0 = boost::parameter::void_,
Chris@16 123 class A1 = boost::parameter::void_,
Chris@16 124 class A2 = boost::parameter::void_,
Chris@16 125 class A3 = boost::parameter::void_,
Chris@16 126 class A4 = boost::parameter::void_
Chris@16 127 >
Chris@16 128 #endif
Chris@16 129 class fibonacci_heap:
Chris@16 130 private detail::make_fibonacci_heap_base<T,
Chris@16 131 typename detail::fibonacci_heap_signature::bind<A0, A1, A2, A3, A4>::type
Chris@16 132 >::type
Chris@16 133 {
Chris@16 134 typedef typename detail::fibonacci_heap_signature::bind<A0, A1, A2, A3, A4>::type bound_args;
Chris@16 135 typedef detail::make_fibonacci_heap_base<T, bound_args> base_maker;
Chris@16 136 typedef typename base_maker::type super_t;
Chris@16 137
Chris@16 138 typedef typename super_t::size_holder_type size_holder;
Chris@16 139 typedef typename super_t::internal_type internal_type;
Chris@16 140 typedef typename base_maker::allocator_argument allocator_argument;
Chris@16 141
Chris@16 142 template <typename Heap1, typename Heap2>
Chris@16 143 friend struct heap_merge_emulate;
Chris@16 144
Chris@16 145 private:
Chris@16 146 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 147 struct implementation_defined:
Chris@16 148 detail::extract_allocator_types<typename base_maker::allocator_argument>
Chris@16 149 {
Chris@16 150 typedef T value_type;
Chris@16 151 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::size_type size_type;
Chris@16 152 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
Chris@16 153
Chris@16 154 typedef typename base_maker::compare_argument value_compare;
Chris@16 155 typedef typename base_maker::allocator_type allocator_type;
Chris@16 156
Chris@16 157 typedef typename allocator_type::pointer node_pointer;
Chris@16 158 typedef typename allocator_type::const_pointer const_node_pointer;
Chris@16 159
Chris@16 160 typedef detail::heap_node_list node_list_type;
Chris@16 161 typedef typename node_list_type::iterator node_list_iterator;
Chris@16 162 typedef typename node_list_type::const_iterator node_list_const_iterator;
Chris@16 163
Chris@16 164 typedef typename base_maker::node_type node;
Chris@16 165
Chris@16 166 typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
Chris@16 167 typedef typename super_t::internal_compare internal_compare;
Chris@16 168 typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
Chris@16 169
Chris@16 170 typedef detail::recursive_tree_iterator<node,
Chris@16 171 node_list_const_iterator,
Chris@16 172 const value_type,
Chris@16 173 value_extractor,
Chris@16 174 detail::list_iterator_converter<node, node_list_type>
Chris@16 175 > iterator;
Chris@16 176 typedef iterator const_iterator;
Chris@16 177
Chris@16 178 typedef detail::tree_iterator<node,
Chris@16 179 const value_type,
Chris@16 180 allocator_type,
Chris@16 181 value_extractor,
Chris@16 182 detail::list_iterator_converter<node, node_list_type>,
Chris@16 183 true,
Chris@16 184 true,
Chris@16 185 value_compare
Chris@16 186 > ordered_iterator;
Chris@16 187 };
Chris@16 188
Chris@16 189 typedef typename implementation_defined::node node;
Chris@16 190 typedef typename implementation_defined::node_pointer node_pointer;
Chris@16 191 typedef typename implementation_defined::node_list_type node_list_type;
Chris@16 192 typedef typename implementation_defined::node_list_iterator node_list_iterator;
Chris@16 193 typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator;
Chris@16 194 typedef typename implementation_defined::internal_compare internal_compare;
Chris@16 195 #endif
Chris@16 196
Chris@16 197 public:
Chris@16 198 typedef T value_type;
Chris@16 199
Chris@16 200 typedef typename implementation_defined::size_type size_type;
Chris@16 201 typedef typename implementation_defined::difference_type difference_type;
Chris@16 202 typedef typename implementation_defined::value_compare value_compare;
Chris@16 203 typedef typename implementation_defined::allocator_type allocator_type;
Chris@16 204 typedef typename implementation_defined::reference reference;
Chris@16 205 typedef typename implementation_defined::const_reference const_reference;
Chris@16 206 typedef typename implementation_defined::pointer pointer;
Chris@16 207 typedef typename implementation_defined::const_pointer const_pointer;
Chris@16 208 /// \copydoc boost::heap::priority_queue::iterator
Chris@16 209 typedef typename implementation_defined::iterator iterator;
Chris@16 210 typedef typename implementation_defined::const_iterator const_iterator;
Chris@16 211 typedef typename implementation_defined::ordered_iterator ordered_iterator;
Chris@16 212
Chris@16 213 typedef typename implementation_defined::handle_type handle_type;
Chris@16 214
Chris@16 215 static const bool constant_time_size = base_maker::constant_time_size;
Chris@16 216 static const bool has_ordered_iterators = true;
Chris@16 217 static const bool is_mergable = true;
Chris@16 218 static const bool is_stable = detail::extract_stable<bound_args>::value;
Chris@16 219 static const bool has_reserve = false;
Chris@16 220
Chris@16 221 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
Chris@16 222 explicit fibonacci_heap(value_compare const & cmp = value_compare()):
Chris@16 223 super_t(cmp), top_element(0)
Chris@16 224 {}
Chris@16 225
Chris@16 226 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
Chris@16 227 fibonacci_heap(fibonacci_heap const & rhs):
Chris@16 228 super_t(rhs), top_element(0)
Chris@16 229 {
Chris@16 230 if (rhs.empty())
Chris@16 231 return;
Chris@16 232
Chris@16 233 clone_forest(rhs);
Chris@16 234 size_holder::set_size(rhs.size());
Chris@16 235 }
Chris@16 236
Chris@16 237 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 238 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
Chris@16 239 fibonacci_heap(fibonacci_heap && rhs):
Chris@16 240 super_t(std::move(rhs)), top_element(rhs.top_element)
Chris@16 241 {
Chris@16 242 roots.splice(roots.begin(), rhs.roots);
Chris@16 243 rhs.top_element = NULL;
Chris@16 244 }
Chris@16 245
Chris@16 246 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
Chris@16 247 fibonacci_heap & operator=(fibonacci_heap && rhs)
Chris@16 248 {
Chris@16 249 clear();
Chris@16 250
Chris@16 251 super_t::operator=(std::move(rhs));
Chris@16 252 roots.splice(roots.begin(), rhs.roots);
Chris@16 253 top_element = rhs.top_element;
Chris@16 254 rhs.top_element = NULL;
Chris@16 255 return *this;
Chris@16 256 }
Chris@16 257 #endif
Chris@16 258
Chris@16 259 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &)
Chris@16 260 fibonacci_heap & operator=(fibonacci_heap const & rhs)
Chris@16 261 {
Chris@16 262 clear();
Chris@16 263 size_holder::set_size(rhs.size());
Chris@16 264 static_cast<super_t&>(*this) = rhs;
Chris@16 265
Chris@16 266 if (rhs.empty())
Chris@16 267 top_element = NULL;
Chris@16 268 else
Chris@16 269 clone_forest(rhs);
Chris@16 270 return *this;
Chris@16 271 }
Chris@16 272
Chris@16 273 ~fibonacci_heap(void)
Chris@16 274 {
Chris@16 275 clear();
Chris@16 276 }
Chris@16 277
Chris@16 278 /// \copydoc boost::heap::priority_queue::empty
Chris@16 279 bool empty(void) const
Chris@16 280 {
Chris@16 281 if (constant_time_size)
Chris@16 282 return size() == 0;
Chris@16 283 else
Chris@16 284 return roots.empty();
Chris@16 285 }
Chris@16 286
Chris@16 287 /// \copydoc boost::heap::priority_queue::size
Chris@16 288 size_type size(void) const
Chris@16 289 {
Chris@16 290 if (constant_time_size)
Chris@16 291 return size_holder::get_size();
Chris@16 292
Chris@16 293 if (empty())
Chris@16 294 return 0;
Chris@16 295 else
Chris@16 296 return detail::count_list_nodes<node, node_list_type>(roots);
Chris@16 297 }
Chris@16 298
Chris@16 299 /// \copydoc boost::heap::priority_queue::max_size
Chris@16 300 size_type max_size(void) const
Chris@16 301 {
Chris@16 302 return allocator_type::max_size();
Chris@16 303 }
Chris@16 304
Chris@16 305 /// \copydoc boost::heap::priority_queue::clear
Chris@16 306 void clear(void)
Chris@16 307 {
Chris@16 308 typedef detail::node_disposer<node, typename node_list_type::value_type, allocator_type> disposer;
Chris@16 309 roots.clear_and_dispose(disposer(*this));
Chris@16 310
Chris@16 311 size_holder::set_size(0);
Chris@16 312 top_element = NULL;
Chris@16 313 }
Chris@16 314
Chris@16 315 /// \copydoc boost::heap::priority_queue::get_allocator
Chris@16 316 allocator_type get_allocator(void) const
Chris@16 317 {
Chris@16 318 return *this;
Chris@16 319 }
Chris@16 320
Chris@16 321 /// \copydoc boost::heap::priority_queue::swap
Chris@16 322 void swap(fibonacci_heap & rhs)
Chris@16 323 {
Chris@16 324 super_t::swap(rhs);
Chris@16 325 std::swap(top_element, rhs.top_element);
Chris@16 326 roots.swap(rhs.roots);
Chris@16 327 }
Chris@16 328
Chris@16 329
Chris@16 330 /// \copydoc boost::heap::priority_queue::top
Chris@16 331 value_type const & top(void) const
Chris@16 332 {
Chris@16 333 BOOST_ASSERT(!empty());
Chris@16 334
Chris@16 335 return super_t::get_value(top_element->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 * \b Complexity: Constant.
Chris@16 342 *
Chris@16 343 * \b Note: Does not invalidate iterators.
Chris@16 344 *
Chris@16 345 * */
Chris@16 346 handle_type push(value_type const & v)
Chris@16 347 {
Chris@16 348 size_holder::increment();
Chris@16 349
Chris@16 350 node_pointer n = allocator_type::allocate(1);
Chris@16 351
Chris@16 352 new(n) node(super_t::make_node(v));
Chris@16 353 roots.push_front(*n);
Chris@16 354
Chris@16 355 if (!top_element || super_t::operator()(top_element->value, n->value))
Chris@16 356 top_element = 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 * \b Complexity: Constant.
Chris@16 365 *
Chris@16 366 * \b Note: Does not invalidate iterators.
Chris@16 367 *
Chris@16 368 * */
Chris@16 369 template <class... Args>
Chris@16 370 handle_type emplace(Args&&... args)
Chris@16 371 {
Chris@16 372 size_holder::increment();
Chris@16 373
Chris@16 374 node_pointer n = allocator_type::allocate(1);
Chris@16 375
Chris@16 376 new(n) node(super_t::make_node(std::forward<Args>(args)...));
Chris@16 377 roots.push_front(*n);
Chris@16 378
Chris@16 379 if (!top_element || super_t::operator()(top_element->value, n->value))
Chris@16 380 top_element = 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). Linear (worst case).
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 node_pointer element = top_element;
Chris@16 396 roots.erase(node_list_type::s_iterator_to(*element));
Chris@16 397
Chris@16 398 finish_erase_or_pop(element);
Chris@16 399 }
Chris@16 400
Chris@16 401 /**
Chris@16 402 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 403 *
Chris@16 404 * \b Complexity: Logarithmic if current value < v, Constant otherwise.
Chris@16 405 *
Chris@16 406 * */
Chris@16 407 void update (handle_type handle, const_reference v)
Chris@16 408 {
Chris@16 409 if (super_t::operator()(super_t::get_value(handle.node_->value), v))
Chris@16 410 increase(handle, v);
Chris@16 411 else
Chris@16 412 decrease(handle, v);
Chris@16 413 }
Chris@16 414
Chris@16 415 /** \copydoc boost::heap::fibonacci_heap::update(handle_type, const_reference)
Chris@16 416 *
Chris@16 417 * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates
Chris@16 418 * the iterator to the object referred to by the handle.
Chris@16 419 * */
Chris@16 420 void update_lazy(handle_type handle, const_reference v)
Chris@16 421 {
Chris@16 422 handle.node_->value = super_t::make_node(v);
Chris@16 423 update_lazy(handle);
Chris@16 424 }
Chris@16 425
Chris@16 426 /**
Chris@16 427 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 428 *
Chris@16 429 * \b Complexity: Logarithmic.
Chris@16 430 *
Chris@16 431 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 432 * */
Chris@16 433 void update (handle_type handle)
Chris@16 434 {
Chris@16 435 node_pointer n = handle.node_;
Chris@16 436 node_pointer parent = n->get_parent();
Chris@16 437
Chris@16 438 if (parent) {
Chris@16 439 n->parent = NULL;
Chris@16 440 roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n));
Chris@16 441 }
Chris@16 442 add_children_to_root(n);
Chris@16 443 consolidate();
Chris@16 444 }
Chris@16 445
Chris@16 446 /** \copydoc boost::heap::fibonacci_heap::update (handle_type handle)
Chris@16 447 *
Chris@16 448 * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates
Chris@16 449 * the iterator to the object referred to by the handle.
Chris@16 450 * */
Chris@16 451 void update_lazy (handle_type handle)
Chris@16 452 {
Chris@16 453 node_pointer n = handle.node_;
Chris@16 454 node_pointer parent = n->get_parent();
Chris@16 455
Chris@16 456 if (parent) {
Chris@16 457 n->parent = NULL;
Chris@16 458 roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n));
Chris@16 459 }
Chris@16 460 add_children_to_root(n);
Chris@16 461 }
Chris@16 462
Chris@16 463
Chris@16 464 /**
Chris@16 465 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 466 *
Chris@16 467 * \b Complexity: Constant.
Chris@16 468 *
Chris@16 469 * \b Note: The new value is expected to be greater than the current one
Chris@16 470 * */
Chris@16 471 void increase (handle_type handle, const_reference v)
Chris@16 472 {
Chris@16 473 handle.node_->value = super_t::make_node(v);
Chris@16 474 increase(handle);
Chris@16 475 }
Chris@16 476
Chris@16 477 /**
Chris@16 478 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 479 *
Chris@16 480 * \b Complexity: Constant.
Chris@16 481 *
Chris@16 482 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 483 * */
Chris@16 484 void increase (handle_type handle)
Chris@16 485 {
Chris@16 486 node_pointer n = handle.node_;
Chris@16 487
Chris@16 488 if (n->parent) {
Chris@16 489 if (super_t::operator()(n->get_parent()->value, n->value)) {
Chris@16 490 node_pointer parent = n->get_parent();
Chris@16 491 cut(n);
Chris@16 492 cascading_cut(parent);
Chris@16 493 }
Chris@16 494 }
Chris@16 495
Chris@16 496 if (super_t::operator()(top_element->value, n->value)) {
Chris@16 497 top_element = n;
Chris@16 498 return;
Chris@16 499 }
Chris@16 500 }
Chris@16 501
Chris@16 502 /**
Chris@16 503 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 504 *
Chris@16 505 * \b Complexity: Logarithmic.
Chris@16 506 *
Chris@16 507 * \b Note: The new value is expected to be less than the current one
Chris@16 508 * */
Chris@16 509 void decrease (handle_type handle, const_reference v)
Chris@16 510 {
Chris@16 511 handle.node_->value = super_t::make_node(v);
Chris@16 512 decrease(handle);
Chris@16 513 }
Chris@16 514
Chris@16 515 /**
Chris@16 516 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 517 *
Chris@16 518 * \b Complexity: Logarithmic.
Chris@16 519 *
Chris@16 520 * \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 521 * */
Chris@16 522 void decrease (handle_type handle)
Chris@16 523 {
Chris@16 524 update(handle);
Chris@16 525 }
Chris@16 526
Chris@16 527 /**
Chris@16 528 * \b Effects: Removes the element handled by \c handle from the priority_queue.
Chris@16 529 *
Chris@16 530 * \b Complexity: Logarithmic.
Chris@16 531 * */
Chris@16 532 void erase(handle_type const & handle)
Chris@16 533 {
Chris@16 534 node_pointer element = handle.node_;
Chris@16 535 node_pointer parent = element->get_parent();
Chris@16 536
Chris@16 537 if (parent)
Chris@16 538 parent->children.erase(node_list_type::s_iterator_to(*element));
Chris@16 539 else
Chris@16 540 roots.erase(node_list_type::s_iterator_to(*element));
Chris@16 541
Chris@16 542 finish_erase_or_pop(element);
Chris@16 543 }
Chris@16 544
Chris@16 545 /// \copydoc boost::heap::priority_queue::begin
Chris@16 546 iterator begin(void) const
Chris@16 547 {
Chris@16 548 return iterator(roots.begin());
Chris@16 549 }
Chris@16 550
Chris@16 551 /// \copydoc boost::heap::priority_queue::end
Chris@16 552 iterator end(void) const
Chris@16 553 {
Chris@16 554 return iterator(roots.end());
Chris@16 555 }
Chris@16 556
Chris@16 557
Chris@16 558 /**
Chris@16 559 * \b Effects: Returns an ordered iterator to the first element contained in the priority queue.
Chris@16 560 *
Chris@16 561 * \b Note: Ordered iterators traverse the priority queue in heap order.
Chris@16 562 * */
Chris@16 563 ordered_iterator ordered_begin(void) const
Chris@16 564 {
Chris@16 565 return ordered_iterator(roots.begin(), roots.end(), top_element, super_t::value_comp());
Chris@16 566 }
Chris@16 567
Chris@16 568 /**
Chris@101 569 * \b Effects: Returns an ordered iterator to the end of the priority queue.
Chris@16 570 *
Chris@16 571 * \b Note: Ordered iterators traverse the priority queue in heap order.
Chris@16 572 * */
Chris@16 573 ordered_iterator ordered_end(void) const
Chris@16 574 {
Chris@16 575 return ordered_iterator(NULL, super_t::value_comp());
Chris@16 576 }
Chris@16 577
Chris@16 578 /**
Chris@16 579 * \b Effects: Merge with priority queue rhs.
Chris@16 580 *
Chris@16 581 * \b Complexity: Constant.
Chris@16 582 *
Chris@16 583 * */
Chris@16 584 void merge(fibonacci_heap & rhs)
Chris@16 585 {
Chris@16 586 size_holder::add(rhs.get_size());
Chris@16 587
Chris@16 588 if (!top_element ||
Chris@16 589 (rhs.top_element && super_t::operator()(top_element->value, rhs.top_element->value)))
Chris@16 590 top_element = rhs.top_element;
Chris@16 591
Chris@16 592 roots.splice(roots.end(), rhs.roots);
Chris@16 593
Chris@101 594 rhs.top_element = NULL;
Chris@16 595 rhs.set_size(0);
Chris@16 596
Chris@16 597 super_t::set_stability_count((std::max)(super_t::get_stability_count(),
Chris@16 598 rhs.get_stability_count()));
Chris@16 599 rhs.set_stability_count(0);
Chris@16 600 }
Chris@16 601
Chris@16 602 /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator
Chris@16 603 static handle_type s_handle_from_iterator(iterator const & it)
Chris@16 604 {
Chris@16 605 node * ptr = const_cast<node *>(it.get_node());
Chris@16 606 return handle_type(ptr);
Chris@16 607 }
Chris@16 608
Chris@16 609 /// \copydoc boost::heap::priority_queue::value_comp
Chris@16 610 value_compare const & value_comp(void) const
Chris@16 611 {
Chris@16 612 return super_t::value_comp();
Chris@16 613 }
Chris@16 614
Chris@16 615 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
Chris@16 616 template <typename HeapType>
Chris@16 617 bool operator<(HeapType const & rhs) const
Chris@16 618 {
Chris@16 619 return detail::heap_compare(*this, rhs);
Chris@16 620 }
Chris@16 621
Chris@16 622 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
Chris@16 623 template <typename HeapType>
Chris@16 624 bool operator>(HeapType const & rhs) const
Chris@16 625 {
Chris@16 626 return detail::heap_compare(rhs, *this);
Chris@16 627 }
Chris@16 628
Chris@16 629 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
Chris@16 630 template <typename HeapType>
Chris@16 631 bool operator>=(HeapType const & rhs) const
Chris@16 632 {
Chris@16 633 return !operator<(rhs);
Chris@16 634 }
Chris@16 635
Chris@16 636 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
Chris@16 637 template <typename HeapType>
Chris@16 638 bool operator<=(HeapType const & rhs) const
Chris@16 639 {
Chris@16 640 return !operator>(rhs);
Chris@16 641 }
Chris@16 642
Chris@16 643 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
Chris@16 644 template <typename HeapType>
Chris@16 645 bool operator==(HeapType const & rhs) const
Chris@16 646 {
Chris@16 647 return detail::heap_equality(*this, rhs);
Chris@16 648 }
Chris@16 649
Chris@16 650 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
Chris@16 651 template <typename HeapType>
Chris@16 652 bool operator!=(HeapType const & rhs) const
Chris@16 653 {
Chris@16 654 return !(*this == rhs);
Chris@16 655 }
Chris@16 656
Chris@16 657 private:
Chris@16 658 #if !defined(BOOST_DOXYGEN_INVOKED)
Chris@16 659 void clone_forest(fibonacci_heap const & rhs)
Chris@16 660 {
Chris@16 661 BOOST_HEAP_ASSERT(roots.empty());
Chris@16 662 typedef typename node::template node_cloner<allocator_type> node_cloner;
Chris@16 663 roots.clone_from(rhs.roots, node_cloner(*this, NULL), detail::nop_disposer());
Chris@16 664
Chris@16 665 top_element = detail::find_max_child<node_list_type, node, internal_compare>(roots, super_t::get_internal_cmp());
Chris@16 666 }
Chris@16 667
Chris@16 668 void cut(node_pointer n)
Chris@16 669 {
Chris@16 670 node_pointer parent = n->get_parent();
Chris@16 671 roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n));
Chris@16 672 n->parent = 0;
Chris@16 673 n->mark = false;
Chris@16 674 }
Chris@16 675
Chris@16 676 void cascading_cut(node_pointer n)
Chris@16 677 {
Chris@16 678 node_pointer parent = n->get_parent();
Chris@16 679
Chris@16 680 if (parent) {
Chris@16 681 if (!parent->mark)
Chris@16 682 parent->mark = true;
Chris@16 683 else {
Chris@16 684 cut(n);
Chris@16 685 cascading_cut(parent);
Chris@16 686 }
Chris@16 687 }
Chris@16 688 }
Chris@16 689
Chris@16 690 void add_children_to_root(node_pointer n)
Chris@16 691 {
Chris@16 692 for (node_list_iterator it = n->children.begin(); it != n->children.end(); ++it) {
Chris@16 693 node_pointer child = static_cast<node_pointer>(&*it);
Chris@16 694 child->parent = 0;
Chris@16 695 }
Chris@16 696
Chris@16 697 roots.splice(roots.end(), n->children);
Chris@16 698 }
Chris@16 699
Chris@16 700 void consolidate(void)
Chris@16 701 {
Chris@16 702 if (roots.empty())
Chris@16 703 return;
Chris@16 704
Chris@16 705 static const size_type max_log2 = sizeof(size_type) * 8;
Chris@16 706 boost::array<node_pointer, max_log2> aux;
Chris@16 707 aux.assign(NULL);
Chris@16 708
Chris@16 709 node_list_iterator it = roots.begin();
Chris@16 710 top_element = static_cast<node_pointer>(&*it);
Chris@16 711
Chris@16 712 do {
Chris@16 713 node_pointer n = static_cast<node_pointer>(&*it);
Chris@16 714 ++it;
Chris@16 715 size_type node_rank = n->child_count();
Chris@16 716
Chris@16 717 if (aux[node_rank] == NULL)
Chris@16 718 aux[node_rank] = n;
Chris@16 719 else {
Chris@16 720 do {
Chris@16 721 node_pointer other = aux[node_rank];
Chris@16 722 if (super_t::operator()(n->value, other->value))
Chris@16 723 std::swap(n, other);
Chris@16 724
Chris@16 725 if (other->parent)
Chris@16 726 n->children.splice(n->children.end(), other->parent->children, node_list_type::s_iterator_to(*other));
Chris@16 727 else
Chris@16 728 n->children.splice(n->children.end(), roots, node_list_type::s_iterator_to(*other));
Chris@16 729
Chris@16 730 other->parent = n;
Chris@16 731
Chris@16 732 aux[node_rank] = NULL;
Chris@16 733 node_rank = n->child_count();
Chris@16 734 } while (aux[node_rank] != NULL);
Chris@16 735 aux[node_rank] = n;
Chris@16 736 }
Chris@16 737
Chris@16 738 if (super_t::operator()(top_element->value, n->value))
Chris@16 739 top_element = n;
Chris@16 740 }
Chris@16 741 while (it != roots.end());
Chris@16 742 }
Chris@16 743
Chris@16 744 void finish_erase_or_pop(node_pointer erased_node)
Chris@16 745 {
Chris@16 746 add_children_to_root(erased_node);
Chris@16 747
Chris@16 748 erased_node->~node();
Chris@16 749 allocator_type::deallocate(erased_node, 1);
Chris@16 750
Chris@16 751 size_holder::decrement();
Chris@16 752 if (!empty())
Chris@16 753 consolidate();
Chris@16 754 else
Chris@16 755 top_element = NULL;
Chris@16 756 }
Chris@16 757
Chris@16 758 mutable node_pointer top_element;
Chris@16 759 node_list_type roots;
Chris@16 760 #endif
Chris@16 761 };
Chris@16 762
Chris@16 763 } /* namespace heap */
Chris@16 764 } /* namespace boost */
Chris@16 765
Chris@16 766 #undef BOOST_HEAP_ASSERT
Chris@16 767
Chris@16 768 #endif /* BOOST_HEAP_FIBONACCI_HEAP_HPP */