annotate DEPENDENCIES/generic/include/boost/heap/skew_heap.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // boost heap: skew 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_SKEW_HEAP_HPP
Chris@16 10 #define BOOST_HEAP_SKEW_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 #include <boost/array.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@16 27
Chris@16 28 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 29 #ifdef BOOST_HEAP_SANITYCHECKS
Chris@16 30 #define BOOST_HEAP_ASSERT BOOST_ASSERT
Chris@16 31 #else
Chris@16 32 #define BOOST_HEAP_ASSERT(expression)
Chris@16 33 #endif
Chris@16 34 #endif
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37 namespace heap {
Chris@16 38 namespace detail {
Chris@16 39
Chris@16 40 template <typename node_pointer, bool store_parent_pointer>
Chris@16 41 struct parent_holder
Chris@16 42 {
Chris@16 43 parent_holder(void):
Chris@16 44 parent_(NULL)
Chris@16 45 {}
Chris@16 46
Chris@16 47 void set_parent(node_pointer parent)
Chris@16 48 {
Chris@16 49 BOOST_HEAP_ASSERT(static_cast<node_pointer>(this) != parent);
Chris@16 50 parent_ = parent;
Chris@16 51 }
Chris@16 52
Chris@16 53 node_pointer get_parent(void) const
Chris@16 54 {
Chris@16 55 return parent_;
Chris@16 56 }
Chris@16 57
Chris@16 58 node_pointer parent_;
Chris@16 59 };
Chris@16 60
Chris@16 61 template <typename node_pointer>
Chris@16 62 struct parent_holder<node_pointer, false>
Chris@16 63 {
Chris@16 64 void set_parent(node_pointer parent)
Chris@16 65 {}
Chris@16 66
Chris@16 67 node_pointer get_parent(void) const
Chris@16 68 {
Chris@16 69 return NULL;
Chris@16 70 }
Chris@16 71 };
Chris@16 72
Chris@16 73
Chris@16 74 template <typename value_type, bool store_parent_pointer>
Chris@16 75 struct skew_heap_node:
Chris@16 76 parent_holder<skew_heap_node<value_type, store_parent_pointer>*, store_parent_pointer>
Chris@16 77 {
Chris@16 78 typedef parent_holder<skew_heap_node<value_type, store_parent_pointer>*, store_parent_pointer> super_t;
Chris@16 79
Chris@16 80 typedef boost::array<skew_heap_node*, 2> child_list_type;
Chris@16 81 typedef typename child_list_type::iterator child_iterator;
Chris@16 82 typedef typename child_list_type::const_iterator const_child_iterator;
Chris@16 83
Chris@16 84 skew_heap_node(value_type const & v):
Chris@16 85 value(v)
Chris@16 86 {
Chris@16 87 children.assign(0);
Chris@16 88 }
Chris@16 89
Chris@16 90 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 91 skew_heap_node(value_type && v):
Chris@16 92 value(v)
Chris@16 93 {
Chris@16 94 children.assign(0);
Chris@16 95 }
Chris@16 96 #endif
Chris@16 97
Chris@16 98 template <typename Alloc>
Chris@16 99 skew_heap_node (skew_heap_node const & rhs, Alloc & allocator, skew_heap_node * parent):
Chris@16 100 value(rhs.value)
Chris@16 101 {
Chris@16 102 super_t::set_parent(parent);
Chris@16 103 node_cloner<skew_heap_node, skew_heap_node, Alloc> cloner(allocator);
Chris@16 104 clone_child(0, rhs, cloner);
Chris@16 105 clone_child(1, rhs, cloner);
Chris@16 106 }
Chris@16 107
Chris@16 108 template <typename Cloner>
Chris@16 109 void clone_child(int index, skew_heap_node const & rhs, Cloner & cloner)
Chris@16 110 {
Chris@16 111 if (rhs.children[index])
Chris@16 112 children[index] = cloner(*rhs.children[index], this);
Chris@16 113 else
Chris@16 114 children[index] = NULL;
Chris@16 115 }
Chris@16 116
Chris@16 117 template <typename Alloc>
Chris@16 118 void clear_subtree(Alloc & alloc)
Chris@16 119 {
Chris@16 120 node_disposer<skew_heap_node, skew_heap_node, Alloc> disposer(alloc);
Chris@16 121 dispose_child(children[0], disposer);
Chris@16 122 dispose_child(children[1], disposer);
Chris@16 123 }
Chris@16 124
Chris@16 125 template <typename Disposer>
Chris@16 126 void dispose_child(skew_heap_node * node, Disposer & disposer)
Chris@16 127 {
Chris@16 128 if (node)
Chris@16 129 disposer(node);
Chris@16 130 }
Chris@16 131
Chris@16 132 std::size_t count_children(void) const
Chris@16 133 {
Chris@16 134 size_t ret = 1;
Chris@16 135 if (children[0])
Chris@16 136 ret += children[0]->count_children();
Chris@16 137 if (children[1])
Chris@16 138 ret += children[1]->count_children();
Chris@16 139
Chris@16 140 return ret;
Chris@16 141 }
Chris@16 142
Chris@16 143 template <typename HeapBase>
Chris@16 144 bool is_heap(typename HeapBase::value_compare const & cmp) const
Chris@16 145 {
Chris@16 146 for (const_child_iterator it = children.begin(); it != children.end(); ++it) {
Chris@16 147 const skew_heap_node * child = *it;
Chris@16 148
Chris@16 149 if (child == NULL)
Chris@16 150 continue;
Chris@16 151
Chris@16 152 if (store_parent_pointer)
Chris@16 153 BOOST_HEAP_ASSERT(child->get_parent() == this);
Chris@16 154
Chris@16 155 if (cmp(HeapBase::get_value(value), HeapBase::get_value(child->value)) ||
Chris@16 156 !child->is_heap<HeapBase>(cmp))
Chris@16 157 return false;
Chris@16 158 }
Chris@16 159 return true;
Chris@16 160 }
Chris@16 161
Chris@16 162 value_type value;
Chris@16 163 boost::array<skew_heap_node*, 2> children;
Chris@16 164 };
Chris@16 165
Chris@16 166
Chris@16 167 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
Chris@16 168 boost::parameter::optional<tag::compare>,
Chris@16 169 boost::parameter::optional<tag::stable>,
Chris@16 170 boost::parameter::optional<tag::store_parent_pointer>,
Chris@16 171 boost::parameter::optional<tag::stability_counter_type>,
Chris@16 172 boost::parameter::optional<tag::constant_time_size>,
Chris@16 173 boost::parameter::optional<tag::mutable_>
Chris@16 174 > skew_heap_signature;
Chris@16 175
Chris@16 176 template <typename T, typename BoundArgs>
Chris@16 177 struct make_skew_heap_base
Chris@16 178 {
Chris@16 179 static const bool constant_time_size = parameter::binding<BoundArgs,
Chris@16 180 tag::constant_time_size,
Chris@16 181 boost::mpl::true_
Chris@16 182 >::type::value;
Chris@16 183
Chris@16 184 typedef typename make_heap_base<T, BoundArgs, constant_time_size>::type base_type;
Chris@16 185 typedef typename make_heap_base<T, BoundArgs, constant_time_size>::allocator_argument allocator_argument;
Chris@16 186 typedef typename make_heap_base<T, BoundArgs, constant_time_size>::compare_argument compare_argument;
Chris@16 187
Chris@16 188 static const bool is_mutable = extract_mutable<BoundArgs>::value;
Chris@16 189 static const bool store_parent_pointer = parameter::binding<BoundArgs,
Chris@16 190 tag::store_parent_pointer,
Chris@16 191 boost::mpl::false_>::type::value || is_mutable;
Chris@16 192
Chris@16 193 typedef skew_heap_node<typename base_type::internal_type, store_parent_pointer> node_type;
Chris@16 194
Chris@16 195 typedef typename allocator_argument::template rebind<node_type>::other allocator_type;
Chris@16 196
Chris@16 197 struct type:
Chris@16 198 base_type,
Chris@16 199 allocator_type
Chris@16 200 {
Chris@16 201 type(compare_argument const & arg):
Chris@16 202 base_type(arg)
Chris@16 203 {}
Chris@16 204
Chris@16 205 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 206 type(type && rhs):
Chris@16 207 base_type(std::move(static_cast<base_type&>(rhs))),
Chris@16 208 allocator_type(std::move(static_cast<allocator_type&>(rhs)))
Chris@16 209 {}
Chris@16 210
Chris@16 211 type(type const & rhs):
Chris@16 212 base_type(rhs),
Chris@16 213 allocator_type(rhs)
Chris@16 214 {}
Chris@16 215
Chris@16 216 type & operator=(type && rhs)
Chris@16 217 {
Chris@16 218 base_type::operator=(std::move(static_cast<base_type&>(rhs)));
Chris@16 219 allocator_type::operator=(std::move(static_cast<allocator_type&>(rhs)));
Chris@16 220 return *this;
Chris@16 221 }
Chris@16 222
Chris@16 223 type & operator=(type const & rhs)
Chris@16 224 {
Chris@16 225 base_type::operator=(static_cast<base_type const &>(rhs));
Chris@16 226 allocator_type::operator=(static_cast<allocator_type const &>(rhs));
Chris@16 227 return *this;
Chris@16 228 }
Chris@16 229 #endif
Chris@16 230 };
Chris@16 231 };
Chris@16 232
Chris@16 233 } /* namespace detail */
Chris@16 234
Chris@16 235 /**
Chris@16 236 * \class skew_heap
Chris@16 237 * \brief skew heap
Chris@16 238 *
Chris@16 239 *
Chris@16 240 * The template parameter T is the type to be managed by the container.
Chris@16 241 * The user can specify additional options and if no options are provided default options are used.
Chris@16 242 *
Chris@16 243 * The container supports the following options:
Chris@16 244 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
Chris@16 245 * - \c boost::heap::stable<>, defaults to \c stable<false>
Chris@16 246 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
Chris@16 247 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
Chris@16 248 * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size<true>
Chris@16 249 * - \c boost::heap::store_parent_pointer<>, defaults to \c store_parent_pointer<true>. Maintaining a parent pointer adds some
Chris@16 250 * maintenance and size overhead, but iterating a heap is more efficient.
Chris@16 251 * - \c boost::heap::mutable<>, defaults to \c mutable<false>.
Chris@16 252 *
Chris@16 253 */
Chris@16 254 #ifdef BOOST_DOXYGEN_INVOKED
Chris@16 255 template<class T, class ...Options>
Chris@16 256 #else
Chris@16 257 template <typename T,
Chris@16 258 class A0 = boost::parameter::void_,
Chris@16 259 class A1 = boost::parameter::void_,
Chris@16 260 class A2 = boost::parameter::void_,
Chris@16 261 class A3 = boost::parameter::void_,
Chris@16 262 class A4 = boost::parameter::void_,
Chris@16 263 class A5 = boost::parameter::void_,
Chris@16 264 class A6 = boost::parameter::void_
Chris@16 265 >
Chris@16 266 #endif
Chris@16 267 class skew_heap:
Chris@16 268 private detail::make_skew_heap_base<T,
Chris@16 269 typename detail::skew_heap_signature::bind<A0, A1, A2, A3, A4, A5, A6>::type
Chris@16 270 >::type
Chris@16 271 {
Chris@16 272 typedef typename detail::skew_heap_signature::bind<A0, A1, A2, A3, A4, A5, A6>::type bound_args;
Chris@16 273 typedef detail::make_skew_heap_base<T, bound_args> base_maker;
Chris@16 274 typedef typename base_maker::type super_t;
Chris@16 275
Chris@16 276 typedef typename super_t::internal_type internal_type;
Chris@16 277 typedef typename super_t::size_holder_type size_holder;
Chris@16 278 typedef typename base_maker::allocator_argument allocator_argument;
Chris@16 279
Chris@16 280 static const bool store_parent_pointer = base_maker::store_parent_pointer;
Chris@16 281 template <typename Heap1, typename Heap2>
Chris@16 282 friend struct heap_merge_emulate;
Chris@16 283
Chris@16 284 struct implementation_defined:
Chris@16 285 detail::extract_allocator_types<typename base_maker::allocator_argument>
Chris@16 286 {
Chris@16 287 typedef T value_type;
Chris@16 288
Chris@16 289 typedef typename base_maker::compare_argument value_compare;
Chris@16 290 typedef typename base_maker::allocator_type allocator_type;
Chris@16 291
Chris@16 292 typedef typename base_maker::node_type node;
Chris@16 293 typedef typename allocator_type::pointer node_pointer;
Chris@16 294 typedef typename allocator_type::const_pointer const_node_pointer;
Chris@16 295
Chris@16 296 typedef detail::value_extractor<value_type, internal_type, super_t> value_extractor;
Chris@16 297
Chris@16 298 typedef boost::array<node_pointer, 2> child_list_type;
Chris@16 299 typedef typename child_list_type::iterator child_list_iterator;
Chris@16 300
Chris@16 301 typedef typename boost::mpl::if_c<false,
Chris@16 302 detail::recursive_tree_iterator<node,
Chris@16 303 child_list_iterator,
Chris@16 304 const value_type,
Chris@16 305 value_extractor,
Chris@16 306 detail::list_iterator_converter<node,
Chris@16 307 child_list_type
Chris@16 308 >
Chris@16 309 >,
Chris@16 310 detail::tree_iterator<node,
Chris@16 311 const value_type,
Chris@16 312 allocator_type,
Chris@16 313 value_extractor,
Chris@16 314 detail::dereferencer<node>,
Chris@16 315 true,
Chris@16 316 false,
Chris@16 317 value_compare
Chris@16 318 >
Chris@16 319 >::type iterator;
Chris@16 320
Chris@16 321 typedef iterator const_iterator;
Chris@16 322
Chris@16 323 typedef detail::tree_iterator<node,
Chris@16 324 const value_type,
Chris@16 325 allocator_type,
Chris@16 326 value_extractor,
Chris@16 327 detail::dereferencer<node>,
Chris@16 328 true,
Chris@16 329 true,
Chris@16 330 value_compare
Chris@16 331 > ordered_iterator;
Chris@16 332
Chris@16 333 typedef typename detail::extract_allocator_types<typename base_maker::allocator_argument>::reference reference;
Chris@16 334 typedef detail::node_handle<node_pointer, super_t, reference> handle_type;
Chris@16 335 };
Chris@16 336
Chris@16 337 typedef typename implementation_defined::value_extractor value_extractor;
Chris@16 338 typedef typename implementation_defined::node node;
Chris@16 339 typedef typename implementation_defined::node_pointer node_pointer;
Chris@16 340
Chris@16 341 public:
Chris@16 342 typedef T value_type;
Chris@16 343
Chris@16 344 typedef typename implementation_defined::size_type size_type;
Chris@16 345 typedef typename implementation_defined::difference_type difference_type;
Chris@16 346 typedef typename implementation_defined::value_compare value_compare;
Chris@16 347 typedef typename implementation_defined::allocator_type allocator_type;
Chris@16 348 typedef typename implementation_defined::reference reference;
Chris@16 349 typedef typename implementation_defined::const_reference const_reference;
Chris@16 350 typedef typename implementation_defined::pointer pointer;
Chris@16 351 typedef typename implementation_defined::const_pointer const_pointer;
Chris@16 352
Chris@16 353 /// \copydoc boost::heap::priority_queue::iterator
Chris@16 354 typedef typename implementation_defined::iterator iterator;
Chris@16 355 typedef typename implementation_defined::const_iterator const_iterator;
Chris@16 356 typedef typename implementation_defined::ordered_iterator ordered_iterator;
Chris@16 357
Chris@16 358 static const bool constant_time_size = super_t::constant_time_size;
Chris@16 359 static const bool has_ordered_iterators = true;
Chris@16 360 static const bool is_mergable = true;
Chris@16 361 static const bool is_stable = detail::extract_stable<bound_args>::value;
Chris@16 362 static const bool has_reserve = false;
Chris@16 363 static const bool is_mutable = detail::extract_mutable<bound_args>::value;
Chris@16 364
Chris@16 365 typedef typename mpl::if_c<is_mutable, typename implementation_defined::handle_type, void*>::type handle_type;
Chris@16 366
Chris@16 367 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
Chris@16 368 explicit skew_heap(value_compare const & cmp = value_compare()):
Chris@16 369 super_t(cmp), root(NULL)
Chris@16 370 {}
Chris@16 371
Chris@16 372 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
Chris@16 373 skew_heap(skew_heap const & rhs):
Chris@16 374 super_t(rhs), root(0)
Chris@16 375 {
Chris@16 376 if (rhs.empty())
Chris@16 377 return;
Chris@16 378
Chris@16 379 clone_tree(rhs);
Chris@16 380 size_holder::set_size(rhs.get_size());
Chris@16 381 }
Chris@16 382
Chris@16 383 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs)
Chris@16 384 skew_heap & operator=(skew_heap const & rhs)
Chris@16 385 {
Chris@16 386 clear();
Chris@16 387 size_holder::set_size(rhs.get_size());
Chris@16 388 static_cast<super_t&>(*this) = rhs;
Chris@16 389
Chris@16 390 clone_tree(rhs);
Chris@16 391 return *this;
Chris@16 392 }
Chris@16 393
Chris@16 394 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 395 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
Chris@16 396 skew_heap(skew_heap && rhs):
Chris@16 397 super_t(std::move(rhs)), root(rhs.root)
Chris@16 398 {
Chris@16 399 rhs.root = NULL;
Chris@16 400 }
Chris@16 401
Chris@16 402 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
Chris@16 403 skew_heap & operator=(skew_heap && rhs)
Chris@16 404 {
Chris@16 405 super_t::operator=(std::move(rhs));
Chris@16 406 root = rhs.root;
Chris@16 407 rhs.root = NULL;
Chris@16 408 return *this;
Chris@16 409 }
Chris@16 410 #endif
Chris@16 411
Chris@16 412 ~skew_heap(void)
Chris@16 413 {
Chris@16 414 clear();
Chris@16 415 }
Chris@16 416
Chris@16 417 /**
Chris@16 418 * \b Effects: Adds a new element to the priority queue.
Chris@16 419 *
Chris@16 420 * \b Complexity: Logarithmic (amortized).
Chris@16 421 *
Chris@16 422 * */
Chris@16 423 typename mpl::if_c<is_mutable, handle_type, void>::type push(value_type const & v)
Chris@16 424 {
Chris@16 425 typedef typename mpl::if_c<is_mutable, push_handle, push_void>::type push_helper;
Chris@16 426 return push_helper::push(this, v);
Chris@16 427 }
Chris@16 428
Chris@16 429 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
Chris@16 430 /**
Chris@16 431 * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place.
Chris@16 432 *
Chris@16 433 * \b Complexity: Logarithmic (amortized).
Chris@16 434 *
Chris@16 435 * */
Chris@16 436 template <typename... Args>
Chris@16 437 typename mpl::if_c<is_mutable, handle_type, void>::type emplace(Args&&... args)
Chris@16 438 {
Chris@16 439 typedef typename mpl::if_c<is_mutable, push_handle, push_void>::type push_helper;
Chris@16 440 return push_helper::emplace(this, std::forward<Args>(args)...);
Chris@16 441 }
Chris@16 442 #endif
Chris@16 443
Chris@16 444 /// \copydoc boost::heap::priority_queue::empty
Chris@16 445 bool empty(void) const
Chris@16 446 {
Chris@16 447 return root == NULL;
Chris@16 448 }
Chris@16 449
Chris@16 450 /// \copydoc boost::heap::binomial_heap::size
Chris@16 451 size_type size(void) const
Chris@16 452 {
Chris@16 453 if (constant_time_size)
Chris@16 454 return size_holder::get_size();
Chris@16 455
Chris@16 456 if (root == NULL)
Chris@16 457 return 0;
Chris@16 458 else
Chris@16 459 return root->count_children();
Chris@16 460 }
Chris@16 461
Chris@16 462 /// \copydoc boost::heap::priority_queue::max_size
Chris@16 463 size_type max_size(void) const
Chris@16 464 {
Chris@16 465 return allocator_type::max_size();
Chris@16 466 }
Chris@16 467
Chris@16 468 /// \copydoc boost::heap::priority_queue::clear
Chris@16 469 void clear(void)
Chris@16 470 {
Chris@16 471 if (empty())
Chris@16 472 return;
Chris@16 473
Chris@16 474 root->template clear_subtree<allocator_type>(*this);
Chris@16 475 root->~node();
Chris@16 476 allocator_type::deallocate(root, 1);
Chris@16 477
Chris@16 478 root = NULL;
Chris@16 479 size_holder::set_size(0);
Chris@16 480 }
Chris@16 481
Chris@16 482 /// \copydoc boost::heap::priority_queue::get_allocator
Chris@16 483 allocator_type get_allocator(void) const
Chris@16 484 {
Chris@16 485 return *this;
Chris@16 486 }
Chris@16 487
Chris@16 488 /// \copydoc boost::heap::priority_queue::swap
Chris@16 489 void swap(skew_heap & rhs)
Chris@16 490 {
Chris@16 491 super_t::swap(rhs);
Chris@16 492 std::swap(root, rhs.root);
Chris@16 493 }
Chris@16 494
Chris@16 495 /// \copydoc boost::heap::priority_queue::top
Chris@16 496 const_reference top(void) const
Chris@16 497 {
Chris@16 498 BOOST_ASSERT(!empty());
Chris@16 499
Chris@16 500 return super_t::get_value(root->value);
Chris@16 501 }
Chris@16 502
Chris@16 503 /**
Chris@16 504 * \b Effects: Removes the top element from the priority queue.
Chris@16 505 *
Chris@16 506 * \b Complexity: Logarithmic (amortized).
Chris@16 507 *
Chris@16 508 * */
Chris@16 509 void pop(void)
Chris@16 510 {
Chris@16 511 BOOST_ASSERT(!empty());
Chris@16 512
Chris@16 513 node_pointer top = root;
Chris@16 514
Chris@16 515 root = merge_children(root);
Chris@16 516 size_holder::decrement();
Chris@16 517
Chris@16 518 if (root)
Chris@16 519 BOOST_HEAP_ASSERT(root->get_parent() == NULL);
Chris@16 520 else
Chris@16 521 BOOST_HEAP_ASSERT(size_holder::get_size() == 0);
Chris@16 522
Chris@16 523 top->~node();
Chris@16 524 allocator_type::deallocate(top, 1);
Chris@16 525 sanity_check();
Chris@16 526 }
Chris@16 527
Chris@16 528 /// \copydoc boost::heap::priority_queue::begin
Chris@16 529 iterator begin(void) const
Chris@16 530 {
Chris@16 531 return iterator(root, super_t::value_comp());
Chris@16 532 }
Chris@16 533
Chris@16 534 /// \copydoc boost::heap::priority_queue::end
Chris@16 535 iterator end(void) const
Chris@16 536 {
Chris@16 537 return iterator();
Chris@16 538 }
Chris@16 539
Chris@16 540 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
Chris@16 541 ordered_iterator ordered_begin(void) const
Chris@16 542 {
Chris@16 543 return ordered_iterator(root, super_t::value_comp());
Chris@16 544 }
Chris@16 545
Chris@16 546 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
Chris@16 547 ordered_iterator ordered_end(void) const
Chris@16 548 {
Chris@16 549 return ordered_iterator(0, super_t::value_comp());
Chris@16 550 }
Chris@16 551
Chris@16 552 /**
Chris@16 553 * \b Effects: Merge all elements from rhs into this
Chris@16 554 *
Chris@16 555 * \b Complexity: Logarithmic (amortized).
Chris@16 556 *
Chris@16 557 * */
Chris@16 558 void merge(skew_heap & rhs)
Chris@16 559 {
Chris@16 560 if (rhs.empty())
Chris@16 561 return;
Chris@16 562
Chris@16 563 merge_node(rhs.root);
Chris@16 564
Chris@16 565 size_holder::add(rhs.get_size());
Chris@16 566 rhs.set_size(0);
Chris@16 567 rhs.root = NULL;
Chris@16 568 sanity_check();
Chris@16 569
Chris@16 570 super_t::set_stability_count((std::max)(super_t::get_stability_count(),
Chris@16 571 rhs.get_stability_count()));
Chris@16 572 rhs.set_stability_count(0);
Chris@16 573 }
Chris@16 574
Chris@16 575 /// \copydoc boost::heap::priority_queue::value_comp
Chris@16 576 value_compare const & value_comp(void) const
Chris@16 577 {
Chris@16 578 return super_t::value_comp();
Chris@16 579 }
Chris@16 580
Chris@16 581 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
Chris@16 582 template <typename HeapType>
Chris@16 583 bool operator<(HeapType const & rhs) const
Chris@16 584 {
Chris@16 585 return detail::heap_compare(*this, rhs);
Chris@16 586 }
Chris@16 587
Chris@16 588 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
Chris@16 589 template <typename HeapType>
Chris@16 590 bool operator>(HeapType const & rhs) const
Chris@16 591 {
Chris@16 592 return detail::heap_compare(rhs, *this);
Chris@16 593 }
Chris@16 594
Chris@16 595 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
Chris@16 596 template <typename HeapType>
Chris@16 597 bool operator>=(HeapType const & rhs) const
Chris@16 598 {
Chris@16 599 return !operator<(rhs);
Chris@16 600 }
Chris@16 601
Chris@16 602 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
Chris@16 603 template <typename HeapType>
Chris@16 604 bool operator<=(HeapType const & rhs) const
Chris@16 605 {
Chris@16 606 return !operator>(rhs);
Chris@16 607 }
Chris@16 608
Chris@16 609 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
Chris@16 610 template <typename HeapType>
Chris@16 611 bool operator==(HeapType const & rhs) const
Chris@16 612 {
Chris@16 613 return detail::heap_equality(*this, rhs);
Chris@16 614 }
Chris@16 615
Chris@16 616 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
Chris@16 617 template <typename HeapType>
Chris@16 618 bool operator!=(HeapType const & rhs) const
Chris@16 619 {
Chris@16 620 return !(*this == rhs);
Chris@16 621 }
Chris@16 622
Chris@16 623
Chris@16 624 /// \copydoc boost::heap::d_ary_heap::s_handle_from_iterator
Chris@16 625 static handle_type s_handle_from_iterator(iterator const & it)
Chris@16 626 {
Chris@16 627 node * ptr = const_cast<node *>(it.get_node());
Chris@16 628 return handle_type(ptr);
Chris@16 629 }
Chris@16 630
Chris@16 631 /**
Chris@16 632 * \b Effects: Removes the element handled by \c handle from the priority_queue.
Chris@16 633 *
Chris@16 634 * \b Complexity: Logarithmic (amortized).
Chris@16 635 * */
Chris@16 636 void erase (handle_type object)
Chris@16 637 {
Chris@16 638 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 639 node_pointer this_node = object.node_;
Chris@16 640
Chris@16 641 unlink_node(this_node);
Chris@16 642 size_holder::decrement();
Chris@16 643
Chris@16 644 sanity_check();
Chris@16 645 this_node->~node();
Chris@16 646 allocator_type::deallocate(this_node, 1);
Chris@16 647 }
Chris@16 648
Chris@16 649 /**
Chris@16 650 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 651 *
Chris@16 652 * \b Complexity: Logarithmic (amortized).
Chris@16 653 *
Chris@16 654 * */
Chris@16 655 void update (handle_type handle, const_reference v)
Chris@16 656 {
Chris@16 657 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 658 if (super_t::operator()(super_t::get_value(handle.node_->value), v))
Chris@16 659 increase(handle, v);
Chris@16 660 else
Chris@16 661 decrease(handle, v);
Chris@16 662 }
Chris@16 663
Chris@16 664 /**
Chris@16 665 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 666 *
Chris@16 667 * \b Complexity: Logarithmic (amortized).
Chris@16 668 *
Chris@16 669 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 670 * */
Chris@16 671 void update (handle_type handle)
Chris@16 672 {
Chris@16 673 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 674 node_pointer this_node = handle.node_;
Chris@16 675
Chris@16 676 if (this_node->get_parent()) {
Chris@16 677 if (super_t::operator()(super_t::get_value(this_node->get_parent()->value),
Chris@16 678 super_t::get_value(this_node->value)))
Chris@16 679 increase(handle);
Chris@16 680 else
Chris@16 681 decrease(handle);
Chris@16 682 }
Chris@16 683 else
Chris@16 684 decrease(handle);
Chris@16 685 }
Chris@16 686
Chris@16 687 /**
Chris@16 688 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 689 *
Chris@16 690 * \b Complexity: Logarithmic (amortized).
Chris@16 691 *
Chris@16 692 * \b Note: The new value is expected to be greater than the current one
Chris@16 693 * */
Chris@16 694 void increase (handle_type handle, const_reference v)
Chris@16 695 {
Chris@16 696 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 697 handle.node_->value = super_t::make_node(v);
Chris@16 698 increase(handle);
Chris@16 699 }
Chris@16 700
Chris@16 701 /**
Chris@16 702 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 703 *
Chris@16 704 * \b Complexity: Logarithmic (amortized).
Chris@16 705 *
Chris@16 706 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
Chris@16 707 * */
Chris@16 708 void increase (handle_type handle)
Chris@16 709 {
Chris@16 710 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 711 node_pointer this_node = handle.node_;
Chris@16 712
Chris@16 713 if (this_node == root)
Chris@16 714 return;
Chris@16 715
Chris@16 716 node_pointer parent = this_node->get_parent();
Chris@16 717
Chris@16 718 if (this_node == parent->children[0])
Chris@16 719 parent->children[0] = NULL;
Chris@16 720 else
Chris@16 721 parent->children[1] = NULL;
Chris@16 722
Chris@16 723 this_node->set_parent(NULL);
Chris@16 724 merge_node(this_node);
Chris@16 725 }
Chris@16 726
Chris@16 727 /**
Chris@16 728 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
Chris@16 729 *
Chris@16 730 * \b Complexity: Logarithmic (amortized).
Chris@16 731 *
Chris@16 732 * \b Note: The new value is expected to be less than the current one
Chris@16 733 * */
Chris@16 734 void decrease (handle_type handle, const_reference v)
Chris@16 735 {
Chris@16 736 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 737 handle.node_->value = super_t::make_node(v);
Chris@16 738 decrease(handle);
Chris@16 739 }
Chris@16 740
Chris@16 741 /**
Chris@16 742 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
Chris@16 743 *
Chris@16 744 * \b Complexity: Logarithmic (amortized).
Chris@16 745 *
Chris@16 746 * \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 747 * */
Chris@16 748 void decrease (handle_type handle)
Chris@16 749 {
Chris@16 750 BOOST_STATIC_ASSERT(is_mutable);
Chris@16 751 node_pointer this_node = handle.node_;
Chris@16 752
Chris@16 753 unlink_node(this_node);
Chris@16 754 this_node->children.assign(0);
Chris@16 755 this_node->set_parent(NULL);
Chris@16 756 merge_node(this_node);
Chris@16 757 }
Chris@16 758
Chris@16 759 private:
Chris@16 760 #if !defined(BOOST_DOXYGEN_INVOKED)
Chris@16 761 struct push_void
Chris@16 762 {
Chris@16 763 static void push(skew_heap * self, const_reference v)
Chris@16 764 {
Chris@16 765 self->push_internal(v);
Chris@16 766 }
Chris@16 767
Chris@16 768 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
Chris@16 769 template <class... Args>
Chris@16 770 static void emplace(skew_heap * self, Args&&... args)
Chris@16 771 {
Chris@16 772 self->emplace_internal(std::forward<Args>(args)...);
Chris@16 773 }
Chris@16 774 #endif
Chris@16 775 };
Chris@16 776
Chris@16 777 struct push_handle
Chris@16 778 {
Chris@16 779 static handle_type push(skew_heap * self, const_reference v)
Chris@16 780 {
Chris@16 781 return handle_type(self->push_internal(v));
Chris@16 782 }
Chris@16 783
Chris@16 784 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
Chris@16 785 template <class... Args>
Chris@16 786 static handle_type emplace(skew_heap * self, Args&&... args)
Chris@16 787 {
Chris@16 788 return handle_type(self->emplace_internal(std::forward<Args>(args)...));
Chris@16 789 }
Chris@16 790 #endif
Chris@16 791 };
Chris@16 792
Chris@16 793 node_pointer push_internal(const_reference v)
Chris@16 794 {
Chris@16 795 size_holder::increment();
Chris@16 796
Chris@16 797 node_pointer n = super_t::allocate(1);
Chris@16 798 new(n) node(super_t::make_node(v));
Chris@16 799
Chris@16 800 merge_node(n);
Chris@16 801 return n;
Chris@16 802 }
Chris@16 803
Chris@16 804 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
Chris@16 805 template <class... Args>
Chris@16 806 node_pointer emplace_internal(Args&&... args)
Chris@16 807 {
Chris@16 808 size_holder::increment();
Chris@16 809
Chris@16 810 node_pointer n = super_t::allocate(1);
Chris@16 811 new(n) node(super_t::make_node(std::forward<Args>(args)...));
Chris@16 812
Chris@16 813 merge_node(n);
Chris@16 814 return n;
Chris@16 815 }
Chris@16 816 #endif
Chris@16 817
Chris@16 818 void unlink_node(node_pointer node)
Chris@16 819 {
Chris@16 820 node_pointer parent = node->get_parent();
Chris@16 821 node_pointer merged_children = merge_children(node);
Chris@16 822
Chris@16 823 if (parent) {
Chris@16 824 if (node == parent->children[0])
Chris@16 825 parent->children[0] = merged_children;
Chris@16 826 else
Chris@16 827 parent->children[1] = merged_children;
Chris@16 828 }
Chris@16 829 else
Chris@16 830 root = merged_children;
Chris@16 831 }
Chris@16 832
Chris@16 833 void clone_tree(skew_heap const & rhs)
Chris@16 834 {
Chris@16 835 BOOST_HEAP_ASSERT(root == NULL);
Chris@16 836 if (rhs.empty())
Chris@16 837 return;
Chris@16 838
Chris@16 839 root = allocator_type::allocate(1);
Chris@16 840
Chris@16 841 new(root) node(*rhs.root, static_cast<allocator_type&>(*this), NULL);
Chris@16 842 }
Chris@16 843
Chris@16 844 void merge_node(node_pointer other)
Chris@16 845 {
Chris@16 846 BOOST_HEAP_ASSERT(other);
Chris@16 847 if (root != NULL)
Chris@16 848 root = merge_nodes(root, other, NULL);
Chris@16 849 else
Chris@16 850 root = other;
Chris@16 851 }
Chris@16 852
Chris@16 853 node_pointer merge_nodes(node_pointer node1, node_pointer node2, node_pointer new_parent)
Chris@16 854 {
Chris@16 855 if (node1 == NULL) {
Chris@16 856 if (node2)
Chris@16 857 node2->set_parent(new_parent);
Chris@16 858 return node2;
Chris@16 859 }
Chris@16 860 if (node2 == NULL) {
Chris@16 861 node1->set_parent(new_parent);
Chris@16 862 return node1;
Chris@16 863 }
Chris@16 864
Chris@16 865 node_pointer merged = merge_nodes_recursive(node1, node2, new_parent);
Chris@16 866 return merged;
Chris@16 867 }
Chris@16 868
Chris@16 869 node_pointer merge_children(node_pointer node)
Chris@16 870 {
Chris@16 871 node_pointer parent = node->get_parent();
Chris@16 872 node_pointer merged_children = merge_nodes(node->children[0], node->children[1], parent);
Chris@16 873
Chris@16 874 return merged_children;
Chris@16 875 }
Chris@16 876
Chris@16 877 node_pointer merge_nodes_recursive(node_pointer node1, node_pointer node2, node_pointer new_parent)
Chris@16 878 {
Chris@16 879 if (super_t::operator()(node1->value, node2->value))
Chris@16 880 std::swap(node1, node2);
Chris@16 881
Chris@16 882 node * parent = node1;
Chris@16 883 node * child = node2;
Chris@16 884
Chris@16 885 if (parent->children[1]) {
Chris@16 886 node * merged = merge_nodes(parent->children[1], child, parent);
Chris@16 887 parent->children[1] = merged;
Chris@16 888 merged->set_parent(parent);
Chris@16 889 } else {
Chris@16 890 parent->children[1] = child;
Chris@16 891 child->set_parent(parent);
Chris@16 892 }
Chris@16 893
Chris@16 894
Chris@16 895 std::swap(parent->children[0], parent->children[1]);
Chris@16 896 parent->set_parent(new_parent);
Chris@16 897 return parent;
Chris@16 898 }
Chris@16 899
Chris@16 900 void sanity_check(void)
Chris@16 901 {
Chris@16 902 #ifdef BOOST_HEAP_SANITYCHECKS
Chris@16 903 if (root)
Chris@16 904 BOOST_HEAP_ASSERT( root->template is_heap<super_t>(super_t::value_comp()) );
Chris@16 905
Chris@16 906 if (constant_time_size) {
Chris@16 907 size_type stored_size = size_holder::get_size();
Chris@16 908
Chris@16 909 size_type counted_size;
Chris@16 910 if (root == NULL)
Chris@16 911 counted_size = 0;
Chris@16 912 else
Chris@16 913 counted_size = root->count_children();
Chris@16 914
Chris@16 915 BOOST_HEAP_ASSERT(counted_size == stored_size);
Chris@16 916 }
Chris@16 917 #endif
Chris@16 918 }
Chris@16 919
Chris@16 920 node_pointer root;
Chris@16 921 #endif
Chris@16 922 };
Chris@16 923
Chris@16 924 } /* namespace heap */
Chris@16 925 } /* namespace boost */
Chris@16 926
Chris@16 927 #undef BOOST_HEAP_ASSERT
Chris@16 928 #endif /* BOOST_HEAP_SKEW_HEAP_HPP */