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