1 // // boost heap: d-ary heap as container adaptor
3 // Copyright (C) 2010 Tim Blechmann
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)
9 #ifndef BOOST_HEAP_D_ARY_HEAP_HPP
10 #define BOOST_HEAP_D_ARY_HEAP_HPP
16 #include <boost/assert.hpp>
18 #include <boost/mem_fn.hpp>
19 #include <boost/heap/detail/heap_comparison.hpp>
20 #include <boost/heap/detail/ordered_adaptor_iterator.hpp>
21 #include <boost/heap/detail/stable_heap.hpp>
22 #include <boost/heap/detail/mutable_heap.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
29 #ifndef BOOST_DOXYGEN_INVOKED
30 #ifdef BOOST_HEAP_SANITYCHECKS
31 #define BOOST_HEAP_ASSERT BOOST_ASSERT
33 #define BOOST_HEAP_ASSERT(expression)
41 struct nop_index_updater
44 static void run(T &, std::size_t)
48 typedef parameter::parameters<boost::parameter::required<tag::arity>,
49 boost::parameter::optional<tag::allocator>,
50 boost::parameter::optional<tag::compare>,
51 boost::parameter::optional<tag::stable>,
52 boost::parameter::optional<tag::stability_counter_type>,
53 boost::parameter::optional<tag::constant_time_size>
54 > d_ary_heap_signature;
57 /* base class for d-ary heap */
62 private make_heap_base<T, BoundArgs, false>::type
64 typedef make_heap_base<T, BoundArgs, false> heap_base_maker;
66 typedef typename heap_base_maker::type super_t;
67 typedef typename super_t::internal_type internal_type;
69 #ifdef BOOST_NO_CXX11_ALLOCATOR
70 typedef typename heap_base_maker::allocator_argument::template rebind<internal_type>::other internal_type_allocator;
72 typedef typename std::allocator_traits<typename heap_base_maker::allocator_argument>::template rebind_alloc<internal_type> internal_type_allocator;
74 typedef std::vector<internal_type, internal_type_allocator> container_type;
75 typedef typename container_type::const_iterator container_iterator;
77 typedef IndexUpdater index_updater;
81 static const unsigned int D = parameter::binding<BoundArgs, tag::arity>::type::value;
83 template <typename Heap1, typename Heap2>
84 friend struct heap_merge_emulate;
86 struct implementation_defined:
87 extract_allocator_types<typename heap_base_maker::allocator_argument>
90 typedef typename detail::extract_allocator_types<typename heap_base_maker::allocator_argument>::size_type size_type;
92 typedef typename heap_base_maker::compare_argument value_compare;
93 typedef typename heap_base_maker::allocator_argument allocator_type;
95 struct ordered_iterator_dispatcher
97 static size_type max_index(const d_ary_heap * heap)
99 return heap->q_.size() - 1;
102 static bool is_leaf(const d_ary_heap * heap, size_type index)
104 return !heap->not_leaf(index);
107 static std::pair<size_type, size_type> get_child_nodes(const d_ary_heap * heap, size_type index)
109 BOOST_HEAP_ASSERT(!is_leaf(heap, index));
110 return std::make_pair(d_ary_heap::first_child_index(index),
111 heap->last_child_index(index));
114 static internal_type const & get_internal_value(const d_ary_heap * heap, size_type index)
116 return heap->q_[index];
119 static value_type const & get_value(internal_type const & arg)
121 return super_t::get_value(arg);
125 typedef detail::ordered_adaptor_iterator<const value_type,
129 typename super_t::internal_compare,
130 ordered_iterator_dispatcher
133 typedef detail::stable_heap_iterator<const value_type, container_iterator, super_t> iterator;
134 typedef iterator const_iterator;
135 typedef void * handle_type;
139 typedef typename implementation_defined::ordered_iterator_dispatcher ordered_iterator_dispatcher;
142 typedef T value_type;
144 typedef typename implementation_defined::size_type size_type;
145 typedef typename implementation_defined::difference_type difference_type;
146 typedef typename implementation_defined::value_compare value_compare;
147 typedef typename implementation_defined::allocator_type allocator_type;
148 typedef typename implementation_defined::reference reference;
149 typedef typename implementation_defined::const_reference const_reference;
150 typedef typename implementation_defined::pointer pointer;
151 typedef typename implementation_defined::const_pointer const_pointer;
152 typedef typename implementation_defined::iterator iterator;
153 typedef typename implementation_defined::const_iterator const_iterator;
154 typedef typename implementation_defined::ordered_iterator ordered_iterator;
155 typedef typename implementation_defined::handle_type handle_type;
157 static const bool is_stable = extract_stable<BoundArgs>::value;
159 explicit d_ary_heap(value_compare const & cmp = value_compare()):
163 d_ary_heap(d_ary_heap const & rhs):
164 super_t(rhs), q_(rhs.q_)
167 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
168 d_ary_heap(d_ary_heap && rhs):
169 super_t(std::move(rhs)), q_(std::move(rhs.q_))
172 d_ary_heap & operator=(d_ary_heap && rhs)
174 super_t::operator=(std::move(rhs));
175 q_ = std::move(rhs.q_);
180 d_ary_heap & operator=(d_ary_heap const & rhs)
182 static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
187 bool empty(void) const
192 size_type size(void) const
197 size_type max_size(void) const
199 return q_.max_size();
207 allocator_type get_allocator(void) const
209 return q_.get_allocator();
212 value_type const & top(void) const
214 BOOST_ASSERT(!empty());
215 return super_t::get_value(q_.front());
218 void push(value_type const & v)
220 q_.push_back(super_t::make_node(v));
221 reset_index(size() - 1, size() - 1);
222 siftup(q_.size() - 1);
225 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
226 template <class... Args>
227 void emplace(Args&&... args)
229 q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
230 reset_index(size() - 1, size() - 1);
231 siftup(q_.size() - 1);
236 BOOST_ASSERT(!empty());
237 std::swap(q_.front(), q_.back());
247 void swap(d_ary_heap & rhs)
253 iterator begin(void) const
255 return iterator(q_.begin());
258 iterator end(void) const
260 return iterator(q_.end());
263 ordered_iterator ordered_begin(void) const
265 return ordered_iterator(0, this, super_t::get_internal_cmp());
268 ordered_iterator ordered_end(void) const
270 return ordered_iterator(size(), this, super_t::get_internal_cmp());
273 void reserve (size_type element_count)
275 q_.reserve(element_count);
278 value_compare const & value_comp(void) const
280 return super_t::value_comp();
284 void reset_index(size_type index, size_type new_index)
286 BOOST_HEAP_ASSERT(index < q_.size());
287 index_updater::run(q_[index], new_index);
290 void siftdown(size_type index)
292 while (not_leaf(index)) {
293 size_type max_child_index = top_child_index(index);
294 if (!super_t::operator()(q_[max_child_index], q_[index])) {
295 reset_index(index, max_child_index);
296 reset_index(max_child_index, index);
297 std::swap(q_[max_child_index], q_[index]);
298 index = max_child_index;
305 /* returns new index */
306 void siftup(size_type index)
309 size_type parent = parent_index(index);
311 if (super_t::operator()(q_[parent], q_[index])) {
312 reset_index(index, parent);
313 reset_index(parent, index);
314 std::swap(q_[parent], q_[index]);
322 bool not_leaf(size_type index) const
324 const size_t first_child = first_child_index(index);
325 return first_child < q_.size();
328 size_type top_child_index(size_type index) const
330 // invariant: index is not a leaf, so the iterator range is not empty
332 const size_t first_index = first_child_index(index);
333 typedef typename container_type::const_iterator container_iterator;
335 const container_iterator first_child = q_.begin() + first_index;
336 const container_iterator end = q_.end();
338 const size_type max_elements = std::distance(first_child, end);
340 const container_iterator last_child = (max_elements > D) ? first_child + D
343 const container_iterator min_element = std::max_element(first_child, last_child, static_cast<super_t const &>(*this));
345 return min_element - q_.begin();
348 static size_type parent_index(size_type index)
350 return (index - 1) / D;
353 static size_type first_child_index(size_type index)
355 return index * D + 1;
358 size_type last_child_index(size_type index) const
360 const size_t first_index = first_child_index(index);
361 const size_type last_index = (std::min)(first_index + D - 1, size() - 1);
371 typedef d_ary_heap<U, typename d_ary_heap_signature::bind<boost::heap::stable<heap_base_maker::is_stable>,
372 boost::heap::stability_counter_type<typename heap_base_maker::stability_counter_type>,
373 boost::heap::arity<D>,
374 boost::heap::compare<V>,
375 boost::heap::allocator<W>
381 template <class U> friend class priority_queue_mutable_wrapper;
383 void update(size_type index)
389 size_type parent = parent_index(index);
391 if (super_t::operator()(q_[parent], q_[index]))
397 void erase(size_type index)
401 size_type parent = parent_index(index);
403 reset_index(index, parent);
404 reset_index(parent, index);
405 std::swap(q_[parent], q_[index]);
411 void increase(size_type index)
416 void decrease(size_type index)
423 template <typename T, typename BoundArgs>
424 struct select_dary_heap
426 static const bool is_mutable = extract_mutable<BoundArgs>::value;
428 typedef typename boost::conditional< is_mutable,
429 priority_queue_mutable_wrapper<d_ary_heap<T, BoundArgs, nop_index_updater > >,
430 d_ary_heap<T, BoundArgs, nop_index_updater >
434 } /* namespace detail */
440 * \brief d-ary heap class
442 * This class implements an immutable priority queue. Internally, the d-ary heap is represented
443 * as dynamically sized array (std::vector), that directly stores the values.
445 * The template parameter T is the type to be managed by the container.
446 * The user can specify additional options and if no options are provided default options are used.
448 * The container supports the following options:
449 * - \c boost::heap::arity<>, required
450 * - \c boost::heap::compare<>, defaults to \c compare<std::less<T> >
451 * - \c boost::heap::stable<>, defaults to \c stable<false>
452 * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type<boost::uintmax_t>
453 * - \c boost::heap::allocator<>, defaults to \c allocator<std::allocator<T> >
454 * - \c boost::heap::mutable_<>, defaults to \c mutable_<false>
457 #ifdef BOOST_DOXYGEN_INVOKED
458 template<class T, class ...Options>
460 template <typename T,
461 class A0 = boost::parameter::void_,
462 class A1 = boost::parameter::void_,
463 class A2 = boost::parameter::void_,
464 class A3 = boost::parameter::void_,
465 class A4 = boost::parameter::void_,
466 class A5 = boost::parameter::void_
470 public detail::select_dary_heap<T, typename detail::d_ary_heap_signature::bind<A0, A1, A2, A3, A4, A5>::type>::type
472 typedef typename detail::d_ary_heap_signature::bind<A0, A1, A2, A3, A4, A5>::type bound_args;
473 typedef typename detail::select_dary_heap<T, bound_args>::type super_t;
475 template <typename Heap1, typename Heap2>
476 friend struct heap_merge_emulate;
478 #ifndef BOOST_DOXYGEN_INVOKED
479 static const bool is_mutable = detail::extract_mutable<bound_args>::value;
481 #define BOOST_HEAP_TYPEDEF_FROM_SUPER_T(NAME) \
482 typedef typename super_t::NAME NAME;
484 struct implementation_defined
486 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(size_type)
487 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(difference_type)
488 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(value_compare)
489 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(allocator_type)
490 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(reference)
491 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_reference)
492 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(pointer)
493 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_pointer)
494 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(iterator)
495 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_iterator)
496 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(ordered_iterator)
497 BOOST_HEAP_TYPEDEF_FROM_SUPER_T(handle_type)
499 #undef BOOST_HEAP_TYPEDEF_FROM_SUPER_T
503 static const bool constant_time_size = true;
504 static const bool has_ordered_iterators = true;
505 static const bool is_mergable = false;
506 static const bool has_reserve = true;
507 static const bool is_stable = super_t::is_stable;
509 typedef T value_type;
510 typedef typename implementation_defined::size_type size_type;
511 typedef typename implementation_defined::difference_type difference_type;
512 typedef typename implementation_defined::value_compare value_compare;
513 typedef typename implementation_defined::allocator_type allocator_type;
514 typedef typename implementation_defined::reference reference;
515 typedef typename implementation_defined::const_reference const_reference;
516 typedef typename implementation_defined::pointer pointer;
517 typedef typename implementation_defined::const_pointer const_pointer;
518 /// \copydoc boost::heap::priority_queue::iterator
519 typedef typename implementation_defined::iterator iterator;
520 typedef typename implementation_defined::const_iterator const_iterator;
521 typedef typename implementation_defined::ordered_iterator ordered_iterator;
522 typedef typename implementation_defined::handle_type handle_type;
524 /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &)
525 explicit d_ary_heap(value_compare const & cmp = value_compare()):
529 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &)
530 d_ary_heap(d_ary_heap const & rhs):
534 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
535 /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&)
536 d_ary_heap(d_ary_heap && rhs):
537 super_t(std::move(rhs))
540 /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&)
541 d_ary_heap & operator=(d_ary_heap && rhs)
543 super_t::operator=(std::move(rhs));
548 /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &)
549 d_ary_heap & operator=(d_ary_heap const & rhs)
551 super_t::operator=(rhs);
555 /// \copydoc boost::heap::priority_queue::empty
556 bool empty(void) const
558 return super_t::empty();
561 /// \copydoc boost::heap::priority_queue::size
562 size_type size(void) const
564 return super_t::size();
567 /// \copydoc boost::heap::priority_queue::max_size
568 size_type max_size(void) const
570 return super_t::max_size();
573 /// \copydoc boost::heap::priority_queue::clear
579 /// \copydoc boost::heap::priority_queue::get_allocator
580 allocator_type get_allocator(void) const
582 return super_t::get_allocator();
585 /// \copydoc boost::heap::priority_queue::top
586 value_type const & top(void) const
588 return super_t::top();
591 /// \copydoc boost::heap::priority_queue::push
592 typename boost::conditional<is_mutable, handle_type, void>::type push(value_type const & v)
594 return super_t::push(v);
597 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
598 /// \copydoc boost::heap::priority_queue::emplace
599 template <class... Args>
600 typename boost::conditional<is_mutable, handle_type, void>::type emplace(Args&&... args)
602 return super_t::emplace(std::forward<Args>(args)...);
606 /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const
607 template <typename HeapType>
608 bool operator<(HeapType const & rhs) const
610 return detail::heap_compare(*this, rhs);
613 /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const
614 template <typename HeapType>
615 bool operator>(HeapType const & rhs) const
617 return detail::heap_compare(rhs, *this);
620 /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const
621 template <typename HeapType>
622 bool operator>=(HeapType const & rhs) const
624 return !operator<(rhs);
627 /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const
628 template <typename HeapType>
629 bool operator<=(HeapType const & rhs) const
631 return !operator>(rhs);
634 /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const
635 template <typename HeapType>
636 bool operator==(HeapType const & rhs) const
638 return detail::heap_equality(*this, rhs);
641 /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const
642 template <typename HeapType>
643 bool operator!=(HeapType const & rhs) const
645 return !(*this == rhs);
649 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
651 * \b Complexity: Logarithmic.
653 * \b Requirement: data structure must be configured as mutable
655 void update(handle_type handle, const_reference v)
657 BOOST_STATIC_ASSERT(is_mutable);
658 super_t::update(handle, v);
662 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
664 * \b Complexity: Logarithmic.
666 * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
668 * \b Requirement: data structure must be configured as mutable
670 void update(handle_type handle)
672 BOOST_STATIC_ASSERT(is_mutable);
673 super_t::update(handle);
677 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
679 * \b Complexity: Logarithmic.
681 * \b Note: The new value is expected to be greater than the current one
683 * \b Requirement: data structure must be configured as mutable
685 void increase(handle_type handle, const_reference v)
687 BOOST_STATIC_ASSERT(is_mutable);
688 super_t::increase(handle, v);
692 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
694 * \b Complexity: Logarithmic.
696 * \b Note: The new value is expected to be greater than the current one. If this is not called, after a handle has been updated, the behavior of the data structure is undefined!
698 * \b Requirement: data structure must be configured as mutable
700 void increase(handle_type handle)
702 BOOST_STATIC_ASSERT(is_mutable);
703 super_t::increase(handle);
707 * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue.
709 * \b Complexity: Logarithmic.
711 * \b Note: The new value is expected to be less than the current one
713 * \b Requirement: data structure must be configured as mutable
715 void decrease(handle_type handle, const_reference v)
717 BOOST_STATIC_ASSERT(is_mutable);
718 super_t::decrease(handle, v);
722 * \b Effects: Updates the heap after the element handled by \c handle has been changed.
724 * \b Complexity: Logarithmic.
726 * \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!
728 * \b Requirement: data structure must be configured as mutable
730 void decrease(handle_type handle)
732 BOOST_STATIC_ASSERT(is_mutable);
733 super_t::decrease(handle);
737 * \b Effects: Removes the element handled by \c handle from the priority_queue.
739 * \b Complexity: Logarithmic.
741 * \b Requirement: data structure must be configured as mutable
743 void erase(handle_type handle)
745 BOOST_STATIC_ASSERT(is_mutable);
746 super_t::erase(handle);
750 * \b Effects: Casts an iterator to a node handle.
752 * \b Complexity: Constant.
754 * \b Requirement: data structure must be configured as mutable
756 static handle_type s_handle_from_iterator(iterator const & it)
758 BOOST_STATIC_ASSERT(is_mutable);
759 return super_t::s_handle_from_iterator(it);
762 /// \copydoc boost::heap::priority_queue::pop
768 /// \copydoc boost::heap::priority_queue::swap
769 void swap(d_ary_heap & rhs)
774 /// \copydoc boost::heap::priority_queue::begin
775 const_iterator begin(void) const
777 return super_t::begin();
780 /// \copydoc boost::heap::priority_queue::begin
783 return super_t::begin();
786 /// \copydoc boost::heap::priority_queue::end
789 return super_t::end();
792 /// \copydoc boost::heap::priority_queue::end
793 const_iterator end(void) const
795 return super_t::end();
798 /// \copydoc boost::heap::fibonacci_heap::ordered_begin
799 ordered_iterator ordered_begin(void) const
801 return super_t::ordered_begin();
804 /// \copydoc boost::heap::fibonacci_heap::ordered_end
805 ordered_iterator ordered_end(void) const
807 return super_t::ordered_end();
810 /// \copydoc boost::heap::priority_queue::reserve
811 void reserve (size_type element_count)
813 super_t::reserve(element_count);
816 /// \copydoc boost::heap::priority_queue::value_comp
817 value_compare const & value_comp(void) const
819 return super_t::value_comp();
823 } /* namespace heap */
824 } /* namespace boost */
826 #undef BOOST_HEAP_ASSERT
828 #endif /* BOOST_HEAP_D_ARY_HEAP_HPP */