123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- #ifndef BOOST_HEAP_PRIORITY_QUEUE_HPP
- #define BOOST_HEAP_PRIORITY_QUEUE_HPP
- #include <algorithm>
- #include <queue>
- #include <utility>
- #include <vector>
- #include <boost/assert.hpp>
- #include <boost/heap/detail/heap_comparison.hpp>
- #include <boost/heap/detail/stable_heap.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- namespace heap {
- namespace detail {
- typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
- boost::parameter::optional<tag::compare>,
- boost::parameter::optional<tag::stable>,
- boost::parameter::optional<tag::stability_counter_type>
- > priority_queue_signature;
- }
- #ifdef BOOST_DOXYGEN_INVOKED
- template<class T, class ...Options>
- #else
- template <typename T,
- class A0 = boost::parameter::void_,
- class A1 = boost::parameter::void_,
- class A2 = boost::parameter::void_,
- class A3 = boost::parameter::void_
- >
- #endif
- class priority_queue:
- private detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false>::type
- {
- typedef detail::make_heap_base<T, typename detail::priority_queue_signature::bind<A0, A1, A2, A3>::type, false> heap_base_maker;
- typedef typename heap_base_maker::type super_t;
- typedef typename super_t::internal_type internal_type;
- typedef typename boost::allocator_rebind<typename heap_base_maker::allocator_argument, internal_type>::type internal_type_allocator;
- typedef std::vector<internal_type, internal_type_allocator> container_type;
- template <typename Heap1, typename Heap2>
- friend struct detail::heap_merge_emulate;
- container_type q_;
- #ifndef BOOST_DOXYGEN_INVOKED
- struct implementation_defined:
- detail::extract_allocator_types<typename heap_base_maker::allocator_argument>
- {
- typedef typename heap_base_maker::compare_argument value_compare;
- typedef detail::stable_heap_iterator<T, typename container_type::const_iterator, super_t> iterator;
- typedef iterator const_iterator;
- typedef typename container_type::allocator_type allocator_type;
- };
- #endif
- public:
- typedef T value_type;
- typedef typename implementation_defined::size_type size_type;
- typedef typename implementation_defined::difference_type difference_type;
- typedef typename implementation_defined::value_compare value_compare;
- typedef typename implementation_defined::allocator_type allocator_type;
- typedef typename implementation_defined::reference reference;
- typedef typename implementation_defined::const_reference const_reference;
- typedef typename implementation_defined::pointer pointer;
- typedef typename implementation_defined::const_pointer const_pointer;
-
- typedef typename implementation_defined::iterator iterator;
- typedef typename implementation_defined::const_iterator const_iterator;
- static const bool constant_time_size = true;
- static const bool has_ordered_iterators = false;
- static const bool is_mergable = false;
- static const bool is_stable = heap_base_maker::is_stable;
- static const bool has_reserve = true;
-
- explicit priority_queue(value_compare const & cmp = value_compare()):
- super_t(cmp)
- {}
-
- priority_queue (priority_queue const & rhs):
- super_t(rhs), q_(rhs.q_)
- {}
- #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
-
- priority_queue(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value):
- super_t(std::move(rhs)), q_(std::move(rhs.q_))
- {}
-
- priority_queue & operator=(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable<super_t>::value)
- {
- super_t::operator=(std::move(rhs));
- q_ = std::move(rhs.q_);
- return *this;
- }
- #endif
-
- priority_queue & operator=(priority_queue const & rhs)
- {
- static_cast<super_t&>(*this) = static_cast<super_t const &>(rhs);
- q_ = rhs.q_;
- return *this;
- }
-
- bool empty(void) const BOOST_NOEXCEPT
- {
- return q_.empty();
- }
-
- size_type size(void) const BOOST_NOEXCEPT
- {
- return q_.size();
- }
-
- size_type max_size(void) const BOOST_NOEXCEPT
- {
- return q_.max_size();
- }
-
- void clear(void) BOOST_NOEXCEPT
- {
- q_.clear();
- }
-
- allocator_type get_allocator(void) const
- {
- return q_.get_allocator();
- }
-
- const_reference top(void) const
- {
- BOOST_ASSERT(!empty());
- return super_t::get_value(q_.front());
- }
-
- void push(value_type const & v)
- {
- q_.push_back(super_t::make_node(v));
- std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
- }
- #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
-
- template <class... Args>
- void emplace(Args&&... args)
- {
- q_.emplace_back(super_t::make_node(std::forward<Args>(args)...));
- std::push_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
- }
- #endif
-
- void pop(void)
- {
- BOOST_ASSERT(!empty());
- std::pop_heap(q_.begin(), q_.end(), static_cast<super_t const &>(*this));
- q_.pop_back();
- }
-
- void swap(priority_queue & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<super_t>::value && boost::is_nothrow_move_assignable<super_t>::value)
- {
- super_t::swap(rhs);
- q_.swap(rhs.q_);
- }
-
- iterator begin(void) const BOOST_NOEXCEPT
- {
- return iterator(q_.begin());
- }
-
- iterator end(void) const BOOST_NOEXCEPT
- {
- return iterator(q_.end());
- }
-
- void reserve(size_type element_count)
- {
- q_.reserve(element_count);
- }
-
- value_compare const & value_comp(void) const
- {
- return super_t::value_comp();
- }
-
- template <typename HeapType>
- bool operator<(HeapType const & rhs) const
- {
- return detail::heap_compare(*this, rhs);
- }
-
- template <typename HeapType>
- bool operator>(HeapType const & rhs) const
- {
- return detail::heap_compare(rhs, *this);
- }
-
- template <typename HeapType>
- bool operator>=(HeapType const & rhs) const
- {
- return !operator<(rhs);
- }
-
- template <typename HeapType>
- bool operator<=(HeapType const & rhs) const
- {
- return !operator>(rhs);
- }
-
- template <typename HeapType>
- bool operator==(HeapType const & rhs) const
- {
- return detail::heap_equality(*this, rhs);
- }
-
- template <typename HeapType>
- bool operator!=(HeapType const & rhs) const
- {
- return !(*this == rhs);
- }
- };
- }
- }
- #endif
|