From 7982bdc650f5ef50efed6bbeebe9afee10753711 Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Thu, 7 Dec 2023 17:27:32 +0800 Subject: [PATCH] cleanup: run clang-format --- .appveyor.yml | 2 +- doc/heap.qbk | 2 +- examples/interface.cpp | 160 ++-- include/boost/heap/binomial_heap.hpp | 743 +++++++++-------- include/boost/heap/d_ary_heap.hpp | 633 +++++++-------- include/boost/heap/detail/heap_comparison.hpp | 172 ++-- include/boost/heap/detail/heap_node.hpp | 308 ++++--- include/boost/heap/detail/ilog2.hpp | 36 +- include/boost/heap/detail/mutable_heap.hpp | 386 ++++----- .../heap/detail/ordered_adaptor_iterator.hpp | 124 ++- include/boost/heap/detail/stable_heap.hpp | 448 ++++++----- include/boost/heap/detail/tree_iterator.hpp | 325 ++++---- include/boost/heap/fibonacci_heap.hpp | 575 +++++++------ include/boost/heap/heap_concepts.hpp | 90 +-- include/boost/heap/heap_merge.hpp | 83 +- include/boost/heap/pairing_heap.hpp | 512 ++++++------ include/boost/heap/policies.hpp | 144 ++-- include/boost/heap/priority_queue.hpp | 208 ++--- include/boost/heap/skew_heap.hpp | 758 +++++++++--------- index.html | 4 +- test/binomial_heap_test.cpp | 68 +- test/common_heap_tests.hpp | 505 ++++++------ test/d_ary_heap_test.cpp | 136 ++-- test/fibonacci_heap_test.cpp | 70 +- test/merge_heap_tests.hpp | 60 +- test/mutable_heap_test.cpp | 90 ++- test/mutable_heap_tests.hpp | 338 ++++---- test/pairing_heap_tests.cpp | 69 +- test/priority_queue_test.cpp | 36 +- test/self_contained_header.cpp | 5 +- test/skew_heap_test.cpp | 133 +-- test/stable_heap_tests.hpp | 72 +- tools/heap_benchmarks.hpp | 240 +++--- tools/high_resolution_timer.hpp | 120 ++- tools/throughput_benchmarks.cpp | 244 +++--- 35 files changed, 3889 insertions(+), 4010 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index cde6d2e..eac613c 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -52,4 +52,4 @@ build: off test_script: - PATH=%ADDPATH%%PATH% - if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD% - - b2 -j 3 libs/heap/test toolset=%TOOLSET% %CXXSTD% \ No newline at end of file + - b2 -j 3 libs/heap/test toolset=%TOOLSET% %CXXSTD% diff --git a/doc/heap.qbk b/doc/heap.qbk index 827aeda..2c9663d 100644 --- a/doc/heap.qbk +++ b/doc/heap.qbk @@ -419,4 +419,4 @@ The data structures can be configured with [@boost:/libs/parameter/doc/html/inde [For mentoring the Summer of Code project] ] ] -[endsect] \ No newline at end of file +[endsect] diff --git a/examples/interface.cpp b/examples/interface.cpp index a3a24c7..91ddb55 100644 --- a/examples/interface.cpp +++ b/examples/interface.cpp @@ -6,8 +6,8 @@ http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ -#include #include +#include #include "../../../boost/heap/fibonacci_heap.hpp" @@ -17,14 +17,14 @@ using namespace boost::heap; //[ basic_interface // PriorityQueue is expected to be a max-heap of integer values -template -void basic_interface(void) +template < typename PriorityQueue > +void basic_interface( void ) { PriorityQueue pq; - pq.push(2); - pq.push(3); - pq.push(1); + pq.push( 2 ); + pq.push( 3 ); + pq.push( 1 ); cout << "Priority Queue: popped elements" << endl; cout << pq.top() << " "; // 3 @@ -39,20 +39,20 @@ void basic_interface(void) //[ iterator_interface // PriorityQueue is expected to be a max-heap of integer values -template -void iterator_interface(void) +template < typename PriorityQueue > +void iterator_interface( void ) { PriorityQueue pq; - pq.push(2); - pq.push(3); - pq.push(1); + pq.push( 2 ); + pq.push( 3 ); + pq.push( 1 ); typename PriorityQueue::iterator begin = pq.begin(); - typename PriorityQueue::iterator end = pq.end(); + typename PriorityQueue::iterator end = pq.end(); cout << "Priority Queue: iteration" << endl; - for (typename PriorityQueue::iterator it = begin; it != end; ++it) + for ( typename PriorityQueue::iterator it = begin; it != end; ++it ) cout << *it << " "; // 1, 2, 3 in unspecified order cout << endl; } @@ -60,20 +60,20 @@ void iterator_interface(void) //[ ordered_iterator_interface // PriorityQueue is expected to be a max-heap of integer values -template -void ordered_iterator_interface(void) +template < typename PriorityQueue > +void ordered_iterator_interface( void ) { PriorityQueue pq; - pq.push(2); - pq.push(3); - pq.push(1); + pq.push( 2 ); + pq.push( 3 ); + pq.push( 1 ); typename PriorityQueue::ordered_iterator begin = pq.ordered_begin(); - typename PriorityQueue::ordered_iterator end = pq.ordered_end(); + typename PriorityQueue::ordered_iterator end = pq.ordered_end(); cout << "Priority Queue: ordered iteration" << endl; - for (typename PriorityQueue::ordered_iterator it = begin; it != end; ++it) + for ( typename PriorityQueue::ordered_iterator it = begin; it != end; ++it ) cout << *it << " "; // 3, 2, 1 (i.e. 1, 2, 3 in heap order) cout << endl; } @@ -82,33 +82,33 @@ void ordered_iterator_interface(void) //[ merge_interface // PriorityQueue is expected to be a max-heap of integer values -template -void merge_interface(void) +template < typename PriorityQueue > +void merge_interface( void ) { PriorityQueue pq; - pq.push(3); - pq.push(5); - pq.push(1); + pq.push( 3 ); + pq.push( 5 ); + pq.push( 1 ); PriorityQueue pq2; - pq2.push(2); - pq2.push(4); - pq2.push(0); + pq2.push( 2 ); + pq2.push( 4 ); + pq2.push( 0 ); - pq.merge(pq2); + pq.merge( pq2 ); cout << "Priority Queue: merge" << endl; cout << "first queue: "; - while (!pq.empty()) { + while ( !pq.empty() ) { cout << pq.top() << " "; // 5 4 3 2 1 0 pq.pop(); } cout << endl; cout << "second queue: "; - while (!pq2.empty()) { + while ( !pq2.empty() ) { cout << pq2.top() << " "; // 4 2 0 pq2.pop(); } @@ -118,33 +118,33 @@ void merge_interface(void) //[ heap_merge_algorithm // PriorityQueue is expected to be a max-heap of integer values -template -void heap_merge_algorithm(void) +template < typename PriorityQueue > +void heap_merge_algorithm( void ) { PriorityQueue pq; - pq.push(3); - pq.push(5); - pq.push(1); + pq.push( 3 ); + pq.push( 5 ); + pq.push( 1 ); PriorityQueue pq2; - pq2.push(2); - pq2.push(4); - pq2.push(0); + pq2.push( 2 ); + pq2.push( 4 ); + pq2.push( 0 ); - boost::heap::heap_merge(pq, pq2); + boost::heap::heap_merge( pq, pq2 ); cout << "Priority Queue: merge" << endl; cout << "first queue: "; - while (!pq.empty()) { + while ( !pq.empty() ) { cout << pq.top() << " "; // 5 4 3 2 1 0 pq.pop(); } cout << endl; cout << "second queue: "; - while (!pq2.empty()) { + while ( !pq2.empty() ) { cout << pq2.top() << " "; // 4 2 0 pq2.pop(); } @@ -154,22 +154,22 @@ void heap_merge_algorithm(void) //[ mutable_interface // PriorityQueue is expected to be a max-heap of integer values -template -void mutable_interface(void) +template < typename PriorityQueue > +void mutable_interface( void ) { - PriorityQueue pq; + PriorityQueue pq; typedef typename PriorityQueue::handle_type handle_t; - handle_t t3 = pq.push(3); - handle_t t5 = pq.push(5); - handle_t t1 = pq.push(1); + handle_t t3 = pq.push( 3 ); + handle_t t5 = pq.push( 5 ); + handle_t t1 = pq.push( 1 ); - pq.update(t3, 4); - pq.increase(t5, 7); - pq.decrease(t1, 0); + pq.update( t3, 4 ); + pq.increase( t5, 7 ); + pq.decrease( t1, 0 ); cout << "Priority Queue: update" << endl; - while (!pq.empty()) { + while ( !pq.empty() ) { cout << pq.top() << " "; // 7, 4, 0 pq.pop(); } @@ -179,27 +179,27 @@ void mutable_interface(void) //[ mutable_fixup_interface // PriorityQueue is expected to be a max-heap of integer values -template -void mutable_fixup_interface(void) +template < typename PriorityQueue > +void mutable_fixup_interface( void ) { - PriorityQueue pq; + PriorityQueue pq; typedef typename PriorityQueue::handle_type handle_t; - handle_t t3 = pq.push(3); - handle_t t5 = pq.push(5); - handle_t t1 = pq.push(1); + handle_t t3 = pq.push( 3 ); + handle_t t5 = pq.push( 5 ); + handle_t t1 = pq.push( 1 ); *t3 = 4; - pq.update(t3); + pq.update( t3 ); *t5 = 7; - pq.increase(t5); + pq.increase( t5 ); *t1 = 0; - pq.decrease(t1); + pq.decrease( t1 ); cout << "Priority Queue: update with fixup" << endl; - while (!pq.empty()) { + while ( !pq.empty() ) { cout << pq.top() << " "; // 7, 4, 0 pq.pop(); } @@ -210,52 +210,52 @@ void mutable_fixup_interface(void) //[ mutable_interface_handle_in_value struct heap_data { - fibonacci_heap::handle_type handle; - int payload; + fibonacci_heap< heap_data >::handle_type handle; + int payload; - heap_data(int i): - payload(i) + heap_data( int i ) : + payload( i ) {} - bool operator<(heap_data const & rhs) const + bool operator<( heap_data const& rhs ) const { return payload < rhs.payload; } }; -void mutable_interface_handle_in_value(void) +void mutable_interface_handle_in_value( void ) { - fibonacci_heap heap; - heap_data f(2); + fibonacci_heap< heap_data > heap; + heap_data f( 2 ); - fibonacci_heap::handle_type handle = heap.push(f); - (*handle).handle = handle; // store handle in node + fibonacci_heap< heap_data >::handle_type handle = heap.push( f ); + ( *handle ).handle = handle; // store handle in node } //] -int main(void) +int main( void ) { using boost::heap::fibonacci_heap; - cout << std::setw(2); + cout << std::setw( 2 ); - basic_interface >(); + basic_interface< fibonacci_heap< int > >(); cout << endl; - iterator_interface >(); + iterator_interface< fibonacci_heap< int > >(); cout << endl; - ordered_iterator_interface >(); + ordered_iterator_interface< fibonacci_heap< int > >(); cout << endl; - merge_interface >(); + merge_interface< fibonacci_heap< int > >(); cout << endl; - mutable_interface >(); + mutable_interface< fibonacci_heap< int > >(); cout << endl; - mutable_fixup_interface >(); + mutable_fixup_interface< fibonacci_heap< int > >(); mutable_interface_handle_in_value(); } diff --git a/include/boost/heap/binomial_heap.hpp b/include/boost/heap/binomial_heap.hpp index 76b1d92..6c0f420 100644 --- a/include/boost/heap/binomial_heap.hpp +++ b/include/boost/heap/binomial_heap.hpp @@ -22,79 +22,75 @@ #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif #ifndef BOOST_DOXYGEN_INVOKED -#ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT -#else -#define BOOST_HEAP_ASSERT(expression) -#endif +# ifdef BOOST_HEAP_SANITYCHECKS +# define BOOST_HEAP_ASSERT BOOST_ASSERT +# else +# define BOOST_HEAP_ASSERT( expression ) +# endif #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > binomial_heap_signature; +typedef parameter::parameters< boost::parameter::optional< tag::allocator >, + boost::parameter::optional< tag::compare >, + boost::parameter::optional< tag::stable >, + boost::parameter::optional< tag::constant_time_size >, + boost::parameter::optional< tag::stability_counter_type > > + binomial_heap_signature; -template +template < typename T, typename Parspec > struct make_binomial_heap_base { - static const bool constant_time_size = parameter::binding::type::value; - typedef typename detail::make_heap_base::type base_type; - typedef typename detail::make_heap_base::allocator_argument allocator_argument; - typedef typename detail::make_heap_base::compare_argument compare_argument; + static const bool constant_time_size + = parameter::binding< Parspec, tag::constant_time_size, boost::true_type >::type::value; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::type base_type; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::allocator_argument allocator_argument; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::compare_argument compare_argument; - typedef parent_pointing_heap_node node_type; + typedef parent_pointing_heap_node< typename base_type::internal_type > node_type; - typedef typename boost::allocator_rebind::type allocator_type; + typedef typename boost::allocator_rebind< allocator_argument, node_type >::type allocator_type; - struct type: - base_type, - allocator_type + struct type : base_type, allocator_type { - type(compare_argument const & arg): - base_type(arg) + type( compare_argument const& arg ) : + base_type( arg ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - type(type const & rhs): - base_type(rhs), allocator_type(rhs) + type( type const& rhs ) : + base_type( rhs ), + allocator_type( rhs ) {} - type(type && rhs): - base_type(std::move(static_cast(rhs))), - allocator_type(std::move(static_cast(rhs))) + type( type&& rhs ) : + base_type( std::move( static_cast< base_type& >( rhs ) ) ), + allocator_type( std::move( static_cast< allocator_type& >( rhs ) ) ) {} - type & operator=(type && rhs) + type& operator=( type&& rhs ) { - base_type::operator=(std::move(static_cast(rhs))); - allocator_type::operator=(std::move(static_cast(rhs))); + base_type::operator=( std::move( static_cast< base_type& >( rhs ) ) ); + allocator_type::operator=( std::move( static_cast< allocator_type& >( rhs ) ) ); return *this; } - type & operator=(type const & rhs) + type& operator=( type const& rhs ) { - base_type::operator=(static_cast(rhs)); - allocator_type::operator=(static_cast(rhs)); + base_type::operator=( static_cast< base_type const& >( rhs ) ); + allocator_type::operator=( static_cast< allocator_type const& >( rhs ) ); return *this; } #endif }; }; -} +} // namespace detail /** * \class binomial_heap @@ -112,174 +108,172 @@ struct make_binomial_heap_base * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +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 binomial_heap: - private detail::make_binomial_heap_base::type - >::type +class binomial_heap : + private detail::make_binomial_heap_base< T, typename detail::binomial_heap_signature::bind< A0, A1, A2, A3 >::type >::type { - typedef typename detail::binomial_heap_signature::bind::type bound_args; - typedef detail::make_binomial_heap_base base_maker; - typedef typename base_maker::type super_t; + typedef typename detail::binomial_heap_signature::bind< A0, A1, A2, A3 >::type bound_args; + typedef detail::make_binomial_heap_base< T, bound_args > base_maker; + typedef typename base_maker::type super_t; - typedef typename super_t::internal_type internal_type; - typedef typename super_t::size_holder_type size_holder; + typedef typename super_t::internal_type internal_type; + typedef typename super_t::size_holder_type size_holder; typedef typename super_t::stability_counter_type stability_counter_type; - typedef typename base_maker::allocator_argument allocator_argument; + typedef typename base_maker::allocator_argument allocator_argument; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; public: - static const bool constant_time_size = super_t::constant_time_size; + static const bool constant_time_size = super_t::constant_time_size; static const bool has_ordered_iterators = true; - static const bool is_mergable = true; - static const bool is_stable = detail::extract_stable::value; - static const bool has_reserve = false; + static const bool is_mergable = true; + static const bool is_stable = detail::extract_stable< bound_args >::value; + static const bool has_reserve = false; private: #ifndef BOOST_DOXYGEN_INVOKED - struct implementation_defined: - detail::extract_allocator_types + struct implementation_defined : detail::extract_allocator_types< typename base_maker::allocator_argument > { typedef T value_type; - typedef typename detail::extract_allocator_types::size_type size_type; - typedef typename detail::extract_allocator_types::reference reference; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::size_type size_type; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::reference reference; typedef typename base_maker::compare_argument value_compare; - typedef typename base_maker::allocator_type allocator_type; - typedef typename base_maker::node_type node; + typedef typename base_maker::allocator_type allocator_type; + typedef typename base_maker::node_type node; - typedef typename boost::allocator_pointer::type node_pointer; - typedef typename boost::allocator_const_pointer::type const_node_pointer; + typedef typename boost::allocator_pointer< allocator_type >::type node_pointer; + typedef typename boost::allocator_const_pointer< allocator_type >::type const_node_pointer; - typedef detail::node_handle handle_type; + typedef detail::node_handle< node_pointer, super_t, reference > handle_type; typedef typename base_maker::node_type node_type; - typedef boost::intrusive::list, - boost::intrusive::constant_time_size - > node_list_type; + typedef boost::intrusive::list< detail::heap_node_base< false >, boost::intrusive::constant_time_size< true > > + node_list_type; - typedef typename node_list_type::iterator node_list_iterator; - typedef typename node_list_type::const_iterator node_list_const_iterator; - typedef detail::value_extractor value_extractor; + typedef typename node_list_type::iterator node_list_iterator; + typedef typename node_list_type::const_iterator node_list_const_iterator; + typedef detail::value_extractor< value_type, internal_type, super_t > value_extractor; - typedef detail::recursive_tree_iterator - > iterator; + typedef detail::recursive_tree_iterator< node_type, + node_list_const_iterator, + const value_type, + value_extractor, + detail::list_iterator_converter< node_type, node_list_type > > + iterator; typedef iterator const_iterator; - typedef detail::tree_iterator, - true, - true, - value_compare - > ordered_iterator; + typedef detail::tree_iterator< node_type, + const value_type, + allocator_type, + value_extractor, + detail::list_iterator_converter< node_type, node_list_type >, + true, + true, + value_compare > + ordered_iterator; }; #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::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; /// \copydoc boost::heap::priority_queue::iterator - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; typedef typename implementation_defined::ordered_iterator ordered_iterator; typedef typename implementation_defined::handle_type handle_type; private: - typedef typename implementation_defined::node_type node_type; - typedef typename implementation_defined::node_list_type node_list_type; - typedef typename implementation_defined::node_pointer node_pointer; - typedef typename implementation_defined::const_node_pointer const_node_pointer; - typedef typename implementation_defined::node_list_iterator node_list_iterator; + typedef typename implementation_defined::node_type node_type; + typedef typename implementation_defined::node_list_type node_list_type; + typedef typename implementation_defined::node_pointer node_pointer; + typedef typename implementation_defined::const_node_pointer const_node_pointer; + typedef typename implementation_defined::node_list_iterator node_list_iterator; typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator; typedef typename super_t::internal_compare internal_compare; public: /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) - explicit binomial_heap(value_compare const & cmp = value_compare()): - super_t(cmp), top_element(0) + explicit binomial_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ), + top_element( 0 ) {} /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) - binomial_heap(binomial_heap const & rhs): - super_t(rhs), top_element(0) + binomial_heap( binomial_heap const& rhs ) : + super_t( rhs ), + top_element( 0 ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - clone_forest(rhs); - size_holder::set_size(rhs.get_size()); + clone_forest( rhs ); + size_holder::set_size( rhs.get_size() ); } /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) - binomial_heap & operator=(binomial_heap const & rhs) + binomial_heap& operator=( binomial_heap const& rhs ) { clear(); - size_holder::set_size(rhs.get_size()); - static_cast(*this) = rhs; + size_holder::set_size( rhs.get_size() ); + static_cast< super_t& >( *this ) = rhs; - if (rhs.empty()) + if ( rhs.empty() ) top_element = NULL; else - clone_forest(rhs); + clone_forest( rhs ); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) - binomial_heap(binomial_heap && rhs): - super_t(std::move(rhs)), top_element(rhs.top_element) + binomial_heap( binomial_heap&& rhs ) : + super_t( std::move( rhs ) ), + top_element( rhs.top_element ) { - trees.splice(trees.begin(), rhs.trees); + trees.splice( trees.begin(), rhs.trees ); rhs.top_element = NULL; } /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) - binomial_heap & operator=(binomial_heap && rhs) + binomial_heap& operator=( binomial_heap&& rhs ) { clear(); - super_t::operator=(std::move(rhs)); - trees.splice(trees.begin(), rhs.trees); - top_element = rhs.top_element; + super_t::operator=( std::move( rhs ) ); + trees.splice( trees.begin(), rhs.trees ); + top_element = rhs.top_element; rhs.top_element = NULL; return *this; } #endif - ~binomial_heap(void) + ~binomial_heap( void ) { clear(); } /// \copydoc boost::heap::priority_queue::empty - bool empty(void) const + bool empty( void ) const { return top_element == NULL; } @@ -290,54 +284,54 @@ public: * \b Complexity: Constant, if configured with constant_time_size, otherwise linear. * * */ - size_type size(void) const + size_type size( void ) const { - if (constant_time_size) + if ( constant_time_size ) return size_holder::get_size(); - if (empty()) + if ( empty() ) return 0; else - return detail::count_list_nodes(trees); + return detail::count_list_nodes< node_type, node_list_type >( trees ); } /// \copydoc boost::heap::priority_queue::max_size - size_type max_size(void) const + size_type max_size( void ) const { const allocator_type& alloc = *this; - return boost::allocator_max_size(alloc); + return boost::allocator_max_size( alloc ); } /// \copydoc boost::heap::priority_queue::clear - void clear(void) + void clear( void ) { - typedef detail::node_disposer disposer; - trees.clear_and_dispose(disposer(*this)); + typedef detail::node_disposer< node_type, typename node_list_type::value_type, allocator_type > disposer; + trees.clear_and_dispose( disposer( *this ) ); - size_holder::set_size(0); + size_holder::set_size( 0 ); top_element = NULL; } /// \copydoc boost::heap::priority_queue::get_allocator - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return *this; } /// \copydoc boost::heap::priority_queue::swap - void swap(binomial_heap & rhs) + void swap( binomial_heap& rhs ) { - super_t::swap(rhs); - std::swap(top_element, rhs.top_element); - trees.swap(rhs.trees); + super_t::swap( rhs ); + std::swap( top_element, rhs.top_element ); + trees.swap( rhs.trees ); } /// \copydoc boost::heap::priority_queue::top - const_reference top(void) const + const_reference top( void ) const { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); - return super_t::get_value(top_element->value); + return super_t::get_value( top_element->value ); } /** @@ -346,42 +340,43 @@ public: * \b Complexity: Logarithmic. * * */ - handle_type push(value_type const & v) + handle_type push( value_type const& v ) { allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node_type(super_t::make_node(v)); - insert_node(trees.begin(), n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node_type( super_t::make_node( v ) ); + insert_node( trees.begin(), n ); - if (!top_element || super_t::operator()(top_element->value, n->value)) + if ( !top_element || super_t::operator()( top_element->value, n->value ) ) top_element = n; size_holder::increment(); sanity_check(); - return handle_type(n); + return handle_type( n ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /** - * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element. + * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns + * handle to element. * * \b Complexity: Logarithmic. * * */ - template - handle_type emplace(Args&&... args) + template < class... Args > + handle_type emplace( Args&&... args ) { allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node_type(super_t::make_node(std::forward(args)...)); - insert_node(trees.begin(), n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node_type( super_t::make_node( std::forward< Args >( args )... ) ); + insert_node( trees.begin(), n ); - if (!top_element || super_t::operator()(top_element->value, n->value)) + if ( !top_element || super_t::operator()( top_element->value, n->value ) ) top_element = n; size_holder::increment(); sanity_check(); - return handle_type(n); + return handle_type( n ); } #endif @@ -391,41 +386,39 @@ public: * \b Complexity: Logarithmic. * * */ - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); node_pointer element = top_element; - trees.erase(node_list_type::s_iterator_to(*element)); + trees.erase( node_list_type::s_iterator_to( *element ) ); size_holder::decrement(); - if (element->child_count()) { - size_type sz = (1 << element->child_count()) - 1; + if ( element->child_count() ) { + size_type sz = ( 1 << element->child_count() ) - 1; - binomial_heap children(value_comp(), element->children, sz); - if (trees.empty()) { + binomial_heap children( value_comp(), element->children, sz ); + if ( trees.empty() ) { stability_counter_type stability_count = super_t::get_stability_count(); - size_t size = constant_time_size ? size_holder::get_size() - : 0; - swap(children); - super_t::set_stability_count(stability_count); + size_t size = constant_time_size ? size_holder::get_size() : 0; + swap( children ); + super_t::set_stability_count( stability_count ); - if (constant_time_size) + if ( constant_time_size ) size_holder::set_size( size ); } else - merge_and_clear_nodes(children); - + merge_and_clear_nodes( children ); } - if (trees.empty()) + if ( trees.empty() ) top_element = NULL; else update_top_element(); element->~node_type(); allocator_type& alloc = *this; - alloc.deallocate(element, 1); + alloc.deallocate( element, 1 ); sanity_check(); } @@ -435,12 +428,12 @@ public: * \b Complexity: Logarithmic. * * */ - void update (handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - if (super_t::operator()(super_t::get_value(handle.node_->value), v)) - increase(handle, v); + if ( super_t::operator()( super_t::get_value( handle.node_->value ), v ) ) + increase( handle, v ); else - decrease(handle, v); + decrease( handle, v ); } /** @@ -450,18 +443,18 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void update (handle_type handle) + void update( handle_type handle ) { node_pointer this_node = handle.node_; - if (this_node->parent) { - if (super_t::operator()(super_t::get_value(this_node->parent->value), super_t::get_value(this_node->value))) - increase(handle); + if ( this_node->parent ) { + if ( super_t::operator()( super_t::get_value( this_node->parent->value ), + super_t::get_value( this_node->value ) ) ) + increase( handle ); else - decrease(handle); - } - else - decrease(handle); + decrease( handle ); + } else + decrease( handle ); } /** @@ -471,10 +464,10 @@ public: * * \b Note: The new value is expected to be greater than the current one * */ - void increase (handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - increase(handle); + handle.node_->value = super_t::make_node( v ); + increase( handle ); } /** @@ -484,10 +477,10 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void increase (handle_type handle) + void increase( handle_type handle ) { node_pointer n = handle.node_; - siftup(n, *this); + siftup( n, *this ); update_top_element(); sanity_check(); @@ -500,10 +493,10 @@ public: * * \b Note: The new value is expected to be less than the current one * */ - void decrease (handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - decrease(handle); + handle.node_->value = super_t::make_node( v ); + decrease( handle ); } /** @@ -511,13 +504,14 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * */ - void decrease (handle_type handle) + void decrease( handle_type handle ) { node_pointer n = handle.node_; - siftdown(n); + siftdown( n ); update_top_element(); } @@ -528,51 +522,50 @@ public: * \b Complexity: Logarithmic. * * */ - void merge(binomial_heap & rhs) + void merge( binomial_heap& rhs ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - if (empty()) { - swap(rhs); + if ( empty() ) { + swap( rhs ); return; } size_type new_size = size_holder::get_size() + rhs.get_size(); - merge_and_clear_nodes(rhs); + merge_and_clear_nodes( rhs ); - size_holder::set_size(new_size); - rhs.set_size(0); + size_holder::set_size( new_size ); + rhs.set_size( 0 ); rhs.top_element = NULL; - super_t::set_stability_count((std::max)(super_t::get_stability_count(), - rhs.get_stability_count())); - rhs.set_stability_count(0); + super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + rhs.set_stability_count( 0 ); } public: /// \copydoc boost::heap::priority_queue::begin - iterator begin(void) const + iterator begin( void ) const { - return iterator(trees.begin()); + return iterator( trees.begin() ); } /// \copydoc boost::heap::priority_queue::end - iterator end(void) const + iterator end( void ) const { - return iterator(trees.end()); + return iterator( trees.end() ); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - return ordered_iterator(trees.begin(), trees.end(), top_element, super_t::value_comp()); + return ordered_iterator( trees.begin(), trees.end(), top_element, super_t::value_comp() ); } /// \copydoc boost::heap::fibonacci_heap::ordered_end - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(NULL, super_t::value_comp()); + return ordered_iterator( NULL, super_t::value_comp() ); } /** @@ -580,357 +573,363 @@ public: * * \b Complexity: Logarithmic. * */ - void erase(handle_type handle) + void erase( handle_type handle ) { node_pointer n = handle.node_; - siftup(n, force_inf()); + siftup( n, force_inf() ); top_element = n; pop(); } /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - node_type * ptr = const_cast(it.get_node()); - return handle_type(ptr); + node_type* ptr = const_cast< node_type* >( it.get_node() ); + return handle_type( ptr ); } /// \copydoc boost::heap::priority_queue::value_comp - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } private: -#if !defined(BOOST_DOXYGEN_INVOKED) - void merge_and_clear_nodes(binomial_heap & rhs) +#if !defined( BOOST_DOXYGEN_INVOKED ) + void merge_and_clear_nodes( binomial_heap& rhs ) { - BOOST_HEAP_ASSERT (!empty()); - BOOST_HEAP_ASSERT (!rhs.empty()); + BOOST_HEAP_ASSERT( !empty() ); + BOOST_HEAP_ASSERT( !rhs.empty() ); node_list_iterator this_iterator = trees.begin(); - node_pointer carry_node = NULL; + node_pointer carry_node = NULL; - while (!rhs.trees.empty()) { - node_pointer rhs_node = static_cast(&rhs.trees.front()); - size_type rhs_degree = rhs_node->child_count(); + while ( !rhs.trees.empty() ) { + node_pointer rhs_node = static_cast< node_pointer >( &rhs.trees.front() ); + size_type rhs_degree = rhs_node->child_count(); - if (super_t::operator()(top_element->value, rhs_node->value)) + if ( super_t::operator()( top_element->value, rhs_node->value ) ) top_element = rhs_node; - try_again: - node_pointer this_node = static_cast(&*this_iterator); - size_type this_degree = this_node->child_count(); +try_again: + node_pointer this_node = static_cast< node_pointer >( &*this_iterator ); + size_type this_degree = this_node->child_count(); sorted_by_degree(); rhs.sorted_by_degree(); - if (this_degree == rhs_degree) { - if (carry_node) { - if (carry_node->child_count() < this_degree) { - trees.insert(this_iterator, *carry_node); + if ( this_degree == rhs_degree ) { + if ( carry_node ) { + if ( carry_node->child_count() < this_degree ) { + trees.insert( this_iterator, *carry_node ); carry_node = NULL; } else { rhs.trees.pop_front(); - carry_node = merge_trees(carry_node, rhs_node); + carry_node = merge_trees( carry_node, rhs_node ); } ++this_iterator; } else { - this_iterator = trees.erase(this_iterator); + this_iterator = trees.erase( this_iterator ); rhs.trees.pop_front(); - carry_node = merge_trees(this_node, rhs_node); + carry_node = merge_trees( this_node, rhs_node ); } - if (this_iterator == trees.end()) + if ( this_iterator == trees.end() ) break; else continue; } - if (this_degree < rhs_degree) { - if (carry_node) { - if (carry_node->child_count() < this_degree) { - trees.insert(this_iterator, *carry_node); + if ( this_degree < rhs_degree ) { + if ( carry_node ) { + if ( carry_node->child_count() < this_degree ) { + trees.insert( this_iterator, *carry_node ); carry_node = NULL; ++this_iterator; - } else if (carry_node->child_count() == rhs_degree) { + } else if ( carry_node->child_count() == rhs_degree ) { rhs.trees.pop_front(); - carry_node = merge_trees(carry_node, rhs_node); + carry_node = merge_trees( carry_node, rhs_node ); continue; } else { - this_iterator = trees.erase(this_iterator); - carry_node = merge_trees(this_node, carry_node); + this_iterator = trees.erase( this_iterator ); + carry_node = merge_trees( this_node, carry_node ); } goto try_again; } else { ++this_iterator; - if (this_iterator == trees.end()) + if ( this_iterator == trees.end() ) break; goto try_again; } - if (this_iterator == trees.end()) + if ( this_iterator == trees.end() ) break; else continue; } - if (this_degree > rhs_degree) { + if ( this_degree > rhs_degree ) { rhs.trees.pop_front(); - if (carry_node) { - if (carry_node->child_count() < rhs_degree) { - trees.insert(this_iterator, *carry_node); - trees.insert(this_iterator, *rhs_node); + if ( carry_node ) { + if ( carry_node->child_count() < rhs_degree ) { + trees.insert( this_iterator, *carry_node ); + trees.insert( this_iterator, *rhs_node ); carry_node = NULL; } else - carry_node = merge_trees(rhs_node, carry_node); + carry_node = merge_trees( rhs_node, carry_node ); } else - trees.insert(this_iterator, *rhs_node); + trees.insert( this_iterator, *rhs_node ); } } - if (!rhs.trees.empty()) { - if (carry_node) { + if ( !rhs.trees.empty() ) { + if ( carry_node ) { node_list_iterator rhs_it = rhs.trees.begin(); - while (static_cast(&*rhs_it)->child_count() < carry_node->child_count()) + while ( static_cast< node_pointer >( &*rhs_it )->child_count() < carry_node->child_count() ) ++rhs_it; - rhs.insert_node(rhs_it, carry_node); + rhs.insert_node( rhs_it, carry_node ); rhs.increment(); sorted_by_degree(); rhs.sorted_by_degree(); - if (trees.empty()) { - trees.splice(trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end()); + if ( trees.empty() ) { + trees.splice( trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end() ); update_top_element(); } else - merge_and_clear_nodes(rhs); + merge_and_clear_nodes( rhs ); } else - trees.splice(trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end()); + trees.splice( trees.end(), rhs.trees, rhs.trees.begin(), rhs.trees.end() ); return; } - if (carry_node) - insert_node(this_iterator, carry_node); + if ( carry_node ) + insert_node( this_iterator, carry_node ); } - void clone_forest(binomial_heap const & rhs) + void clone_forest( binomial_heap const& rhs ) { - BOOST_HEAP_ASSERT(trees.empty()); - typedef typename node_type::template node_cloner node_cloner; - trees.clone_from(rhs.trees, node_cloner(*this, NULL), detail::nop_disposer()); + BOOST_HEAP_ASSERT( trees.empty() ); + typedef typename node_type::template node_cloner< allocator_type > node_cloner; + trees.clone_from( rhs.trees, node_cloner( *this, NULL ), detail::nop_disposer() ); update_top_element(); } struct force_inf { - template - bool operator()(X const &, X const &) const + template < typename X > + bool operator()( X const&, X const& ) const { return false; } }; - template - void siftup(node_pointer n, Compare const & cmp) + template < typename Compare > + void siftup( node_pointer n, Compare const& cmp ) { - while (n->parent) { - node_pointer parent = n->parent; + while ( n->parent ) { + node_pointer parent = n->parent; node_pointer grand_parent = parent->parent; - if (cmp(n->value, parent->value)) + if ( cmp( n->value, parent->value ) ) return; n->remove_from_parent(); - n->swap_children(parent); + n->swap_children( parent ); n->update_children(); parent->update_children(); - if (grand_parent) { + if ( grand_parent ) { parent->remove_from_parent(); - grand_parent->add_child(n); + grand_parent->add_child( n ); } else { - node_list_iterator it = trees.erase(node_list_type::s_iterator_to(*parent)); - trees.insert(it, *n); + node_list_iterator it = trees.erase( node_list_type::s_iterator_to( *parent ) ); + trees.insert( it, *n ); } - n->add_child(parent); + n->add_child( parent ); } } - void siftdown(node_pointer n) + void siftdown( node_pointer n ) { - while (n->child_count()) { - node_pointer max_child = detail::find_max_child(n->children, super_t::get_internal_cmp()); + while ( n->child_count() ) { + node_pointer max_child + = detail::find_max_child< node_list_type, node_type, internal_compare >( n->children, + super_t::get_internal_cmp() ); - if (super_t::operator()(max_child->value, n->value)) + if ( super_t::operator()( max_child->value, n->value ) ) return; max_child->remove_from_parent(); - n->swap_children(max_child); + n->swap_children( max_child ); n->update_children(); max_child->update_children(); node_pointer parent = n->parent; - if (parent) { + if ( parent ) { n->remove_from_parent(); - max_child->add_child(n); - parent->add_child(max_child); + max_child->add_child( n ); + parent->add_child( max_child ); } else { - node_list_iterator position = trees.erase(node_list_type::s_iterator_to(*n)); - max_child->add_child(n); - trees.insert(position, *max_child); + node_list_iterator position = trees.erase( node_list_type::s_iterator_to( *n ) ); + max_child->add_child( n ); + trees.insert( position, *max_child ); } } } - void insert_node(node_list_iterator it, node_pointer n) + void insert_node( node_list_iterator it, node_pointer n ) { - if (it != trees.end()) - BOOST_HEAP_ASSERT(static_cast(&*it)->child_count() >= n->child_count()); + if ( it != trees.end() ) + BOOST_HEAP_ASSERT( static_cast< node_pointer >( &*it )->child_count() >= n->child_count() ); - while(true) { - BOOST_HEAP_ASSERT(!n->is_linked()); - if (it == trees.end()) + while ( true ) { + BOOST_HEAP_ASSERT( !n->is_linked() ); + if ( it == trees.end() ) break; - node_pointer this_node = static_cast(&*it); - size_type this_degree = this_node->child_count(); - size_type n_degree = n->child_count(); - if (this_degree == n_degree) { - BOOST_HEAP_ASSERT(it->is_linked()); - it = trees.erase(it); + node_pointer this_node = static_cast< node_pointer >( &*it ); + size_type this_degree = this_node->child_count(); + size_type n_degree = n->child_count(); + if ( this_degree == n_degree ) { + BOOST_HEAP_ASSERT( it->is_linked() ); + it = trees.erase( it ); - n = merge_trees(n, this_node); + n = merge_trees( n, this_node ); } else break; } - trees.insert(it, *n); + trees.insert( it, *n ); } // private constructor, just used in pop() - explicit binomial_heap(value_compare const & cmp, node_list_type & child_list, size_type size): - super_t(cmp) + explicit binomial_heap( value_compare const& cmp, node_list_type& child_list, size_type size ) : + super_t( cmp ) { - size_holder::set_size(size); - if (size) - top_element = static_cast(&*child_list.begin()); // not correct, but we will reset it later + size_holder::set_size( size ); + if ( size ) + top_element = static_cast< node_pointer >( &*child_list.begin() ); // not correct, but we will reset it later else top_element = NULL; - for (node_list_iterator it = child_list.begin(); it != child_list.end(); ++it) { - node_pointer n = static_cast(&*it); - n->parent = NULL; + for ( node_list_iterator it = child_list.begin(); it != child_list.end(); ++it ) { + node_pointer n = static_cast< node_pointer >( &*it ); + n->parent = NULL; } - trees.splice(trees.end(), child_list, child_list.begin(), child_list.end()); + trees.splice( trees.end(), child_list, child_list.begin(), child_list.end() ); - trees.sort(detail::cmp_by_degree()); + trees.sort( detail::cmp_by_degree< node_type >() ); } - node_pointer merge_trees (node_pointer node1, node_pointer node2) + node_pointer merge_trees( node_pointer node1, node_pointer node2 ) { - BOOST_HEAP_ASSERT(node1->child_count() == node2->child_count()); + BOOST_HEAP_ASSERT( node1->child_count() == node2->child_count() ); - if (super_t::operator()(node1->value, node2->value)) - std::swap(node1, node2); + if ( super_t::operator()( node1->value, node2->value ) ) + std::swap( node1, node2 ); - if (node2->parent) + if ( node2->parent ) node2->remove_from_parent(); - node1->add_child(node2); + node1->add_child( node2 ); return node1; } - void update_top_element(void) + void update_top_element( void ) { - top_element = detail::find_max_child(trees, super_t::get_internal_cmp()); + top_element + = detail::find_max_child< node_list_type, node_type, internal_compare >( trees, + super_t::get_internal_cmp() ); } - void sorted_by_degree(void) const + void sorted_by_degree( void ) const { -#ifdef BOOST_HEAP_SANITYCHECKS +# ifdef BOOST_HEAP_SANITYCHECKS int degree = -1; - for (node_list_const_iterator it = trees.begin(); it != trees.end(); ++it) { - const_node_pointer n = static_cast(&*it); - BOOST_HEAP_ASSERT(int(n->child_count()) > degree); + for ( node_list_const_iterator it = trees.begin(); it != trees.end(); ++it ) { + const_node_pointer n = static_cast< const_node_pointer >( &*it ); + BOOST_HEAP_ASSERT( int( n->child_count() ) > degree ); degree = n->child_count(); - BOOST_HEAP_ASSERT((detail::is_heap(n, *this))); + BOOST_HEAP_ASSERT( ( detail::is_heap< node_type, super_t >( n, *this ) ) ); - size_type child_nodes = detail::count_nodes(n); - BOOST_HEAP_ASSERT(child_nodes == size_type(1 << static_cast(&*it)->child_count())); + size_type child_nodes = detail::count_nodes< node_type >( n ); + BOOST_HEAP_ASSERT( child_nodes + == size_type( 1 << static_cast< const_node_pointer >( &*it )->child_count() ) ); } -#endif +# endif } - void sanity_check(void) + void sanity_check( void ) { -#ifdef BOOST_HEAP_SANITYCHECKS +# ifdef BOOST_HEAP_SANITYCHECKS sorted_by_degree(); - if (!empty()) { - node_pointer found_top = detail::find_max_child(trees, super_t::get_internal_cmp()); - BOOST_HEAP_ASSERT(top_element == found_top); + if ( !empty() ) { + node_pointer found_top + = detail::find_max_child< node_list_type, node_type, internal_compare >( trees, + super_t::get_internal_cmp() ); + BOOST_HEAP_ASSERT( top_element == found_top ); } - if (constant_time_size) { - size_t counted = detail::count_list_nodes(trees); - size_t stored = size_holder::get_size(); - BOOST_HEAP_ASSERT(counted == stored); + if ( constant_time_size ) { + size_t counted = detail::count_list_nodes< node_type, node_list_type >( trees ); + size_t stored = size_holder::get_size(); + BOOST_HEAP_ASSERT( counted == stored ); } -#endif +# endif } - node_pointer top_element; + node_pointer top_element; node_list_type trees; #endif // BOOST_DOXYGEN_INVOKED }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #undef BOOST_HEAP_ASSERT diff --git a/include/boost/heap/d_ary_heap.hpp b/include/boost/heap/d_ary_heap.hpp index 6e54c8c..1b42627 100644 --- a/include/boost/heap/d_ary_heap.hpp +++ b/include/boost/heap/d_ary_heap.hpp @@ -15,121 +15,116 @@ #include -#include #include +#include #include #include -#include +#include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif #ifndef BOOST_DOXYGEN_INVOKED -#ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT -#else -#define BOOST_HEAP_ASSERT(expression) -#endif +# ifdef BOOST_HEAP_SANITYCHECKS +# define BOOST_HEAP_ASSERT BOOST_ASSERT +# else +# define BOOST_HEAP_ASSERT( expression ) +# endif #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { struct nop_index_updater { - template - static void run(T &, std::size_t) + template < typename T > + static void run( T&, std::size_t ) {} }; -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > d_ary_heap_signature; +typedef parameter::parameters< boost::parameter::required< tag::arity >, + boost::parameter::optional< tag::allocator >, + boost::parameter::optional< tag::compare >, + boost::parameter::optional< tag::stable >, + boost::parameter::optional< tag::stability_counter_type >, + boost::parameter::optional< tag::constant_time_size > > + d_ary_heap_signature; /* base class for d-ary heap */ -template -class d_ary_heap: - private make_heap_base::type +template < typename T, class BoundArgs, class IndexUpdater > +class d_ary_heap : private make_heap_base< T, BoundArgs, false >::type { - typedef make_heap_base heap_base_maker; + typedef make_heap_base< T, BoundArgs, false > heap_base_maker; - typedef typename heap_base_maker::type super_t; + typedef typename heap_base_maker::type super_t; typedef typename super_t::internal_type internal_type; - typedef typename boost::allocator_rebind::type internal_type_allocator; - typedef std::vector container_type; - typedef typename container_type::const_iterator container_iterator; + 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; + typedef typename container_type::const_iterator container_iterator; typedef IndexUpdater index_updater; container_type q_; - static const unsigned int D = parameter::binding::type::value; + static const unsigned int D = parameter::binding< BoundArgs, tag::arity >::type::value; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; - struct implementation_defined: - extract_allocator_types + struct implementation_defined : extract_allocator_types< typename heap_base_maker::allocator_argument > { typedef T value_type; - typedef typename detail::extract_allocator_types::size_type size_type; + typedef + typename detail::extract_allocator_types< typename heap_base_maker::allocator_argument >::size_type size_type; - typedef typename heap_base_maker::compare_argument value_compare; + typedef typename heap_base_maker::compare_argument value_compare; typedef typename heap_base_maker::allocator_argument allocator_type; struct ordered_iterator_dispatcher { - static size_type max_index(const d_ary_heap * heap) + static size_type max_index( const d_ary_heap* heap ) { return heap->q_.size() - 1; } - static bool is_leaf(const d_ary_heap * heap, size_type index) + static bool is_leaf( const d_ary_heap* heap, size_type index ) { - return !heap->not_leaf(index); + return !heap->not_leaf( index ); } - static std::pair get_child_nodes(const d_ary_heap * heap, size_type index) + static std::pair< size_type, size_type > get_child_nodes( const d_ary_heap* heap, size_type index ) { - BOOST_HEAP_ASSERT(!is_leaf(heap, index)); - return std::make_pair(d_ary_heap::first_child_index(index), - heap->last_child_index(index)); + BOOST_HEAP_ASSERT( !is_leaf( heap, index ) ); + return std::make_pair( d_ary_heap::first_child_index( index ), heap->last_child_index( index ) ); } - static internal_type const & get_internal_value(const d_ary_heap * heap, size_type index) + static internal_type const& get_internal_value( const d_ary_heap* heap, size_type index ) { - return heap->q_[index]; + return heap->q_[ index ]; } - static value_type const & get_value(internal_type const & arg) + static value_type const& get_value( internal_type const& arg ) { - return super_t::get_value(arg); + return super_t::get_value( arg ); } }; - typedef detail::ordered_adaptor_iterator ordered_iterator; - - typedef detail::stable_heap_iterator iterator; - typedef iterator const_iterator; - typedef void * handle_type; + typedef detail::ordered_adaptor_iterator< const value_type, + internal_type, + d_ary_heap, + allocator_type, + typename super_t::internal_compare, + ordered_iterator_dispatcher > + ordered_iterator; + typedef detail::stable_heap_iterator< const value_type, container_iterator, super_t > iterator; + typedef iterator const_iterator; + typedef void* handle_type; }; typedef typename implementation_defined::ordered_iterator_dispatcher ordered_iterator_dispatcher; @@ -137,300 +132,297 @@ class d_ary_heap: 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; + 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; typedef typename implementation_defined::ordered_iterator ordered_iterator; - typedef typename implementation_defined::handle_type handle_type; + typedef typename implementation_defined::handle_type handle_type; - static const bool is_stable = extract_stable::value; + static const bool is_stable = extract_stable< BoundArgs >::value; - explicit d_ary_heap(value_compare const & cmp = value_compare()): - super_t(cmp) + explicit d_ary_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ) {} - d_ary_heap(d_ary_heap const & rhs): - super_t(rhs), q_(rhs.q_) + d_ary_heap( d_ary_heap const& rhs ) : + super_t( rhs ), + q_( rhs.q_ ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - d_ary_heap(d_ary_heap && rhs): - super_t(std::move(rhs)), q_(std::move(rhs.q_)) + d_ary_heap( d_ary_heap&& rhs ) : + super_t( std::move( rhs ) ), + q_( std::move( rhs.q_ ) ) {} - d_ary_heap & operator=(d_ary_heap && rhs) + d_ary_heap& operator=( d_ary_heap&& rhs ) { - super_t::operator=(std::move(rhs)); - q_ = std::move(rhs.q_); + super_t::operator=( std::move( rhs ) ); + q_ = std::move( rhs.q_ ); return *this; } #endif - d_ary_heap & operator=(d_ary_heap const & rhs) + d_ary_heap& operator=( d_ary_heap const& rhs ) { - static_cast(*this) = static_cast(rhs); - q_ = rhs.q_; + static_cast< super_t& >( *this ) = static_cast< super_t const& >( rhs ); + q_ = rhs.q_; return *this; } - bool empty(void) const + bool empty( void ) const { return q_.empty(); } - size_type size(void) const + size_type size( void ) const { return q_.size(); } - size_type max_size(void) const + size_type max_size( void ) const { return q_.max_size(); } - void clear(void) + void clear( void ) { q_.clear(); } - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return q_.get_allocator(); } - value_type const & top(void) const + value_type const& top( void ) const { - BOOST_ASSERT(!empty()); - return super_t::get_value(q_.front()); + BOOST_ASSERT( !empty() ); + return super_t::get_value( q_.front() ); } - void push(value_type const & v) + void push( value_type const& v ) { - q_.push_back(super_t::make_node(v)); - reset_index(size() - 1, size() - 1); - siftup(q_.size() - 1); + q_.push_back( super_t::make_node( v ) ); + reset_index( size() - 1, size() - 1 ); + siftup( q_.size() - 1 ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - void emplace(Args&&... args) +#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)...)); - reset_index(size() - 1, size() - 1); - siftup(q_.size() - 1); + q_.emplace_back( super_t::make_node( std::forward< Args >( args )... ) ); + reset_index( size() - 1, size() - 1 ); + siftup( q_.size() - 1 ); } #endif - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); - std::swap(q_.front(), q_.back()); + BOOST_ASSERT( !empty() ); + std::swap( q_.front(), q_.back() ); q_.pop_back(); - if (q_.empty()) + if ( q_.empty() ) return; - reset_index(0, 0); - siftdown(0); + reset_index( 0, 0 ); + siftdown( 0 ); } - void swap(d_ary_heap & rhs) + void swap( d_ary_heap& rhs ) { - super_t::swap(rhs); - q_.swap(rhs.q_); + super_t::swap( rhs ); + q_.swap( rhs.q_ ); } - iterator begin(void) const + iterator begin( void ) const { - return iterator(q_.begin()); + return iterator( q_.begin() ); } - iterator end(void) const + iterator end( void ) const { - return iterator(q_.end()); + return iterator( q_.end() ); } - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - return ordered_iterator(0, this, super_t::get_internal_cmp()); + return ordered_iterator( 0, this, super_t::get_internal_cmp() ); } - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(size(), this, super_t::get_internal_cmp()); + return ordered_iterator( size(), this, super_t::get_internal_cmp() ); } - void reserve (size_type element_count) + void reserve( size_type element_count ) { - q_.reserve(element_count); + q_.reserve( element_count ); } - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } private: - void reset_index(size_type index, size_type new_index) + void reset_index( size_type index, size_type new_index ) { - BOOST_HEAP_ASSERT(index < q_.size()); - index_updater::run(q_[index], new_index); + BOOST_HEAP_ASSERT( index < q_.size() ); + index_updater::run( q_[ index ], new_index ); } - void siftdown(size_type index) + void siftdown( size_type index ) { - while (not_leaf(index)) { - size_type max_child_index = top_child_index(index); - if (!super_t::operator()(q_[max_child_index], q_[index])) { - reset_index(index, max_child_index); - reset_index(max_child_index, index); - std::swap(q_[max_child_index], q_[index]); + while ( not_leaf( index ) ) { + size_type max_child_index = top_child_index( index ); + if ( !super_t::operator()( q_[ max_child_index ], q_[ index ] ) ) { + reset_index( index, max_child_index ); + reset_index( max_child_index, index ); + std::swap( q_[ max_child_index ], q_[ index ] ); index = max_child_index; - } - else + } else return; } } /* returns new index */ - void siftup(size_type index) + void siftup( size_type index ) { - while (index != 0) { - size_type parent = parent_index(index); + while ( index != 0 ) { + size_type parent = parent_index( index ); - if (super_t::operator()(q_[parent], q_[index])) { - reset_index(index, parent); - reset_index(parent, index); - std::swap(q_[parent], q_[index]); + if ( super_t::operator()( q_[ parent ], q_[ index ] ) ) { + reset_index( index, parent ); + reset_index( parent, index ); + std::swap( q_[ parent ], q_[ index ] ); index = parent; - } - else + } else return; } } - bool not_leaf(size_type index) const + bool not_leaf( size_type index ) const { - const size_t first_child = first_child_index(index); + const size_t first_child = first_child_index( index ); return first_child < q_.size(); } - size_type top_child_index(size_type index) const + size_type top_child_index( size_type index ) const { // invariant: index is not a leaf, so the iterator range is not empty - const size_t first_index = first_child_index(index); + const size_t first_index = first_child_index( index ); typedef typename container_type::const_iterator container_iterator; const container_iterator first_child = q_.begin() + first_index; - const container_iterator end = q_.end(); + const container_iterator end = q_.end(); - const size_type max_elements = std::distance(first_child, end); + const size_type max_elements = std::distance( first_child, end ); - const container_iterator last_child = (max_elements > D) ? first_child + D - : end; + const container_iterator last_child = ( max_elements > D ) ? first_child + D : end; - const container_iterator min_element = std::max_element(first_child, last_child, static_cast(*this)); + const container_iterator min_element + = std::max_element( first_child, last_child, static_cast< super_t const& >( *this ) ); return min_element - q_.begin(); } - static size_type parent_index(size_type index) + static size_type parent_index( size_type index ) { - return (index - 1) / D; + return ( index - 1 ) / D; } - static size_type first_child_index(size_type index) + static size_type first_child_index( size_type index ) { return index * D + 1; } - size_type last_child_index(size_type index) const + size_type last_child_index( size_type index ) const { - const size_t first_index = first_child_index(index); - const size_type last_index = (std::min)(first_index + D - 1, size() - 1); + const size_t first_index = first_child_index( index ); + const size_type last_index = ( std::min )( first_index + D - 1, size() - 1 ); return last_index; } - template - struct rebind { - typedef d_ary_heap, - boost::heap::stability_counter_type, - boost::heap::arity, - boost::heap::compare, - boost::heap::allocator - >::type, - X - > other; + template < typename U, typename V, typename W, typename X > + struct rebind + { + typedef d_ary_heap< U, + typename d_ary_heap_signature::bind< + boost::heap::stable< heap_base_maker::is_stable >, + boost::heap::stability_counter_type< typename heap_base_maker::stability_counter_type >, + boost::heap::arity< D >, + boost::heap::compare< V >, + boost::heap::allocator< W > >::type, + X > + other; }; - template friend class priority_queue_mutable_wrapper; + template < class U > + friend class priority_queue_mutable_wrapper; - void update(size_type index) + void update( size_type index ) { - if (index == 0) { - siftdown(index); + if ( index == 0 ) { + siftdown( index ); return; } - size_type parent = parent_index(index); + size_type parent = parent_index( index ); - if (super_t::operator()(q_[parent], q_[index])) - siftup(index); + if ( super_t::operator()( q_[ parent ], q_[ index ] ) ) + siftup( index ); else - siftdown(index); + siftdown( index ); } - void erase(size_type index) + void erase( size_type index ) { - while (index != 0) - { - size_type parent = parent_index(index); + while ( index != 0 ) { + size_type parent = parent_index( index ); - reset_index(index, parent); - reset_index(parent, index); - std::swap(q_[parent], q_[index]); + reset_index( index, parent ); + reset_index( parent, index ); + std::swap( q_[ parent ], q_[ index ] ); index = parent; } pop(); } - void increase(size_type index) + void increase( size_type index ) { - siftup(index); + siftup( index ); } - void decrease(size_type index) + void decrease( size_type index ) { - siftdown(index); + siftdown( index ); } }; -template +template < typename T, typename BoundArgs > struct select_dary_heap { - static const bool is_mutable = extract_mutable::value; + static const bool is_mutable = extract_mutable< BoundArgs >::value; typedef typename boost::conditional< is_mutable, - priority_queue_mutable_wrapper >, - d_ary_heap - >::type type; + priority_queue_mutable_wrapper< d_ary_heap< T, BoundArgs, nop_index_updater > >, + d_ary_heap< T, BoundArgs, nop_index_updater > >::type type; }; } /* namespace detail */ - /** * \class d_ary_heap * \brief d-ary heap class @@ -451,194 +443,192 @@ struct select_dary_heap * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +template < typename T, + class A0 = boost::parameter::void_, + class A1 = boost::parameter::void_, + class A2 = boost::parameter::void_, + class A3 = boost::parameter::void_, + class A4 = boost::parameter::void_, + class A5 = boost::parameter::void_ > #endif -class d_ary_heap: - public detail::select_dary_heap::type>::type +class d_ary_heap : + public detail::select_dary_heap< T, typename detail::d_ary_heap_signature::bind< A0, A1, A2, A3, A4, A5 >::type >::type { - typedef typename detail::d_ary_heap_signature::bind::type bound_args; - typedef typename detail::select_dary_heap::type super_t; + typedef typename detail::d_ary_heap_signature::bind< A0, A1, A2, A3, A4, A5 >::type bound_args; + typedef typename detail::select_dary_heap< T, bound_args >::type super_t; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; #ifndef BOOST_DOXYGEN_INVOKED - static const bool is_mutable = detail::extract_mutable::value; + static const bool is_mutable = detail::extract_mutable< bound_args >::value; -#define BOOST_HEAP_TYPEDEF_FROM_SUPER_T(NAME) \ - typedef typename super_t::NAME NAME; +# define BOOST_HEAP_TYPEDEF_FROM_SUPER_T( NAME ) typedef typename super_t::NAME NAME; struct implementation_defined { - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(size_type) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(difference_type) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(value_compare) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(allocator_type) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(reference) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_reference) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(pointer) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_pointer) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(iterator) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(const_iterator) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(ordered_iterator) - BOOST_HEAP_TYPEDEF_FROM_SUPER_T(handle_type) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( size_type ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( difference_type ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( value_compare ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( allocator_type ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( reference ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( const_reference ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( pointer ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( const_pointer ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( iterator ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( const_iterator ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( ordered_iterator ) + BOOST_HEAP_TYPEDEF_FROM_SUPER_T( handle_type ) }; -#undef BOOST_HEAP_TYPEDEF_FROM_SUPER_T +# undef BOOST_HEAP_TYPEDEF_FROM_SUPER_T #endif public: - static const bool constant_time_size = true; + static const bool constant_time_size = true; static const bool has_ordered_iterators = true; - static const bool is_mergable = false; - static const bool has_reserve = true; - static const bool is_stable = super_t::is_stable; + static const bool is_mergable = false; + static const bool has_reserve = true; + static const bool is_stable = super_t::is_stable; - 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 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; /// \copydoc boost::heap::priority_queue::iterator - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; typedef typename implementation_defined::ordered_iterator ordered_iterator; - typedef typename implementation_defined::handle_type handle_type; + typedef typename implementation_defined::handle_type handle_type; /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) - explicit d_ary_heap(value_compare const & cmp = value_compare()): - super_t(cmp) + explicit d_ary_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ) {} /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) - d_ary_heap(d_ary_heap const & rhs): - super_t(rhs) + d_ary_heap( d_ary_heap const& rhs ) : + super_t( rhs ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) - d_ary_heap(d_ary_heap && rhs): - super_t(std::move(rhs)) + d_ary_heap( d_ary_heap&& rhs ) : + super_t( std::move( rhs ) ) {} /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) - d_ary_heap & operator=(d_ary_heap && rhs) + d_ary_heap& operator=( d_ary_heap&& rhs ) { - super_t::operator=(std::move(rhs)); + super_t::operator=( std::move( rhs ) ); return *this; } #endif /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) - d_ary_heap & operator=(d_ary_heap const & rhs) + d_ary_heap& operator=( d_ary_heap const& rhs ) { - super_t::operator=(rhs); + super_t::operator=( rhs ); return *this; } /// \copydoc boost::heap::priority_queue::empty - bool empty(void) const + bool empty( void ) const { return super_t::empty(); } /// \copydoc boost::heap::priority_queue::size - size_type size(void) const + size_type size( void ) const { return super_t::size(); } /// \copydoc boost::heap::priority_queue::max_size - size_type max_size(void) const + size_type max_size( void ) const { return super_t::max_size(); } /// \copydoc boost::heap::priority_queue::clear - void clear(void) + void clear( void ) { super_t::clear(); } /// \copydoc boost::heap::priority_queue::get_allocator - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return super_t::get_allocator(); } /// \copydoc boost::heap::priority_queue::top - value_type const & top(void) const + value_type const& top( void ) const { return super_t::top(); } /// \copydoc boost::heap::priority_queue::push - typename boost::conditional::type push(value_type const & v) + typename boost::conditional< is_mutable, handle_type, void >::type push( value_type const& v ) { - return super_t::push(v); + return super_t::push( v ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /// \copydoc boost::heap::priority_queue::emplace - template - typename boost::conditional::type emplace(Args&&... args) + template < class... Args > + typename boost::conditional< is_mutable, handle_type, void >::type emplace( Args&&... args ) { - return super_t::emplace(std::forward(args)...); + return super_t::emplace( std::forward< Args >( args )... ); } #endif /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } /** @@ -648,10 +638,10 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - void update(handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::update(handle, v); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::update( handle, v ); } /** @@ -663,13 +653,13 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - void update(handle_type handle) + void update( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::update(handle); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::update( handle ); } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \b Complexity: Logarithmic. @@ -678,10 +668,10 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - void increase(handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::increase(handle, v); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::increase( handle, v ); } /** @@ -689,17 +679,18 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * * \b Requirement: data structure must be configured as mutable * */ - void increase(handle_type handle) + void increase( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::increase(handle); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::increase( handle ); } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \b Complexity: Logarithmic. @@ -708,10 +699,10 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - void decrease(handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::decrease(handle, v); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::decrease( handle, v ); } /** @@ -719,14 +710,15 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * * \b Requirement: data structure must be configured as mutable * */ - void decrease(handle_type handle) + void decrease( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::decrease(handle); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::decrease( handle ); } /** @@ -736,10 +728,10 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - void erase(handle_type handle) + void erase( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); - super_t::erase(handle); + BOOST_STATIC_ASSERT( is_mutable ); + super_t::erase( handle ); } /** @@ -749,75 +741,74 @@ public: * * \b Requirement: data structure must be configured as mutable * */ - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - BOOST_STATIC_ASSERT(is_mutable); - return super_t::s_handle_from_iterator(it); + BOOST_STATIC_ASSERT( is_mutable ); + return super_t::s_handle_from_iterator( it ); } /// \copydoc boost::heap::priority_queue::pop - void pop(void) + void pop( void ) { super_t::pop(); } /// \copydoc boost::heap::priority_queue::swap - void swap(d_ary_heap & rhs) + void swap( d_ary_heap& rhs ) { - super_t::swap(rhs); + super_t::swap( rhs ); } /// \copydoc boost::heap::priority_queue::begin - const_iterator begin(void) const + const_iterator begin( void ) const { return super_t::begin(); } /// \copydoc boost::heap::priority_queue::begin - iterator begin(void) + iterator begin( void ) { return super_t::begin(); } /// \copydoc boost::heap::priority_queue::end - iterator end(void) + iterator end( void ) { return super_t::end(); } /// \copydoc boost::heap::priority_queue::end - const_iterator end(void) const + const_iterator end( void ) const { return super_t::end(); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { return super_t::ordered_begin(); } /// \copydoc boost::heap::fibonacci_heap::ordered_end - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { return super_t::ordered_end(); } /// \copydoc boost::heap::priority_queue::reserve - void reserve (size_type element_count) + void reserve( size_type element_count ) { - super_t::reserve(element_count); + super_t::reserve( element_count ); } /// \copydoc boost::heap::priority_queue::value_comp - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #undef BOOST_HEAP_ASSERT diff --git a/include/boost/heap/detail/heap_comparison.hpp b/include/boost/heap/detail/heap_comparison.hpp index 69d9814..a2fa54d 100644 --- a/include/boost/heap/detail/heap_comparison.hpp +++ b/include/boost/heap/detail/heap_comparison.hpp @@ -10,81 +10,77 @@ #define BOOST_HEAP_DETAIL_HEAP_COMPARISON_HPP #include -#include #include #include +#include #include #ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT +# define BOOST_HEAP_ASSERT BOOST_ASSERT #else -#define BOOST_HEAP_ASSERT(expression) +# define BOOST_HEAP_ASSERT( expression ) #endif -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { -template -bool value_equality(Heap1 const & lhs, Heap2 const & rhs, - typename Heap1::value_type lval, typename Heap2::value_type rval) +template < typename Heap1, typename Heap2 > +bool value_equality( Heap1 const& lhs, Heap2 const& rhs, typename Heap1::value_type lval, typename Heap2::value_type rval ) { - typename Heap1::value_compare const & cmp = lhs.value_comp(); - bool ret = !(cmp(lval, rval)) && !(cmp(rval, lval)); + typename Heap1::value_compare const& cmp = lhs.value_comp(); + bool ret = !( cmp( lval, rval ) ) && !( cmp( rval, lval ) ); // if this assertion is triggered, the value_compare objects of lhs and rhs return different values - BOOST_ASSERT((ret == (!(rhs.value_comp()(lval, rval)) && !(rhs.value_comp()(rval, lval))))); + BOOST_ASSERT( ( ret == ( !( rhs.value_comp()( lval, rval ) ) && !( rhs.value_comp()( rval, lval ) ) ) ) ); return ret; } -template -bool value_compare(Heap1 const & lhs, Heap2 const & rhs, - typename Heap1::value_type lval, typename Heap2::value_type rval) +template < typename Heap1, typename Heap2 > +bool value_compare( Heap1 const& lhs, Heap2 const& rhs, typename Heap1::value_type lval, typename Heap2::value_type rval ) { - typename Heap1::value_compare const & cmp = lhs.value_comp(); - bool ret = cmp(lval, rval); + typename Heap1::value_compare const& cmp = lhs.value_comp(); + bool ret = cmp( lval, rval ); // if this assertion is triggered, the value_compare objects of lhs and rhs return different values - BOOST_ASSERT((ret == rhs.value_comp()(lval, rval))); + BOOST_ASSERT( ( ret == rhs.value_comp()( lval, rval ) ) ); return ret; } struct heap_equivalence_copy { - template - bool operator()(Heap1 const & lhs, Heap2 const & rhs) + template < typename Heap1, typename Heap2 > + bool operator()( Heap1 const& lhs, Heap2 const& rhs ) { - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap1 >)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap2 >)); // if this assertion is triggered, the value_compare types are incompatible - BOOST_STATIC_ASSERT((boost::is_same::value)); + BOOST_STATIC_ASSERT( ( boost::is_same< typename Heap1::value_compare, typename Heap2::value_compare >::value ) ); - if (Heap1::constant_time_size && Heap2::constant_time_size) - if (lhs.size() != rhs.size()) + if ( Heap1::constant_time_size && Heap2::constant_time_size ) + if ( lhs.size() != rhs.size() ) return false; - if (lhs.empty() && rhs.empty()) + if ( lhs.empty() && rhs.empty() ) return true; - Heap1 lhs_copy(lhs); - Heap2 rhs_copy(rhs); + Heap1 lhs_copy( lhs ); + Heap2 rhs_copy( rhs ); - while (true) { - if (!value_equality(lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top())) + while ( true ) { + if ( !value_equality( lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top() ) ) return false; lhs_copy.pop(); rhs_copy.pop(); - if (lhs_copy.empty() && rhs_copy.empty()) + if ( lhs_copy.empty() && rhs_copy.empty() ) return true; - if (lhs_copy.empty()) + if ( lhs_copy.empty() ) return false; - if (rhs_copy.empty()) + if ( rhs_copy.empty() ) return false; } } @@ -93,93 +89,87 @@ struct heap_equivalence_copy struct heap_equivalence_iteration { - template - bool operator()(Heap1 const & lhs, Heap2 const & rhs) + template < typename Heap1, typename Heap2 > + bool operator()( Heap1 const& lhs, Heap2 const& rhs ) { - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap1 >)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap2 >)); // if this assertion is triggered, the value_compare types are incompatible - BOOST_STATIC_ASSERT((boost::is_same::value)); + BOOST_STATIC_ASSERT( ( boost::is_same< typename Heap1::value_compare, typename Heap2::value_compare >::value ) ); - if (Heap1::constant_time_size && Heap2::constant_time_size) - if (lhs.size() != rhs.size()) + if ( Heap1::constant_time_size && Heap2::constant_time_size ) + if ( lhs.size() != rhs.size() ) return false; - if (lhs.empty() && rhs.empty()) + if ( lhs.empty() && rhs.empty() ) return true; - typename Heap1::ordered_iterator it1 = lhs.ordered_begin(); + typename Heap1::ordered_iterator it1 = lhs.ordered_begin(); typename Heap1::ordered_iterator it1_end = lhs.ordered_end(); - typename Heap1::ordered_iterator it2 = rhs.ordered_begin(); + typename Heap1::ordered_iterator it2 = rhs.ordered_begin(); typename Heap1::ordered_iterator it2_end = rhs.ordered_end(); - while (true) { - if (!value_equality(lhs, rhs, *it1, *it2)) + while ( true ) { + if ( !value_equality( lhs, rhs, *it1, *it2 ) ) return false; ++it1; ++it2; - if (it1 == it1_end && it2 == it2_end) + if ( it1 == it1_end && it2 == it2_end ) return true; - if (it1 == it1_end || it2 == it2_end) + if ( it1 == it1_end || it2 == it2_end ) return false; } } }; -template -bool heap_equality(Heap1 const & lhs, Heap2 const & rhs) +template < typename Heap1, typename Heap2 > +bool heap_equality( Heap1 const& lhs, Heap2 const& rhs ) { const bool use_ordered_iterators = Heap1::has_ordered_iterators && Heap2::has_ordered_iterators; - typedef typename boost::conditional::type equivalence_check; + typedef typename boost::conditional< use_ordered_iterators, heap_equivalence_iteration, heap_equivalence_copy >::type + equivalence_check; equivalence_check eq_check; - return eq_check(lhs, rhs); + return eq_check( lhs, rhs ); } struct heap_compare_iteration { - template - bool operator()(Heap1 const & lhs, Heap2 const & rhs) + template < typename Heap1, typename Heap2 > + bool operator()( Heap1 const& lhs, Heap2 const& rhs ) { - typename Heap1::size_type left_size = lhs.size(); + typename Heap1::size_type left_size = lhs.size(); typename Heap2::size_type right_size = rhs.size(); - if (left_size < right_size) + if ( left_size < right_size ) return true; - if (left_size > right_size) + if ( left_size > right_size ) return false; - typename Heap1::ordered_iterator it1 = lhs.ordered_begin(); + typename Heap1::ordered_iterator it1 = lhs.ordered_begin(); typename Heap1::ordered_iterator it1_end = lhs.ordered_end(); - typename Heap1::ordered_iterator it2 = rhs.ordered_begin(); + typename Heap1::ordered_iterator it2 = rhs.ordered_begin(); typename Heap1::ordered_iterator it2_end = rhs.ordered_end(); - while (true) { - if (value_compare(lhs, rhs, *it1, *it2)) + while ( true ) { + if ( value_compare( lhs, rhs, *it1, *it2 ) ) return true; - if (value_compare(lhs, rhs, *it2, *it1)) + if ( value_compare( lhs, rhs, *it2, *it1 ) ) return false; ++it1; ++it2; - if (it1 == it1_end && it2 == it2_end) + if ( it1 == it1_end && it2 == it2_end ) return true; - if (it1 == it1_end || it2 == it2_end) + if ( it1 == it1_end || it2 == it2_end ) return false; } } @@ -187,58 +177,50 @@ struct heap_compare_iteration struct heap_compare_copy { - template - bool operator()(Heap1 const & lhs, Heap2 const & rhs) + template < typename Heap1, typename Heap2 > + bool operator()( Heap1 const& lhs, Heap2 const& rhs ) { - typename Heap1::size_type left_size = lhs.size(); + typename Heap1::size_type left_size = lhs.size(); typename Heap2::size_type right_size = rhs.size(); - if (left_size < right_size) + if ( left_size < right_size ) return true; - if (left_size > right_size) + if ( left_size > right_size ) return false; - Heap1 lhs_copy(lhs); - Heap2 rhs_copy(rhs); + Heap1 lhs_copy( lhs ); + Heap2 rhs_copy( rhs ); - while (true) { - if (value_compare(lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top())) + while ( true ) { + if ( value_compare( lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top() ) ) return true; - if (value_compare(lhs_copy, rhs_copy, rhs_copy.top(), lhs_copy.top())) + if ( value_compare( lhs_copy, rhs_copy, rhs_copy.top(), lhs_copy.top() ) ) return false; lhs_copy.pop(); rhs_copy.pop(); - if (lhs_copy.empty() && rhs_copy.empty()) + if ( lhs_copy.empty() && rhs_copy.empty() ) return false; } } }; -template -bool heap_compare(Heap1 const & lhs, Heap2 const & rhs) +template < typename Heap1, typename Heap2 > +bool heap_compare( Heap1 const& lhs, Heap2 const& rhs ) { const bool use_ordered_iterators = Heap1::has_ordered_iterators && Heap2::has_ordered_iterators; - typedef typename boost::conditional::type compare_check; + typedef + typename boost::conditional< use_ordered_iterators, heap_compare_iteration, heap_compare_copy >::type compare_check; compare_check check_object; - return check_object(lhs, rhs); + return check_object( lhs, rhs ); } -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #undef BOOST_HEAP_ASSERT diff --git a/include/boost/heap/detail/heap_node.hpp b/include/boost/heap/detail/heap_node.hpp index 755eac3..92ca3d4 100644 --- a/include/boost/heap/detail/heap_node.hpp +++ b/include/boost/heap/detail/heap_node.hpp @@ -10,81 +10,76 @@ #define BOOST_HEAP_DETAIL_HEAP_NODE_HPP #include -#include #include #include +#include #include #ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT +# define BOOST_HEAP_ASSERT BOOST_ASSERT #else -#define BOOST_HEAP_ASSERT(expression) +# define BOOST_HEAP_ASSERT( expression ) #endif -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { namespace bi = boost::intrusive; -template -struct heap_node_base: - bi::list_base_hook, - bi::link_mode - >::type - > +template < bool auto_unlink = false > +struct heap_node_base : + bi::list_base_hook< + typename boost::conditional< auto_unlink, bi::link_mode< bi::auto_unlink >, bi::link_mode< bi::safe_link > >::type > {}; -typedef bi::list > heap_node_list; +typedef bi::list< heap_node_base< false > > heap_node_list; struct nop_disposer { - template - void operator()(T * n) + template < typename T > + void operator()( T* n ) { - BOOST_HEAP_ASSERT(false); + BOOST_HEAP_ASSERT( false ); } }; -template -bool is_heap(const Node * n, typename HeapBase::value_compare const & cmp) +template < typename Node, typename HeapBase > +bool is_heap( const Node* n, typename HeapBase::value_compare const& cmp ) { - for (typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it) { - Node const & this_node = static_cast(*it); - const Node * child = static_cast(&this_node); + for ( typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it ) { + Node const& this_node = static_cast< Node const& >( *it ); + const Node* child = static_cast< const Node* >( &this_node ); - if (cmp(HeapBase::get_value(n->value), HeapBase::get_value(child->value)) || - !is_heap(child, cmp)) + if ( cmp( HeapBase::get_value( n->value ), HeapBase::get_value( child->value ) ) + || !is_heap< Node, HeapBase >( child, cmp ) ) return false; } return true; } -template -std::size_t count_nodes(const Node * n); +template < typename Node > +std::size_t count_nodes( const Node* n ); -template -std::size_t count_list_nodes(List const & node_list) +template < typename Node, typename List > +std::size_t count_list_nodes( List const& node_list ) { std::size_t ret = 0; - for (typename List::const_iterator it = node_list.begin(); it != node_list.end(); ++it) { - const Node * child = static_cast(&*it); - ret += count_nodes(child); + for ( typename List::const_iterator it = node_list.begin(); it != node_list.end(); ++it ) { + const Node* child = static_cast< const Node* >( &*it ); + ret += count_nodes< Node >( child ); } return ret; } -template -std::size_t count_nodes(const Node * n) +template < typename Node > +std::size_t count_nodes( const Node* n ) { - return 1 + count_list_nodes(n->children); + return 1 + count_list_nodes< Node, typename Node::child_list >( n->children ); } -template -void destroy_node(Node& node) +template < class Node > +void destroy_node( Node& node ) { node.~Node(); } @@ -100,31 +95,29 @@ void destroy_node(Node& node) * Node::Node(Node const &, Alloc &, Node * parent) * * */ -template +template < typename Node, typename NodeBase, typename Alloc > struct node_cloner { - node_cloner(Alloc & allocator): - allocator(allocator) + node_cloner( Alloc& allocator ) : + allocator( allocator ) {} - Node * operator() (NodeBase const & node) + Node* operator()( NodeBase const& node ) { - Node * ret = allocator.allocate(1); - new (ret) Node(static_cast(node), allocator); + Node* ret = allocator.allocate( 1 ); + new ( ret ) Node( static_cast< Node const& >( node ), allocator ); return ret; } - Node * operator() (NodeBase const & node, Node * parent) + Node* operator()( NodeBase const& node, Node* parent ) { - Node * ret = allocator.allocate(1); - new (ret) Node(static_cast(node), allocator, parent); + Node* ret = allocator.allocate( 1 ); + new ( ret ) Node( static_cast< Node const& >( node ), allocator, parent ); return ret; } private: - Alloc & allocator; + Alloc& allocator; }; /* node disposer @@ -133,241 +126,238 @@ private: * Node::clear_subtree(Alloc &) clears the subtree via allocator * * */ -template +template < typename Node, typename NodeBase, typename Alloc > struct node_disposer { - typedef typename boost::allocator_pointer::type node_pointer; + typedef typename boost::allocator_pointer< Alloc >::type node_pointer; - node_disposer(Alloc & alloc): - alloc_(alloc) + node_disposer( Alloc& alloc ) : + alloc_( alloc ) {} - void operator()(NodeBase * base) + void operator()( NodeBase* base ) { - node_pointer n = static_cast(base); - n->clear_subtree(alloc_); - boost::heap::detail::destroy_node(*n); - alloc_.deallocate(n, 1); + node_pointer n = static_cast< node_pointer >( base ); + n->clear_subtree( alloc_ ); + boost::heap::detail::destroy_node( *n ); + alloc_.deallocate( n, 1 ); } - Alloc & alloc_; + Alloc& alloc_; }; -template -struct heap_node: - heap_node_base +template < typename ValueType, bool constant_time_child_size = true > +struct heap_node : heap_node_base< !constant_time_child_size > { - typedef heap_node_base node_base; + typedef heap_node_base< !constant_time_child_size > node_base; public: typedef ValueType value_type; - typedef bi::list > child_list; + typedef bi::list< node_base, bi::constant_time_size< constant_time_child_size > > child_list; - typedef typename child_list::iterator child_iterator; + typedef typename child_list::iterator child_iterator; typedef typename child_list::const_iterator const_child_iterator; - typedef typename child_list::size_type size_type; + typedef typename child_list::size_type size_type; - heap_node(ValueType const & v): - value(v) + heap_node( ValueType const& v ) : + value( v ) {} -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - heap_node(Args&&... args): - value(std::forward(args)...) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + heap_node( Args&&... args ) : + value( std::forward< Args >( args )... ) {} #endif -/* protected: */ - heap_node(heap_node const & rhs): - value(rhs.value) + /* protected: */ + heap_node( heap_node const& rhs ) : + value( rhs.value ) { /* we don't copy the child list, but clone it later */ } public: - - template - heap_node (heap_node const & rhs, Alloc & allocator): - value(rhs.value) + template < typename Alloc > + heap_node( heap_node const& rhs, Alloc& allocator ) : + value( rhs.value ) { - children.clone_from(rhs.children, node_cloner(allocator), nop_disposer()); + children.clone_from( rhs.children, node_cloner< heap_node, node_base, Alloc >( allocator ), nop_disposer() ); } - size_type child_count(void) const + size_type child_count( void ) const { - BOOST_STATIC_ASSERT(constant_time_child_size); + BOOST_STATIC_ASSERT( constant_time_child_size ); return children.size(); } - void add_child(heap_node * n) + void add_child( heap_node* n ) { - children.push_back(*n); + children.push_back( *n ); } - template - void clear_subtree(Alloc & alloc) + template < typename Alloc > + void clear_subtree( Alloc& alloc ) { - children.clear_and_dispose(node_disposer(alloc)); + children.clear_and_dispose( node_disposer< heap_node, node_base, Alloc >( alloc ) ); } - void swap_children(heap_node * rhs) + void swap_children( heap_node* rhs ) { - children.swap(rhs->children); + children.swap( rhs->children ); } - ValueType value; + ValueType value; child_list children; }; -template -struct parent_pointing_heap_node: - heap_node +template < typename value_type > +struct parent_pointing_heap_node : heap_node< value_type > { - typedef heap_node super_t; + typedef heap_node< value_type > super_t; - parent_pointing_heap_node(value_type const & v): - super_t(v), parent(NULL) + parent_pointing_heap_node( value_type const& v ) : + super_t( v ), + parent( NULL ) {} -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - parent_pointing_heap_node(Args&&... args): - super_t(std::forward(args)...), parent(NULL) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + parent_pointing_heap_node( Args&&... args ) : + super_t( std::forward< Args >( args )... ), + parent( NULL ) {} #endif - template + template < typename Alloc > struct node_cloner { - node_cloner(Alloc & allocator, parent_pointing_heap_node * parent): - allocator(allocator), parent_(parent) + node_cloner( Alloc& allocator, parent_pointing_heap_node* parent ) : + allocator( allocator ), + parent_( parent ) {} - parent_pointing_heap_node * operator() (typename super_t::node_base const & node) + parent_pointing_heap_node* operator()( typename super_t::node_base const& node ) { - parent_pointing_heap_node * ret = allocator.allocate(1); - new (ret) parent_pointing_heap_node(static_cast(node), allocator, parent_); + parent_pointing_heap_node* ret = allocator.allocate( 1 ); + new ( ret ) + parent_pointing_heap_node( static_cast< parent_pointing_heap_node const& >( node ), allocator, parent_ ); return ret; } private: - Alloc & allocator; - parent_pointing_heap_node * parent_; + Alloc& allocator; + parent_pointing_heap_node* parent_; }; - template - parent_pointing_heap_node (parent_pointing_heap_node const & rhs, Alloc & allocator, parent_pointing_heap_node * parent): - super_t(static_cast(rhs)), parent(parent) + template < typename Alloc > + parent_pointing_heap_node( parent_pointing_heap_node const& rhs, + Alloc& allocator, + parent_pointing_heap_node* parent ) : + super_t( static_cast< super_t const& >( rhs ) ), + parent( parent ) { - super_t::children.clone_from(rhs.children, node_cloner(allocator, this), nop_disposer()); + super_t::children.clone_from( rhs.children, node_cloner< Alloc >( allocator, this ), nop_disposer() ); } - void update_children(void) + void update_children( void ) { typedef heap_node_list::iterator node_list_iterator; - for (node_list_iterator it = super_t::children.begin(); it != super_t::children.end(); ++it) { - parent_pointing_heap_node * child = static_cast(&*it); - child->parent = this; + for ( node_list_iterator it = super_t::children.begin(); it != super_t::children.end(); ++it ) { + parent_pointing_heap_node* child = static_cast< parent_pointing_heap_node* >( &*it ); + child->parent = this; } } - void remove_from_parent(void) + void remove_from_parent( void ) { - BOOST_HEAP_ASSERT(parent); - parent->children.erase(heap_node_list::s_iterator_to(*this)); + BOOST_HEAP_ASSERT( parent ); + parent->children.erase( heap_node_list::s_iterator_to( *this ) ); parent = NULL; } - void add_child(parent_pointing_heap_node * n) + void add_child( parent_pointing_heap_node* n ) { - BOOST_HEAP_ASSERT(n->parent == NULL); + BOOST_HEAP_ASSERT( n->parent == NULL ); n->parent = this; - super_t::add_child(n); + super_t::add_child( n ); } - parent_pointing_heap_node * get_parent(void) + parent_pointing_heap_node* get_parent( void ) { return parent; } - const parent_pointing_heap_node * get_parent(void) const + const parent_pointing_heap_node* get_parent( void ) const { return parent; } - parent_pointing_heap_node * parent; + parent_pointing_heap_node* parent; }; -template -struct marked_heap_node: - parent_pointing_heap_node +template < typename value_type > +struct marked_heap_node : parent_pointing_heap_node< value_type > { - typedef parent_pointing_heap_node super_t; + typedef parent_pointing_heap_node< value_type > super_t; - marked_heap_node(value_type const & v): - super_t(v), mark(false) + marked_heap_node( value_type const& v ) : + super_t( v ), + mark( false ) {} -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - marked_heap_node(Args&&... args): - super_t(std::forward(args)...), mark(false) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + marked_heap_node( Args&&... args ) : + super_t( std::forward< Args >( args )... ), + mark( false ) {} #endif - marked_heap_node * get_parent(void) + marked_heap_node* get_parent( void ) { - return static_cast(super_t::parent); + return static_cast< marked_heap_node* >( super_t::parent ); } - const marked_heap_node * get_parent(void) const + const marked_heap_node* get_parent( void ) const { - return static_cast(super_t::parent); + return static_cast< marked_heap_node* >( super_t::parent ); } bool mark; }; -template +template < typename Node > struct cmp_by_degree { - template - bool operator()(NodeBase const & left, - NodeBase const & right) + template < typename NodeBase > + bool operator()( NodeBase const& left, NodeBase const& right ) { - return static_cast(&left)->child_count() < static_cast(&right)->child_count(); + return static_cast< const Node* >( &left )->child_count() < static_cast< const Node* >( &right )->child_count(); } }; -template -Node * find_max_child(List const & list, Cmp const & cmp) +template < typename List, typename Node, typename Cmp > +Node* find_max_child( List const& list, Cmp const& cmp ) { - BOOST_HEAP_ASSERT(!list.empty()); + BOOST_HEAP_ASSERT( !list.empty() ); - const Node * ret = static_cast (&list.front()); - for (typename List::const_iterator it = list.begin(); it != list.end(); ++it) { - const Node * current = static_cast (&*it); + const Node* ret = static_cast< const Node* >( &list.front() ); + for ( typename List::const_iterator it = list.begin(); it != list.end(); ++it ) { + const Node* current = static_cast< const Node* >( &*it ); - if (cmp(ret->value, current->value)) + if ( cmp( ret->value, current->value ) ) ret = current; } - return const_cast(ret); + return const_cast< Node* >( ret ); } -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #undef BOOST_HEAP_ASSERT #endif /* BOOST_HEAP_DETAIL_HEAP_NODE_HPP */ diff --git a/include/boost/heap/detail/ilog2.hpp b/include/boost/heap/detail/ilog2.hpp index c6b65c6..63ebb64 100644 --- a/include/boost/heap/detail/ilog2.hpp +++ b/include/boost/heap/detail/ilog2.hpp @@ -11,38 +11,37 @@ #include // std::size_t -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -template +template < typename IntType > struct log2 { - IntType operator()(IntType value) + IntType operator()( IntType value ) { IntType l = 0; - while( (value >> l) > 1 ) + while ( ( value >> l ) > 1 ) ++l; return l; } }; #ifdef __GNUC__ -template<> -struct log2 +template <> +struct log2< unsigned int > { - unsigned int operator()(unsigned int value) + unsigned int operator()( unsigned int value ) { - return sizeof(unsigned int)*8 - __builtin_clz(value - 1); + return sizeof( unsigned int ) * 8 - __builtin_clz( value - 1 ); } }; -template<> -struct log2 +template <> +struct log2< unsigned long > { - unsigned long operator()(unsigned long value) + unsigned long operator()( unsigned long value ) { - return sizeof(unsigned long)*8 - __builtin_clzl(value - 1); + return sizeof( unsigned long ) * 8 - __builtin_clzl( value - 1 ); } }; @@ -51,14 +50,13 @@ struct log2 } /* namespace detail */ -template -IntType log2(IntType value) +template < typename IntType > +IntType log2( IntType value ) { - detail::log2 fn; - return fn(value); + detail::log2< IntType > fn; + return fn( value ); } -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #endif /* BOOST_HEAP_DETAIL_ILOG2_HPP */ diff --git a/include/boost/heap/detail/mutable_heap.hpp b/include/boost/heap/detail/mutable_heap.hpp index 0fc0c5c..bb098e0 100644 --- a/include/boost/heap/detail/mutable_heap.hpp +++ b/include/boost/heap/detail/mutable_heap.hpp @@ -16,12 +16,10 @@ #include #include -#include #include +#include -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { /* wrapper for a mutable heap container adaptors * @@ -29,81 +27,81 @@ namespace detail { * but instead from std::list iterators. this way, the mutability is achieved * */ -template +template < typename PriorityQueueType > class priority_queue_mutable_wrapper { public: - typedef typename PriorityQueueType::value_type value_type; - typedef typename PriorityQueueType::size_type size_type; - typedef typename PriorityQueueType::value_compare value_compare; + typedef typename PriorityQueueType::value_type value_type; + typedef typename PriorityQueueType::size_type size_type; + typedef typename PriorityQueueType::value_compare value_compare; typedef typename PriorityQueueType::allocator_type allocator_type; - typedef typename PriorityQueueType::reference reference; + typedef typename PriorityQueueType::reference reference; typedef typename PriorityQueueType::const_reference const_reference; - typedef typename PriorityQueueType::pointer pointer; - typedef typename PriorityQueueType::const_pointer const_pointer; - static const bool is_stable = PriorityQueueType::is_stable; + typedef typename PriorityQueueType::pointer pointer; + typedef typename PriorityQueueType::const_pointer const_pointer; + static const bool is_stable = PriorityQueueType::is_stable; private: - typedef std::pair node_type; + typedef std::pair< value_type, size_type > node_type; - typedef std::list::type> object_list; + typedef std::list< node_type, typename boost::allocator_rebind< allocator_type, node_type >::type > object_list; - typedef typename object_list::iterator list_iterator; + typedef typename object_list::iterator list_iterator; typedef typename object_list::const_iterator const_list_iterator; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; typedef typename PriorityQueueType::super_t::stability_counter_type stability_counter_type; - stability_counter_type get_stability_count(void) const + stability_counter_type get_stability_count( void ) const { return q_.get_stability_count(); } - void set_stability_count(stability_counter_type new_count) + void set_stability_count( stability_counter_type new_count ) { - q_.set_stability_count(new_count); + q_.set_stability_count( new_count ); } struct index_updater { - template - static void run(It & it, size_type new_index) + template < typename It > + static void run( It& it, size_type new_index ) { - q_type::get_value(it)->second = new_index; + q_type::get_value( it )->second = new_index; } }; public: struct handle_type { - value_type & operator*() const + value_type& operator*() const { return iterator->first; } - handle_type (void) + handle_type( void ) {} - handle_type(handle_type const & rhs): - iterator(rhs.iterator) + handle_type( handle_type const& rhs ) : + iterator( rhs.iterator ) {} - bool operator==(handle_type const & rhs) const + bool operator==( handle_type const& rhs ) const { return iterator == rhs.iterator; } - bool operator!=(handle_type const & rhs) const + bool operator!=( handle_type const& rhs ) const { return iterator != rhs.iterator; } private: - explicit handle_type(list_iterator const & it): - iterator(it) + explicit handle_type( list_iterator const& it ) : + iterator( it ) {} list_iterator iterator; @@ -112,93 +110,94 @@ public: }; private: - struct indirect_cmp: - public value_compare + struct indirect_cmp : public value_compare { - indirect_cmp(value_compare const & cmp = value_compare()): - value_compare(cmp) + indirect_cmp( value_compare const& cmp = value_compare() ) : + value_compare( cmp ) {} - bool operator()(const_list_iterator const & lhs, const_list_iterator const & rhs) const + bool operator()( const_list_iterator const& lhs, const_list_iterator const& rhs ) const { - return value_compare::operator()(lhs->first, rhs->first); + return value_compare::operator()( lhs->first, rhs->first ); } }; - typedef typename PriorityQueueType::template rebind::other q_type; + typedef + typename PriorityQueueType::template rebind< list_iterator, indirect_cmp, allocator_type, index_updater >::other + q_type; protected: - q_type q_; + q_type q_; object_list objects; protected: - priority_queue_mutable_wrapper(value_compare const & cmp = value_compare()): - q_(cmp) + priority_queue_mutable_wrapper( value_compare const& cmp = value_compare() ) : + q_( cmp ) {} - priority_queue_mutable_wrapper(priority_queue_mutable_wrapper const & rhs): - q_(rhs.q_), objects(rhs.objects) + priority_queue_mutable_wrapper( priority_queue_mutable_wrapper const& rhs ) : + q_( rhs.q_ ), + objects( rhs.objects ) { - for (typename object_list::iterator it = objects.begin(); it != objects.end(); ++it) - q_.push(it); + for ( typename object_list::iterator it = objects.begin(); it != objects.end(); ++it ) + q_.push( it ); } - priority_queue_mutable_wrapper & operator=(priority_queue_mutable_wrapper const & rhs) + priority_queue_mutable_wrapper& operator=( priority_queue_mutable_wrapper const& rhs ) { - q_ = rhs.q_; + q_ = rhs.q_; objects = rhs.objects; q_.clear(); - for (typename object_list::iterator it = objects.begin(); it != objects.end(); ++it) - q_.push(it); + for ( typename object_list::iterator it = objects.begin(); it != objects.end(); ++it ) + q_.push( it ); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - priority_queue_mutable_wrapper (priority_queue_mutable_wrapper && rhs): - q_(std::move(rhs.q_)) + priority_queue_mutable_wrapper( priority_queue_mutable_wrapper&& rhs ) : + q_( std::move( rhs.q_ ) ) { /// FIXME: msvc seems to invalidate iterators when moving std::list - std::swap(objects, rhs.objects); + std::swap( objects, rhs.objects ); } - priority_queue_mutable_wrapper & operator=(priority_queue_mutable_wrapper && rhs) + priority_queue_mutable_wrapper& operator=( priority_queue_mutable_wrapper&& rhs ) { - q_ = std::move(rhs.q_); + q_ = std::move( rhs.q_ ); objects.clear(); - std::swap(objects, rhs.objects); + std::swap( objects, rhs.objects ); return *this; } #endif public: - template - class iterator_base: - public boost::iterator_adaptor, - iterator_type, - value_type const, - boost::bidirectional_traversal_tag> + template < typename iterator_type > + class iterator_base : + public boost::iterator_adaptor< iterator_base< iterator_type >, + iterator_type, + value_type const, + boost::bidirectional_traversal_tag > { - typedef boost::iterator_adaptor, - iterator_type, - value_type const, - boost::bidirectional_traversal_tag> super_t; + typedef boost::iterator_adaptor< iterator_base< iterator_type >, + iterator_type, + value_type const, + boost::bidirectional_traversal_tag > + super_t; friend class boost::iterator_core_access; friend class priority_queue_mutable_wrapper; - iterator_base(void): - super_t(0) + iterator_base( void ) : + super_t( 0 ) {} - template - explicit iterator_base(T const & it): - super_t(it) + template < typename T > + explicit iterator_base( T const& it ) : + super_t( it ) {} - value_type const & dereference() const + value_type const& dereference() const { return super_t::base()->first; } @@ -209,162 +208,163 @@ public: } }; - typedef iterator_base iterator; - typedef iterator_base const_iterator; + typedef iterator_base< list_iterator > iterator; + typedef iterator_base< const_list_iterator > const_iterator; typedef typename object_list::difference_type difference_type; - class ordered_iterator: - public boost::iterator_adaptor, + class ordered_iterator : + public boost::iterator_adaptor< ordered_iterator, const_list_iterator, value_type const, boost::forward_traversal_tag >, q_type::ordered_iterator_dispatcher { - typedef boost::iterator_adaptor adaptor_type; + typedef boost::iterator_adaptor< ordered_iterator, const_list_iterator, value_type const, boost::forward_traversal_tag > + adaptor_type; - typedef const_list_iterator iterator; + typedef const_list_iterator iterator; typedef typename q_type::ordered_iterator_dispatcher ordered_iterator_dispatcher; friend class boost::iterator_core_access; public: - ordered_iterator(void): - adaptor_type(0), unvisited_nodes(indirect_cmp()), q_(NULL) + ordered_iterator( void ) : + adaptor_type( 0 ), + unvisited_nodes( indirect_cmp() ), + q_( NULL ) {} - ordered_iterator(const priority_queue_mutable_wrapper * q, indirect_cmp const & cmp): - adaptor_type(0), unvisited_nodes(cmp), q_(q) + ordered_iterator( const priority_queue_mutable_wrapper* q, indirect_cmp const& cmp ) : + adaptor_type( 0 ), + unvisited_nodes( cmp ), + q_( q ) {} - ordered_iterator(const_list_iterator it, const priority_queue_mutable_wrapper * q, indirect_cmp const & cmp): - adaptor_type(it), unvisited_nodes(cmp), q_(q) + ordered_iterator( const_list_iterator it, const priority_queue_mutable_wrapper* q, indirect_cmp const& cmp ) : + adaptor_type( it ), + unvisited_nodes( cmp ), + q_( q ) { - if (it != q->objects.end()) - discover_nodes(it); + if ( it != q->objects.end() ) + discover_nodes( it ); } - bool operator!=(ordered_iterator const & rhs) const + bool operator!=( ordered_iterator const& rhs ) const { return adaptor_type::base() != rhs.base(); } - bool operator==(ordered_iterator const & rhs) const + bool operator==( ordered_iterator const& rhs ) const { - return !operator!=(rhs); + return !operator!=( rhs ); } private: - void increment(void) + void increment( void ) { - if (unvisited_nodes.empty()) + if ( unvisited_nodes.empty() ) adaptor_type::base_reference() = q_->objects.end(); else { iterator next = unvisited_nodes.top(); unvisited_nodes.pop(); - discover_nodes(next); + discover_nodes( next ); adaptor_type::base_reference() = next; } } - value_type const & dereference() const + value_type const& dereference() const { return adaptor_type::base()->first; } - void discover_nodes(iterator current) + void discover_nodes( iterator current ) { - size_type current_index = current->second; - const q_type * q = &(q_->q_); + size_type current_index = current->second; + const q_type* q = &( q_->q_ ); - if (ordered_iterator_dispatcher::is_leaf(q, current_index)) + if ( ordered_iterator_dispatcher::is_leaf( q, current_index ) ) return; - std::pair child_range = ordered_iterator_dispatcher::get_child_nodes(q, current_index); + std::pair< size_type, size_type > child_range + = ordered_iterator_dispatcher::get_child_nodes( q, current_index ); - for (size_type i = child_range.first; i <= child_range.second; ++i) { - typename q_type::internal_type const & internal_value_at_index = ordered_iterator_dispatcher::get_internal_value(q, i); - typename q_type::value_type const & value_at_index = q_->q_.get_value(internal_value_at_index); + for ( size_type i = child_range.first; i <= child_range.second; ++i ) { + typename q_type::internal_type const& internal_value_at_index + = ordered_iterator_dispatcher::get_internal_value( q, i ); + typename q_type::value_type const& value_at_index = q_->q_.get_value( internal_value_at_index ); - unvisited_nodes.push(value_at_index); + unvisited_nodes.push( value_at_index ); } } - std::priority_queue::type>, - indirect_cmp - > unvisited_nodes; - const priority_queue_mutable_wrapper * q_; + std::priority_queue< iterator, + std::vector< iterator, typename boost::allocator_rebind< allocator_type, iterator >::type >, + indirect_cmp > + unvisited_nodes; + const priority_queue_mutable_wrapper* q_; }; - bool empty(void) const + bool empty( void ) const { return q_.empty(); } - size_type size(void) const + size_type size( void ) const { return q_.size(); } - size_type max_size(void) const + size_type max_size( void ) const { return objects.max_size(); } - void clear(void) + void clear( void ) { q_.clear(); objects.clear(); } - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return q_.get_allocator(); } - void swap(priority_queue_mutable_wrapper & rhs) + void swap( priority_queue_mutable_wrapper& rhs ) { - objects.swap(rhs.objects); - q_.swap(rhs.q_); + objects.swap( rhs.objects ); + q_.swap( rhs.q_ ); } - const_reference top(void) const + const_reference top( void ) const { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); return q_.top()->first; } - handle_type push(value_type const & v) + handle_type push( value_type const& v ) { - objects.push_front(std::make_pair(v, 0)); + objects.push_front( std::make_pair( v, 0 ) ); list_iterator ret = objects.begin(); - q_.push(ret); - return handle_type(ret); + q_.push( ret ); + return handle_type( ret ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - handle_type emplace(Args&&... args) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + handle_type emplace( Args&&... args ) { - objects.push_front(std::make_pair(std::forward(args)..., 0)); + objects.push_front( std::make_pair( std::forward< Args >( args )..., 0 ) ); list_iterator ret = objects.begin(); - q_.push(ret); - return handle_type(ret); + q_.push( ret ); + return handle_type( ret ); } #endif - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); list_iterator q_top = q_.top(); q_.pop(); - objects.erase(q_top); + objects.erase( q_top ); } /** @@ -373,15 +373,15 @@ public: * \b Complexity: Logarithmic. * * */ - void update(handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - list_iterator it = handle.iterator; - value_type const & current_value = it->first; - value_compare const & cmp = q_.value_comp(); - if (cmp(v, current_value)) - decrease(handle, v); + list_iterator it = handle.iterator; + value_type const& current_value = it->first; + value_compare const& cmp = q_.value_comp(); + if ( cmp( v, current_value ) ) + decrease( handle, v ); else - increase(handle, v); + increase( handle, v ); } /** @@ -391,25 +391,25 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void update(handle_type handle) + void update( handle_type handle ) { - list_iterator it = handle.iterator; - size_type index = it->second; - q_.update(index); + list_iterator it = handle.iterator; + size_type index = it->second; + q_.update( index ); } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \b Complexity: Logarithmic. * * \b Note: The new value is expected to be greater than the current one * */ - void increase(handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - BOOST_ASSERT(!value_compare()(v, handle.iterator->first)); + BOOST_ASSERT( !value_compare()( v, handle.iterator->first ) ); handle.iterator->first = v; - increase(handle); + increase( handle ); } /** @@ -417,27 +417,28 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * */ - void increase(handle_type handle) + void increase( handle_type handle ) { - list_iterator it = handle.iterator; - size_type index = it->second; - q_.increase(index); + list_iterator it = handle.iterator; + size_type index = it->second; + q_.increase( index ); } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \b Complexity: Logarithmic. * * \b Note: The new value is expected to be less than the current one * */ - void decrease(handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - BOOST_ASSERT(!value_compare()(handle.iterator->first, v)); + BOOST_ASSERT( !value_compare()( handle.iterator->first, v ) ); handle.iterator->first = v; - decrease(handle); + decrease( handle ); } /** @@ -445,13 +446,14 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * */ - void decrease(handle_type handle) + void decrease( handle_type handle ) { - list_iterator it = handle.iterator; - size_type index = it->second; - q_.decrease(index); + list_iterator it = handle.iterator; + size_type index = it->second; + q_.decrease( index ); } /** @@ -459,66 +461,64 @@ public: * * \b Complexity: Logarithmic. * */ - void erase(handle_type handle) + void erase( handle_type handle ) { - list_iterator it = handle.iterator; - size_type index = it->second; - q_.erase(index); - objects.erase(it); + list_iterator it = handle.iterator; + size_type index = it->second; + q_.erase( index ); + objects.erase( it ); } - const_iterator begin(void) const + const_iterator begin( void ) const { - return const_iterator(objects.begin()); + return const_iterator( objects.begin() ); } - const_iterator end(void) const + const_iterator end( void ) const { - return const_iterator(objects.end()); + return const_iterator( objects.end() ); } - iterator begin(void) + iterator begin( void ) { - return iterator(objects.begin()); + return iterator( objects.begin() ); } - iterator end(void) + iterator end( void ) { - return iterator(objects.end()); + return iterator( objects.end() ); } - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - if (!empty()) - return ordered_iterator(q_.top(), this, indirect_cmp(q_.value_comp())); + if ( !empty() ) + return ordered_iterator( q_.top(), this, indirect_cmp( q_.value_comp() ) ); else return ordered_end(); } - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(objects.end(), this, indirect_cmp(q_.value_comp())); + return ordered_iterator( objects.end(), this, indirect_cmp( q_.value_comp() ) ); } - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - return handle_type(it.get_list_iterator()); + return handle_type( it.get_list_iterator() ); } - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return q_.value_comp(); } - void reserve (size_type element_count) + void reserve( size_type element_count ) { - q_.reserve(element_count); + q_.reserve( element_count ); } }; -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #endif /* BOOST_HEAP_DETAIL_MUTABLE_HEAP_HPP */ diff --git a/include/boost/heap/detail/ordered_adaptor_iterator.hpp b/include/boost/heap/detail/ordered_adaptor_iterator.hpp index 54b8c38..b2b4b5a 100644 --- a/include/boost/heap/detail/ordered_adaptor_iterator.hpp +++ b/include/boost/heap/detail/ordered_adaptor_iterator.hpp @@ -13,13 +13,11 @@ #include #include +#include #include #include -#include -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { /* ordered iterator helper classes for container adaptors * @@ -27,120 +25,112 @@ namespace detail { * * * static size_type max_index(const ContainerType * heap); // return maximum index * * static bool is_leaf(const ContainerType * heap, size_type index); // return if index denotes a leaf - * * static std::pair get_child_nodes(const ContainerType * heap, size_type index); // get index range of child nodes - * * static internal_type const & get_internal_value(const ContainerType * heap, size_type index); // get internal value at index + * * static std::pair get_child_nodes(const ContainerType * heap, size_type index); // get index + * range of child nodes + * * static internal_type const & get_internal_value(const ContainerType * heap, size_type index); // get internal value + * at index * * static value_type const & get_value(internal_type const & arg) const; // get value_type from internal_type * * */ -template -class ordered_adaptor_iterator: - public boost::iterator_facade, - ValueType, - boost::forward_traversal_tag - >, +template < typename ValueType, typename InternalType, typename ContainerType, typename Alloc, typename ValueCompare, typename Dispatcher > +class ordered_adaptor_iterator : + public boost::iterator_facade< + ordered_adaptor_iterator< ValueType, InternalType, ContainerType, Alloc, ValueCompare, Dispatcher >, + ValueType, + boost::forward_traversal_tag >, Dispatcher { friend class boost::iterator_core_access; - struct compare_by_heap_value: - ValueCompare + struct compare_by_heap_value : ValueCompare { - const ContainerType * container; + const ContainerType* container; - compare_by_heap_value (const ContainerType * container, ValueCompare const & cmp): - ValueCompare(cmp), container(container) + compare_by_heap_value( const ContainerType* container, ValueCompare const& cmp ) : + ValueCompare( cmp ), + container( container ) {} - bool operator()(size_t lhs, size_t rhs) + bool operator()( size_t lhs, size_t rhs ) { - BOOST_ASSERT(lhs <= Dispatcher::max_index(container)); - BOOST_ASSERT(rhs <= Dispatcher::max_index(container)); - return ValueCompare::operator()(Dispatcher::get_internal_value(container, lhs), - Dispatcher::get_internal_value(container, rhs)); + BOOST_ASSERT( lhs <= Dispatcher::max_index( container ) ); + BOOST_ASSERT( rhs <= Dispatcher::max_index( container ) ); + return ValueCompare::operator()( Dispatcher::get_internal_value( container, lhs ), + Dispatcher::get_internal_value( container, rhs ) ); } }; - const ContainerType * container; - size_t current_index; // current index: special value -1 denotes `end' iterator + const ContainerType* container; + size_t current_index; // current index: special value -1 denotes `end' iterator public: - ordered_adaptor_iterator(void): - container(NULL), current_index((std::numeric_limits::max)()), - unvisited_nodes(compare_by_heap_value(NULL, ValueCompare())) + ordered_adaptor_iterator( void ) : + container( NULL ), + current_index( ( std::numeric_limits< size_t >::max )() ), + unvisited_nodes( compare_by_heap_value( NULL, ValueCompare() ) ) {} - ordered_adaptor_iterator(const ContainerType * container, ValueCompare const & cmp): - container(container), current_index(container->size()), - unvisited_nodes(compare_by_heap_value(container, ValueCompare())) + ordered_adaptor_iterator( const ContainerType* container, ValueCompare const& cmp ) : + container( container ), + current_index( container->size() ), + unvisited_nodes( compare_by_heap_value( container, ValueCompare() ) ) {} - ordered_adaptor_iterator(size_t initial_index, const ContainerType * container, ValueCompare const & cmp): - container(container), current_index(initial_index), - unvisited_nodes(compare_by_heap_value(container, cmp)) + ordered_adaptor_iterator( size_t initial_index, const ContainerType* container, ValueCompare const& cmp ) : + container( container ), + current_index( initial_index ), + unvisited_nodes( compare_by_heap_value( container, cmp ) ) { - discover_nodes(initial_index); + discover_nodes( initial_index ); } private: - bool equal (ordered_adaptor_iterator const & rhs) const + bool equal( ordered_adaptor_iterator const& rhs ) const { - if (current_index != rhs.current_index) + if ( current_index != rhs.current_index ) return false; - if (container != rhs.container) // less likely than first check + if ( container != rhs.container ) // less likely than first check return false; return true; } - void increment(void) + void increment( void ) { - if (unvisited_nodes.empty()) - current_index = Dispatcher::max_index(container) + 1; + if ( unvisited_nodes.empty() ) + current_index = Dispatcher::max_index( container ) + 1; else { current_index = unvisited_nodes.top(); unvisited_nodes.pop(); - discover_nodes(current_index); + discover_nodes( current_index ); } } - ValueType const & dereference() const + ValueType const& dereference() const { - BOOST_ASSERT(current_index <= Dispatcher::max_index(container)); - return Dispatcher::get_value(Dispatcher::get_internal_value(container, current_index)); + BOOST_ASSERT( current_index <= Dispatcher::max_index( container ) ); + return Dispatcher::get_value( Dispatcher::get_internal_value( container, current_index ) ); } - void discover_nodes(size_t index) + void discover_nodes( size_t index ) { - if (Dispatcher::is_leaf(container, index)) + if ( Dispatcher::is_leaf( container, index ) ) return; - std::pair child_range = Dispatcher::get_child_nodes(container, index); + std::pair< size_t, size_t > child_range = Dispatcher::get_child_nodes( container, index ); - for (size_t i = child_range.first; i <= child_range.second; ++i) - unvisited_nodes.push(i); + for ( size_t i = child_range.first; i <= child_range.second; ++i ) + unvisited_nodes.push( i ); } - std::priority_queue::type>, - compare_by_heap_value - > unvisited_nodes; + std::priority_queue< size_t, + std::vector< size_t, typename boost::allocator_rebind< Alloc, size_t >::type >, + compare_by_heap_value > + unvisited_nodes; }; -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #endif /* BOOST_HEAP_DETAIL_ORDERED_ADAPTOR_ITERATOR_HPP */ diff --git a/include/boost/heap/detail/stable_heap.hpp b/include/boost/heap/detail/stable_heap.hpp index bfec26d..2bc0a98 100644 --- a/include/boost/heap/detail/stable_heap.hpp +++ b/include/boost/heap/detail/stable_heap.hpp @@ -13,50 +13,45 @@ #include #include -#include -#include #include +#include #include +#include -#include #include +#include -#include #include +#include -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { -template +template < bool ConstantSize, class SizeType > struct size_holder { static const bool constant_time_size = ConstantSize; typedef SizeType size_type; - size_holder(void) BOOST_NOEXCEPT: - size_(0) + size_holder( void ) BOOST_NOEXCEPT : size_( 0 ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - size_holder(size_holder && rhs) BOOST_NOEXCEPT: - size_(rhs.size_) + size_holder( size_holder&& rhs ) BOOST_NOEXCEPT : size_( rhs.size_ ) { rhs.size_ = 0; } - size_holder(size_holder const & rhs) BOOST_NOEXCEPT: - size_(rhs.size_) + size_holder( size_holder const& rhs ) BOOST_NOEXCEPT : size_( rhs.size_ ) {} - size_holder & operator=(size_holder && rhs) BOOST_NOEXCEPT + size_holder& operator=( size_holder&& rhs ) BOOST_NOEXCEPT { - size_ = rhs.size_; + size_ = rhs.size_; rhs.size_ = 0; return *this; } - size_holder & operator=(size_holder const & rhs) BOOST_NOEXCEPT + size_holder& operator=( size_holder const& rhs ) BOOST_NOEXCEPT { size_ = rhs.size_; return *this; @@ -64,60 +59,76 @@ struct size_holder #endif SizeType get_size() const BOOST_NOEXCEPT - { return size_; } + { + return size_; + } - void set_size(SizeType size) BOOST_NOEXCEPT - { size_ = size; } + void set_size( SizeType size ) BOOST_NOEXCEPT + { + size_ = size; + } void decrement() BOOST_NOEXCEPT - { --size_; } + { + --size_; + } void increment() BOOST_NOEXCEPT - { ++size_; } + { + ++size_; + } - void add(SizeType value) BOOST_NOEXCEPT - { size_ += value; } + void add( SizeType value ) BOOST_NOEXCEPT + { + size_ += value; + } - void sub(SizeType value) BOOST_NOEXCEPT - { size_ -= value; } + void sub( SizeType value ) BOOST_NOEXCEPT + { + size_ -= value; + } - void swap(size_holder & rhs) BOOST_NOEXCEPT - { std::swap(size_, rhs.size_); } + void swap( size_holder& rhs ) BOOST_NOEXCEPT + { + std::swap( size_, rhs.size_ ); + } SizeType size_; }; -template -struct size_holder +template < class SizeType > +struct size_holder< false, SizeType > { static const bool constant_time_size = false; typedef SizeType size_type; - size_holder(void) BOOST_NOEXCEPT + size_holder( void ) BOOST_NOEXCEPT {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - size_holder(size_holder && rhs) BOOST_NOEXCEPT + size_holder( size_holder&& rhs ) BOOST_NOEXCEPT {} - size_holder(size_holder const & rhs) BOOST_NOEXCEPT + size_holder( size_holder const& rhs ) BOOST_NOEXCEPT {} - size_holder & operator=(size_holder && rhs) BOOST_NOEXCEPT + size_holder& operator=( size_holder&& rhs ) BOOST_NOEXCEPT { return *this; } - size_holder & operator=(size_holder const & rhs) BOOST_NOEXCEPT + size_holder& operator=( size_holder const& rhs ) BOOST_NOEXCEPT { return *this; } #endif size_type get_size() const BOOST_NOEXCEPT - { return 0; } + { + return 0; + } - void set_size(size_type) BOOST_NOEXCEPT + void set_size( size_type ) BOOST_NOEXCEPT {} void decrement() BOOST_NOEXCEPT @@ -126,122 +137,119 @@ struct size_holder void increment() BOOST_NOEXCEPT {} - void add(SizeType /*value*/) BOOST_NOEXCEPT + void add( SizeType /*value*/ ) BOOST_NOEXCEPT {} - void sub(SizeType /*value*/) BOOST_NOEXCEPT + void sub( SizeType /*value*/ ) BOOST_NOEXCEPT {} - void swap(size_holder & /*rhs*/) BOOST_NOEXCEPT + void swap( size_holder& /*rhs*/ ) BOOST_NOEXCEPT {} }; // note: MSVC does not implement lookup correctly, we therefore have to place the Cmp object as member inside the // struct. of course, this prevents EBO and significantly reduces the readability of this code -template -struct heap_base: +template < typename T, typename Cmp, bool constant_time_size, typename StabilityCounterType = boost::uintmax_t, bool stable = false > +struct heap_base : #ifndef BOOST_MSVC Cmp, #endif - size_holder + size_holder< constant_time_size, size_t > { - typedef StabilityCounterType stability_counter_type; - typedef T value_type; - typedef T internal_type; - typedef size_holder size_holder_type; - typedef Cmp value_compare; - typedef Cmp internal_compare; - static const bool is_stable = stable; + typedef StabilityCounterType stability_counter_type; + typedef T value_type; + typedef T internal_type; + typedef size_holder< constant_time_size, size_t > size_holder_type; + typedef Cmp value_compare; + typedef Cmp internal_compare; + static const bool is_stable = stable; #ifdef BOOST_MSVC Cmp cmp_; #endif - heap_base (Cmp const & cmp = Cmp()): + heap_base( Cmp const& cmp = Cmp() ) : #ifndef BOOST_MSVC - Cmp(cmp) + Cmp( cmp ) #else - cmp_(cmp) + cmp_( cmp ) #endif {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - heap_base(heap_base && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value): -#ifndef BOOST_MSVC - Cmp(std::move(static_cast(rhs))), -#endif - size_holder_type(std::move(static_cast(rhs))) -#ifdef BOOST_MSVC - , cmp_(std::move(rhs.cmp_)) -#endif + heap_base( heap_base&& rhs ) BOOST_NOEXCEPT_IF( boost::is_nothrow_move_constructible< Cmp >::value ) : +# ifndef BOOST_MSVC + Cmp( std::move( static_cast< Cmp& >( rhs ) ) ), +# endif + size_holder_type( std::move( static_cast< size_holder_type& >( rhs ) ) ) +# ifdef BOOST_MSVC + , + cmp_( std::move( rhs.cmp_ ) ) +# endif {} - heap_base(heap_base const & rhs): -#ifndef BOOST_MSVC - Cmp(static_cast(rhs)), -#endif - size_holder_type(static_cast(rhs)) -#ifdef BOOST_MSVC - , cmp_(rhs.value_comp()) -#endif + heap_base( heap_base const& rhs ) : +# ifndef BOOST_MSVC + Cmp( static_cast< Cmp const& >( rhs ) ), +# endif + size_holder_type( static_cast< size_holder_type const& >( rhs ) ) +# ifdef BOOST_MSVC + , + cmp_( rhs.value_comp() ) +# endif {} - heap_base & operator=(heap_base && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable::value) + heap_base& operator=( heap_base&& rhs ) BOOST_NOEXCEPT_IF( boost::is_nothrow_move_assignable< Cmp >::value ) { - value_comp_ref().operator=(std::move(rhs.value_comp_ref())); - size_holder_type::operator=(std::move(static_cast(rhs))); + value_comp_ref().operator=( std::move( rhs.value_comp_ref() ) ); + size_holder_type::operator=( std::move( static_cast< size_holder_type& >( rhs ) ) ); return *this; } - heap_base & operator=(heap_base const & rhs) + heap_base& operator=( heap_base const& rhs ) { - value_comp_ref().operator=(rhs.value_comp()); - size_holder_type::operator=(static_cast(rhs)); + value_comp_ref().operator=( rhs.value_comp() ); + size_holder_type::operator=( static_cast< size_holder_type const& >( rhs ) ); return *this; } #endif - bool operator()(internal_type const & lhs, internal_type const & rhs) const + bool operator()( internal_type const& lhs, internal_type const& rhs ) const { - return value_comp().operator()(lhs, rhs); + return value_comp().operator()( lhs, rhs ); } - internal_type make_node(T const & val) + internal_type make_node( T const& val ) { return val; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - T && make_node(T && val) + T&& make_node( T&& val ) { - return std::forward(val); + return std::forward< T >( val ); } #endif -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - internal_type make_node(Args && ... val) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + internal_type make_node( Args&&... val ) { - return internal_type(std::forward(val)...); + return internal_type( std::forward< Args >( val )... ); } #endif - static T & get_value(internal_type & val) BOOST_NOEXCEPT + static T& get_value( internal_type& val ) BOOST_NOEXCEPT { return val; } - static T const & get_value(internal_type const & val) BOOST_NOEXCEPT + static T const& get_value( internal_type const& val ) BOOST_NOEXCEPT { return val; } - Cmp const & value_comp(void) const BOOST_NOEXCEPT + Cmp const& value_comp( void ) const BOOST_NOEXCEPT { #ifndef BOOST_MSVC return *this; @@ -250,30 +258,31 @@ struct heap_base: #endif } - Cmp const & get_internal_cmp(void) const BOOST_NOEXCEPT + Cmp const& get_internal_cmp( void ) const BOOST_NOEXCEPT { return value_comp(); } - void swap(heap_base & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value && boost::is_nothrow_move_assignable::value) + void swap( heap_base& rhs ) BOOST_NOEXCEPT_IF( + boost::is_nothrow_move_constructible< Cmp >::value&& boost::is_nothrow_move_assignable< Cmp >::value ) { - std::swap(value_comp_ref(), rhs.value_comp_ref()); - size_holder::swap(rhs); + std::swap( value_comp_ref(), rhs.value_comp_ref() ); + size_holder< constant_time_size, size_t >::swap( rhs ); } - stability_counter_type get_stability_count(void) const BOOST_NOEXCEPT + stability_counter_type get_stability_count( void ) const BOOST_NOEXCEPT { return 0; } - void set_stability_count(stability_counter_type) BOOST_NOEXCEPT + void set_stability_count( stability_counter_type ) BOOST_NOEXCEPT {} - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; private: - Cmp & value_comp_ref(void) + Cmp& value_comp_ref( void ) { #ifndef BOOST_MSVC return *this; @@ -284,134 +293,134 @@ private: }; -template -struct heap_base: +template < typename T, typename Cmp, bool constant_time_size, typename StabilityCounterType > +struct heap_base< T, Cmp, constant_time_size, StabilityCounterType, true > : #ifndef BOOST_MSVC Cmp, #endif - size_holder + size_holder< constant_time_size, size_t > { typedef StabilityCounterType stability_counter_type; - typedef T value_type; + typedef T value_type; struct internal_type { -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - internal_type(stability_counter_type cnt, Args && ... args): - first(std::forward(args)...), second(cnt) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + internal_type( stability_counter_type cnt, Args&&... args ) : + first( std::forward< Args >( args )... ), + second( cnt ) {} #endif - internal_type(stability_counter_type const & cnt, T const & value): - first(value), second(cnt) + internal_type( stability_counter_type const& cnt, T const& value ) : + first( value ), + second( cnt ) {} - T first; + T first; stability_counter_type second; }; - typedef size_holder size_holder_type; - typedef Cmp value_compare; + typedef size_holder< constant_time_size, size_t > size_holder_type; + typedef Cmp value_compare; #ifdef BOOST_MSVC Cmp cmp_; #endif - heap_base (Cmp const & cmp = Cmp()): + heap_base( Cmp const& cmp = Cmp() ) : #ifndef BOOST_MSVC - Cmp(cmp), + Cmp( cmp ), #else - cmp_(cmp), + cmp_( cmp ), #endif - counter_(0) + counter_( 0 ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - heap_base(heap_base && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value): -#ifndef BOOST_MSVC - Cmp(std::move(static_cast(rhs))), -#else - cmp_(std::move(rhs.cmp_)), -#endif - size_holder_type(std::move(static_cast(rhs))), counter_(rhs.counter_) + heap_base( heap_base&& rhs ) BOOST_NOEXCEPT_IF( boost::is_nothrow_move_constructible< Cmp >::value ) : +# ifndef BOOST_MSVC + Cmp( std::move( static_cast< Cmp& >( rhs ) ) ), +# else + cmp_( std::move( rhs.cmp_ ) ), +# endif + size_holder_type( std::move( static_cast< size_holder_type& >( rhs ) ) ), + counter_( rhs.counter_ ) { rhs.counter_ = 0; } - heap_base(heap_base const & rhs): -#ifndef BOOST_MSVC - Cmp(static_cast(rhs)), -#else - cmp_(rhs.value_comp()), -#endif - size_holder_type(static_cast(rhs)), counter_(rhs.counter_) + heap_base( heap_base const& rhs ) : +# ifndef BOOST_MSVC + Cmp( static_cast< Cmp const& >( rhs ) ), +# else + cmp_( rhs.value_comp() ), +# endif + size_holder_type( static_cast< size_holder_type const& >( rhs ) ), + counter_( rhs.counter_ ) {} - heap_base & operator=(heap_base && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable::value) + heap_base& operator=( heap_base&& rhs ) BOOST_NOEXCEPT_IF( boost::is_nothrow_move_assignable< Cmp >::value ) { - value_comp_ref().operator=(std::move(rhs.value_comp_ref())); - size_holder_type::operator=(std::move(static_cast(rhs))); + value_comp_ref().operator=( std::move( rhs.value_comp_ref() ) ); + size_holder_type::operator=( std::move( static_cast< size_holder_type& >( rhs ) ) ); - counter_ = rhs.counter_; + counter_ = rhs.counter_; rhs.counter_ = 0; return *this; } - heap_base & operator=(heap_base const & rhs) + heap_base& operator=( heap_base const& rhs ) { - value_comp_ref().operator=(rhs.value_comp()); - size_holder_type::operator=(static_cast(rhs)); + value_comp_ref().operator=( rhs.value_comp() ); + size_holder_type::operator=( static_cast< size_holder_type const& >( rhs ) ); counter_ = rhs.counter_; return *this; } #endif - bool operator()(internal_type const & lhs, internal_type const & rhs) const + bool operator()( internal_type const& lhs, internal_type const& rhs ) const { - return get_internal_cmp()(lhs, rhs); + return get_internal_cmp()( lhs, rhs ); } - bool operator()(T const & lhs, T const & rhs) const + bool operator()( T const& lhs, T const& rhs ) const { - return value_comp()(lhs, rhs); + return value_comp()( lhs, rhs ); } - internal_type make_node(T const & val) + internal_type make_node( T const& val ) { stability_counter_type count = ++counter_; - if (counter_ == (std::numeric_limits::max)()) - BOOST_THROW_EXCEPTION(std::runtime_error("boost::heap counter overflow")); - return internal_type(count, val); + if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() ) + BOOST_THROW_EXCEPTION( std::runtime_error( "boost::heap counter overflow" ) ); + return internal_type( count, val ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - internal_type make_node(Args&&... args) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + internal_type make_node( Args&&... args ) { stability_counter_type count = ++counter_; - if (counter_ == (std::numeric_limits::max)()) - BOOST_THROW_EXCEPTION(std::runtime_error("boost::heap counter overflow")); - return internal_type (count, std::forward(args)...); + if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() ) + BOOST_THROW_EXCEPTION( std::runtime_error( "boost::heap counter overflow" ) ); + return internal_type( count, std::forward< Args >( args )... ); } #endif - static T & get_value(internal_type & val) BOOST_NOEXCEPT + static T& get_value( internal_type& val ) BOOST_NOEXCEPT { return val.first; } - static T const & get_value(internal_type const & val) BOOST_NOEXCEPT + static T const& get_value( internal_type const& val ) BOOST_NOEXCEPT { return val.first; } - Cmp const & value_comp(void) const BOOST_NOEXCEPT + Cmp const& value_comp( void ) const BOOST_NOEXCEPT { #ifndef BOOST_MSVC return *this; @@ -420,56 +429,56 @@ struct heap_base: #endif } - struct internal_compare: - Cmp + struct internal_compare : Cmp { - internal_compare(Cmp const & cmp = Cmp()): - Cmp(cmp) + internal_compare( Cmp const& cmp = Cmp() ) : + Cmp( cmp ) {} - bool operator()(internal_type const & lhs, internal_type const & rhs) const + bool operator()( internal_type const& lhs, internal_type const& rhs ) const { - if (Cmp::operator()(lhs.first, rhs.first)) + if ( Cmp::operator()( lhs.first, rhs.first ) ) return true; - if (Cmp::operator()(rhs.first, lhs.first)) + if ( Cmp::operator()( rhs.first, lhs.first ) ) return false; return lhs.second > rhs.second; } }; - internal_compare get_internal_cmp(void) const + internal_compare get_internal_cmp( void ) const { - return internal_compare(value_comp()); + return internal_compare( value_comp() ); } - void swap(heap_base & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value && boost::is_nothrow_move_assignable::value) + void swap( heap_base& rhs ) BOOST_NOEXCEPT_IF( + boost::is_nothrow_move_constructible< Cmp >::value&& boost::is_nothrow_move_assignable< Cmp >::value ) { #ifndef BOOST_MSVC - std::swap(static_cast(*this), static_cast(rhs)); + std::swap( static_cast< Cmp& >( *this ), static_cast< Cmp& >( rhs ) ); #else - std::swap(cmp_, rhs.cmp_); + std::swap( cmp_, rhs.cmp_ ); #endif - std::swap(counter_, rhs.counter_); - size_holder::swap(rhs); + std::swap( counter_, rhs.counter_ ); + size_holder< constant_time_size, size_t >::swap( rhs ); } - stability_counter_type get_stability_count(void) const + stability_counter_type get_stability_count( void ) const { return counter_; } - void set_stability_count(stability_counter_type new_count) + void set_stability_count( stability_counter_type new_count ) { counter_ = new_count; } - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; private: - Cmp & value_comp_ref(void) BOOST_NOEXCEPT + Cmp& value_comp_ref( void ) BOOST_NOEXCEPT { #ifndef BOOST_MSVC return *this; @@ -481,27 +490,24 @@ private: stability_counter_type counter_; }; -template +template < typename node_pointer, typename extractor, typename reference > struct node_handle { - explicit node_handle(node_pointer n = 0): - node_(n) + explicit node_handle( node_pointer n = 0 ) : + node_( n ) {} reference operator*() const { - return extractor::get_value(node_->value); + return extractor::get_value( node_->value ); } - bool operator==(node_handle const & rhs) const + bool operator==( node_handle const& rhs ) const { return node_ == rhs.node_; } - bool operator!=(node_handle const & rhs) const + bool operator!=( node_handle const& rhs ) const { return node_ != rhs.node_; } @@ -509,77 +515,69 @@ struct node_handle node_pointer node_; }; -template +template < typename value_type, typename internal_type, typename extractor > struct value_extractor { - value_type const & operator()(internal_type const & data) const + value_type const& operator()( internal_type const& data ) const { - return extractor::get_value(data); + return extractor::get_value( data ); } }; -template -class stable_heap_iterator: - public boost::iterator_adaptor, - ContainerIterator, - T const, - boost::random_access_traversal_tag> -{ - typedef boost::iterator_adaptor +class stable_heap_iterator : + public boost::iterator_adaptor< stable_heap_iterator< T, ContainerIterator, Extractor >, ContainerIterator, T const, - boost::random_access_traversal_tag> super_t; + boost::random_access_traversal_tag > +{ + typedef boost::iterator_adaptor< stable_heap_iterator, ContainerIterator, T const, boost::random_access_traversal_tag > + super_t; public: - stable_heap_iterator(void): - super_t(0) + stable_heap_iterator( void ) : + super_t( 0 ) {} - explicit stable_heap_iterator(ContainerIterator const & it): - super_t(it) + explicit stable_heap_iterator( ContainerIterator const& it ) : + super_t( it ) {} private: friend class boost::iterator_core_access; - T const & dereference() const + T const& dereference() const { - return Extractor::get_value(*super_t::base()); + return Extractor::get_value( *super_t::base() ); } }; -template +template < typename T, typename Parspec, bool constant_time_size > struct make_heap_base { - typedef typename parameter::binding >::type compare_argument; - typedef typename parameter::binding >::type allocator_argument; - typedef typename parameter::binding::type stability_counter_type; + typedef typename parameter::binding< Parspec, tag::compare, std::less< T > >::type compare_argument; + typedef typename parameter::binding< Parspec, tag::allocator, std::allocator< T > >::type allocator_argument; + typedef + typename parameter::binding< Parspec, tag::stability_counter_type, boost::uintmax_t >::type stability_counter_type; - static const bool is_stable = extract_stable::value; + static const bool is_stable = extract_stable< Parspec >::value; - typedef heap_base type; + typedef heap_base< T, compare_argument, constant_time_size, stability_counter_type, is_stable > type; }; -template +template < typename Alloc > struct extract_allocator_types { - typedef typename boost::allocator_size_type::type size_type; - typedef typename boost::allocator_difference_type::type difference_type; - typedef typename Alloc::value_type& reference; - typedef typename Alloc::value_type const& const_reference; - typedef typename boost::allocator_pointer::type pointer; - typedef typename boost::allocator_const_pointer::type const_pointer; + typedef typename boost::allocator_size_type< Alloc >::type size_type; + typedef typename boost::allocator_difference_type< Alloc >::type difference_type; + typedef typename Alloc::value_type& reference; + typedef typename Alloc::value_type const& const_reference; + typedef typename boost::allocator_pointer< Alloc >::type pointer; + typedef typename boost::allocator_const_pointer< Alloc >::type const_pointer; }; -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #endif /* BOOST_HEAP_DETAIL_STABLE_HEAP_HPP */ diff --git a/include/boost/heap/detail/tree_iterator.hpp b/include/boost/heap/detail/tree_iterator.hpp index 459b621..9c028cc 100644 --- a/include/boost/heap/detail/tree_iterator.hpp +++ b/include/boost/heap/detail/tree_iterator.hpp @@ -17,126 +17,118 @@ #include #include -namespace boost { -namespace heap { -namespace detail { +namespace boost { namespace heap { namespace detail { -template +template < typename type > struct identity { - type& operator()(type& x) const BOOST_NOEXCEPT - { return x; } + type& operator()( type& x ) const BOOST_NOEXCEPT + { + return x; + } - const type& operator()(const type& x) const BOOST_NOEXCEPT - { return x; } + const type& operator()( const type& x ) const BOOST_NOEXCEPT + { + return x; + } }; -template +template < typename Node > struct dereferencer { - template - Node * operator()(Iterator const & it) + template < typename Iterator > + Node* operator()( Iterator const& it ) { - return static_cast(*it); + return static_cast< Node* >( *it ); } }; -template +template < typename Node > struct pointer_to_reference { - template - const Node * operator()(Iterator const & it) + template < typename Iterator > + const Node* operator()( Iterator const& it ) { - return static_cast(&*it); + return static_cast< const Node* >( &*it ); } }; -template +template < typename HandleType, typename Alloc, typename ValueCompare > struct unordered_tree_iterator_storage { - unordered_tree_iterator_storage(ValueCompare const & cmp) + unordered_tree_iterator_storage( ValueCompare const& cmp ) {} - void push(HandleType h) + void push( HandleType h ) { - data_.push_back(h); + data_.push_back( h ); } - HandleType const & top(void) + HandleType const& top( void ) { return data_.back(); } - void pop(void) + void pop( void ) { data_.pop_back(); } - bool empty(void) const + bool empty( void ) const { return data_.empty(); } - std::vector::type> data_; + std::vector< HandleType, typename boost::allocator_rebind< Alloc, HandleType >::type > data_; }; -template -struct ordered_tree_iterator_storage: - ValueExtractor +template < typename ValueType, typename HandleType, typename Alloc, typename ValueCompare, typename ValueExtractor > +struct ordered_tree_iterator_storage : ValueExtractor { - struct compare_values_by_handle: - ValueExtractor, - ValueCompare + struct compare_values_by_handle : ValueExtractor, ValueCompare { - compare_values_by_handle(ValueCompare const & cmp): - ValueCompare(cmp) + compare_values_by_handle( ValueCompare const& cmp ) : + ValueCompare( cmp ) {} - bool operator()(HandleType const & lhs, HandleType const & rhs) const + bool operator()( HandleType const& lhs, HandleType const& rhs ) const { - ValueType const & lhs_value = ValueExtractor::operator()(lhs->value); - ValueType const & rhs_value = ValueExtractor::operator()(rhs->value); - return ValueCompare::operator()(lhs_value, rhs_value); + ValueType const& lhs_value = ValueExtractor::operator()( lhs->value ); + ValueType const& rhs_value = ValueExtractor::operator()( rhs->value ); + return ValueCompare::operator()( lhs_value, rhs_value ); } }; - ordered_tree_iterator_storage(ValueCompare const & cmp): - data_(compare_values_by_handle(cmp)) + ordered_tree_iterator_storage( ValueCompare const& cmp ) : + data_( compare_values_by_handle( cmp ) ) {} - void push(HandleType h) + void push( HandleType h ) { - data_.push(h); + data_.push( h ); } - void pop(void) + void pop( void ) { data_.pop(); } - HandleType const & top(void) + HandleType const& top( void ) { return data_.top(); } - bool empty(void) const BOOST_NOEXCEPT + bool empty( void ) const BOOST_NOEXCEPT { return data_.empty(); } - std::priority_queue::type>, - compare_values_by_handle> data_; + std::priority_queue< HandleType, + std::vector< HandleType, typename boost::allocator_rebind< Alloc, HandleType >::type >, + compare_values_by_handle > + data_; }; @@ -147,205 +139,184 @@ struct ordered_tree_iterator_storage: * ValueExtractor can convert Node->value to ValueType * * */ -template , - typename ValueExtractor = identity, - typename PointerExtractor = dereferencer, - bool check_null_pointer = false, - bool ordered_iterator = false, - typename ValueCompare = std::less - > -class tree_iterator: - public boost::iterator_adaptor, - const Node *, - ValueType, - boost::forward_traversal_tag - >, +template < typename Node, + typename ValueType, + typename Alloc = std::allocator< Node >, + typename ValueExtractor = identity< typename Node::value_type >, + typename PointerExtractor = dereferencer< Node >, + bool check_null_pointer = false, + bool ordered_iterator = false, + typename ValueCompare = std::less< ValueType > > +class tree_iterator : + public boost::iterator_adaptor< + tree_iterator< Node, ValueType, Alloc, ValueExtractor, PointerExtractor, check_null_pointer, ordered_iterator, ValueCompare >, + const Node*, + ValueType, + boost::forward_traversal_tag >, ValueExtractor, PointerExtractor { - typedef boost::iterator_adaptor, - const Node *, - ValueType, - boost::forward_traversal_tag - > adaptor_type; + typedef boost::iterator_adaptor< + tree_iterator< Node, ValueType, Alloc, ValueExtractor, PointerExtractor, check_null_pointer, ordered_iterator, ValueCompare >, + const Node*, + ValueType, + boost::forward_traversal_tag > + adaptor_type; friend class boost::iterator_core_access; - typedef typename boost::conditional< ordered_iterator, - ordered_tree_iterator_storage, - unordered_tree_iterator_storage - >::type - unvisited_node_container; + typedef typename boost::conditional< + ordered_iterator, + ordered_tree_iterator_storage< ValueType, const Node*, Alloc, ValueCompare, ValueExtractor >, + unordered_tree_iterator_storage< const Node*, Alloc, ValueCompare > >::type unvisited_node_container; public: - tree_iterator(void): - adaptor_type(0), unvisited_nodes(ValueCompare()) + tree_iterator( void ) : + adaptor_type( 0 ), + unvisited_nodes( ValueCompare() ) {} - tree_iterator(ValueCompare const & cmp): - adaptor_type(0), unvisited_nodes(cmp) + tree_iterator( ValueCompare const& cmp ) : + adaptor_type( 0 ), + unvisited_nodes( cmp ) {} - tree_iterator(const Node * it, ValueCompare const & cmp): - adaptor_type(it), unvisited_nodes(cmp) + tree_iterator( const Node* it, ValueCompare const& cmp ) : + adaptor_type( it ), + unvisited_nodes( cmp ) { - if (it) - discover_nodes(it); + if ( it ) + discover_nodes( it ); } /* fills the iterator from a list of possible top nodes */ - template - tree_iterator(NodePointerIterator begin, NodePointerIterator end, const Node * top_node, ValueCompare const & cmp): - adaptor_type(0), unvisited_nodes(cmp) + template < typename NodePointerIterator > + tree_iterator( NodePointerIterator begin, NodePointerIterator end, const Node* top_node, ValueCompare const& cmp ) : + adaptor_type( 0 ), + unvisited_nodes( cmp ) { - BOOST_STATIC_ASSERT(ordered_iterator); - if (begin == end) + BOOST_STATIC_ASSERT( ordered_iterator ); + if ( begin == end ) return; adaptor_type::base_reference() = top_node; - discover_nodes(top_node); + discover_nodes( top_node ); - for (NodePointerIterator it = begin; it != end; ++it) { - const Node * current_node = static_cast(&*it); - if (current_node != top_node) - unvisited_nodes.push(current_node); + for ( NodePointerIterator it = begin; it != end; ++it ) { + const Node* current_node = static_cast< const Node* >( &*it ); + if ( current_node != top_node ) + unvisited_nodes.push( current_node ); } } - bool operator!=(tree_iterator const & rhs) const + bool operator!=( tree_iterator const& rhs ) const { return adaptor_type::base() != rhs.base(); } - bool operator==(tree_iterator const & rhs) const + bool operator==( tree_iterator const& rhs ) const { - return !operator!=(rhs); + return !operator!=( rhs ); } - const Node * get_node() const + const Node* get_node() const { return adaptor_type::base_reference(); } private: - void increment(void) + void increment( void ) { - if (unvisited_nodes.empty()) + if ( unvisited_nodes.empty() ) adaptor_type::base_reference() = 0; else { - const Node * next = unvisited_nodes.top(); + const Node* next = unvisited_nodes.top(); unvisited_nodes.pop(); - discover_nodes(next); + discover_nodes( next ); adaptor_type::base_reference() = next; } } - ValueType const & dereference() const + ValueType const& dereference() const { - return ValueExtractor::operator()(adaptor_type::base_reference()->value); + return ValueExtractor::operator()( adaptor_type::base_reference()->value ); } - void discover_nodes(const Node * n) + void discover_nodes( const Node* n ) { - for (typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it) { - const Node * n = PointerExtractor::operator()(it); - if (check_null_pointer && n == NULL) + for ( typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it ) { + const Node* n = PointerExtractor::operator()( it ); + if ( check_null_pointer && n == NULL ) continue; - unvisited_nodes.push(n); + unvisited_nodes.push( n ); } } unvisited_node_container unvisited_nodes; }; -template +template < typename Node, typename NodeList > struct list_iterator_converter { - typename NodeList::const_iterator operator()(const Node * node) + typename NodeList::const_iterator operator()( const Node* node ) { - return NodeList::s_iterator_to(*node); + return NodeList::s_iterator_to( *node ); } - Node * operator()(typename NodeList::const_iterator it) + Node* operator()( typename NodeList::const_iterator it ) { - return const_cast(static_cast(&*it)); + return const_cast< Node* >( static_cast< const Node* >( &*it ) ); } }; -template , - typename IteratorCoverter = identity - > -class recursive_tree_iterator: - public boost::iterator_adaptor, +template < typename Node, + typename NodeIterator, + typename ValueType, + typename ValueExtractor = identity< typename Node::value_type >, + typename IteratorCoverter = identity< NodeIterator > > +class recursive_tree_iterator : + public boost::iterator_adaptor< recursive_tree_iterator< Node, NodeIterator, ValueType, ValueExtractor, IteratorCoverter >, NodeIterator, ValueType const, - boost::bidirectional_traversal_tag>, - ValueExtractor, IteratorCoverter + boost::bidirectional_traversal_tag >, + ValueExtractor, + IteratorCoverter { - typedef boost::iterator_adaptor, - NodeIterator, - ValueType const, - boost::bidirectional_traversal_tag> adaptor_type; + typedef boost::iterator_adaptor< + recursive_tree_iterator< Node, NodeIterator, ValueType, ValueExtractor, IteratorCoverter >, + NodeIterator, + ValueType const, + boost::bidirectional_traversal_tag > + adaptor_type; friend class boost::iterator_core_access; public: - recursive_tree_iterator(void): - adaptor_type(0) + recursive_tree_iterator( void ) : + adaptor_type( 0 ) {} - explicit recursive_tree_iterator(NodeIterator const & it): - adaptor_type(it) + explicit recursive_tree_iterator( NodeIterator const& it ) : + adaptor_type( it ) {} - void increment(void) + void increment( void ) { NodeIterator next = adaptor_type::base_reference(); - const Node * n = get_node(next); - if (n->children.empty()) { - const Node * parent = get_node(next)->get_parent(); + const Node* n = get_node( next ); + if ( n->children.empty() ) { + const Node* parent = get_node( next )->get_parent(); ++next; - while (true) { - if (parent == NULL || next != parent->children.end()) + while ( true ) { + if ( parent == NULL || next != parent->children.end() ) break; - next = IteratorCoverter::operator()(parent); - parent = get_node(next)->get_parent(); + next = IteratorCoverter::operator()( parent ); + parent = get_node( next )->get_parent(); ++next; } } else @@ -355,25 +326,23 @@ public: return; } - ValueType const & dereference() const + ValueType const& dereference() const { - return ValueExtractor::operator()(get_node(adaptor_type::base_reference())->value); + return ValueExtractor::operator()( get_node( adaptor_type::base_reference() )->value ); } - static const Node * get_node(NodeIterator const & it) + static const Node* get_node( NodeIterator const& it ) { - return static_cast(&*it); + return static_cast< const Node* >( &*it ); } - const Node * get_node() const + const Node* get_node() const { - return get_node(adaptor_type::base_reference()); + return get_node( adaptor_type::base_reference() ); } }; -} /* namespace detail */ -} /* namespace heap */ -} /* namespace boost */ +}}} // namespace boost::heap::detail #endif /* BOOST_HEAP_DETAIL_TREE_ITERATOR_HPP */ diff --git a/include/boost/heap/fibonacci_heap.hpp b/include/boost/heap/fibonacci_heap.hpp index 59e8795..60564c4 100644 --- a/include/boost/heap/fibonacci_heap.hpp +++ b/include/boost/heap/fibonacci_heap.hpp @@ -23,82 +23,76 @@ #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif #ifndef BOOST_DOXYGEN_INVOKED -#ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT -#else -#define BOOST_HEAP_ASSERT(expression) -#endif +# ifdef BOOST_HEAP_SANITYCHECKS +# define BOOST_HEAP_ASSERT BOOST_ASSERT +# else +# define BOOST_HEAP_ASSERT( expression ) +# endif #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > fibonacci_heap_signature; +typedef parameter::parameters< boost::parameter::optional< tag::allocator >, + boost::parameter::optional< tag::compare >, + boost::parameter::optional< tag::stable >, + boost::parameter::optional< tag::constant_time_size >, + boost::parameter::optional< tag::stability_counter_type > > + fibonacci_heap_signature; -template +template < typename T, typename Parspec > struct make_fibonacci_heap_base { - static const bool constant_time_size = parameter::binding::type::value; + static const bool constant_time_size + = parameter::binding< Parspec, tag::constant_time_size, boost::true_type >::type::value; - typedef typename detail::make_heap_base::type base_type; - typedef typename detail::make_heap_base::allocator_argument allocator_argument; - typedef typename detail::make_heap_base::compare_argument compare_argument; - typedef marked_heap_node node_type; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::type base_type; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::allocator_argument allocator_argument; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::compare_argument compare_argument; + typedef marked_heap_node< typename base_type::internal_type > node_type; - typedef typename boost::allocator_rebind::type allocator_type; + typedef typename boost::allocator_rebind< allocator_argument, node_type >::type allocator_type; - struct type: - base_type, - allocator_type + struct type : base_type, allocator_type { - type(compare_argument const & arg): - base_type(arg) + type( compare_argument const& arg ) : + base_type( arg ) {} - type(type const & rhs): - base_type(static_cast(rhs)), - allocator_type(static_cast(rhs)) + type( type const& rhs ) : + base_type( static_cast< base_type const& >( rhs ) ), + allocator_type( static_cast< allocator_type const& >( rhs ) ) {} - type & operator=(type const & rhs) + type& operator=( type const& rhs ) { - base_type::operator=(static_cast(rhs)); - allocator_type::operator=(static_cast(rhs)); + base_type::operator=( static_cast< base_type const& >( rhs ) ); + allocator_type::operator=( static_cast< allocator_type const& >( rhs ) ); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - type(type && rhs): - base_type(std::move(static_cast(rhs))), - allocator_type(std::move(static_cast(rhs))) + type( type&& rhs ) : + base_type( std::move( static_cast< base_type& >( rhs ) ) ), + allocator_type( std::move( static_cast< allocator_type& >( rhs ) ) ) {} - type & operator=(type && rhs) + type& operator=( type&& rhs ) { - base_type::operator=(std::move(static_cast(rhs))); - allocator_type::operator=(std::move(static_cast(rhs))); + base_type::operator=( std::move( static_cast< base_type& >( rhs ) ) ); + allocator_type::operator=( std::move( static_cast< allocator_type& >( rhs ) ) ); return *this; } #endif }; }; -} - +} // namespace detail /** @@ -117,224 +111,224 @@ struct make_fibonacci_heap_base * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +template < typename T, + class A0 = boost::parameter::void_, + class A1 = boost::parameter::void_, + class A2 = boost::parameter::void_, + class A3 = boost::parameter::void_, + class A4 = boost::parameter::void_ > #endif -class fibonacci_heap: - private detail::make_fibonacci_heap_base::type - >::type +class fibonacci_heap : + private detail:: + make_fibonacci_heap_base< T, typename detail::fibonacci_heap_signature::bind< A0, A1, A2, A3, A4 >::type >::type { - typedef typename detail::fibonacci_heap_signature::bind::type bound_args; - typedef detail::make_fibonacci_heap_base base_maker; - typedef typename base_maker::type super_t; + typedef typename detail::fibonacci_heap_signature::bind< A0, A1, A2, A3, A4 >::type bound_args; + typedef detail::make_fibonacci_heap_base< T, bound_args > base_maker; + typedef typename base_maker::type super_t; - typedef typename super_t::size_holder_type size_holder; - typedef typename super_t::internal_type internal_type; + typedef typename super_t::size_holder_type size_holder; + typedef typename super_t::internal_type internal_type; typedef typename base_maker::allocator_argument allocator_argument; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; private: #ifndef BOOST_DOXYGEN_INVOKED - struct implementation_defined: - detail::extract_allocator_types + struct implementation_defined : detail::extract_allocator_types< typename base_maker::allocator_argument > { typedef T value_type; - typedef typename detail::extract_allocator_types::size_type size_type; - typedef typename detail::extract_allocator_types::reference reference; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::size_type size_type; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::reference reference; typedef typename base_maker::compare_argument value_compare; - typedef typename base_maker::allocator_type allocator_type; + typedef typename base_maker::allocator_type allocator_type; - typedef typename boost::allocator_pointer::type node_pointer; - typedef typename boost::allocator_const_pointer::type const_node_pointer; + typedef typename boost::allocator_pointer< allocator_type >::type node_pointer; + typedef typename boost::allocator_const_pointer< allocator_type >::type const_node_pointer; - typedef detail::heap_node_list node_list_type; - typedef typename node_list_type::iterator node_list_iterator; + typedef detail::heap_node_list node_list_type; + typedef typename node_list_type::iterator node_list_iterator; typedef typename node_list_type::const_iterator node_list_const_iterator; typedef typename base_maker::node_type node; - typedef detail::value_extractor value_extractor; - typedef typename super_t::internal_compare internal_compare; - typedef detail::node_handle handle_type; + typedef detail::value_extractor< value_type, internal_type, super_t > value_extractor; + typedef typename super_t::internal_compare internal_compare; + typedef detail::node_handle< node_pointer, super_t, reference > handle_type; - typedef detail::recursive_tree_iterator - > iterator; + typedef detail::recursive_tree_iterator< node, + node_list_const_iterator, + const value_type, + value_extractor, + detail::list_iterator_converter< node, node_list_type > > + iterator; typedef iterator const_iterator; - typedef detail::tree_iterator, - true, - true, - value_compare - > ordered_iterator; + typedef detail::tree_iterator< node, + const value_type, + allocator_type, + value_extractor, + detail::list_iterator_converter< node, node_list_type >, + true, + true, + value_compare > + ordered_iterator; }; - typedef typename implementation_defined::node node; - typedef typename implementation_defined::node_pointer node_pointer; - typedef typename implementation_defined::node_list_type node_list_type; - typedef typename implementation_defined::node_list_iterator node_list_iterator; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_pointer node_pointer; + typedef typename implementation_defined::node_list_type node_list_type; + typedef typename implementation_defined::node_list_iterator node_list_iterator; typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator; - typedef typename implementation_defined::internal_compare internal_compare; + typedef typename implementation_defined::internal_compare internal_compare; #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::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; /// \copydoc boost::heap::priority_queue::iterator - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; typedef typename implementation_defined::ordered_iterator ordered_iterator; typedef typename implementation_defined::handle_type handle_type; - static const bool constant_time_size = base_maker::constant_time_size; + static const bool constant_time_size = base_maker::constant_time_size; static const bool has_ordered_iterators = true; - static const bool is_mergable = true; - static const bool is_stable = detail::extract_stable::value; - static const bool has_reserve = false; + static const bool is_mergable = true; + static const bool is_stable = detail::extract_stable< bound_args >::value; + static const bool has_reserve = false; /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) - explicit fibonacci_heap(value_compare const & cmp = value_compare()): - super_t(cmp), top_element(0) + explicit fibonacci_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ), + top_element( 0 ) {} /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) - fibonacci_heap(fibonacci_heap const & rhs): - super_t(rhs), top_element(0) + fibonacci_heap( fibonacci_heap const& rhs ) : + super_t( rhs ), + top_element( 0 ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - clone_forest(rhs); - size_holder::set_size(rhs.size()); + clone_forest( rhs ); + size_holder::set_size( rhs.size() ); } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) - fibonacci_heap(fibonacci_heap && rhs): - super_t(std::move(rhs)), top_element(rhs.top_element) + fibonacci_heap( fibonacci_heap&& rhs ) : + super_t( std::move( rhs ) ), + top_element( rhs.top_element ) { - roots.splice(roots.begin(), rhs.roots); + roots.splice( roots.begin(), rhs.roots ); rhs.top_element = NULL; } /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) - fibonacci_heap & operator=(fibonacci_heap && rhs) + fibonacci_heap& operator=( fibonacci_heap&& rhs ) { clear(); - super_t::operator=(std::move(rhs)); - roots.splice(roots.begin(), rhs.roots); - top_element = rhs.top_element; + super_t::operator=( std::move( rhs ) ); + roots.splice( roots.begin(), rhs.roots ); + top_element = rhs.top_element; rhs.top_element = NULL; return *this; } #endif /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) - fibonacci_heap & operator=(fibonacci_heap const & rhs) + fibonacci_heap& operator=( fibonacci_heap const& rhs ) { clear(); - size_holder::set_size(rhs.size()); - static_cast(*this) = rhs; + size_holder::set_size( rhs.size() ); + static_cast< super_t& >( *this ) = rhs; - if (rhs.empty()) + if ( rhs.empty() ) top_element = NULL; else - clone_forest(rhs); + clone_forest( rhs ); return *this; } - ~fibonacci_heap(void) + ~fibonacci_heap( void ) { clear(); } /// \copydoc boost::heap::priority_queue::empty - bool empty(void) const + bool empty( void ) const { - if (constant_time_size) + if ( constant_time_size ) return size() == 0; else return roots.empty(); } /// \copydoc boost::heap::priority_queue::size - size_type size(void) const + size_type size( void ) const { - if (constant_time_size) + if ( constant_time_size ) return size_holder::get_size(); - if (empty()) + if ( empty() ) return 0; else - return detail::count_list_nodes(roots); + return detail::count_list_nodes< node, node_list_type >( roots ); } /// \copydoc boost::heap::priority_queue::max_size - size_type max_size(void) const + size_type max_size( void ) const { const allocator_type& alloc = *this; - return boost::allocator_max_size(alloc); + return boost::allocator_max_size( alloc ); } /// \copydoc boost::heap::priority_queue::clear - void clear(void) + void clear( void ) { - typedef detail::node_disposer disposer; - roots.clear_and_dispose(disposer(*this)); + typedef detail::node_disposer< node, typename node_list_type::value_type, allocator_type > disposer; + roots.clear_and_dispose( disposer( *this ) ); - size_holder::set_size(0); + size_holder::set_size( 0 ); top_element = NULL; } /// \copydoc boost::heap::priority_queue::get_allocator - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return *this; } /// \copydoc boost::heap::priority_queue::swap - void swap(fibonacci_heap & rhs) + void swap( fibonacci_heap& rhs ) { - super_t::swap(rhs); - std::swap(top_element, rhs.top_element); - roots.swap(rhs.roots); + super_t::swap( rhs ); + std::swap( top_element, rhs.top_element ); + roots.swap( rhs.roots ); } /// \copydoc boost::heap::priority_queue::top - value_type const & top(void) const + value_type const& top( void ) const { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); - return super_t::get_value(top_element->value); + return super_t::get_value( top_element->value ); } /** @@ -345,42 +339,43 @@ public: * \b Note: Does not invalidate iterators. * * */ - handle_type push(value_type const & v) + handle_type push( value_type const& v ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(v)); - roots.push_front(*n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( v ) ); + roots.push_front( *n ); - if (!top_element || super_t::operator()(top_element->value, n->value)) + if ( !top_element || super_t::operator()( top_element->value, n->value ) ) top_element = n; - return handle_type(n); + return handle_type( n ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /** - * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element. + * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns + * handle to element. * * \b Complexity: Constant. * * \b Note: Does not invalidate iterators. * * */ - template - handle_type emplace(Args&&... args) + template < class... Args > + handle_type emplace( Args&&... args ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(std::forward(args)...)); - roots.push_front(*n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( std::forward< Args >( args )... ) ); + roots.push_front( *n ); - if (!top_element || super_t::operator()(top_element->value, n->value)) + if ( !top_element || super_t::operator()( top_element->value, n->value ) ) top_element = n; - return handle_type(n); + return handle_type( n ); } #endif @@ -390,14 +385,14 @@ public: * \b Complexity: Logarithmic (amortized). Linear (worst case). * * */ - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); node_pointer element = top_element; - roots.erase(node_list_type::s_iterator_to(*element)); + roots.erase( node_list_type::s_iterator_to( *element ) ); - finish_erase_or_pop(element); + finish_erase_or_pop( element ); } /** @@ -406,12 +401,12 @@ public: * \b Complexity: Logarithmic if current value < v, Constant otherwise. * * */ - void update (handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - if (super_t::operator()(super_t::get_value(handle.node_->value), v)) - increase(handle, v); + if ( super_t::operator()( super_t::get_value( handle.node_->value ), v ) ) + increase( handle, v ); else - decrease(handle, v); + decrease( handle, v ); } /** \copydoc boost::heap::fibonacci_heap::update(handle_type, const_reference) @@ -419,10 +414,10 @@ public: * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates * the iterator to the object referred to by the handle. * */ - void update_lazy(handle_type handle, const_reference v) + void update_lazy( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - update_lazy(handle); + handle.node_->value = super_t::make_node( v ); + update_lazy( handle ); } /** @@ -432,9 +427,9 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void update (handle_type handle) + void update( handle_type handle ) { - update_lazy(handle); + update_lazy( handle ); consolidate(); } @@ -443,33 +438,33 @@ public: * \b Rationale: The lazy update function is a modification of the traditional update, that just invalidates * the iterator to the object referred to by the handle. * */ - void update_lazy (handle_type handle) + void update_lazy( handle_type handle ) { - node_pointer n = handle.node_; + node_pointer n = handle.node_; node_pointer parent = n->get_parent(); - if (parent) { + if ( parent ) { n->parent = NULL; - roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n)); + roots.splice( roots.begin(), parent->children, node_list_type::s_iterator_to( *n ) ); } - add_children_to_root(n); + add_children_to_root( n ); - if (super_t::operator()(top_element->value, n->value)) + if ( super_t::operator()( top_element->value, n->value ) ) top_element = n; } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \b Complexity: Constant. * * \b Note: The new value is expected to be greater than the current one * */ - void increase (handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - increase(handle); + handle.node_->value = super_t::make_node( v ); + increase( handle ); } /** @@ -479,19 +474,19 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void increase (handle_type handle) + void increase( handle_type handle ) { node_pointer n = handle.node_; - if (n->parent) { - if (super_t::operator()(n->get_parent()->value, n->value)) { + if ( n->parent ) { + if ( super_t::operator()( n->get_parent()->value, n->value ) ) { node_pointer parent = n->get_parent(); - cut(n); - cascading_cut(parent); + cut( n ); + cascading_cut( parent ); } } - if (super_t::operator()(top_element->value, n->value)) { + if ( super_t::operator()( top_element->value, n->value ) ) { top_element = n; return; } @@ -504,10 +499,10 @@ public: * * \b Note: The new value is expected to be less than the current one * */ - void decrease (handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - decrease(handle); + handle.node_->value = super_t::make_node( v ); + decrease( handle ); } /** @@ -515,11 +510,12 @@ public: * * \b Complexity: Logarithmic. * - * \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! + * \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! * */ - void decrease (handle_type handle) + void decrease( handle_type handle ) { - update(handle); + update( handle ); } /** @@ -527,29 +523,29 @@ public: * * \b Complexity: Logarithmic. * */ - void erase(handle_type const & handle) + void erase( handle_type const& handle ) { node_pointer element = handle.node_; - node_pointer parent = element->get_parent(); + node_pointer parent = element->get_parent(); - if (parent) - parent->children.erase(node_list_type::s_iterator_to(*element)); + if ( parent ) + parent->children.erase( node_list_type::s_iterator_to( *element ) ); else - roots.erase(node_list_type::s_iterator_to(*element)); + roots.erase( node_list_type::s_iterator_to( *element ) ); - finish_erase_or_pop(element); + finish_erase_or_pop( element ); } /// \copydoc boost::heap::priority_queue::begin - iterator begin(void) const + iterator begin( void ) const { - return iterator(roots.begin()); + return iterator( roots.begin() ); } /// \copydoc boost::heap::priority_queue::end - iterator end(void) const + iterator end( void ) const { - return iterator(roots.end()); + return iterator( roots.end() ); } @@ -558,9 +554,9 @@ public: * * \b Note: Ordered iterators traverse the priority queue in heap order. * */ - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - return ordered_iterator(roots.begin(), roots.end(), top_element, super_t::value_comp()); + return ordered_iterator( roots.begin(), roots.end(), top_element, super_t::value_comp() ); } /** @@ -568,9 +564,9 @@ public: * * \b Note: Ordered iterators traverse the priority queue in heap order. * */ - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(NULL, super_t::value_comp()); + return ordered_iterator( NULL, super_t::value_comp() ); } /** @@ -579,188 +575,187 @@ public: * \b Complexity: Constant. * * */ - void merge(fibonacci_heap & rhs) + void merge( fibonacci_heap& rhs ) { - size_holder::add(rhs.get_size()); + size_holder::add( rhs.get_size() ); - if (!top_element || - (rhs.top_element && super_t::operator()(top_element->value, rhs.top_element->value))) + if ( !top_element || ( rhs.top_element && super_t::operator()( top_element->value, rhs.top_element->value ) ) ) top_element = rhs.top_element; - roots.splice(roots.end(), rhs.roots); + roots.splice( roots.end(), rhs.roots ); rhs.top_element = NULL; - rhs.set_size(0); + rhs.set_size( 0 ); - super_t::set_stability_count((std::max)(super_t::get_stability_count(), - rhs.get_stability_count())); - rhs.set_stability_count(0); + super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + rhs.set_stability_count( 0 ); } /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - node * ptr = const_cast(it.get_node()); - return handle_type(ptr); + node* ptr = const_cast< node* >( it.get_node() ); + return handle_type( ptr ); } /// \copydoc boost::heap::priority_queue::value_comp - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } private: -#if !defined(BOOST_DOXYGEN_INVOKED) - void clone_forest(fibonacci_heap const & rhs) +#if !defined( BOOST_DOXYGEN_INVOKED ) + void clone_forest( fibonacci_heap const& rhs ) { - BOOST_HEAP_ASSERT(roots.empty()); - typedef typename node::template node_cloner node_cloner; - roots.clone_from(rhs.roots, node_cloner(*this, NULL), detail::nop_disposer()); + BOOST_HEAP_ASSERT( roots.empty() ); + typedef typename node::template node_cloner< allocator_type > node_cloner; + roots.clone_from( rhs.roots, node_cloner( *this, NULL ), detail::nop_disposer() ); - top_element = detail::find_max_child(roots, super_t::get_internal_cmp()); + top_element + = detail::find_max_child< node_list_type, node, internal_compare >( roots, super_t::get_internal_cmp() ); } - void cut(node_pointer n) + void cut( node_pointer n ) { node_pointer parent = n->get_parent(); - roots.splice(roots.begin(), parent->children, node_list_type::s_iterator_to(*n)); + roots.splice( roots.begin(), parent->children, node_list_type::s_iterator_to( *n ) ); n->parent = 0; - n->mark = false; + n->mark = false; } - void cascading_cut(node_pointer n) + void cascading_cut( node_pointer n ) { node_pointer parent = n->get_parent(); - if (parent) { - if (!parent->mark) + if ( parent ) { + if ( !parent->mark ) parent->mark = true; else { - cut(n); - cascading_cut(parent); + cut( n ); + cascading_cut( parent ); } } } - void add_children_to_root(node_pointer n) + void add_children_to_root( node_pointer n ) { - for (node_list_iterator it = n->children.begin(); it != n->children.end(); ++it) { - node_pointer child = static_cast(&*it); - child->parent = 0; + for ( node_list_iterator it = n->children.begin(); it != n->children.end(); ++it ) { + node_pointer child = static_cast< node_pointer >( &*it ); + child->parent = 0; } - roots.splice(roots.end(), n->children); + roots.splice( roots.end(), n->children ); } - void consolidate(void) + void consolidate( void ) { - if (roots.empty()) + if ( roots.empty() ) return; - static const size_type max_log2 = sizeof(size_type) * 8; - boost::array aux; - aux.assign(NULL); + static const size_type max_log2 = sizeof( size_type ) * 8; + boost::array< node_pointer, max_log2 > aux; + aux.assign( NULL ); node_list_iterator it = roots.begin(); - top_element = static_cast(&*it); + top_element = static_cast< node_pointer >( &*it ); do { - node_pointer n = static_cast(&*it); + node_pointer n = static_cast< node_pointer >( &*it ); ++it; size_type node_rank = n->child_count(); - if (aux[node_rank] == NULL) - aux[node_rank] = n; + if ( aux[ node_rank ] == NULL ) + aux[ node_rank ] = n; else { do { - node_pointer other = aux[node_rank]; - if (super_t::operator()(n->value, other->value)) - std::swap(n, other); + node_pointer other = aux[ node_rank ]; + if ( super_t::operator()( n->value, other->value ) ) + std::swap( n, other ); - if (other->parent) - n->children.splice(n->children.end(), other->parent->children, node_list_type::s_iterator_to(*other)); + if ( other->parent ) + n->children.splice( n->children.end(), + other->parent->children, + node_list_type::s_iterator_to( *other ) ); else - n->children.splice(n->children.end(), roots, node_list_type::s_iterator_to(*other)); + n->children.splice( n->children.end(), roots, node_list_type::s_iterator_to( *other ) ); other->parent = n; - aux[node_rank] = NULL; - node_rank = n->child_count(); - } while (aux[node_rank] != NULL); - aux[node_rank] = n; + aux[ node_rank ] = NULL; + node_rank = n->child_count(); + } while ( aux[ node_rank ] != NULL ); + aux[ node_rank ] = n; } - if (!super_t::operator()(n->value, top_element->value)) + if ( !super_t::operator()( n->value, top_element->value ) ) top_element = n; - } - while (it != roots.end()); + } while ( it != roots.end() ); } - void finish_erase_or_pop(node_pointer erased_node) + void finish_erase_or_pop( node_pointer erased_node ) { - add_children_to_root(erased_node); + add_children_to_root( erased_node ); erased_node->~node(); allocator_type& alloc = *this; - alloc.deallocate(erased_node, 1); + alloc.deallocate( erased_node, 1 ); size_holder::decrement(); - if (!empty()) + if ( !empty() ) consolidate(); else top_element = NULL; } mutable node_pointer top_element; - node_list_type roots; + node_list_type roots; #endif }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #undef BOOST_HEAP_ASSERT diff --git a/include/boost/heap/heap_concepts.hpp b/include/boost/heap/heap_concepts.hpp index 62c1604..d088074 100644 --- a/include/boost/heap/heap_concepts.hpp +++ b/include/boost/heap/heap_concepts.hpp @@ -11,37 +11,35 @@ #include -namespace boost { -namespace heap { +namespace boost { namespace heap { -template -struct PriorityQueue: - boost::ForwardContainer +template < class C > +struct PriorityQueue : boost::ForwardContainer< C > { - typedef typename C::iterator iterator; - typedef typename C::const_iterator const_iterator; - typedef typename C::allocator_type allocator_type; - typedef typename C::value_compare value_compare; - typedef typename C::value_type value_type; + typedef typename C::iterator iterator; + typedef typename C::const_iterator const_iterator; + typedef typename C::allocator_type allocator_type; + typedef typename C::value_compare value_compare; + typedef typename C::value_type value_type; typedef typename C::const_reference const_reference; - BOOST_CONCEPT_USAGE(PriorityQueue) + BOOST_CONCEPT_USAGE( PriorityQueue ) { - BOOST_CONCEPT_ASSERT((boost::Assignable)); - BOOST_CONCEPT_ASSERT((boost::Container)); - BOOST_CONCEPT_ASSERT((boost::EqualityComparable)); - BOOST_CONCEPT_ASSERT((boost::Comparable)); + BOOST_CONCEPT_ASSERT( (boost::Assignable< value_type >)); + BOOST_CONCEPT_ASSERT( (boost::Container< C >)); + BOOST_CONCEPT_ASSERT( (boost::EqualityComparable< C >)); + BOOST_CONCEPT_ASSERT( (boost::Comparable< C >)); - BOOST_CONCEPT_ASSERT((boost::Const_BinaryPredicate)); + BOOST_CONCEPT_ASSERT( (boost::Const_BinaryPredicate< value_compare, value_type, value_type >)); - c.swap(c2); + c.swap( c2 ); c.clear(); a = c.get_allocator(); typename PriorityQueue::value_type v; - c.push(v); + c.push( v ); v = c.top(); c.pop(); @@ -50,61 +48,59 @@ struct PriorityQueue: // verify tags has_ordered_iterators = C::has_ordered_iterators; - is_mergable = C::is_mergable; - is_stable = C::is_stable; + is_mergable = C::is_mergable; + is_stable = C::is_stable; } private: - C c, c2; - allocator_type a; + C c, c2; + allocator_type a; typename C::value_type v; - value_compare cmp; - bool has_ordered_iterators, is_mergable, is_stable; + value_compare cmp; + bool has_ordered_iterators, is_mergable, is_stable; }; -template -struct MergablePriorityQueue: - PriorityQueue +template < class C > +struct MergablePriorityQueue : PriorityQueue< C > { - BOOST_CONCEPT_USAGE(MergablePriorityQueue) + BOOST_CONCEPT_USAGE( MergablePriorityQueue ) { C c, c2; - c.merge(c2); + c.merge( c2 ); } }; -template -struct MutablePriorityQueue: - PriorityQueue +template < class C > +struct MutablePriorityQueue : PriorityQueue< C > { typedef typename C::handle_type handle_type; - BOOST_CONCEPT_USAGE(MutablePriorityQueue) + BOOST_CONCEPT_USAGE( MutablePriorityQueue ) { - BOOST_CONCEPT_ASSERT((boost::Assignable)); + BOOST_CONCEPT_ASSERT( (boost::Assignable< typename MutablePriorityQueue::handle_type >)); - typename MutablePriorityQueue::value_type v; - typename MutablePriorityQueue::handle_type h = c.push(v); - typename MutablePriorityQueue::handle_type h2 = c.push(v); - c.update(h, v); - c.increase(h, v); - c.decrease(h, v); + typename MutablePriorityQueue::value_type v; + typename MutablePriorityQueue::handle_type h = c.push( v ); + typename MutablePriorityQueue::handle_type h2 = c.push( v ); + c.update( h, v ); + c.increase( h, v ); + c.decrease( h, v ); - c.update(h); - c.increase(h); - c.decrease(h); + c.update( h ); + c.increase( h ); + c.decrease( h ); - equal = (h == h2); - not_equal = (h != h2); + equal = ( h == h2 ); + not_equal = ( h != h2 ); h2 = h; } - C c; + C c; bool equal, not_equal; }; -}} +}} // namespace boost::heap #endif /* BOOST_HEAP_CONCEPTS_HPP */ diff --git a/include/boost/heap/heap_merge.hpp b/include/boost/heap/heap_merge.hpp index 3aaa4cf..7ca5615 100644 --- a/include/boost/heap/heap_merge.hpp +++ b/include/boost/heap/heap_merge.hpp @@ -17,83 +17,78 @@ #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -template +template < typename Heap1, typename Heap2 > struct heap_merge_emulate { struct dummy_reserver { - static void reserve (Heap1 & lhs, std::size_t required_size) + static void reserve( Heap1& lhs, std::size_t required_size ) {} }; struct reserver { - static void reserve (Heap1 & lhs, std::size_t required_size) + static void reserve( Heap1& lhs, std::size_t required_size ) { - lhs.reserve(required_size); + lhs.reserve( required_size ); } }; - typedef typename boost::conditional::type space_reserver; + typedef typename boost::conditional< Heap1::has_reserve, reserver, dummy_reserver >::type space_reserver; - static void merge(Heap1 & lhs, Heap2 & rhs) + static void merge( Heap1& lhs, Heap2& rhs ) { - if (Heap1::constant_time_size && Heap2::constant_time_size) { - if (Heap1::has_reserve) { + if ( Heap1::constant_time_size && Heap2::constant_time_size ) { + if ( Heap1::has_reserve ) { std::size_t required_size = lhs.size() + rhs.size(); - space_reserver::reserve(lhs, required_size); + space_reserver::reserve( lhs, required_size ); } } // FIXME: container adaptors could benefit from first appending all elements and then restoring the heap order - // FIXME: optimize: if we have ordered iterators and we can efficiently insert keys with a below the lowest key in the heap + // FIXME: optimize: if we have ordered iterators and we can efficiently insert keys with a below the lowest key + // in the heap // d-ary, b and fibonacci heaps fall into this category - while (!rhs.empty()) { - lhs.push(rhs.top()); + while ( !rhs.empty() ) { + lhs.push( rhs.top() ); rhs.pop(); } - lhs.set_stability_count((std::max)(lhs.get_stability_count(), - rhs.get_stability_count())); - rhs.set_stability_count(0); + lhs.set_stability_count( ( std::max )( lhs.get_stability_count(), rhs.get_stability_count() ) ); + rhs.set_stability_count( 0 ); } - }; -template +template < typename Heap > struct heap_merge_same_mergable { - static void merge(Heap & lhs, Heap & rhs) + static void merge( Heap& lhs, Heap& rhs ) { - lhs.merge(rhs); + lhs.merge( rhs ); } }; -template +template < typename Heap > struct heap_merge_same { static const bool is_mergable = Heap::is_mergable; - typedef typename boost::conditional, - heap_merge_emulate - >::type heap_merger; + typedef + typename boost::conditional< is_mergable, heap_merge_same_mergable< Heap >, heap_merge_emulate< Heap, Heap > >::type + heap_merger; - static void merge(Heap & lhs, Heap & rhs) + static void merge( Heap& lhs, Heap& rhs ) { - heap_merger::merge(lhs, rhs); + heap_merger::merge( lhs, rhs ); } }; @@ -105,29 +100,25 @@ struct heap_merge_same * \b Effect: lhs contains all elements that have been part of rhs, rhs is empty. * * */ -template -void heap_merge(Heap1 & lhs, Heap2 & rhs) +template < typename Heap1, typename Heap2 > +void heap_merge( Heap1& lhs, Heap2& rhs ) { - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap1 >)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< Heap2 >)); // if this assertion is triggered, the value_compare types are incompatible - BOOST_STATIC_ASSERT((boost::is_same::value)); + BOOST_STATIC_ASSERT( ( boost::is_same< typename Heap1::value_compare, typename Heap2::value_compare >::value ) ); - const bool same_heaps = boost::is_same::value; + const bool same_heaps = boost::is_same< Heap1, Heap2 >::value; - typedef typename boost::conditional, - detail::heap_merge_emulate - >::type heap_merger; + typedef typename boost::conditional< same_heaps, + detail::heap_merge_same< Heap1 >, + detail::heap_merge_emulate< Heap1, Heap2 > >::type heap_merger; - heap_merger::merge(lhs, rhs); + heap_merger::merge( lhs, rhs ); } -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #endif /* BOOST_HEAP_MERGE_HPP */ diff --git a/include/boost/heap/pairing_heap.hpp b/include/boost/heap/pairing_heap.hpp index c591cc9..03378ed 100644 --- a/include/boost/heap/pairing_heap.hpp +++ b/include/boost/heap/pairing_heap.hpp @@ -17,86 +17,82 @@ #include #include -#include #include #include +#include #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif #ifndef BOOST_DOXYGEN_INVOKED -#ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT -#else -#define BOOST_HEAP_ASSERT(expression) -#endif +# ifdef BOOST_HEAP_SANITYCHECKS +# define BOOST_HEAP_ASSERT BOOST_ASSERT +# else +# define BOOST_HEAP_ASSERT( expression ) +# endif #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > pairing_heap_signature; +typedef parameter::parameters< boost::parameter::optional< tag::allocator >, + boost::parameter::optional< tag::compare >, + boost::parameter::optional< tag::stable >, + boost::parameter::optional< tag::constant_time_size >, + boost::parameter::optional< tag::stability_counter_type > > + pairing_heap_signature; -template +template < typename T, typename Parspec > struct make_pairing_heap_base { - static const bool constant_time_size = parameter::binding::type::value; - typedef typename detail::make_heap_base::type base_type; - typedef typename detail::make_heap_base::allocator_argument allocator_argument; - typedef typename detail::make_heap_base::compare_argument compare_argument; + static const bool constant_time_size + = parameter::binding< Parspec, tag::constant_time_size, boost::true_type >::type::value; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::type base_type; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::allocator_argument allocator_argument; + typedef typename detail::make_heap_base< T, Parspec, constant_time_size >::compare_argument compare_argument; - typedef heap_node node_type; + typedef heap_node< typename base_type::internal_type, false > node_type; - typedef typename boost::allocator_rebind::type allocator_type; + typedef typename boost::allocator_rebind< allocator_argument, node_type >::type allocator_type; - struct type: - base_type, - allocator_type + struct type : base_type, allocator_type { - type(compare_argument const & arg): - base_type(arg) + type( compare_argument const& arg ) : + base_type( arg ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - type(type const & rhs): - base_type(rhs), allocator_type(rhs) + type( type const& rhs ) : + base_type( rhs ), + allocator_type( rhs ) {} - type(type && rhs): - base_type(std::move(static_cast(rhs))), - allocator_type(std::move(static_cast(rhs))) + type( type&& rhs ) : + base_type( std::move( static_cast< base_type& >( rhs ) ) ), + allocator_type( std::move( static_cast< allocator_type& >( rhs ) ) ) {} - type & operator=(type && rhs) + type& operator=( type&& rhs ) { - base_type::operator=(std::move(static_cast(rhs))); - allocator_type::operator=(std::move(static_cast(rhs))); + base_type::operator=( std::move( static_cast< base_type& >( rhs ) ) ); + allocator_type::operator=( std::move( static_cast< allocator_type& >( rhs ) ) ); return *this; } - type & operator=(type const & rhs) + type& operator=( type const& rhs ) { - base_type::operator=(static_cast(rhs)); - allocator_type::operator=(static_cast(rhs)); + base_type::operator=( static_cast< base_type const& >( rhs ) ); + allocator_type::operator=( static_cast< const allocator_type& >( rhs ) ); return *this; } #endif }; }; -} +} // namespace detail /** * \class pairing_heap @@ -121,226 +117,224 @@ struct make_pairing_heap_base * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +template < typename T, + class A0 = boost::parameter::void_, + class A1 = boost::parameter::void_, + class A2 = boost::parameter::void_, + class A3 = boost::parameter::void_, + class A4 = boost::parameter::void_ > #endif -class pairing_heap: - private detail::make_pairing_heap_base::type - >::type +class pairing_heap : + private detail::make_pairing_heap_base< T, typename detail::pairing_heap_signature::bind< A0, A1, A2, A3, A4 >::type >::type { - typedef typename detail::pairing_heap_signature::bind::type bound_args; - typedef detail::make_pairing_heap_base base_maker; - typedef typename base_maker::type super_t; + typedef typename detail::pairing_heap_signature::bind< A0, A1, A2, A3, A4 >::type bound_args; + typedef detail::make_pairing_heap_base< T, bound_args > base_maker; + typedef typename base_maker::type super_t; - typedef typename super_t::internal_type internal_type; - typedef typename super_t::size_holder_type size_holder; + typedef typename super_t::internal_type internal_type; + typedef typename super_t::size_holder_type size_holder; typedef typename base_maker::allocator_argument allocator_argument; private: - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; #ifndef BOOST_DOXYGEN_INVOKED - struct implementation_defined: - detail::extract_allocator_types + struct implementation_defined : detail::extract_allocator_types< typename base_maker::allocator_argument > { typedef T value_type; - typedef typename detail::extract_allocator_types::size_type size_type; - typedef typename detail::extract_allocator_types::reference reference; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::size_type size_type; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::reference reference; typedef typename base_maker::compare_argument value_compare; - typedef typename base_maker::allocator_type allocator_type; + typedef typename base_maker::allocator_type allocator_type; - typedef typename boost::allocator_pointer::type node_pointer; - typedef typename boost::allocator_const_pointer::type const_node_pointer; + typedef typename boost::allocator_pointer< allocator_type >::type node_pointer; + typedef typename boost::allocator_const_pointer< allocator_type >::type const_node_pointer; - typedef detail::heap_node_list node_list_type; - typedef typename node_list_type::iterator node_list_iterator; + typedef detail::heap_node_list node_list_type; + typedef typename node_list_type::iterator node_list_iterator; typedef typename node_list_type::const_iterator node_list_const_iterator; typedef typename base_maker::node_type node; - typedef detail::value_extractor value_extractor; - typedef typename super_t::internal_compare internal_compare; - typedef detail::node_handle handle_type; + typedef detail::value_extractor< value_type, internal_type, super_t > value_extractor; + typedef typename super_t::internal_compare internal_compare; + typedef detail::node_handle< node_pointer, super_t, reference > handle_type; - typedef detail::tree_iterator, - false, - false, - value_compare - > iterator; + typedef detail::tree_iterator< node, + const value_type, + allocator_type, + value_extractor, + detail::pointer_to_reference< node >, + false, + false, + value_compare > + iterator; typedef iterator const_iterator; - typedef detail::tree_iterator, - false, - true, - value_compare - > ordered_iterator; + typedef detail::tree_iterator< node, + const value_type, + allocator_type, + value_extractor, + detail::pointer_to_reference< node >, + false, + true, + value_compare > + ordered_iterator; }; - typedef typename implementation_defined::node node; - typedef typename implementation_defined::node_pointer node_pointer; - typedef typename implementation_defined::node_list_type node_list_type; - typedef typename implementation_defined::node_list_iterator node_list_iterator; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_pointer node_pointer; + typedef typename implementation_defined::node_list_type node_list_type; + typedef typename implementation_defined::node_list_iterator node_list_iterator; typedef typename implementation_defined::node_list_const_iterator node_list_const_iterator; - typedef typename implementation_defined::internal_compare internal_compare; + typedef typename implementation_defined::internal_compare internal_compare; - typedef boost::intrusive::list, - boost::intrusive::constant_time_size - > node_child_list; + typedef boost::intrusive::list< detail::heap_node_base< true >, boost::intrusive::constant_time_size< false > > + node_child_list; #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::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; /// \copydoc boost::heap::priority_queue::iterator - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; typedef typename implementation_defined::ordered_iterator ordered_iterator; typedef typename implementation_defined::handle_type handle_type; - static const bool constant_time_size = super_t::constant_time_size; + static const bool constant_time_size = super_t::constant_time_size; static const bool has_ordered_iterators = true; - static const bool is_mergable = true; - static const bool is_stable = detail::extract_stable::value; - static const bool has_reserve = false; + static const bool is_mergable = true; + static const bool is_stable = detail::extract_stable< bound_args >::value; + static const bool has_reserve = false; /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) - explicit pairing_heap(value_compare const & cmp = value_compare()): - super_t(cmp), root(NULL) + explicit pairing_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ), + root( NULL ) {} /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) - pairing_heap(pairing_heap const & rhs): - super_t(rhs), root(NULL) + pairing_heap( pairing_heap const& rhs ) : + super_t( rhs ), + root( NULL ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - clone_tree(rhs); - size_holder::set_size(rhs.get_size()); + clone_tree( rhs ); + size_holder::set_size( rhs.get_size() ); } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) - pairing_heap(pairing_heap && rhs): - super_t(std::move(rhs)), root(rhs.root) + pairing_heap( pairing_heap&& rhs ) : + super_t( std::move( rhs ) ), + root( rhs.root ) { rhs.root = NULL; } /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) - pairing_heap & operator=(pairing_heap && rhs) + pairing_heap& operator=( pairing_heap&& rhs ) { - super_t::operator=(std::move(rhs)); - root = rhs.root; + super_t::operator=( std::move( rhs ) ); + root = rhs.root; rhs.root = NULL; return *this; } #endif /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs) - pairing_heap & operator=(pairing_heap const & rhs) + pairing_heap& operator=( pairing_heap const& rhs ) { clear(); - size_holder::set_size(rhs.get_size()); - static_cast(*this) = rhs; + size_holder::set_size( rhs.get_size() ); + static_cast< super_t& >( *this ) = rhs; - clone_tree(rhs); + clone_tree( rhs ); return *this; } - ~pairing_heap(void) + ~pairing_heap( void ) { - while (!empty()) + while ( !empty() ) pop(); } /// \copydoc boost::heap::priority_queue::empty - bool empty(void) const + bool empty( void ) const { return root == NULL; } /// \copydoc boost::heap::binomial_heap::size - size_type size(void) const + size_type size( void ) const { - if (constant_time_size) + if ( constant_time_size ) return size_holder::get_size(); - if (root == NULL) + if ( root == NULL ) return 0; else - return detail::count_nodes(root); + return detail::count_nodes( root ); } /// \copydoc boost::heap::priority_queue::max_size - size_type max_size(void) const + size_type max_size( void ) const { const allocator_type& alloc = *this; - return boost::allocator_max_size(alloc); + return boost::allocator_max_size( alloc ); } /// \copydoc boost::heap::priority_queue::clear - void clear(void) + void clear( void ) { - if (empty()) + if ( empty() ) return; - root->template clear_subtree(*this); + root->template clear_subtree< allocator_type >( *this ); root->~node(); allocator_type& alloc = *this; - alloc.deallocate(root, 1); + alloc.deallocate( root, 1 ); root = NULL; - size_holder::set_size(0); + size_holder::set_size( 0 ); } /// \copydoc boost::heap::priority_queue::get_allocator - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return *this; } /// \copydoc boost::heap::priority_queue::swap - void swap(pairing_heap & rhs) + void swap( pairing_heap& rhs ) { - super_t::swap(rhs); - std::swap(root, rhs.root); + super_t::swap( rhs ); + std::swap( root, rhs.root ); } /// \copydoc boost::heap::priority_queue::top - const_reference top(void) const + const_reference top( void ) const { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); - return super_t::get_value(root->value); + return super_t::get_value( root->value ); } /** @@ -353,20 +347,21 @@ public: * \b Complexity: 2**2*log(log(N)) (amortized). * * */ - handle_type push(value_type const & v) + handle_type push( value_type const& v ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(v)); - merge_node(n); - return handle_type(n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( v ) ); + merge_node( n ); + return handle_type( n ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /** - * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns handle to element. + * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. Returns + * handle to element. * * \cond * \b Complexity: \f$2^2log(log(N))\f$ (amortized). @@ -375,16 +370,16 @@ public: * \b Complexity: 2**2*log(log(N)) (amortized). * * */ - template - handle_type emplace(Args&&... args) + template < class... Args > + handle_type emplace( Args&&... args ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(std::forward(args)...)); - merge_node(n); - return handle_type(n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( std::forward< Args >( args )... ) ); + merge_node( n ); + return handle_type( n ); } #endif @@ -394,11 +389,11 @@ public: * \b Complexity: Logarithmic (amortized). * * */ - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); - erase(handle_type(root)); + erase( handle_type( root ) ); } /** @@ -411,10 +406,10 @@ public: * \b Complexity: 2**2*log(log(N)) (amortized). * * */ - void update (handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - handle.node_->value = super_t::make_node(v); - update(handle); + handle.node_->value = super_t::make_node( v ); + update( handle ); } /** @@ -428,19 +423,19 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void update (handle_type handle) + void update( handle_type handle ) { node_pointer n = handle.node_; n->unlink(); - if (!n->children.empty()) - n = merge_nodes(n, merge_node_list(n->children)); + if ( !n->children.empty() ) + n = merge_nodes( n, merge_node_list( n->children ) ); - if (n != root) - merge_node(n); + if ( n != root ) + merge_node( n ); } - /** + /** * \b Effects: Assigns \c v to the element handled by \c handle & updates the priority queue. * * \cond @@ -451,9 +446,9 @@ public: * * \b Note: The new value is expected to be greater than the current one * */ - void increase (handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - update(handle, v); + update( handle, v ); } /** @@ -467,9 +462,9 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void increase (handle_type handle) + void increase( handle_type handle ) { - update(handle); + update( handle ); } /** @@ -483,9 +478,9 @@ public: * * \b Note: The new value is expected to be less than the current one * */ - void decrease (handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - update(handle, v); + update( handle, v ); } /** @@ -497,11 +492,12 @@ public: * * \b Complexity: 2**2*log(log(N)) (amortized). * - * \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! + * \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! * */ - void decrease (handle_type handle) + void decrease( handle_type handle ) { - update(handle); + update( handle ); } /** @@ -513,16 +509,16 @@ public: * * \b Complexity: 2**2*log(log(N)) (amortized). * */ - void erase(handle_type handle) + void erase( handle_type handle ) { node_pointer n = handle.node_; - if (n != root) { + if ( n != root ) { n->unlink(); - if (!n->children.empty()) - merge_node(merge_node_list(n->children)); + if ( !n->children.empty() ) + merge_node( merge_node_list( n->children ) ); } else { - if (!n->children.empty()) - root = merge_node_list(n->children); + if ( !n->children.empty() ) + root = merge_node_list( n->children ); else root = NULL; } @@ -530,39 +526,39 @@ public: size_holder::decrement(); n->~node(); allocator_type& alloc = *this; - alloc.deallocate(n, 1); + alloc.deallocate( n, 1 ); } /// \copydoc boost::heap::priority_queue::begin - iterator begin(void) const + iterator begin( void ) const { - return iterator(root, super_t::value_comp()); + return iterator( root, super_t::value_comp() ); } /// \copydoc boost::heap::priority_queue::end - iterator end(void) const + iterator end( void ) const { - return iterator(super_t::value_comp()); + return iterator( super_t::value_comp() ); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - return ordered_iterator(root, super_t::value_comp()); + return ordered_iterator( root, super_t::value_comp() ); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(NULL, super_t::value_comp()); + return ordered_iterator( NULL, super_t::value_comp() ); } /// \copydoc boost::heap::d_ary_heap_mutable::s_handle_from_iterator - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - node * ptr = const_cast(it.get_node()); - return handle_type(ptr); + node* ptr = const_cast< node* >( it.get_node() ); + return handle_type( ptr ); } /** @@ -575,131 +571,130 @@ public: * \b Complexity: 2**2*log(log(N)) (amortized). * * */ - void merge(pairing_heap & rhs) + void merge( pairing_heap& rhs ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - merge_node(rhs.root); + merge_node( rhs.root ); - size_holder::add(rhs.get_size()); - rhs.set_size(0); + size_holder::add( rhs.get_size() ); + rhs.set_size( 0 ); rhs.root = NULL; - super_t::set_stability_count((std::max)(super_t::get_stability_count(), - rhs.get_stability_count())); - rhs.set_stability_count(0); + super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + rhs.set_stability_count( 0 ); } /// \copydoc boost::heap::priority_queue::value_comp - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } private: -#if !defined(BOOST_DOXYGEN_INVOKED) - void clone_tree(pairing_heap const & rhs) +#if !defined( BOOST_DOXYGEN_INVOKED ) + void clone_tree( pairing_heap const& rhs ) { - BOOST_HEAP_ASSERT(root == NULL); - if (rhs.empty()) + BOOST_HEAP_ASSERT( root == NULL ); + if ( rhs.empty() ) return; - root = allocator_type::allocate(1); + root = allocator_type::allocate( 1 ); - new(root) node(static_cast(*rhs.root), static_cast(*this)); + new ( root ) node( static_cast< node const& >( *rhs.root ), static_cast< allocator_type& >( *this ) ); } - void merge_node(node_pointer other) + void merge_node( node_pointer other ) { - BOOST_HEAP_ASSERT(other); - if (root != NULL) - root = merge_nodes(root, other); + BOOST_HEAP_ASSERT( other ); + if ( root != NULL ) + root = merge_nodes( root, other ); else root = other; } - node_pointer merge_node_list(node_child_list & children) + node_pointer merge_node_list( node_child_list& children ) { - BOOST_HEAP_ASSERT(!children.empty()); - node_pointer merged = merge_first_pair(children); - if (children.empty()) + BOOST_HEAP_ASSERT( !children.empty() ); + node_pointer merged = merge_first_pair( children ); + if ( children.empty() ) return merged; node_child_list node_list; - node_list.push_back(*merged); + node_list.push_back( *merged ); do { - node_pointer next_merged = merge_first_pair(children); - node_list.push_back(*next_merged); - } while (!children.empty()); + node_pointer next_merged = merge_first_pair( children ); + node_list.push_back( *next_merged ); + } while ( !children.empty() ); - return merge_node_list(node_list); + return merge_node_list( node_list ); } - node_pointer merge_first_pair(node_child_list & children) + node_pointer merge_first_pair( node_child_list& children ) { - BOOST_HEAP_ASSERT(!children.empty()); - node_pointer first_child = static_cast(&children.front()); + BOOST_HEAP_ASSERT( !children.empty() ); + node_pointer first_child = static_cast< node_pointer >( &children.front() ); children.pop_front(); - if (children.empty()) + if ( children.empty() ) return first_child; - node_pointer second_child = static_cast(&children.front()); + node_pointer second_child = static_cast< node_pointer >( &children.front() ); children.pop_front(); - return merge_nodes(first_child, second_child); + return merge_nodes( first_child, second_child ); } - node_pointer merge_nodes(node_pointer node1, node_pointer node2) + node_pointer merge_nodes( node_pointer node1, node_pointer node2 ) { - if (super_t::operator()(node1->value, node2->value)) - std::swap(node1, node2); + if ( super_t::operator()( node1->value, node2->value ) ) + std::swap( node1, node2 ); node2->unlink(); - node1->children.push_front(*node2); + node1->children.push_front( *node2 ); return node1; } @@ -708,8 +703,7 @@ private: }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #undef BOOST_HEAP_ASSERT #endif /* BOOST_HEAP_PAIRING_HEAP_HPP */ diff --git a/include/boost/heap/policies.hpp b/include/boost/heap/policies.hpp index 6a251a9..21d3efd 100644 --- a/include/boost/heap/policies.hpp +++ b/include/boost/heap/policies.hpp @@ -10,165 +10,177 @@ #define BOOST_HEAP_POLICIES_HPP #include -#include -#include #include #include +#include #include +#include #include #include #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { #ifndef BOOST_DOXYGEN_INVOKED -BOOST_PARAMETER_TEMPLATE_KEYWORD(allocator) -BOOST_PARAMETER_TEMPLATE_KEYWORD(compare) +BOOST_PARAMETER_TEMPLATE_KEYWORD( allocator ) +BOOST_PARAMETER_TEMPLATE_KEYWORD( compare ) -namespace tag { struct stable; } +namespace tag { +struct stable; +} // namespace tag -template -struct stable: - boost::parameter::template_keyword > +template < bool T > +struct stable : boost::parameter::template_keyword< tag::stable, boost::integral_constant< bool, T > > {}; -namespace tag { struct mutable_; } +namespace tag { +struct mutable_; +} // namespace tag -template -struct mutable_: - boost::parameter::template_keyword > +template < bool T > +struct mutable_ : boost::parameter::template_keyword< tag::mutable_, boost::integral_constant< bool, T > > {}; -namespace tag { struct constant_time_size; } +namespace tag { +struct constant_time_size; +} // namespace tag -template -struct constant_time_size: - boost::parameter::template_keyword > +template < bool T > +struct constant_time_size : + boost::parameter::template_keyword< tag::constant_time_size, boost::integral_constant< bool, T > > {}; -namespace tag { struct store_parent_pointer; } +namespace tag { +struct store_parent_pointer; +} // namespace tag -template -struct store_parent_pointer: - boost::parameter::template_keyword > +template < bool T > +struct store_parent_pointer : + boost::parameter::template_keyword< tag::store_parent_pointer, boost::integral_constant< bool, T > > {}; -namespace tag { struct arity; } +namespace tag { +struct arity; +} // namespace tag -template -struct arity: - boost::parameter::template_keyword > +template < unsigned int T > +struct arity : boost::parameter::template_keyword< tag::arity, boost::integral_constant< int, T > > {}; -namespace tag { struct objects_per_page; } +namespace tag { +struct objects_per_page; +} // namespace tag -template -struct objects_per_page: - boost::parameter::template_keyword > +template < unsigned int T > +struct objects_per_page : boost::parameter::template_keyword< tag::objects_per_page, boost::integral_constant< int, T > > {}; -BOOST_PARAMETER_TEMPLATE_KEYWORD(stability_counter_type) +BOOST_PARAMETER_TEMPLATE_KEYWORD( stability_counter_type ) namespace detail { -template +template < typename bound_args, typename tag_type > struct has_arg { - typedef typename boost::parameter::binding::type type; - static const bool value = !boost::is_void::value; + typedef typename boost::parameter::binding< bound_args, tag_type, void >::type type; + static const bool value = !boost::is_void< type >::value; }; -template +template < typename bound_args > struct extract_stable { - static const bool has_stable = has_arg::value; + static const bool has_stable = has_arg< bound_args, tag::stable >::value; - typedef typename boost::conditional::type, - boost::false_type - >::type stable_t; + typedef + typename boost::conditional< has_stable, typename has_arg< bound_args, tag::stable >::type, boost::false_type >::type + stable_t; static const bool value = stable_t::value; }; -template +template < typename bound_args > struct extract_mutable { - static const bool has_mutable = has_arg::value; + static const bool has_mutable = has_arg< bound_args, tag::mutable_ >::value; - typedef typename boost::conditional::type, - boost::false_type - >::type mutable_t; + typedef typename boost::conditional< has_mutable, + typename has_arg< bound_args, tag::mutable_ >::type, + boost::false_type >::type mutable_t; static const bool value = mutable_t::value; }; -} +} // namespace detail #else /** \brief Specifies the predicate for the heap order */ -template -struct compare{}; +template < typename T > +struct compare +{}; /** \brief Configure heap as mutable * * Certain heaps need to be configured specifically do be mutable. * * */ -template -struct mutable_{}; +template < bool T > +struct mutable_ +{}; /** \brief Specifies allocator for the internal memory management */ -template -struct allocator{}; +template < typename T > +struct allocator +{}; /** \brief Configure a heap as \b stable * * A priority queue is stable, if elements with the same priority are popped from the heap, in the same order as * they are inserted. * */ -template -struct stable{}; +template < bool T > +struct stable +{}; /** \brief Specifies the type for stability counter * * */ -template -struct stability_counter_type{}; +template < typename IntType > +struct stability_counter_type +{}; /** \brief Configures complexity of size() * * Specifies, whether size() should have linear or constant complexity. * */ -template -struct constant_time_size{}; +template < bool T > +struct constant_time_size +{}; /** \brief Store parent pointer in heap node. * * Maintaining a parent pointer adds some maintenance and size overhead, but iterating a heap is more efficient. * */ -template -struct store_parent_pointer{}; +template < bool T > +struct store_parent_pointer +{}; /** \brief Specify arity. * * Specifies the arity of a D-ary heap * */ -template -struct arity{}; +template < unsigned int T > +struct arity +{}; #endif -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #endif /* BOOST_HEAP_POLICIES_HPP */ diff --git a/include/boost/heap/priority_queue.hpp b/include/boost/heap/priority_queue.hpp index ca0f06d..cc3820b 100644 --- a/include/boost/heap/priority_queue.hpp +++ b/include/boost/heap/priority_queue.hpp @@ -20,20 +20,19 @@ #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > priority_queue_signature; -} +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; +} // namespace detail /** * \class priority_queue @@ -51,62 +50,62 @@ typedef parameter::parameters, * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +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::type, false>::type +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::type, false> heap_base_maker; + 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 heap_base_maker::type super_t; typedef typename super_t::internal_type internal_type; - typedef typename boost::allocator_rebind::type internal_type_allocator; - typedef std::vector container_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 + 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 + 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 iterator; - typedef iterator const_iterator; - typedef typename container_type::allocator_type allocator_type; + 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 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::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::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; /** * \b Note: The iterator does not traverse the priority queue in order of the priorities. * */ - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; - static const bool constant_time_size = true; + 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; + static const bool is_mergable = false; + static const bool is_stable = heap_base_maker::is_stable; + static const bool has_reserve = true; /** * \b Effects: constructs an empty priority queue. @@ -114,8 +113,8 @@ public: * \b Complexity: Constant. * * */ - explicit priority_queue(value_compare const & cmp = value_compare()): - super_t(cmp) + explicit priority_queue( value_compare const& cmp = value_compare() ) : + super_t( cmp ) {} /** @@ -124,8 +123,9 @@ public: * \b Complexity: Linear. * * */ - priority_queue (priority_queue const & rhs): - super_t(rhs), q_(rhs.q_) + priority_queue( priority_queue const& rhs ) : + super_t( rhs ), + q_( rhs.q_ ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES @@ -136,8 +136,9 @@ public: * * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined * */ - priority_queue(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value): - super_t(std::move(rhs)), q_(std::move(rhs.q_)) + 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_ ) ) {} /** @@ -147,10 +148,11 @@ public: * * \b Note: Only available, if BOOST_NO_CXX11_RVALUE_REFERENCES is not defined * */ - priority_queue & operator=(priority_queue && rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_assignable::value) + 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_); + super_t::operator=( std::move( rhs ) ); + q_ = std::move( rhs.q_ ); return *this; } #endif @@ -161,10 +163,10 @@ public: * \b Complexity: Linear. * * */ - priority_queue & operator=(priority_queue const & rhs) + priority_queue& operator=( priority_queue const& rhs ) { - static_cast(*this) = static_cast(rhs); - q_ = rhs.q_; + static_cast< super_t& >( *this ) = static_cast< super_t const& >( rhs ); + q_ = rhs.q_; return *this; } @@ -174,7 +176,7 @@ public: * \b Complexity: Constant. * * */ - bool empty(void) const BOOST_NOEXCEPT + bool empty( void ) const BOOST_NOEXCEPT { return q_.empty(); } @@ -185,7 +187,7 @@ public: * \b Complexity: Constant. * * */ - size_type size(void) const BOOST_NOEXCEPT + size_type size( void ) const BOOST_NOEXCEPT { return q_.size(); } @@ -196,7 +198,7 @@ public: * \b Complexity: Constant. * * */ - size_type max_size(void) const BOOST_NOEXCEPT + size_type max_size( void ) const BOOST_NOEXCEPT { return q_.max_size(); } @@ -207,7 +209,7 @@ public: * \b Complexity: Linear. * * */ - void clear(void) BOOST_NOEXCEPT + void clear( void ) BOOST_NOEXCEPT { q_.clear(); } @@ -218,7 +220,7 @@ public: * \b Complexity: Constant. * * */ - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return q_.get_allocator(); } @@ -229,10 +231,10 @@ public: * \b Complexity: Constant. * * */ - const_reference top(void) const + const_reference top( void ) const { - BOOST_ASSERT(!empty()); - return super_t::get_value(q_.front()); + BOOST_ASSERT( !empty() ); + return super_t::get_value( q_.front() ); } /** @@ -241,24 +243,24 @@ public: * \b Complexity: Logarithmic (amortized). Linear (worst case). * * */ - void push(value_type const & v) + void push( value_type const& v ) { - q_.push_back(super_t::make_node(v)); - std::push_heap(q_.begin(), q_.end(), static_cast(*this)); + 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) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /** * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. * * \b Complexity: Logarithmic (amortized). Linear (worst case). * * */ - template - void emplace(Args&&... args) + template < class... Args > + void emplace( Args&&... args ) { - q_.emplace_back(super_t::make_node(std::forward(args)...)); - std::push_heap(q_.begin(), q_.end(), static_cast(*this)); + 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 @@ -268,10 +270,10 @@ public: * \b Complexity: Logarithmic (amortized). Linear (worst case). * * */ - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); - std::pop_heap(q_.begin(), q_.end(), static_cast(*this)); + BOOST_ASSERT( !empty() ); + std::pop_heap( q_.begin(), q_.end(), static_cast< super_t const& >( *this ) ); q_.pop_back(); } @@ -281,10 +283,11 @@ public: * \b Complexity: Constant. * * */ - void swap(priority_queue & rhs) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible::value && boost::is_nothrow_move_assignable::value) + 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_); + super_t::swap( rhs ); + q_.swap( rhs.q_ ); } /** @@ -293,9 +296,9 @@ public: * \b Complexity: Constant. * * */ - iterator begin(void) const BOOST_NOEXCEPT + iterator begin( void ) const BOOST_NOEXCEPT { - return iterator(q_.begin()); + return iterator( q_.begin() ); } /** @@ -304,9 +307,9 @@ public: * \b Complexity: Constant. * * */ - iterator end(void) const BOOST_NOEXCEPT + iterator end( void ) const BOOST_NOEXCEPT { - return iterator(q_.end()); + return iterator( q_.end() ); } /** @@ -317,16 +320,16 @@ public: * \b Node: Invalidates iterators * * */ - void reserve(size_type element_count) + void reserve( size_type element_count ) { - q_.reserve(element_count); + q_.reserve( element_count ); } /** * \b Effect: Returns the value_compare object used by the priority queue * * */ - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } @@ -337,10 +340,10 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /** @@ -349,10 +352,10 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /** @@ -361,10 +364,10 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /** @@ -373,10 +376,10 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /** \brief Equivalent comparison @@ -385,10 +388,10 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /** \brief Equivalent comparison @@ -397,14 +400,13 @@ public: * \b Requirement: the \c value_compare object of both heaps must match. * * */ - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #endif /* BOOST_HEAP_PRIORITY_QUEUE_HPP */ diff --git a/include/boost/heap/skew_heap.hpp b/include/boost/heap/skew_heap.hpp index cbb18ad..325bf7e 100644 --- a/include/boost/heap/skew_heap.hpp +++ b/include/boost/heap/skew_heap.hpp @@ -13,8 +13,8 @@ #include #include -#include #include +#include #include #include @@ -23,35 +23,34 @@ #include #ifdef BOOST_HAS_PRAGMA_ONCE -#pragma once +# pragma once #endif #ifndef BOOST_DOXYGEN_INVOKED -#ifdef BOOST_HEAP_SANITYCHECKS -#define BOOST_HEAP_ASSERT BOOST_ASSERT -#else -#define BOOST_HEAP_ASSERT(expression) -#endif +# ifdef BOOST_HEAP_SANITYCHECKS +# define BOOST_HEAP_ASSERT BOOST_ASSERT +# else +# define BOOST_HEAP_ASSERT( expression ) +# endif #endif -namespace boost { -namespace heap { +namespace boost { namespace heap { namespace detail { -template +template < typename node_pointer, bool store_parent_pointer > struct parent_holder { - parent_holder(void): - parent_(NULL) + parent_holder( void ) : + parent_( NULL ) {} - void set_parent(node_pointer parent) + void set_parent( node_pointer parent ) { - BOOST_HEAP_ASSERT(static_cast(this) != parent); + BOOST_HEAP_ASSERT( static_cast< node_pointer >( this ) != parent ); parent_ = parent; } - node_pointer get_parent(void) const + node_pointer get_parent( void ) const { return parent_; } @@ -59,172 +58,166 @@ struct parent_holder node_pointer parent_; }; -template -struct parent_holder +template < typename node_pointer > +struct parent_holder< node_pointer, false > { - void set_parent(node_pointer parent) + void set_parent( node_pointer parent ) {} - node_pointer get_parent(void) const + node_pointer get_parent( void ) const { return NULL; } }; -template -struct skew_heap_node: - parent_holder*, store_parent_pointer> +template < typename value_type, bool store_parent_pointer > +struct skew_heap_node : parent_holder< skew_heap_node< value_type, store_parent_pointer >*, store_parent_pointer > { - typedef parent_holder*, store_parent_pointer> super_t; + typedef parent_holder< skew_heap_node< value_type, store_parent_pointer >*, store_parent_pointer > super_t; - typedef boost::array child_list_type; - typedef typename child_list_type::iterator child_iterator; + typedef boost::array< skew_heap_node*, 2 > child_list_type; + typedef typename child_list_type::iterator child_iterator; typedef typename child_list_type::const_iterator const_child_iterator; - skew_heap_node(value_type const & v): - value(v) + skew_heap_node( value_type const& v ) : + value( v ) { - children.assign(0); + children.assign( 0 ); } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - skew_heap_node(value_type && v): - value(v) + skew_heap_node( value_type&& v ) : + value( v ) { - children.assign(0); + children.assign( 0 ); } #endif - template - skew_heap_node (skew_heap_node const & rhs, Alloc & allocator, skew_heap_node * parent): - value(rhs.value) + template < typename Alloc > + skew_heap_node( skew_heap_node const& rhs, Alloc& allocator, skew_heap_node* parent ) : + value( rhs.value ) { - super_t::set_parent(parent); - node_cloner cloner(allocator); - clone_child(0, rhs, cloner); - clone_child(1, rhs, cloner); + super_t::set_parent( parent ); + node_cloner< skew_heap_node, skew_heap_node, Alloc > cloner( allocator ); + clone_child( 0, rhs, cloner ); + clone_child( 1, rhs, cloner ); } - template - void clone_child(int index, skew_heap_node const & rhs, Cloner & cloner) + template < typename Cloner > + void clone_child( int index, skew_heap_node const& rhs, Cloner& cloner ) { - if (rhs.children[index]) - children[index] = cloner(*rhs.children[index], this); + if ( rhs.children[ index ] ) + children[ index ] = cloner( *rhs.children[ index ], this ); else - children[index] = NULL; + children[ index ] = NULL; } - template - void clear_subtree(Alloc & alloc) + template < typename Alloc > + void clear_subtree( Alloc& alloc ) { - node_disposer disposer(alloc); - dispose_child(children[0], disposer); - dispose_child(children[1], disposer); + node_disposer< skew_heap_node, skew_heap_node, Alloc > disposer( alloc ); + dispose_child( children[ 0 ], disposer ); + dispose_child( children[ 1 ], disposer ); } - template - void dispose_child(skew_heap_node * node, Disposer & disposer) + template < typename Disposer > + void dispose_child( skew_heap_node* node, Disposer& disposer ) { - if (node) - disposer(node); + if ( node ) + disposer( node ); } - std::size_t count_children(void) const + std::size_t count_children( void ) const { size_t ret = 1; - if (children[0]) - ret += children[0]->count_children(); - if (children[1]) - ret += children[1]->count_children(); + if ( children[ 0 ] ) + ret += children[ 0 ]->count_children(); + if ( children[ 1 ] ) + ret += children[ 1 ]->count_children(); return ret; } - template - bool is_heap(typename HeapBase::value_compare const & cmp) const + template < typename HeapBase > + bool is_heap( typename HeapBase::value_compare const& cmp ) const { - for (const_child_iterator it = children.begin(); it != children.end(); ++it) { - const skew_heap_node * child = *it; + for ( const_child_iterator it = children.begin(); it != children.end(); ++it ) { + const skew_heap_node* child = *it; - if (child == NULL) + if ( child == NULL ) continue; - if (store_parent_pointer) - BOOST_HEAP_ASSERT(child->get_parent() == this); + if ( store_parent_pointer ) + BOOST_HEAP_ASSERT( child->get_parent() == this ); - if (cmp(HeapBase::get_value(value), HeapBase::get_value(child->value)) || - !child->is_heap(cmp)) + if ( cmp( HeapBase::get_value( value ), HeapBase::get_value( child->value ) ) + || !child->is_heap< HeapBase >( cmp ) ) return false; } return true; } - value_type value; - boost::array children; + value_type value; + boost::array< skew_heap_node*, 2 > children; }; -typedef parameter::parameters, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional, - boost::parameter::optional - > skew_heap_signature; +typedef parameter::parameters< boost::parameter::optional< tag::allocator >, + boost::parameter::optional< tag::compare >, + boost::parameter::optional< tag::stable >, + boost::parameter::optional< tag::store_parent_pointer >, + boost::parameter::optional< tag::stability_counter_type >, + boost::parameter::optional< tag::constant_time_size >, + boost::parameter::optional< tag::mutable_ > > + skew_heap_signature; -template +template < typename T, typename BoundArgs > struct make_skew_heap_base { - static const bool constant_time_size = parameter::binding::type::value; + static const bool constant_time_size + = parameter::binding< BoundArgs, tag::constant_time_size, boost::true_type >::type::value; - typedef typename make_heap_base::type base_type; - typedef typename make_heap_base::allocator_argument allocator_argument; - typedef typename make_heap_base::compare_argument compare_argument; + typedef typename make_heap_base< T, BoundArgs, constant_time_size >::type base_type; + typedef typename make_heap_base< T, BoundArgs, constant_time_size >::allocator_argument allocator_argument; + typedef typename make_heap_base< T, BoundArgs, constant_time_size >::compare_argument compare_argument; - static const bool is_mutable = extract_mutable::value; - static const bool store_parent_pointer = parameter::binding::type::value || is_mutable; + static const bool is_mutable = extract_mutable< BoundArgs >::value; + static const bool store_parent_pointer + = parameter::binding< BoundArgs, tag::store_parent_pointer, boost::false_type >::type::value || is_mutable; - typedef skew_heap_node node_type; + typedef skew_heap_node< typename base_type::internal_type, store_parent_pointer > node_type; - typedef typename boost::allocator_rebind::type allocator_type; + typedef typename boost::allocator_rebind< allocator_argument, node_type >::type allocator_type; - struct type: - base_type, - allocator_type + struct type : base_type, allocator_type { - type(compare_argument const & arg): - base_type(arg) + type( compare_argument const& arg ) : + base_type( arg ) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - type(type && rhs): - base_type(std::move(static_cast(rhs))), - allocator_type(std::move(static_cast(rhs))) + type( type&& rhs ) : + base_type( std::move( static_cast< base_type& >( rhs ) ) ), + allocator_type( std::move( static_cast< allocator_type& >( rhs ) ) ) {} - type(type const & rhs): - base_type(rhs), - allocator_type(rhs) + type( type const& rhs ) : + base_type( rhs ), + allocator_type( rhs ) {} - type & operator=(type && rhs) + type& operator=( type&& rhs ) { - base_type::operator=(std::move(static_cast(rhs))); - allocator_type::operator=(std::move(static_cast(rhs))); + base_type::operator=( std::move( static_cast< base_type& >( rhs ) ) ); + allocator_type::operator=( std::move( static_cast< allocator_type& >( rhs ) ) ); return *this; } - type & operator=(type const & rhs) + type& operator=( type const& rhs ) { - base_type::operator=(static_cast(rhs)); - allocator_type::operator=(static_cast(rhs)); + base_type::operator=( static_cast< base_type const& >( rhs ) ); + allocator_type::operator=( static_cast< allocator_type const& >( rhs ) ); return *this; } #endif @@ -247,170 +240,160 @@ struct make_skew_heap_base * - \c boost::heap::stability_counter_type<>, defaults to \c stability_counter_type * - \c boost::heap::allocator<>, defaults to \c allocator > * - \c boost::heap::constant_time_size<>, defaults to \c constant_time_size - * - \c boost::heap::store_parent_pointer<>, defaults to \c store_parent_pointer. Maintaining a parent pointer adds some - * maintenance and size overhead, but iterating a heap is more efficient. + * - \c boost::heap::store_parent_pointer<>, defaults to \c store_parent_pointer. Maintaining a parent pointer + * adds some maintenance and size overhead, but iterating a heap is more efficient. * - \c boost::heap::mutable<>, defaults to \c mutable. * */ #ifdef BOOST_DOXYGEN_INVOKED -template +template < class T, class... Options > #else -template +template < typename T, + class A0 = boost::parameter::void_, + class A1 = boost::parameter::void_, + class A2 = boost::parameter::void_, + class A3 = boost::parameter::void_, + class A4 = boost::parameter::void_, + class A5 = boost::parameter::void_, + class A6 = boost::parameter::void_ > #endif -class skew_heap: - private detail::make_skew_heap_base::type - >::type +class skew_heap : + private detail::make_skew_heap_base< T, typename detail::skew_heap_signature::bind< A0, A1, A2, A3, A4, A5, A6 >::type >::type { - typedef typename detail::skew_heap_signature::bind::type bound_args; - typedef detail::make_skew_heap_base base_maker; - typedef typename base_maker::type super_t; + typedef typename detail::skew_heap_signature::bind< A0, A1, A2, A3, A4, A5, A6 >::type bound_args; + typedef detail::make_skew_heap_base< T, bound_args > base_maker; + typedef typename base_maker::type super_t; - typedef typename super_t::internal_type internal_type; - typedef typename super_t::size_holder_type size_holder; + typedef typename super_t::internal_type internal_type; + typedef typename super_t::size_holder_type size_holder; typedef typename base_maker::allocator_argument allocator_argument; static const bool store_parent_pointer = base_maker::store_parent_pointer; - template + template < typename Heap1, typename Heap2 > friend struct heap_merge_emulate; - struct implementation_defined: - detail::extract_allocator_types + struct implementation_defined : detail::extract_allocator_types< typename base_maker::allocator_argument > { typedef T value_type; typedef typename base_maker::compare_argument value_compare; - typedef typename base_maker::allocator_type allocator_type; + typedef typename base_maker::allocator_type allocator_type; - typedef typename base_maker::node_type node; - typedef typename boost::allocator_pointer::type node_pointer; - typedef typename boost::allocator_const_pointer::type const_node_pointer; + typedef typename base_maker::node_type node; + typedef typename boost::allocator_pointer< allocator_type >::type node_pointer; + typedef typename boost::allocator_const_pointer< allocator_type >::type const_node_pointer; - typedef detail::value_extractor value_extractor; + typedef detail::value_extractor< value_type, internal_type, super_t > value_extractor; - typedef boost::array child_list_type; + typedef boost::array< node_pointer, 2 > child_list_type; typedef typename child_list_type::iterator child_list_iterator; - typedef typename boost::conditional - >, - detail::tree_iterator, - true, - false, - value_compare - > - >::type iterator; + typedef typename boost::conditional< + false, + detail::recursive_tree_iterator< node, + child_list_iterator, + const value_type, + value_extractor, + detail::list_iterator_converter< node, child_list_type > >, + detail::tree_iterator< node, + const value_type, + allocator_type, + value_extractor, + detail::dereferencer< node >, + true, + false, + value_compare > >::type iterator; typedef iterator const_iterator; - typedef detail::tree_iterator, - true, - true, - value_compare - > ordered_iterator; + typedef detail:: + tree_iterator< node, const value_type, allocator_type, value_extractor, detail::dereferencer< node >, true, true, value_compare > + ordered_iterator; - typedef typename detail::extract_allocator_types::reference reference; - typedef detail::node_handle handle_type; + typedef typename detail::extract_allocator_types< typename base_maker::allocator_argument >::reference reference; + typedef detail::node_handle< node_pointer, super_t, reference > handle_type; }; typedef typename implementation_defined::value_extractor value_extractor; - typedef typename implementation_defined::node node; - typedef typename implementation_defined::node_pointer node_pointer; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_pointer node_pointer; public: typedef T value_type; - typedef typename implementation_defined::size_type size_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::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::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; /// \copydoc boost::heap::priority_queue::iterator - typedef typename implementation_defined::iterator iterator; - typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; typedef typename implementation_defined::ordered_iterator ordered_iterator; - static const bool constant_time_size = super_t::constant_time_size; + static const bool constant_time_size = super_t::constant_time_size; static const bool has_ordered_iterators = true; - static const bool is_mergable = true; - static const bool is_stable = detail::extract_stable::value; - static const bool has_reserve = false; - static const bool is_mutable = detail::extract_mutable::value; + static const bool is_mergable = true; + static const bool is_stable = detail::extract_stable< bound_args >::value; + static const bool has_reserve = false; + static const bool is_mutable = detail::extract_mutable< bound_args >::value; - typedef typename boost::conditional::type handle_type; + typedef + typename boost::conditional< is_mutable, typename implementation_defined::handle_type, void* >::type handle_type; /// \copydoc boost::heap::priority_queue::priority_queue(value_compare const &) - explicit skew_heap(value_compare const & cmp = value_compare()): - super_t(cmp), root(NULL) + explicit skew_heap( value_compare const& cmp = value_compare() ) : + super_t( cmp ), + root( NULL ) {} /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue const &) - skew_heap(skew_heap const & rhs): - super_t(rhs), root(0) + skew_heap( skew_heap const& rhs ) : + super_t( rhs ), + root( 0 ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - clone_tree(rhs); - size_holder::set_size(rhs.get_size()); + clone_tree( rhs ); + size_holder::set_size( rhs.get_size() ); } /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs) - skew_heap & operator=(skew_heap const & rhs) + skew_heap& operator=( skew_heap const& rhs ) { clear(); - size_holder::set_size(rhs.get_size()); - static_cast(*this) = rhs; + size_holder::set_size( rhs.get_size() ); + static_cast< super_t& >( *this ) = rhs; - clone_tree(rhs); + clone_tree( rhs ); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /// \copydoc boost::heap::priority_queue::priority_queue(priority_queue &&) - skew_heap(skew_heap && rhs): - super_t(std::move(rhs)), root(rhs.root) + skew_heap( skew_heap&& rhs ) : + super_t( std::move( rhs ) ), + root( rhs.root ) { rhs.root = NULL; } /// \copydoc boost::heap::priority_queue::operator=(priority_queue &&) - skew_heap & operator=(skew_heap && rhs) + skew_heap& operator=( skew_heap&& rhs ) { - super_t::operator=(std::move(rhs)); - root = rhs.root; + super_t::operator=( std::move( rhs ) ); + root = rhs.root; rhs.root = NULL; return *this; } #endif - ~skew_heap(void) + ~skew_heap( void ) { clear(); } @@ -421,85 +404,85 @@ public: * \b Complexity: Logarithmic (amortized). * * */ - typename boost::conditional::type push(value_type const & v) + typename boost::conditional< is_mutable, handle_type, void >::type push( value_type const& v ) { - typedef typename boost::conditional::type push_helper; - return push_helper::push(this, v); + typedef typename boost::conditional< is_mutable, push_handle, push_void >::type push_helper; + return push_helper::push( this, v ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) /** * \b Effects: Adds a new element to the priority queue. The element is directly constructed in-place. * * \b Complexity: Logarithmic (amortized). * * */ - template - typename boost::conditional::type emplace(Args&&... args) + template < typename... Args > + typename boost::conditional< is_mutable, handle_type, void >::type emplace( Args&&... args ) { - typedef typename boost::conditional::type push_helper; - return push_helper::emplace(this, std::forward(args)...); + typedef typename boost::conditional< is_mutable, push_handle, push_void >::type push_helper; + return push_helper::emplace( this, std::forward< Args >( args )... ); } #endif /// \copydoc boost::heap::priority_queue::empty - bool empty(void) const + bool empty( void ) const { return root == NULL; } /// \copydoc boost::heap::binomial_heap::size - size_type size(void) const + size_type size( void ) const { - if (constant_time_size) + if ( constant_time_size ) return size_holder::get_size(); - if (root == NULL) + if ( root == NULL ) return 0; else return root->count_children(); } /// \copydoc boost::heap::priority_queue::max_size - size_type max_size(void) const + size_type max_size( void ) const { const allocator_type& alloc = *this; - return boost::allocator_max_size(alloc); + return boost::allocator_max_size( alloc ); } /// \copydoc boost::heap::priority_queue::clear - void clear(void) + void clear( void ) { - if (empty()) + if ( empty() ) return; - root->template clear_subtree(*this); + root->template clear_subtree< allocator_type >( *this ); root->~node(); allocator_type& alloc = *this; - alloc.deallocate(root, 1); + alloc.deallocate( root, 1 ); root = NULL; - size_holder::set_size(0); + size_holder::set_size( 0 ); } /// \copydoc boost::heap::priority_queue::get_allocator - allocator_type get_allocator(void) const + allocator_type get_allocator( void ) const { return *this; } /// \copydoc boost::heap::priority_queue::swap - void swap(skew_heap & rhs) + void swap( skew_heap& rhs ) { - super_t::swap(rhs); - std::swap(root, rhs.root); + super_t::swap( rhs ); + std::swap( root, rhs.root ); } /// \copydoc boost::heap::priority_queue::top - const_reference top(void) const + const_reference top( void ) const { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); - return super_t::get_value(root->value); + return super_t::get_value( root->value ); } /** @@ -508,48 +491,48 @@ public: * \b Complexity: Logarithmic (amortized). * * */ - void pop(void) + void pop( void ) { - BOOST_ASSERT(!empty()); + BOOST_ASSERT( !empty() ); node_pointer top = root; - root = merge_children(root); + root = merge_children( root ); size_holder::decrement(); - if (root) - BOOST_HEAP_ASSERT(root->get_parent() == NULL); + if ( root ) + BOOST_HEAP_ASSERT( root->get_parent() == NULL ); else - BOOST_HEAP_ASSERT(size_holder::get_size() == 0); + BOOST_HEAP_ASSERT( size_holder::get_size() == 0 ); top->~node(); allocator_type& alloc = *this; - alloc.deallocate(top, 1); + alloc.deallocate( top, 1 ); sanity_check(); } /// \copydoc boost::heap::priority_queue::begin - iterator begin(void) const + iterator begin( void ) const { - return iterator(root, super_t::value_comp()); + return iterator( root, super_t::value_comp() ); } /// \copydoc boost::heap::priority_queue::end - iterator end(void) const + iterator end( void ) const { return iterator(); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_begin(void) const + ordered_iterator ordered_begin( void ) const { - return ordered_iterator(root, super_t::value_comp()); + return ordered_iterator( root, super_t::value_comp() ); } /// \copydoc boost::heap::fibonacci_heap::ordered_begin - ordered_iterator ordered_end(void) const + ordered_iterator ordered_end( void ) const { - return ordered_iterator(0, super_t::value_comp()); + return ordered_iterator( 0, super_t::value_comp() ); } /** @@ -558,77 +541,76 @@ public: * \b Complexity: Logarithmic (amortized). * * */ - void merge(skew_heap & rhs) + void merge( skew_heap& rhs ) { - if (rhs.empty()) + if ( rhs.empty() ) return; - merge_node(rhs.root); + merge_node( rhs.root ); - size_holder::add(rhs.get_size()); - rhs.set_size(0); + size_holder::add( rhs.get_size() ); + rhs.set_size( 0 ); rhs.root = NULL; sanity_check(); - super_t::set_stability_count((std::max)(super_t::get_stability_count(), - rhs.get_stability_count())); - rhs.set_stability_count(0); + super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + rhs.set_stability_count( 0 ); } /// \copydoc boost::heap::priority_queue::value_comp - value_compare const & value_comp(void) const + value_compare const& value_comp( void ) const { return super_t::value_comp(); } /// \copydoc boost::heap::priority_queue::operator<(HeapType const & rhs) const - template - bool operator<(HeapType const & rhs) const + template < typename HeapType > + bool operator<( HeapType const& rhs ) const { - return detail::heap_compare(*this, rhs); + return detail::heap_compare( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator>(HeapType const & rhs) const - template - bool operator>(HeapType const & rhs) const + template < typename HeapType > + bool operator>( HeapType const& rhs ) const { - return detail::heap_compare(rhs, *this); + return detail::heap_compare( rhs, *this ); } /// \copydoc boost::heap::priority_queue::operator>=(HeapType const & rhs) const - template - bool operator>=(HeapType const & rhs) const + template < typename HeapType > + bool operator>=( HeapType const& rhs ) const { - return !operator<(rhs); + return !operator<( rhs ); } /// \copydoc boost::heap::priority_queue::operator<=(HeapType const & rhs) const - template - bool operator<=(HeapType const & rhs) const + template < typename HeapType > + bool operator<=( HeapType const& rhs ) const { - return !operator>(rhs); + return !operator>( rhs ); } /// \copydoc boost::heap::priority_queue::operator==(HeapType const & rhs) const - template - bool operator==(HeapType const & rhs) const + template < typename HeapType > + bool operator==( HeapType const& rhs ) const { - return detail::heap_equality(*this, rhs); + return detail::heap_equality( *this, rhs ); } /// \copydoc boost::heap::priority_queue::operator!=(HeapType const & rhs) const - template - bool operator!=(HeapType const & rhs) const + template < typename HeapType > + bool operator!=( HeapType const& rhs ) const { - return !(*this == rhs); + return !( *this == rhs ); } /// \copydoc boost::heap::d_ary_heap::s_handle_from_iterator - static handle_type s_handle_from_iterator(iterator const & it) + static handle_type s_handle_from_iterator( iterator const& it ) { - node * ptr = const_cast(it.get_node()); - return handle_type(ptr); + node* ptr = const_cast< node* >( it.get_node() ); + return handle_type( ptr ); } /** @@ -636,18 +618,18 @@ public: * * \b Complexity: Logarithmic (amortized). * */ - void erase (handle_type object) + void erase( handle_type object ) { - BOOST_STATIC_ASSERT(is_mutable); + BOOST_STATIC_ASSERT( is_mutable ); node_pointer this_node = object.node_; - unlink_node(this_node); + unlink_node( this_node ); size_holder::decrement(); sanity_check(); this_node->~node(); allocator_type& alloc = *this; - alloc.deallocate(this_node, 1); + alloc.deallocate( this_node, 1 ); } /** @@ -656,13 +638,13 @@ public: * \b Complexity: Logarithmic (amortized). * * */ - void update (handle_type handle, const_reference v) + void update( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - if (super_t::operator()(super_t::get_value(handle.node_->value), v)) - increase(handle, v); + BOOST_STATIC_ASSERT( is_mutable ); + if ( super_t::operator()( super_t::get_value( handle.node_->value ), v ) ) + increase( handle, v ); else - decrease(handle, v); + decrease( handle, v ); } /** @@ -672,20 +654,19 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void update (handle_type handle) + void update( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); + BOOST_STATIC_ASSERT( is_mutable ); node_pointer this_node = handle.node_; - if (this_node->get_parent()) { - if (super_t::operator()(super_t::get_value(this_node->get_parent()->value), - super_t::get_value(this_node->value))) - increase(handle); + if ( this_node->get_parent() ) { + if ( super_t::operator()( super_t::get_value( this_node->get_parent()->value ), + super_t::get_value( this_node->value ) ) ) + increase( handle ); else - decrease(handle); - } - else - decrease(handle); + decrease( handle ); + } else + decrease( handle ); } /** @@ -695,11 +676,11 @@ public: * * \b Note: The new value is expected to be greater than the current one * */ - void increase (handle_type handle, const_reference v) + void increase( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - handle.node_->value = super_t::make_node(v); - increase(handle); + BOOST_STATIC_ASSERT( is_mutable ); + handle.node_->value = super_t::make_node( v ); + increase( handle ); } /** @@ -709,23 +690,23 @@ public: * * \b Note: If this is not called, after a handle has been updated, the behavior of the data structure is undefined! * */ - void increase (handle_type handle) + void increase( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); + BOOST_STATIC_ASSERT( is_mutable ); node_pointer this_node = handle.node_; - if (this_node == root) + if ( this_node == root ) return; node_pointer parent = this_node->get_parent(); - if (this_node == parent->children[0]) - parent->children[0] = NULL; + if ( this_node == parent->children[ 0 ] ) + parent->children[ 0 ] = NULL; else - parent->children[1] = NULL; + parent->children[ 1 ] = NULL; - this_node->set_parent(NULL); - merge_node(this_node); + this_node->set_parent( NULL ); + merge_node( this_node ); } /** @@ -735,11 +716,11 @@ public: * * \b Note: The new value is expected to be less than the current one * */ - void decrease (handle_type handle, const_reference v) + void decrease( handle_type handle, const_reference v ) { - BOOST_STATIC_ASSERT(is_mutable); - handle.node_->value = super_t::make_node(v); - decrease(handle); + BOOST_STATIC_ASSERT( is_mutable ); + handle.node_->value = super_t::make_node( v ); + decrease( handle ); } /** @@ -747,186 +728,185 @@ public: * * \b Complexity: Logarithmic (amortized). * - * \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! + * \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! * */ - void decrease (handle_type handle) + void decrease( handle_type handle ) { - BOOST_STATIC_ASSERT(is_mutable); + BOOST_STATIC_ASSERT( is_mutable ); node_pointer this_node = handle.node_; - unlink_node(this_node); - this_node->children.assign(0); - this_node->set_parent(NULL); - merge_node(this_node); + unlink_node( this_node ); + this_node->children.assign( 0 ); + this_node->set_parent( NULL ); + merge_node( this_node ); } private: -#if !defined(BOOST_DOXYGEN_INVOKED) +#if !defined( BOOST_DOXYGEN_INVOKED ) struct push_void { - static void push(skew_heap * self, const_reference v) + static void push( skew_heap* self, const_reference v ) { - self->push_internal(v); + self->push_internal( v ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - static void emplace(skew_heap * self, Args&&... args) +# if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + static void emplace( skew_heap* self, Args&&... args ) { - self->emplace_internal(std::forward(args)...); + self->emplace_internal( std::forward< Args >( args )... ); } -#endif +# endif }; struct push_handle { - static handle_type push(skew_heap * self, const_reference v) + static handle_type push( skew_heap* self, const_reference v ) { - return handle_type(self->push_internal(v)); + return handle_type( self->push_internal( v ) ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - static handle_type emplace(skew_heap * self, Args&&... args) +# if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + static handle_type emplace( skew_heap* self, Args&&... args ) { - return handle_type(self->emplace_internal(std::forward(args)...)); + return handle_type( self->emplace_internal( std::forward< Args >( args )... ) ); } -#endif +# endif }; - node_pointer push_internal(const_reference v) + node_pointer push_internal( const_reference v ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(v)); - merge_node(n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( v ) ); + merge_node( n ); return n; } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template - node_pointer emplace_internal(Args&&... args) +# if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + template < class... Args > + node_pointer emplace_internal( Args&&... args ) { size_holder::increment(); allocator_type& alloc = *this; - node_pointer n = alloc.allocate(1); - new(n) node(super_t::make_node(std::forward(args)...)); - merge_node(n); + node_pointer n = alloc.allocate( 1 ); + new ( n ) node( super_t::make_node( std::forward< Args >( args )... ) ); + merge_node( n ); return n; } -#endif +# endif - void unlink_node(node_pointer node) + void unlink_node( node_pointer node ) { - node_pointer parent = node->get_parent(); - node_pointer merged_children = merge_children(node); + node_pointer parent = node->get_parent(); + node_pointer merged_children = merge_children( node ); - if (parent) { - if (node == parent->children[0]) - parent->children[0] = merged_children; + if ( parent ) { + if ( node == parent->children[ 0 ] ) + parent->children[ 0 ] = merged_children; else - parent->children[1] = merged_children; - } - else + parent->children[ 1 ] = merged_children; + } else root = merged_children; } - void clone_tree(skew_heap const & rhs) + void clone_tree( skew_heap const& rhs ) { - BOOST_HEAP_ASSERT(root == NULL); - if (rhs.empty()) + BOOST_HEAP_ASSERT( root == NULL ); + if ( rhs.empty() ) return; allocator_type& alloc = *this; - root = alloc.allocate(1); - new(root) node(*rhs.root, alloc, NULL); + root = alloc.allocate( 1 ); + new ( root ) node( *rhs.root, alloc, NULL ); } - void merge_node(node_pointer other) + void merge_node( node_pointer other ) { - BOOST_HEAP_ASSERT(other); - if (root != NULL) - root = merge_nodes(root, other, NULL); + BOOST_HEAP_ASSERT( other ); + if ( root != NULL ) + root = merge_nodes( root, other, NULL ); else root = other; } - node_pointer merge_nodes(node_pointer node1, node_pointer node2, node_pointer new_parent) + node_pointer merge_nodes( node_pointer node1, node_pointer node2, node_pointer new_parent ) { - if (node1 == NULL) { - if (node2) - node2->set_parent(new_parent); + if ( node1 == NULL ) { + if ( node2 ) + node2->set_parent( new_parent ); return node2; } - if (node2 == NULL) { - node1->set_parent(new_parent); + if ( node2 == NULL ) { + node1->set_parent( new_parent ); return node1; } - node_pointer merged = merge_nodes_recursive(node1, node2, new_parent); + node_pointer merged = merge_nodes_recursive( node1, node2, new_parent ); return merged; } - node_pointer merge_children(node_pointer node) + node_pointer merge_children( node_pointer node ) { - node_pointer parent = node->get_parent(); - node_pointer merged_children = merge_nodes(node->children[0], node->children[1], parent); + node_pointer parent = node->get_parent(); + node_pointer merged_children = merge_nodes( node->children[ 0 ], node->children[ 1 ], parent ); return merged_children; } - node_pointer merge_nodes_recursive(node_pointer node1, node_pointer node2, node_pointer new_parent) + node_pointer merge_nodes_recursive( node_pointer node1, node_pointer node2, node_pointer new_parent ) { - if (super_t::operator()(node1->value, node2->value)) - std::swap(node1, node2); + if ( super_t::operator()( node1->value, node2->value ) ) + std::swap( node1, node2 ); - node * parent = node1; - node * child = node2; + node* parent = node1; + node* child = node2; - if (parent->children[1]) { - node * merged = merge_nodes(parent->children[1], child, parent); - parent->children[1] = merged; - merged->set_parent(parent); + if ( parent->children[ 1 ] ) { + node* merged = merge_nodes( parent->children[ 1 ], child, parent ); + parent->children[ 1 ] = merged; + merged->set_parent( parent ); } else { - parent->children[1] = child; - child->set_parent(parent); + parent->children[ 1 ] = child; + child->set_parent( parent ); } - std::swap(parent->children[0], parent->children[1]); - parent->set_parent(new_parent); + std::swap( parent->children[ 0 ], parent->children[ 1 ] ); + parent->set_parent( new_parent ); return parent; } - void sanity_check(void) + void sanity_check( void ) { -#ifdef BOOST_HEAP_SANITYCHECKS - if (root) - BOOST_HEAP_ASSERT( root->template is_heap(super_t::value_comp()) ); +# ifdef BOOST_HEAP_SANITYCHECKS + if ( root ) + BOOST_HEAP_ASSERT( root->template is_heap< super_t >( super_t::value_comp() ) ); - if (constant_time_size) { + if ( constant_time_size ) { size_type stored_size = size_holder::get_size(); size_type counted_size; - if (root == NULL) + if ( root == NULL ) counted_size = 0; else counted_size = root->count_children(); - BOOST_HEAP_ASSERT(counted_size == stored_size); + BOOST_HEAP_ASSERT( counted_size == stored_size ); } -#endif +# endif } node_pointer root; #endif }; -} /* namespace heap */ -} /* namespace boost */ +}} // namespace boost::heap #undef BOOST_HEAP_ASSERT #endif /* BOOST_HEAP_SKEW_HEAP_HPP */ diff --git a/index.html b/index.html index 2717f97..50a1de5 100644 --- a/index.html +++ b/index.html @@ -6,8 +6,8 @@ Automatic redirection failed, please go to ../../doc/html/heap.html  

© Copyright Beman Dawes, 2001

-

Distributed under the Boost Software License, Version 1.0. (See accompanying -file LICENSE_1_0.txt or copy +

Distributed under the Boost Software License, Version 1.0. (See accompanying +file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)

diff --git a/test/binomial_heap_test.cpp b/test/binomial_heap_test.cpp index e3a1eb0..e23e280 100644 --- a/test/binomial_heap_test.cpp +++ b/test/binomial_heap_test.cpp @@ -12,59 +12,63 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" -#include "mutable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "mutable_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_binomial_heap_test(void) +template < bool stable, bool constant_time_size > +void run_binomial_heap_test( void ) { - typedef boost::heap::binomial_heap, - boost::heap::compare >, - boost::heap::allocator >, - boost::heap::constant_time_size > pri_queue; + typedef boost::heap::binomial_heap< int, + boost::heap::stable< stable >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > >, + boost::heap::constant_time_size< constant_time_size > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::MutablePriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::MergablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::MutablePriorityQueue< pri_queue >)); + BOOST_CONCEPT_ASSERT( (boost::heap::MergablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - run_mutable_heap_tests(); - run_ordered_iterator_tests(); + run_mutable_heap_tests< pri_queue >(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::binomial_heap, - boost::heap::constant_time_size > stable_pri_queue; + if ( stable ) { + typedef boost::heap::binomial_heap< q_tester, + boost::heap::stable< stable >, + boost::heap::constant_time_size< constant_time_size > > + stable_pri_queue; - run_stable_heap_tests(); + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( binomial_heap_test ) { - run_binomial_heap_test(); - run_binomial_heap_test(); - run_binomial_heap_test(); - run_binomial_heap_test(); + run_binomial_heap_test< false, false >(); + run_binomial_heap_test< false, true >(); + run_binomial_heap_test< true, false >(); + run_binomial_heap_test< true, true >(); - RUN_EMPLACE_TEST(binomial_heap); + RUN_EMPLACE_TEST( binomial_heap ); } BOOST_AUTO_TEST_CASE( binomial_heap_compare_lookup_test ) { - typedef boost::heap::binomial_heap, - boost::heap::allocator > > pri_queue; - run_common_heap_tests(); + typedef boost::heap:: + binomial_heap< int, boost::heap::compare< less_with_T >, boost::heap::allocator< std::allocator< int > > > + pri_queue; + run_common_heap_tests< pri_queue >(); } BOOST_AUTO_TEST_CASE( binomial_heap_leak_test ) { - typedef boost::heap::binomial_heap > pri_queue; - run_leak_check_test(); + typedef boost::heap::binomial_heap< boost::shared_ptr< int > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/common_heap_tests.hpp b/test/common_heap_tests.hpp index 4454c2a..cb76d6b 100644 --- a/test/common_heap_tests.hpp +++ b/test/common_heap_tests.hpp @@ -19,501 +19,492 @@ #include #ifdef BOOST_NO_CXX98_RANDOM_SHUFFLE -#include -#include +# include +# include -template -void random_shuffle(RandomIt first, RandomIt last) +template < class RandomIt > +void random_shuffle( RandomIt first, RandomIt last ) { - typedef typename std::iterator_traits::difference_type difference_type; - difference_type n = last - first; - for (difference_type i = n-1; i > 0; --i) { - difference_type j = std::rand() % (i + 1); - if (j != i) { - using std::swap; - swap(first[i], first[j]); + typedef typename std::iterator_traits< RandomIt >::difference_type difference_type; + difference_type n = last - first; + for ( difference_type i = n - 1; i > 0; --i ) { + difference_type j = std::rand() % ( i + 1 ); + if ( j != i ) { + using std::swap; + swap( first[ i ], first[ j ] ); } } } #else - + using std::random_shuffle; #endif typedef boost::default_constructible_archetype< - boost::less_than_comparable_archetype< - boost::copy_constructible_archetype< - boost::assignable_archetype< - > > > > test_value_type; + boost::less_than_comparable_archetype< boost::copy_constructible_archetype< boost::assignable_archetype<> > > > + test_value_type; -typedef std::vector test_data; -const int test_size = 32; +typedef std::vector< int > test_data; +const int test_size = 32; struct dummy_run { - static void run(void) + static void run( void ) {} }; -test_data make_test_data(int size, int offset = 0, int strive = 1) +test_data make_test_data( int size, int offset = 0, int strive = 1 ) { test_data ret; - for (int i = 0; i != size; ++i) - ret.push_back(i * strive + offset); + for ( int i = 0; i != size; ++i ) + ret.push_back( i * strive + offset ); return ret; } -template -void check_q(pri_queue & q, data_container const & expected) +template < typename pri_queue, typename data_container > +void check_q( pri_queue& q, data_container const& expected ) { - BOOST_REQUIRE_EQUAL(q.size(), expected.size()); + BOOST_REQUIRE_EQUAL( q.size(), expected.size() ); - for (unsigned int i = 0; i != expected.size(); ++i) - { - BOOST_REQUIRE_EQUAL(q.size(), expected.size() - i); - BOOST_REQUIRE_EQUAL(q.top(), expected[expected.size()-1-i]); + for ( unsigned int i = 0; i != expected.size(); ++i ) { + BOOST_REQUIRE_EQUAL( q.size(), expected.size() - i ); + BOOST_REQUIRE_EQUAL( q.top(), expected[ expected.size() - 1 - i ] ); q.pop(); } - BOOST_REQUIRE(q.empty()); + BOOST_REQUIRE( q.empty() ); } -template -void fill_q(pri_queue & q, data_container const & data) +template < typename pri_queue, typename data_container > +void fill_q( pri_queue& q, data_container const& data ) { - for (unsigned int i = 0; i != data.size(); ++i) - q.push(data[i]); + for ( unsigned int i = 0; i != data.size(); ++i ) + q.push( data[ i ] ); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) -template -void fill_emplace_q(pri_queue & q, data_container const & data) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) +template < typename pri_queue, typename data_container > +void fill_emplace_q( pri_queue& q, data_container const& data ) { - for (unsigned int i = 0; i != data.size(); ++i) { - typename pri_queue::value_type value = data[i]; - q.emplace(std::move(value)); + for ( unsigned int i = 0; i != data.size(); ++i ) { + typename pri_queue::value_type value = data[ i ]; + q.emplace( std::move( value ) ); } } #endif -template -void pri_queue_test_sequential_push(void) +template < typename pri_queue > +void pri_queue_test_sequential_push( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - fill_q(q, data); - check_q(q, data); + test_data data = make_test_data( i ); + fill_q( q, data ); + check_q( q, data ); } } -template -void pri_queue_test_sequential_reverse_push(void) +template < typename pri_queue > +void pri_queue_test_sequential_reverse_push( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - std::reverse(data.begin(), data.end()); - fill_q(q, data); - std::reverse(data.begin(), data.end()); - check_q(q, data); + test_data data = make_test_data( i ); + std::reverse( data.begin(), data.end() ); + fill_q( q, data ); + std::reverse( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_emplace(void) +template < typename pri_queue > +void pri_queue_test_emplace( void ) { -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - for (int i = 0; i != test_size; ++i) - { +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - std::reverse(data.begin(), data.end()); - fill_emplace_q(q, data); - std::reverse(data.begin(), data.end()); - check_q(q, data); + test_data data = make_test_data( i ); + std::reverse( data.begin(), data.end() ); + fill_emplace_q( q, data ); + std::reverse( data.begin(), data.end() ); + check_q( q, data ); } #endif } -template -void pri_queue_test_random_push(void) +template < typename pri_queue > +void pri_queue_test_random_push( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); + test_data data = make_test_data( i ); - test_data shuffled (data); - random_shuffle(shuffled.begin(), shuffled.end()); + test_data shuffled( data ); + random_shuffle( shuffled.begin(), shuffled.end() ); - fill_q(q, shuffled); + fill_q( q, shuffled ); - check_q(q, data); + check_q( q, data ); } } -template -void pri_queue_test_copyconstructor(void) +template < typename pri_queue > +void pri_queue_test_copyconstructor( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - fill_q(q, data); + test_data data = make_test_data( i ); + fill_q( q, data ); - pri_queue r(q); + pri_queue r( q ); - check_q(r, data); + check_q( r, data ); } } -template -void pri_queue_test_assignment(void) +template < typename pri_queue > +void pri_queue_test_assignment( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - fill_q(q, data); + test_data data = make_test_data( i ); + fill_q( q, data ); pri_queue r; r = q; - check_q(r, data); + check_q( r, data ); } } -template -void pri_queue_test_moveconstructor(void) +template < typename pri_queue > +void pri_queue_test_moveconstructor( void ) { #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES pri_queue q; - test_data data = make_test_data(test_size); - fill_q(q, data); + test_data data = make_test_data( test_size ); + fill_q( q, data ); - pri_queue r(std::move(q)); + pri_queue r( std::move( q ) ); - check_q(r, data); - BOOST_REQUIRE(q.empty()); + check_q( r, data ); + BOOST_REQUIRE( q.empty() ); #endif } -template -void pri_queue_test_move_assignment(void) +template < typename pri_queue > +void pri_queue_test_move_assignment( void ) { #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES pri_queue q; - test_data data = make_test_data(test_size); - fill_q(q, data); + test_data data = make_test_data( test_size ); + fill_q( q, data ); pri_queue r; - r = std::move(q); + r = std::move( q ); - check_q(r, data); - BOOST_REQUIRE(q.empty()); + check_q( r, data ); + BOOST_REQUIRE( q.empty() ); #endif } -template -void pri_queue_test_swap(void) +template < typename pri_queue > +void pri_queue_test_swap( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - test_data shuffled (data); - random_shuffle(shuffled.begin(), shuffled.end()); - fill_q(q, shuffled); + test_data data = make_test_data( i ); + test_data shuffled( data ); + random_shuffle( shuffled.begin(), shuffled.end() ); + fill_q( q, shuffled ); pri_queue r; - q.swap(r); - check_q(r, data); - BOOST_REQUIRE(q.empty()); + q.swap( r ); + check_q( r, data ); + BOOST_REQUIRE( q.empty() ); } } -template -void pri_queue_test_iterators(void) +template < typename pri_queue > +void pri_queue_test_iterators( void ) { - for (int i = 0; i != test_size; ++i) { - test_data data = make_test_data(test_size); - test_data shuffled (data); - random_shuffle(shuffled.begin(), shuffled.end()); + for ( int i = 0; i != test_size; ++i ) { + test_data data = make_test_data( test_size ); + test_data shuffled( data ); + random_shuffle( shuffled.begin(), shuffled.end() ); pri_queue q; - BOOST_REQUIRE(q.begin() == q.end()); - fill_q(q, shuffled); + BOOST_REQUIRE( q.begin() == q.end() ); + fill_q( q, shuffled ); - for (unsigned long j = 0; j != data.size(); ++j) - BOOST_REQUIRE(std::find(q.begin(), q.end(), data[j]) != q.end()); + for ( unsigned long j = 0; j != data.size(); ++j ) + BOOST_REQUIRE( std::find( q.begin(), q.end(), data[ j ] ) != q.end() ); - for (unsigned long j = 0; j != data.size(); ++j) - BOOST_REQUIRE(std::find(q.begin(), q.end(), data[j] + data.size()) == q.end()); + for ( unsigned long j = 0; j != data.size(); ++j ) + BOOST_REQUIRE( std::find( q.begin(), q.end(), data[ j ] + data.size() ) == q.end() ); - test_data data_from_queue(q.begin(), q.end()); - std::sort(data_from_queue.begin(), data_from_queue.end()); + test_data data_from_queue( q.begin(), q.end() ); + std::sort( data_from_queue.begin(), data_from_queue.end() ); - BOOST_REQUIRE(data == data_from_queue); + BOOST_REQUIRE( data == data_from_queue ); - for (unsigned long j = 0; j != data.size(); ++j) { - BOOST_REQUIRE_EQUAL((long)std::distance(q.begin(), q.end()), (long)(data.size() - j)); + for ( unsigned long j = 0; j != data.size(); ++j ) { + BOOST_REQUIRE_EQUAL( (long)std::distance( q.begin(), q.end() ), (long)( data.size() - j ) ); q.pop(); } } } -template -void pri_queue_test_ordered_iterators(void) +template < typename pri_queue > +void pri_queue_test_ordered_iterators( void ) { - for (int i = 0; i != test_size; ++i) { - test_data data = make_test_data(i); - test_data shuffled (data); - random_shuffle(shuffled.begin(), shuffled.end()); + for ( int i = 0; i != test_size; ++i ) { + test_data data = make_test_data( i ); + test_data shuffled( data ); + random_shuffle( shuffled.begin(), shuffled.end() ); pri_queue q; - BOOST_REQUIRE(q.ordered_begin() == q.ordered_end()); - fill_q(q, shuffled); + BOOST_REQUIRE( q.ordered_begin() == q.ordered_end() ); + fill_q( q, shuffled ); - test_data data_from_queue(q.ordered_begin(), q.ordered_end()); - std::reverse(data_from_queue.begin(), data_from_queue.end()); - BOOST_REQUIRE(data == data_from_queue); + test_data data_from_queue( q.ordered_begin(), q.ordered_end() ); + std::reverse( data_from_queue.begin(), data_from_queue.end() ); + BOOST_REQUIRE( data == data_from_queue ); - for (unsigned long j = 0; j != data.size(); ++j) - BOOST_REQUIRE(std::find(q.ordered_begin(), q.ordered_end(), data[j]) != q.ordered_end()); + for ( unsigned long j = 0; j != data.size(); ++j ) + BOOST_REQUIRE( std::find( q.ordered_begin(), q.ordered_end(), data[ j ] ) != q.ordered_end() ); - for (unsigned long j = 0; j != data.size(); ++j) - BOOST_REQUIRE(std::find(q.ordered_begin(), q.ordered_end(), data[j] + data.size()) == q.ordered_end()); + for ( unsigned long j = 0; j != data.size(); ++j ) + BOOST_REQUIRE( std::find( q.ordered_begin(), q.ordered_end(), data[ j ] + data.size() ) == q.ordered_end() ); - for (unsigned long j = 0; j != data.size(); ++j) { - BOOST_REQUIRE_EQUAL((long)std::distance(q.begin(), q.end()), (long)(data.size() - j)); + for ( unsigned long j = 0; j != data.size(); ++j ) { + BOOST_REQUIRE_EQUAL( (long)std::distance( q.begin(), q.end() ), (long)( data.size() - j ) ); q.pop(); } } } -template -void pri_queue_test_equality(void) +template < typename pri_queue > +void pri_queue_test_equality( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - fill_q(q, data); - std::reverse(data.begin(), data.end()); - fill_q(r, data); + test_data data = make_test_data( i ); + fill_q( q, data ); + std::reverse( data.begin(), data.end() ); + fill_q( r, data ); - BOOST_REQUIRE(r == q); + BOOST_REQUIRE( r == q ); } } -template -void pri_queue_test_inequality(void) +template < typename pri_queue > +void pri_queue_test_inequality( void ) { - for (int i = 1; i != test_size; ++i) - { + for ( int i = 1; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - fill_q(q, data); - data[0] = data.back() + 1; - fill_q(r, data); + test_data data = make_test_data( i ); + fill_q( q, data ); + data[ 0 ] = data.back() + 1; + fill_q( r, data ); - BOOST_REQUIRE(r != q); + BOOST_REQUIRE( r != q ); } } -template -void pri_queue_test_less(void) +template < typename pri_queue > +void pri_queue_test_less( void ) { - for (int i = 1; i != test_size; ++i) - { + for ( int i = 1; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - test_data r_data(data); + test_data data = make_test_data( i ); + test_data r_data( data ); r_data.pop_back(); - fill_q(q, data); - fill_q(r, r_data); + fill_q( q, data ); + fill_q( r, r_data ); - BOOST_REQUIRE(r < q); + BOOST_REQUIRE( r < q ); } - for (int i = 1; i != test_size; ++i) - { + for ( int i = 1; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - test_data r_data(data); - data.push_back(data.back() + 1); + test_data data = make_test_data( i ); + test_data r_data( data ); + data.push_back( data.back() + 1 ); - fill_q(q, data); - fill_q(r, r_data); + fill_q( q, data ); + fill_q( r, r_data ); - BOOST_REQUIRE(r < q); + BOOST_REQUIRE( r < q ); } - for (int i = 1; i != test_size; ++i) - { + for ( int i = 1; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - test_data r_data(data); + test_data data = make_test_data( i ); + test_data r_data( data ); data.back() += 1; - fill_q(q, data); - fill_q(r, r_data); + fill_q( q, data ); + fill_q( r, r_data ); - BOOST_REQUIRE(r < q); + BOOST_REQUIRE( r < q ); } - for (int i = 1; i != test_size; ++i) - { + for ( int i = 1; i != test_size; ++i ) { pri_queue q, r; - test_data data = make_test_data(i); - test_data r_data(data); + test_data data = make_test_data( i ); + test_data r_data( data ); r_data.front() -= 1; - fill_q(q, data); - fill_q(r, r_data); + fill_q( q, data ); + fill_q( r, r_data ); - BOOST_REQUIRE(r < q); + BOOST_REQUIRE( r < q ); } - } -template -void pri_queue_test_clear(void) +template < typename pri_queue > +void pri_queue_test_clear( void ) { - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(i); - fill_q(q, data); + test_data data = make_test_data( i ); + fill_q( q, data ); q.clear(); - BOOST_REQUIRE(q.size() == 0); - BOOST_REQUIRE(q.empty()); + BOOST_REQUIRE( q.size() == 0 ); + BOOST_REQUIRE( q.empty() ); } } -template -void run_concept_check(void) +template < typename pri_queue > +void run_concept_check( void ) { - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< pri_queue >)); } -template -void run_common_heap_tests(void) +template < typename pri_queue > +void run_common_heap_tests( void ) { - pri_queue_test_sequential_push(); - pri_queue_test_sequential_reverse_push(); - pri_queue_test_random_push(); - pri_queue_test_equality(); - pri_queue_test_inequality(); - pri_queue_test_less(); - pri_queue_test_clear(); + pri_queue_test_sequential_push< pri_queue >(); + pri_queue_test_sequential_reverse_push< pri_queue >(); + pri_queue_test_random_push< pri_queue >(); + pri_queue_test_equality< pri_queue >(); + pri_queue_test_inequality< pri_queue >(); + pri_queue_test_less< pri_queue >(); + pri_queue_test_clear< pri_queue >(); - pri_queue_test_emplace(); + pri_queue_test_emplace< pri_queue >(); } -template -void run_iterator_heap_tests(void) +template < typename pri_queue > +void run_iterator_heap_tests( void ) { - pri_queue_test_iterators(); + pri_queue_test_iterators< pri_queue >(); } -template -void run_copyable_heap_tests(void) +template < typename pri_queue > +void run_copyable_heap_tests( void ) { - pri_queue_test_copyconstructor (); - pri_queue_test_assignment(); - pri_queue_test_swap(); + pri_queue_test_copyconstructor< pri_queue >(); + pri_queue_test_assignment< pri_queue >(); + pri_queue_test_swap< pri_queue >(); } -template -void run_moveable_heap_tests(void) +template < typename pri_queue > +void run_moveable_heap_tests( void ) { - pri_queue_test_moveconstructor (); - pri_queue_test_move_assignment (); + pri_queue_test_moveconstructor< pri_queue >(); + pri_queue_test_move_assignment< pri_queue >(); } -template -void run_reserve_heap_tests(void) +template < typename pri_queue > +void run_reserve_heap_tests( void ) { - test_data data = make_test_data(test_size); + test_data data = make_test_data( test_size ); pri_queue q; - q.reserve(6); - fill_q(q, data); + q.reserve( 6 ); + fill_q( q, data ); - check_q(q, data); + check_q( q, data ); } -template -void run_leak_check_test(void) +template < typename pri_queue > +void run_leak_check_test( void ) { pri_queue q; - q.push(boost::shared_ptr(new int(0))); + q.push( boost::shared_ptr< int >( new int( 0 ) ) ); } struct less_with_T { typedef int T; - bool operator()(const int& a, const int& b) const + bool operator()( const int& a, const int& b ) const { return a < b; } }; -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) -class thing { +class thing +{ public: - thing( int a_, int b_, int c_ ) : a(a_), b(b_), c(c_) {} + thing( int a_, int b_, int c_ ) : + a( a_ ), + b( b_ ), + c( c_ ) + {} + public: int a; int b; int c; }; -class cmpthings { +class cmpthings +{ public: - bool operator() ( const thing& lhs, const thing& rhs ) const { + bool operator()( const thing& lhs, const thing& rhs ) const + { return lhs.a > rhs.a; } - bool operator() ( const thing& lhs, const thing& rhs ) { + bool operator()( const thing& lhs, const thing& rhs ) + { return lhs.a > rhs.a; } }; -#define RUN_EMPLACE_TEST(HEAP_TYPE) \ - do { \ - cmpthings ord; \ - boost::heap::HEAP_TYPE > vpq(ord); \ - vpq.emplace(5, 6, 7); \ - boost::heap::HEAP_TYPE, boost::heap::stable > vpq2(ord); \ - vpq2.emplace(5, 6, 7); \ - } while(0); +# define RUN_EMPLACE_TEST( HEAP_TYPE ) \ + do { \ + cmpthings ord; \ + boost::heap::HEAP_TYPE< thing, boost::heap::compare< cmpthings > > vpq( ord ); \ + vpq.emplace( 5, 6, 7 ); \ + boost::heap::HEAP_TYPE< thing, boost::heap::compare< cmpthings >, boost::heap::stable< true > > vpq2( ord ); \ + vpq2.emplace( 5, 6, 7 ); \ + } while ( 0 ); #else -#define RUN_EMPLACE_TEST(HEAP_TYPE) +# define RUN_EMPLACE_TEST( HEAP_TYPE ) #endif diff --git a/test/d_ary_heap_test.cpp b/test/d_ary_heap_test.cpp index c459f3f..e3f36d7 100644 --- a/test/d_ary_heap_test.cpp +++ b/test/d_ary_heap_test.cpp @@ -8,9 +8,9 @@ #define BOOST_TEST_MAIN #ifdef BOOST_HEAP_INCLUDE_TESTS -#include +# include #else -#include +# include #endif #include @@ -18,118 +18,118 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" -#include "mutable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "mutable_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_d_ary_heap_test(void) +template < int D, bool stable > +void run_d_ary_heap_test( void ) { - typedef boost::heap::d_ary_heap, - boost::heap::stable, - boost::heap::compare >, - boost::heap::allocator > > pri_queue; + typedef boost::heap::d_ary_heap< int, + boost::heap::arity< D >, + boost::heap::stable< stable >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< pri_queue >)); - run_concept_check(); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); - run_reserve_heap_tests(); - run_merge_tests(); + run_concept_check< pri_queue >(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); + run_reserve_heap_tests< pri_queue >(); + run_merge_tests< pri_queue >(); - run_ordered_iterator_tests(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::d_ary_heap, - boost::heap::stable - > stable_pri_queue; + if ( stable ) { + typedef boost::heap::d_ary_heap< q_tester, boost::heap::arity< D >, boost::heap::stable< stable > > stable_pri_queue; - run_stable_heap_tests(); + run_stable_heap_tests< stable_pri_queue >(); } -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) && !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) cmpthings ord; - boost::heap::d_ary_heap, boost::heap::compare, boost::heap::stable > vpq(ord); - vpq.emplace(5, 6, 7); + boost::heap::d_ary_heap< thing, boost::heap::arity< D >, boost::heap::compare< cmpthings >, boost::heap::stable< stable > > + vpq( ord ); + vpq.emplace( 5, 6, 7 ); #endif } BOOST_AUTO_TEST_CASE( d_ary_heap_test ) { - run_d_ary_heap_test<2, false>(); - run_d_ary_heap_test<3, false>(); - run_d_ary_heap_test<4, false>(); - run_d_ary_heap_test<5, false>(); + run_d_ary_heap_test< 2, false >(); + run_d_ary_heap_test< 3, false >(); + run_d_ary_heap_test< 4, false >(); + run_d_ary_heap_test< 5, false >(); } BOOST_AUTO_TEST_CASE( d_ary_heap_stable_test ) { - run_d_ary_heap_test<2, true>(); - run_d_ary_heap_test<3, true>(); - run_d_ary_heap_test<4, true>(); - run_d_ary_heap_test<5, true>(); + run_d_ary_heap_test< 2, true >(); + run_d_ary_heap_test< 3, true >(); + run_d_ary_heap_test< 4, true >(); + run_d_ary_heap_test< 5, true >(); } -template -void run_d_ary_heap_mutable_test(void) +template < int D, bool stable > +void run_d_ary_heap_mutable_test( void ) { - typedef boost::heap::d_ary_heap, - boost::heap::arity, - boost::heap::stable - > pri_queue; + typedef boost::heap::d_ary_heap< int, boost::heap::mutable_< true >, boost::heap::arity< D >, boost::heap::stable< stable > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::MutablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::MutablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_moveable_heap_tests(); - run_reserve_heap_tests(); - run_mutable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); + run_reserve_heap_tests< pri_queue >(); + run_mutable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - run_ordered_iterator_tests(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::d_ary_heap, - boost::heap::arity, - boost::heap::stable - > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap:: + d_ary_heap< q_tester, boost::heap::mutable_< true >, boost::heap::arity< D >, boost::heap::stable< stable > > + stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( d_ary_heap_mutable_test ) { - run_d_ary_heap_mutable_test<2, false>(); - run_d_ary_heap_mutable_test<3, false>(); - run_d_ary_heap_mutable_test<4, false>(); - run_d_ary_heap_mutable_test<5, false>(); + run_d_ary_heap_mutable_test< 2, false >(); + run_d_ary_heap_mutable_test< 3, false >(); + run_d_ary_heap_mutable_test< 4, false >(); + run_d_ary_heap_mutable_test< 5, false >(); } BOOST_AUTO_TEST_CASE( d_ary_heap_mutable_stable_test ) { - run_d_ary_heap_mutable_test<2, true>(); - run_d_ary_heap_mutable_test<3, true>(); - run_d_ary_heap_mutable_test<4, true>(); - run_d_ary_heap_mutable_test<5, true>(); + run_d_ary_heap_mutable_test< 2, true >(); + run_d_ary_heap_mutable_test< 3, true >(); + run_d_ary_heap_mutable_test< 4, true >(); + run_d_ary_heap_mutable_test< 5, true >(); } BOOST_AUTO_TEST_CASE( d_ary_heap_compare_lookup_test ) { - typedef boost::heap::d_ary_heap, - boost::heap::compare, - boost::heap::allocator > > pri_queue; - run_common_heap_tests(); + typedef boost::heap::d_ary_heap< int, + boost::heap::arity< 2 >, + boost::heap::compare< less_with_T >, + boost::heap::allocator< std::allocator< int > > > + pri_queue; + run_common_heap_tests< pri_queue >(); } BOOST_AUTO_TEST_CASE( d_ary_heap_leak_test ) { - typedef boost::heap::d_ary_heap, boost::heap::arity<2> > pri_queue; - run_leak_check_test(); + typedef boost::heap::d_ary_heap< boost::shared_ptr< int >, boost::heap::arity< 2 > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/fibonacci_heap_test.cpp b/test/fibonacci_heap_test.cpp index 4fbf077..55caacd 100644 --- a/test/fibonacci_heap_test.cpp +++ b/test/fibonacci_heap_test.cpp @@ -14,63 +14,65 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" -#include "mutable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "mutable_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_fibonacci_heap_test(void) +template < bool stable, bool constant_time_size > +void run_fibonacci_heap_test( void ) { - typedef boost::heap::fibonacci_heap, - boost::heap::compare >, - boost::heap::allocator >, - boost::heap::constant_time_size - > pri_queue; + typedef boost::heap::fibonacci_heap< int, + boost::heap::stable< stable >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > >, + boost::heap::constant_time_size< constant_time_size > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::MutablePriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::MergablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::MutablePriorityQueue< pri_queue >)); + BOOST_CONCEPT_ASSERT( (boost::heap::MergablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - run_mutable_heap_tests(); - run_ordered_iterator_tests(); + run_mutable_heap_tests< pri_queue >(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::fibonacci_heap, - boost::heap::constant_time_size - > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap::fibonacci_heap< q_tester, + boost::heap::stable< stable >, + boost::heap::constant_time_size< constant_time_size > > + stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( fibonacci_heap_test ) { - run_fibonacci_heap_test(); - run_fibonacci_heap_test(); + run_fibonacci_heap_test< true, false >(); + run_fibonacci_heap_test< true, true >(); - run_fibonacci_heap_test(); - run_fibonacci_heap_test(); + run_fibonacci_heap_test< false, false >(); + run_fibonacci_heap_test< false, true >(); - RUN_EMPLACE_TEST(fibonacci_heap); + RUN_EMPLACE_TEST( fibonacci_heap ); } BOOST_AUTO_TEST_CASE( fibonacci_heap_compare_lookup_test ) { - typedef boost::heap::fibonacci_heap, - boost::heap::allocator > > pri_queue; - run_common_heap_tests(); + typedef boost::heap:: + fibonacci_heap< int, boost::heap::compare< less_with_T >, boost::heap::allocator< std::allocator< int > > > + pri_queue; + run_common_heap_tests< pri_queue >(); } BOOST_AUTO_TEST_CASE( fibonacci_heap_leak_test ) { - typedef boost::heap::fibonacci_heap > pri_queue; - run_leak_check_test(); + typedef boost::heap::fibonacci_heap< boost::shared_ptr< int > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/merge_heap_tests.hpp b/test/merge_heap_tests.hpp index 6d2a01b..9d9d18f 100644 --- a/test/merge_heap_tests.hpp +++ b/test/merge_heap_tests.hpp @@ -9,67 +9,63 @@ #include "common_heap_tests.hpp" #include -#define GENERATE_TEST_DATA(INDEX) \ - test_data data = make_test_data(test_size, 0, 1); \ - random_shuffle(data.begin(), data.end()); \ +#define GENERATE_TEST_DATA( INDEX ) \ + test_data data = make_test_data( test_size, 0, 1 ); \ + random_shuffle( data.begin(), data.end() ); \ \ - test_data data_q (data.begin(), data.begin() + INDEX); \ - test_data data_r (data.begin() + INDEX, data.end()); \ + test_data data_q( data.begin(), data.begin() + INDEX ); \ + test_data data_r( data.begin() + INDEX, data.end() ); \ \ - std::stable_sort(data.begin(), data.end()); + std::stable_sort( data.begin(), data.end() ); -template +template < typename pri_queue > struct pri_queue_test_merge { - static void run(void) + static void run( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q, r; - GENERATE_TEST_DATA(i); + GENERATE_TEST_DATA( i ); - fill_q(q, data_q); - fill_q(r, data_r); + fill_q( q, data_q ); + fill_q( r, data_r ); - q.merge(r); + q.merge( r ); - BOOST_REQUIRE(r.empty()); - check_q(q, data); + BOOST_REQUIRE( r.empty() ); + check_q( q, data ); } } }; -template +template < typename pri_queue1, typename pri_queue2 > struct pri_queue_test_heap_merge { - static void run (void) + static void run( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue1 q; pri_queue2 r; - GENERATE_TEST_DATA(i); + GENERATE_TEST_DATA( i ); - fill_q(q, data_q); - fill_q(r, data_r); + fill_q( q, data_q ); + fill_q( r, data_r ); - boost::heap::heap_merge(q, r); + boost::heap::heap_merge( q, r ); - BOOST_REQUIRE(r.empty()); - check_q(q, data); + BOOST_REQUIRE( r.empty() ); + check_q( q, data ); } } }; - -template -void run_merge_tests(void) +template < typename pri_queue > +void run_merge_tests( void ) { - boost::conditional, - dummy_run - >::type::run(); + boost::conditional< pri_queue::is_mergable, pri_queue_test_merge< pri_queue >, dummy_run >::type::run(); - pri_queue_test_heap_merge::run(); + pri_queue_test_heap_merge< pri_queue, pri_queue >::run(); } diff --git a/test/mutable_heap_test.cpp b/test/mutable_heap_test.cpp index 49cefbf..4b1d8be 100644 --- a/test/mutable_heap_test.cpp +++ b/test/mutable_heap_test.cpp @@ -9,44 +9,46 @@ #define BOOST_TEST_MAIN #include +#include #include #include #include -#include #include using namespace boost::heap; -#if BOOST_WORKAROUND(BOOST_MSVC, != 1800) -typedef fibonacci_heap::handle_type handle_type_1; -typedef d_ary_heap, mutable_ >::handle_type handle_type_2; -typedef pairing_heap::handle_type handle_type_3; -typedef binomial_heap::handle_type handle_type_4; -typedef skew_heap >::handle_type handle_type_5; +#if BOOST_WORKAROUND( BOOST_MSVC, != 1800 ) +typedef fibonacci_heap< struct fwd_declared_struct_1 >::handle_type handle_type_1; +typedef d_ary_heap< struct fwd_declared_struct_2, arity< 4 >, mutable_< true > >::handle_type handle_type_2; +typedef pairing_heap< struct fwd_declared_struct_3 >::handle_type handle_type_3; +typedef binomial_heap< struct fwd_declared_struct_4 >::handle_type handle_type_4; +typedef skew_heap< struct fwd_declared_struct_5, mutable_< true > >::handle_type handle_type_5; #endif -template -void run_handle_as_member_test(void) +template < typename HeapType > +void run_handle_as_member_test( void ) { typedef typename HeapType::value_type value_type; - HeapType heap; - value_type f(2); - typename value_type::handle_type handle = heap.push(f); - value_type & fInHeap = *handle; - fInHeap.handle = handle; + HeapType heap; + value_type f( 2 ); + typename value_type::handle_type handle = heap.push( f ); + value_type& fInHeap = *handle; + fInHeap.handle = handle; } struct fibonacci_heap_data { - typedef fibonacci_heap::handle_type handle_type; + typedef fibonacci_heap< fibonacci_heap_data >::handle_type handle_type; handle_type handle; - int i; + int i; - fibonacci_heap_data(int i):i(i) {} + fibonacci_heap_data( int i ) : + i( i ) + {} - bool operator<(fibonacci_heap_data const & rhs) const + bool operator<( fibonacci_heap_data const& rhs ) const { return i < rhs.i; } @@ -54,19 +56,21 @@ struct fibonacci_heap_data BOOST_AUTO_TEST_CASE( fibonacci_heap_handle_as_member ) { - run_handle_as_member_test >(); + run_handle_as_member_test< fibonacci_heap< fibonacci_heap_data > >(); } struct d_heap_data { - typedef d_ary_heap, mutable_ >::handle_type handle_type; + typedef d_ary_heap< d_heap_data, arity< 4 >, mutable_< true > >::handle_type handle_type; handle_type handle; - int i; + int i; - d_heap_data(int i):i(i) {} + d_heap_data( int i ) : + i( i ) + {} - bool operator<(d_heap_data const & rhs) const + bool operator<( d_heap_data const& rhs ) const { return i < rhs.i; } @@ -75,19 +79,21 @@ struct d_heap_data BOOST_AUTO_TEST_CASE( d_heap_handle_as_member ) { - run_handle_as_member_test, mutable_ > >(); + run_handle_as_member_test< d_ary_heap< d_heap_data, arity< 4 >, mutable_< true > > >(); } struct pairing_heap_data { - typedef pairing_heap::handle_type handle_type; + typedef pairing_heap< pairing_heap_data >::handle_type handle_type; handle_type handle; - int i; + int i; - pairing_heap_data(int i):i(i) {} + pairing_heap_data( int i ) : + i( i ) + {} - bool operator<(pairing_heap_data const & rhs) const + bool operator<( pairing_heap_data const& rhs ) const { return i < rhs.i; } @@ -96,41 +102,45 @@ struct pairing_heap_data BOOST_AUTO_TEST_CASE( pairing_heap_handle_as_member ) { - run_handle_as_member_test >(); + run_handle_as_member_test< pairing_heap< pairing_heap_data > >(); } struct binomial_heap_data { - typedef binomial_heap::handle_type handle_type; + typedef binomial_heap< binomial_heap_data >::handle_type handle_type; handle_type handle; - int i; + int i; - binomial_heap_data(int i):i(i) {} + binomial_heap_data( int i ) : + i( i ) + {} - bool operator<(binomial_heap_data const & rhs) const + bool operator<( binomial_heap_data const& rhs ) const { - return i < rhs.i; + return i < rhs.i; } }; BOOST_AUTO_TEST_CASE( binomial_heap_handle_as_member ) { - run_handle_as_member_test >(); + run_handle_as_member_test< binomial_heap< binomial_heap_data > >(); } struct skew_heap_data { - typedef skew_heap >::handle_type handle_type; + typedef skew_heap< skew_heap_data, mutable_< true > >::handle_type handle_type; handle_type handle; - int i; + int i; - skew_heap_data(int i):i(i) {} + skew_heap_data( int i ) : + i( i ) + {} - bool operator<(skew_heap_data const & rhs) const + bool operator<( skew_heap_data const& rhs ) const { return i < rhs.i; } @@ -139,5 +149,5 @@ struct skew_heap_data BOOST_AUTO_TEST_CASE( skew_heap_handle_as_member ) { - run_handle_as_member_test > >(); + run_handle_as_member_test< skew_heap< skew_heap_data, mutable_< true > > >(); } diff --git a/test/mutable_heap_tests.hpp b/test/mutable_heap_tests.hpp index 3df1d30..6459a46 100644 --- a/test/mutable_heap_tests.hpp +++ b/test/mutable_heap_tests.hpp @@ -7,319 +7,315 @@ =============================================================================*/ // random uses boost.fusion, which clashes with boost.test -//#define USE_BOOST_RANDOM +// #define USE_BOOST_RANDOM #ifdef USE_BOOST_RANDOM -#include +# include #else -#include +# include #endif #include "common_heap_tests.hpp" -#define PUSH_WITH_HANDLES(HANDLES, Q, DATA) \ - std::vector HANDLES; \ +#define PUSH_WITH_HANDLES( HANDLES, Q, DATA ) \ + std::vector< typename pri_queue::handle_type > HANDLES; \ \ - for (unsigned int k = 0; k != data.size(); ++k) \ - HANDLES.push_back(Q.push(DATA[k])); + for ( unsigned int k = 0; k != data.size(); ++k ) \ + HANDLES.push_back( Q.push( DATA[ k ] ) ); - -template -void pri_queue_test_update_decrease(void) +template < typename pri_queue > +void pri_queue_test_update_decrease( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - *handles[i] = -1; - data[i] = -1; - q.update(handles[i]); + *handles[ i ] = -1; + data[ i ] = -1; + q.update( handles[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_update_decrease_function(void) +template < typename pri_queue > +void pri_queue_test_update_decrease_function( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = -1; - q.update(handles[i], -1); + data[ i ] = -1; + q.update( handles[ i ], -1 ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_update_function_identity(void) +template < typename pri_queue > +void pri_queue_test_update_function_identity( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - q.update(handles[i], data[i]); + q.update( handles[ i ], data[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_update_shuffled(void) +template < typename pri_queue > +void pri_queue_test_update_shuffled( void ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - test_data shuffled (data); - random_shuffle(shuffled.begin(), shuffled.end()); + test_data shuffled( data ); + random_shuffle( shuffled.begin(), shuffled.end() ); - for (int i = 0; i != test_size; ++i) - q.update(handles[i], shuffled[i]); + for ( int i = 0; i != test_size; ++i ) + q.update( handles[ i ], shuffled[ i ] ); - check_q(q, data); + check_q( q, data ); } -template -void pri_queue_test_update_increase(void) +template < typename pri_queue > +void pri_queue_test_update_increase( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = data.back()+1; - *handles[i] = data[i]; - q.update(handles[i]); + data[ i ] = data.back() + 1; + *handles[ i ] = data[ i ]; + q.update( handles[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_update_increase_function(void) +template < typename pri_queue > +void pri_queue_test_update_increase_function( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = data.back()+1; - q.update(handles[i], data[i]); + data[ i ] = data.back() + 1; + q.update( handles[ i ], data[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_decrease(void) +template < typename pri_queue > +void pri_queue_test_decrease( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - *handles[i] = -1; - data[i] = -1; - q.decrease(handles[i]); + *handles[ i ] = -1; + data[ i ] = -1; + q.decrease( handles[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_decrease_function(void) +template < typename pri_queue > +void pri_queue_test_decrease_function( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = -1; - q.decrease(handles[i], -1); + data[ i ] = -1; + q.decrease( handles[ i ], -1 ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_decrease_function_identity(void) +template < typename pri_queue > +void pri_queue_test_decrease_function_identity( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - q.decrease(handles[i], data[i]); + q.decrease( handles[ i ], data[ i ] ); - check_q(q, data); + check_q( q, data ); } } -template -void pri_queue_test_increase(void) +template < typename pri_queue > +void pri_queue_test_increase( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = data.back()+1; - *handles[i] = data[i]; - q.increase(handles[i]); + data[ i ] = data.back() + 1; + *handles[ i ] = data[ i ]; + q.increase( handles[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_increase_function(void) +template < typename pri_queue > +void pri_queue_test_increase_function( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - data[i] = data.back()+1; - q.increase(handles[i], data[i]); + data[ i ] = data.back() + 1; + q.increase( handles[ i ], data[ i ] ); - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void pri_queue_test_increase_function_identity(void) +template < typename pri_queue > +void pri_queue_test_increase_function_identity( void ) { - for (int i = 0; i != test_size; ++i) { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - q.increase(handles[i], data[i]); + q.increase( handles[ i ], data[ i ] ); - check_q(q, data); + check_q( q, data ); } } -template -void pri_queue_test_erase(void) +template < typename pri_queue > +void pri_queue_test_erase( void ) { #ifdef USE_BOOST_RANDOM boost::mt19937 rng; #endif - for (int i = 0; i != test_size; ++i) - { + for ( int i = 0; i != test_size; ++i ) { pri_queue q; - test_data data = make_test_data(test_size); - PUSH_WITH_HANDLES(handles, q, data); + test_data data = make_test_data( test_size ); + PUSH_WITH_HANDLES( handles, q, data ); - for (int j = 0; j != i; ++j) - { + for ( int j = 0; j != i; ++j ) { #ifdef USE_BOOST_RANDOM - boost::uniform_int<> range(0, data.size() - 1); - boost::variate_generator > gen(rng, range); + boost::uniform_int<> range( 0, data.size() - 1 ); + boost::variate_generator< boost::mt19937&, boost::uniform_int<> > gen( rng, range ); int index = gen(); #else - int index = std::rand() % (data.size() - 1); + int index = std::rand() % ( data.size() - 1 ); #endif - q.erase(handles[index]); - handles.erase(handles.begin() + index); - data.erase(data.begin() + index); + q.erase( handles[ index ] ); + handles.erase( handles.begin() + index ); + data.erase( data.begin() + index ); } - std::sort(data.begin(), data.end()); - check_q(q, data); + std::sort( data.begin(), data.end() ); + check_q( q, data ); } } -template -void run_mutable_heap_update_tests(void) +template < typename pri_queue > +void run_mutable_heap_update_tests( void ) { - pri_queue_test_update_increase(); - pri_queue_test_update_decrease(); + pri_queue_test_update_increase< pri_queue >(); + pri_queue_test_update_decrease< pri_queue >(); - pri_queue_test_update_shuffled(); + pri_queue_test_update_shuffled< pri_queue >(); } -template -void run_mutable_heap_update_function_tests(void) +template < typename pri_queue > +void run_mutable_heap_update_function_tests( void ) { - pri_queue_test_update_increase_function(); - pri_queue_test_update_decrease_function(); - pri_queue_test_update_function_identity(); + pri_queue_test_update_increase_function< pri_queue >(); + pri_queue_test_update_decrease_function< pri_queue >(); + pri_queue_test_update_function_identity< pri_queue >(); } -template -void run_mutable_heap_decrease_tests(void) +template < typename pri_queue > +void run_mutable_heap_decrease_tests( void ) { - pri_queue_test_decrease(); - pri_queue_test_decrease_function(); - pri_queue_test_decrease_function_identity(); + pri_queue_test_decrease< pri_queue >(); + pri_queue_test_decrease_function< pri_queue >(); + pri_queue_test_decrease_function_identity< pri_queue >(); } -template -void run_mutable_heap_increase_tests(void) +template < typename pri_queue > +void run_mutable_heap_increase_tests( void ) { - pri_queue_test_increase(); - pri_queue_test_increase_function(); - pri_queue_test_increase_function_identity(); + pri_queue_test_increase< pri_queue >(); + pri_queue_test_increase_function< pri_queue >(); + pri_queue_test_increase_function_identity< pri_queue >(); } -template -void run_mutable_heap_erase_tests(void) +template < typename pri_queue > +void run_mutable_heap_erase_tests( void ) { - pri_queue_test_erase(); + pri_queue_test_erase< pri_queue >(); } -template -void run_mutable_heap_test_handle_from_iterator(void) +template < typename pri_queue > +void run_mutable_heap_test_handle_from_iterator( void ) { pri_queue que; - que.push(3); - que.push(1); - que.push(4); + que.push( 3 ); + que.push( 1 ); + que.push( 4 ); - que.update(pri_queue::s_handle_from_iterator(que.begin()), - 6); + que.update( pri_queue::s_handle_from_iterator( que.begin() ), 6 ); } -template -void run_mutable_heap_tests(void) +template < typename pri_queue > +void run_mutable_heap_tests( void ) { - run_mutable_heap_update_function_tests(); - run_mutable_heap_update_tests(); - run_mutable_heap_decrease_tests(); - run_mutable_heap_increase_tests(); - run_mutable_heap_erase_tests(); - run_mutable_heap_test_handle_from_iterator(); + run_mutable_heap_update_function_tests< pri_queue >(); + run_mutable_heap_update_tests< pri_queue >(); + run_mutable_heap_decrease_tests< pri_queue >(); + run_mutable_heap_increase_tests< pri_queue >(); + run_mutable_heap_erase_tests< pri_queue >(); + run_mutable_heap_test_handle_from_iterator< pri_queue >(); } -template +template < typename pri_queue > void run_ordered_iterator_tests() { - pri_queue_test_ordered_iterators(); + pri_queue_test_ordered_iterators< pri_queue >(); } diff --git a/test/pairing_heap_tests.cpp b/test/pairing_heap_tests.cpp index 250339d..33610fa 100644 --- a/test/pairing_heap_tests.cpp +++ b/test/pairing_heap_tests.cpp @@ -14,61 +14,64 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" -#include "mutable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "mutable_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_pairing_heap_test(void) +template < bool stable, bool constant_time_size > +void run_pairing_heap_test( void ) { - typedef boost::heap::pairing_heap, - boost::heap::compare >, - boost::heap::allocator >, - boost::heap::constant_time_size > pri_queue; + typedef boost::heap::pairing_heap< int, + boost::heap::stable< stable >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > >, + boost::heap::constant_time_size< constant_time_size > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::MutablePriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::MergablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::MutablePriorityQueue< pri_queue >)); + BOOST_CONCEPT_ASSERT( (boost::heap::MergablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - run_mutable_heap_tests(); + run_mutable_heap_tests< pri_queue >(); - run_ordered_iterator_tests(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::pairing_heap, - boost::heap::constant_time_size - > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap::pairing_heap< q_tester, + boost::heap::stable< stable >, + boost::heap::constant_time_size< constant_time_size > > + stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( pairing_heap_test ) { - run_pairing_heap_test(); - run_pairing_heap_test(); - run_pairing_heap_test(); - run_pairing_heap_test(); + run_pairing_heap_test< false, false >(); + run_pairing_heap_test< false, true >(); + run_pairing_heap_test< true, false >(); + run_pairing_heap_test< true, true >(); - RUN_EMPLACE_TEST(pairing_heap); + RUN_EMPLACE_TEST( pairing_heap ); } BOOST_AUTO_TEST_CASE( pairing_heap_compare_lookup_test ) { - typedef boost::heap::pairing_heap, - boost::heap::allocator > > pri_queue; - run_common_heap_tests(); + typedef boost::heap:: + pairing_heap< int, boost::heap::compare< less_with_T >, boost::heap::allocator< std::allocator< int > > > + pri_queue; + run_common_heap_tests< pri_queue >(); } BOOST_AUTO_TEST_CASE( pairing_heap_leak_test ) { - typedef boost::heap::pairing_heap > pri_queue; - run_leak_check_test(); + typedef boost::heap::pairing_heap< boost::shared_ptr< int > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/priority_queue_test.cpp b/test/priority_queue_test.cpp index d441186..2c66e46 100644 --- a/test/priority_queue_test.cpp +++ b/test/priority_queue_test.cpp @@ -12,36 +12,36 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_common_priority_queue_tests(void) +template < bool stable > +void run_common_priority_queue_tests( void ) { - typedef boost::heap::priority_queue > pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); + typedef boost::heap::priority_queue< int, boost::heap::stable< stable > > pri_queue; + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< pri_queue >)); - run_concept_check(); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); - run_merge_tests(); + run_concept_check< pri_queue >(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); + run_merge_tests< pri_queue >(); - if (stable) { - typedef boost::heap::priority_queue > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap::priority_queue< q_tester, boost::heap::stable< stable > > stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( std_pri_queue_test ) { - run_common_priority_queue_tests(); - run_common_priority_queue_tests(); + run_common_priority_queue_tests< false >(); + run_common_priority_queue_tests< true >(); } BOOST_AUTO_TEST_CASE( std_pri_queue_leak_test ) { - typedef boost::heap::priority_queue > pri_queue; - run_leak_check_test(); + typedef boost::heap::priority_queue< boost::shared_ptr< int > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/self_contained_header.cpp b/test/self_contained_header.cpp index bc8a5d7..4e0b8cd 100644 --- a/test/self_contained_header.cpp +++ b/test/self_contained_header.cpp @@ -9,14 +9,15 @@ * \author Andrey Semashev * \date 28.10.2018 * - * \brief This file contains a test boilerplate for checking that every public header is self-contained and does not have any missing #includes. + * \brief This file contains a test boilerplate for checking that every public header is self-contained and does not + * have any missing #includes. */ #define BOOST_HEAP_TEST_INCLUDE_HEADER() #include BOOST_HEAP_TEST_INCLUDE_HEADER() -int main(int, char*[]) +int main( int, char*[] ) { return 0; } diff --git a/test/skew_heap_test.cpp b/test/skew_heap_test.cpp index 8d25dd4..b807303 100644 --- a/test/skew_heap_test.cpp +++ b/test/skew_heap_test.cpp @@ -14,107 +14,112 @@ #include #include "common_heap_tests.hpp" -#include "stable_heap_tests.hpp" -#include "mutable_heap_tests.hpp" #include "merge_heap_tests.hpp" +#include "mutable_heap_tests.hpp" +#include "stable_heap_tests.hpp" -template -void run_skew_heap_test(void) +template < bool stable, bool constant_time_size, bool store_parent_pointer > +void run_skew_heap_test( void ) { - typedef boost::heap::skew_heap, - boost::heap::compare >, - boost::heap::allocator >, - boost::heap::constant_time_size, - boost::heap::store_parent_pointer - > pri_queue; + typedef boost::heap::skew_heap< int, + boost::heap::stable< stable >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > >, + boost::heap::constant_time_size< constant_time_size >, + boost::heap::store_parent_pointer< store_parent_pointer > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::MergablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::PriorityQueue< pri_queue >)); + BOOST_CONCEPT_ASSERT( (boost::heap::MergablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - pri_queue_test_ordered_iterators(); + pri_queue_test_ordered_iterators< pri_queue >(); - if (stable) { - typedef boost::heap::skew_heap, - boost::heap::constant_time_size, - boost::heap::store_parent_pointer - > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap::skew_heap< q_tester, + boost::heap::stable< stable >, + boost::heap::constant_time_size< constant_time_size >, + boost::heap::store_parent_pointer< store_parent_pointer > > + stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } -template -void run_skew_heap_mutable_test(void) +template < bool stable, bool constant_time_size > +void run_skew_heap_mutable_test( void ) { - typedef boost::heap::skew_heap, boost::heap::mutable_, - boost::heap::compare >, - boost::heap::allocator >, - boost::heap::constant_time_size - > pri_queue; + typedef boost::heap::skew_heap< int, + boost::heap::stable< stable >, + boost::heap::mutable_< true >, + boost::heap::compare< std::less< int > >, + boost::heap::allocator< std::allocator< int > >, + boost::heap::constant_time_size< constant_time_size > > + pri_queue; - BOOST_CONCEPT_ASSERT((boost::heap::MutablePriorityQueue)); - BOOST_CONCEPT_ASSERT((boost::heap::MergablePriorityQueue)); + BOOST_CONCEPT_ASSERT( (boost::heap::MutablePriorityQueue< pri_queue >)); + BOOST_CONCEPT_ASSERT( (boost::heap::MergablePriorityQueue< pri_queue >)); - run_common_heap_tests(); - run_iterator_heap_tests(); - run_copyable_heap_tests(); - run_moveable_heap_tests(); + run_common_heap_tests< pri_queue >(); + run_iterator_heap_tests< pri_queue >(); + run_copyable_heap_tests< pri_queue >(); + run_moveable_heap_tests< pri_queue >(); - run_merge_tests(); + run_merge_tests< pri_queue >(); - run_mutable_heap_tests(); + run_mutable_heap_tests< pri_queue >(); - run_ordered_iterator_tests(); + run_ordered_iterator_tests< pri_queue >(); - if (stable) { - typedef boost::heap::skew_heap, boost::heap::mutable_, - boost::heap::constant_time_size - > stable_pri_queue; - run_stable_heap_tests(); + if ( stable ) { + typedef boost::heap::skew_heap< q_tester, + boost::heap::stable< stable >, + boost::heap::mutable_< true >, + boost::heap::constant_time_size< constant_time_size > > + stable_pri_queue; + run_stable_heap_tests< stable_pri_queue >(); } } BOOST_AUTO_TEST_CASE( skew_heap_test ) { - run_skew_heap_test(); - run_skew_heap_test(); - run_skew_heap_test(); - run_skew_heap_test(); + run_skew_heap_test< false, false, true >(); + run_skew_heap_test< false, true, true >(); + run_skew_heap_test< true, false, true >(); + run_skew_heap_test< true, true, true >(); - run_skew_heap_test(); - run_skew_heap_test(); - run_skew_heap_test(); - run_skew_heap_test(); + run_skew_heap_test< false, false, false >(); + run_skew_heap_test< false, true, false >(); + run_skew_heap_test< true, false, false >(); + run_skew_heap_test< true, true, false >(); - RUN_EMPLACE_TEST(skew_heap); + RUN_EMPLACE_TEST( skew_heap ); } BOOST_AUTO_TEST_CASE( skew_heap_mutable_test ) { - run_skew_heap_mutable_test(); - run_skew_heap_mutable_test(); - run_skew_heap_mutable_test(); - run_skew_heap_mutable_test(); + run_skew_heap_mutable_test< false, false >(); + run_skew_heap_mutable_test< false, true >(); + run_skew_heap_mutable_test< true, false >(); + run_skew_heap_mutable_test< true, true >(); } BOOST_AUTO_TEST_CASE( skew_heap_compare_lookup_test ) { - typedef boost::heap::skew_heap, - boost::heap::allocator > > pri_queue; - run_common_heap_tests(); + typedef boost::heap::skew_heap< int, boost::heap::compare< less_with_T >, boost::heap::allocator< std::allocator< int > > > + pri_queue; + run_common_heap_tests< pri_queue >(); } BOOST_AUTO_TEST_CASE( skew_heap_leak_test ) { - typedef boost::heap::skew_heap > pri_queue; - run_leak_check_test(); + typedef boost::heap::skew_heap< boost::shared_ptr< int > > pri_queue; + run_leak_check_test< pri_queue >(); } diff --git a/test/stable_heap_tests.hpp b/test/stable_heap_tests.hpp index 0b4e799..4fc764f 100644 --- a/test/stable_heap_tests.hpp +++ b/test/stable_heap_tests.hpp @@ -6,94 +6,94 @@ http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ -#include #include "common_heap_tests.hpp" +#include struct q_tester { - q_tester(int i = 0, int j = 0): - value(i), id(j) + q_tester( int i = 0, int j = 0 ) : + value( i ), + id( j ) {} - bool operator< (q_tester const & rhs) const + bool operator<( q_tester const& rhs ) const { return value < rhs.value; } - bool operator> (q_tester const & rhs) const + bool operator>( q_tester const& rhs ) const { return value > rhs.value; } - bool operator== (q_tester const & rhs) const + bool operator==( q_tester const& rhs ) const { - return (value == rhs.value) && (id == rhs.id); + return ( value == rhs.value ) && ( id == rhs.id ); } int value; int id; }; -std::ostream& operator<< (std::ostream& out, q_tester const & t) +std::ostream& operator<<( std::ostream& out, q_tester const& t ) { out << "[" << t.value << " " << t.id << "]"; return out; } -typedef std::vector stable_test_data; +typedef std::vector< q_tester > stable_test_data; -stable_test_data make_stable_test_data(int size, int same_count = 3, - int offset = 0, int strive = 1) +stable_test_data make_stable_test_data( int size, int same_count = 3, int offset = 0, int strive = 1 ) { stable_test_data ret; - for (int i = 0; i != size; ++i) - for (int j = 0; j != same_count; ++j) - ret.push_back(q_tester(i * strive + offset, j)); + for ( int i = 0; i != size; ++i ) + for ( int j = 0; j != same_count; ++j ) + ret.push_back( q_tester( i * strive + offset, j ) ); return ret; } struct compare_by_id { - bool operator()(q_tester const & lhs, q_tester const & rhs) + bool operator()( q_tester const& lhs, q_tester const& rhs ) { return lhs.id > rhs.id; } }; -template -void pri_queue_stable_test_sequential_push(void) +template < typename pri_queue > +void pri_queue_stable_test_sequential_push( void ) { - stable_test_data data = make_stable_test_data(test_size); + stable_test_data data = make_stable_test_data( test_size ); pri_queue q; - fill_q(q, data); - std::stable_sort(data.begin(), data.end(), compare_by_id()); - std::stable_sort(data.begin(), data.end(), std::less()); - check_q(q, data); + fill_q( q, data ); + std::stable_sort( data.begin(), data.end(), compare_by_id() ); + std::stable_sort( data.begin(), data.end(), std::less< q_tester >() ); + check_q( q, data ); } -template -void pri_queue_stable_test_sequential_reverse_push(void) +template < typename pri_queue > +void pri_queue_stable_test_sequential_reverse_push( void ) { - stable_test_data data = make_stable_test_data(test_size); - pri_queue q; - stable_test_data push_data(data); + stable_test_data data = make_stable_test_data( test_size ); + pri_queue q; + stable_test_data push_data( data ); - std::stable_sort(push_data.begin(), push_data.end(), std::greater()); + std::stable_sort( push_data.begin(), push_data.end(), std::greater< q_tester >() ); - fill_q(q, push_data); + fill_q( q, push_data ); - std::stable_sort(data.begin(), data.end(), compare_by_id()); - std::stable_sort(data.begin(), data.end(), std::less()); + std::stable_sort( data.begin(), data.end(), compare_by_id() ); + std::stable_sort( data.begin(), data.end(), std::less< q_tester >() ); - check_q(q, data); + check_q( q, data ); } -template -void run_stable_heap_tests(void) +template < typename pri_queue > +void run_stable_heap_tests( void ) { - pri_queue_stable_test_sequential_push(); - pri_queue_stable_test_sequential_reverse_push(); + pri_queue_stable_test_sequential_push< pri_queue >(); + pri_queue_stable_test_sequential_reverse_push< pri_queue >(); } diff --git a/tools/heap_benchmarks.hpp b/tools/heap_benchmarks.hpp index 08bc657..8960be5 100644 --- a/tools/heap_benchmarks.hpp +++ b/tools/heap_benchmarks.hpp @@ -9,269 +9,269 @@ #include #include -#include #include "high_resolution_timer.hpp" +#include #include -#if defined(__GNUC__) && (!defined __INTEL_COMPILER) -#define no_inline __attribute__ ((noinline)) +#if defined( __GNUC__ ) && ( !defined __INTEL_COMPILER ) +# define no_inline __attribute__( ( noinline ) ) #else -#define no_inline +# define no_inline #endif -typedef std::vector test_data; +typedef std::vector< int > test_data; const int num_benchmarks = 1; -inline test_data make_sequential_test_data(int size) +inline test_data make_sequential_test_data( int size ) { - test_data v(size); - for (int i = 0; i != size; ++i) - v[i] = i; + test_data v( size ); + for ( int i = 0; i != size; ++i ) + v[ i ] = i; return v; } -inline test_data make_test_data(int size) +inline test_data make_test_data( int size ) { - test_data v = make_sequential_test_data(size); - std::random_shuffle(v.begin(), v.end()); + test_data v = make_sequential_test_data( size ); + std::random_shuffle( v.begin(), v.end() ); return v; } const int max_data = 20; -test_data const & get_data(int index) +test_data const& get_data( int index ) { - static std::vector data; - if (data.empty()) - for (int i = 0; i != num_benchmarks; ++i) - data.push_back(make_test_data((1<<(max_data+1))+1024)); + static std::vector< test_data > data; + if ( data.empty() ) + for ( int i = 0; i != num_benchmarks; ++i ) + data.push_back( make_test_data( ( 1 << ( max_data + 1 ) ) + 1024 ) ); - return data[index]; + return data[ index ]; } -#define DEFINE_BENCHMARKS_SELECTOR(SUFFIX) \ -struct make_##SUFFIX \ -{ \ - template \ - struct rebind { \ - typedef run_##SUFFIX type; \ - }; \ -}; +#define DEFINE_BENCHMARKS_SELECTOR( SUFFIX ) \ + struct make_##SUFFIX \ + { \ + template < typename heap > \ + struct rebind \ + { \ + typedef run_##SUFFIX< heap > type; \ + }; \ + }; -template -void fill_heap(pri_queue & q, int index, int size, int offset = 0) +template < typename pri_queue > +void fill_heap( pri_queue& q, int index, int size, int offset = 0 ) { - test_data const & data = get_data(index); + test_data const& data = get_data( index ); - for (int i = 0; i != size + 1; ++i) { - q.push(data[i]); + for ( int i = 0; i != size + 1; ++i ) { + q.push( data[ i ] ); int top = q.top(); q.pop(); - q.push(top); + q.push( top ); } } -template -void fill_heap_with_handles(pri_queue & q, handle_container & handles, int index, int size, int offset = 0) +template < typename pri_queue, typename handle_container > +void fill_heap_with_handles( pri_queue& q, handle_container& handles, int index, int size, int offset = 0 ) { - test_data const & data = get_data(index); + test_data const& data = get_data( index ); - for (int i = 0; i != size + 1; ++i) { - handles[i] = q.push(data[i]); + for ( int i = 0; i != size + 1; ++i ) { + handles[ i ] = q.push( data[ i ] ); - if (i > 0) { - typename pri_queue::handle_type last = handles[i-1]; - int val = *last; - q.erase(last); - handles[i-1] = q.push(val); + if ( i > 0 ) { + typename pri_queue::handle_type last = handles[ i - 1 ]; + int val = *last; + q.erase( last ); + handles[ i - 1 ] = q.push( val ); } } } -template +template < typename pri_queue > struct run_sequential_push { - run_sequential_push(int size): - size(size) + run_sequential_push( int size ) : + size( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); - fill_heap(q, index, size); + fill_heap( q, index, size ); } - no_inline void operator()(int index) + no_inline void operator()( int index ) { - test_data const & data = get_data(index); + test_data const& data = get_data( index ); - for (int i = 0; i != 16; ++i) - q.push(data[(1< +template < typename pri_queue > struct run_sequential_pop { - run_sequential_pop(int size): - size(size) + run_sequential_pop( int size ) : + size( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); - fill_heap(q, index, size); + fill_heap( q, index, size ); } - no_inline void operator()(int index) + no_inline void operator()( int index ) { - for (int i = 0; i != 16; ++i) + for ( int i = 0; i != 16; ++i ) q.pop(); } pri_queue q; - int size; + int size; }; -DEFINE_BENCHMARKS_SELECTOR(sequential_pop) +DEFINE_BENCHMARKS_SELECTOR( sequential_pop ) -template +template < typename pri_queue > struct run_sequential_increase { - run_sequential_increase(int size): - size(size), handles(size) + run_sequential_increase( int size ) : + size( size ), + handles( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); - fill_heap_with_handles(q, handles, index, size); + fill_heap_with_handles( q, handles, index, size ); } - no_inline void operator()(int index) + no_inline void operator()( int index ) { - test_data const & data = get_data(index); - for (int i = 0; i != 16; ++i) - q.increase(handles[i], data[i] + (1< handles; + pri_queue q; + int size; + std::vector< typename pri_queue::handle_type > handles; }; -DEFINE_BENCHMARKS_SELECTOR(sequential_increase) +DEFINE_BENCHMARKS_SELECTOR( sequential_increase ) -template +template < typename pri_queue > struct run_sequential_decrease { - run_sequential_decrease(int size): - size(size), handles(size) + run_sequential_decrease( int size ) : + size( size ), + handles( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); - fill_heap_with_handles(q, handles, index, size); + fill_heap_with_handles( q, handles, index, size ); } - no_inline void operator()(int index) + no_inline void operator()( int index ) { - test_data const & data = get_data(index); - for (int i = 0; i != 16; ++i) - q.decrease(handles[i], data[i] + (1< handles; + pri_queue q; + int size; + std::vector< typename pri_queue::handle_type > handles; }; -DEFINE_BENCHMARKS_SELECTOR(sequential_decrease) +DEFINE_BENCHMARKS_SELECTOR( sequential_decrease ) -template +template < typename pri_queue > struct run_merge_and_clear { - run_merge_and_clear(int size): - size(size) + run_merge_and_clear( int size ) : + size( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); r.clear(); - fill_heap(q, index, size); - fill_heap(r, index, size, size); + fill_heap( q, index, size ); + fill_heap( r, index, size, size ); } - no_inline void operator()(int index) + no_inline void operator()( int index ) { - boost::heap::heap_merge(q, r); + boost::heap::heap_merge( q, r ); } pri_queue q, r; - int size; + int size; }; -DEFINE_BENCHMARKS_SELECTOR(merge_and_clear) +DEFINE_BENCHMARKS_SELECTOR( merge_and_clear ) -template +template < typename pri_queue > struct run_equivalence { - run_equivalence(int size): - size(size) + run_equivalence( int size ) : + size( size ) {} - void prepare(int index) + void prepare( int index ) { q.clear(); r.clear(); s.clear(); - fill_heap(q, index, size); - fill_heap(r, index, size, size); - fill_heap(s, index, size); + fill_heap( q, index, size ); + fill_heap( r, index, size, size ); + fill_heap( s, index, size ); } - no_inline bool operator()(int index) + no_inline bool operator()( int index ) { - return (q == r) ^ (q == s); + return ( q == r ) ^ ( q == s ); } pri_queue q, r, s; - int size; + int size; }; -DEFINE_BENCHMARKS_SELECTOR(equivalence) +DEFINE_BENCHMARKS_SELECTOR( equivalence ) -template -inline double run_benchmark(benchmark & b) +template < typename benchmark > +inline double run_benchmark( benchmark& b ) { boost::high_resolution_timer timer; - std::vector results; + std::vector< double > results; - for (int i = 0; i != num_benchmarks; ++i) - { - b.prepare(i); + for ( int i = 0; i != num_benchmarks; ++i ) { + b.prepare( i ); timer.restart(); - b(i); + b( i ); double t = timer.elapsed(); - results.push_back(t); + results.push_back( t ); } - return results.at(results.size()/2); // median + return results.at( results.size() / 2 ); // median } diff --git a/tools/high_resolution_timer.hpp b/tools/high_resolution_timer.hpp index 9893b24..de7fc3c 100644 --- a/tools/high_resolution_timer.hpp +++ b/tools/high_resolution_timer.hpp @@ -6,18 +6,18 @@ (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ -#if !defined(BOOST_HIGH_RESOLUTION_TIMER_HPP) -#define BOOST_HIGH_RESOLUTION_TIMER_HPP +#if !defined( BOOST_HIGH_RESOLUTION_TIMER_HPP ) +# define BOOST_HIGH_RESOLUTION_TIMER_HPP -#include -#include +# include +# include -#if _POSIX_C_SOURCE >= 199309L +# if _POSIX_C_SOURCE >= 199309L -#include "time.h" +# include "time.h" -#include -#include +# include +# include namespace boost { @@ -31,28 +31,27 @@ public: void restart() { - int status = clock_gettime(CLOCK_REALTIME, &start_time); + int status = clock_gettime( CLOCK_REALTIME, &start_time ); - if (status == -1) - boost::throw_exception(std::runtime_error("Couldn't initialize start_time")); + if ( status == -1 ) + boost::throw_exception( std::runtime_error( "Couldn't initialize start_time" ) ); } - double elapsed() const // return elapsed time in seconds + double elapsed() const // return elapsed time in seconds { struct timespec now; - int status = clock_gettime(CLOCK_REALTIME, &now); + int status = clock_gettime( CLOCK_REALTIME, &now ); - if (status == -1) - boost::throw_exception(std::runtime_error("Couldn't get current time")); + if ( status == -1 ) + boost::throw_exception( std::runtime_error( "Couldn't get current time" ) ); struct timespec diff; - double ret_sec = double(now.tv_sec - start_time.tv_sec); - double ret_nsec = double(now.tv_nsec - start_time.tv_nsec); + double ret_sec = double( now.tv_sec - start_time.tv_sec ); + double ret_nsec = double( now.tv_nsec - start_time.tv_nsec ); - while (ret_nsec < 0) - { + while ( ret_nsec < 0 ) { ret_sec -= 1.0; ret_nsec += 1e9; } @@ -62,12 +61,12 @@ public: return ret; } - double elapsed_max() const // return estimated maximum value for elapsed() + double elapsed_max() const // return estimated maximum value for elapsed() { - return double((std::numeric_limits::max)()); + return double( ( std::numeric_limits< double >::max )() ); } - double elapsed_min() const // return minimum value for elapsed() + double elapsed_min() const // return minimum value for elapsed() { return 0.0; } @@ -78,9 +77,9 @@ private: } // namespace boost -#elif defined(__APPLE__) +# elif defined( __APPLE__ ) -#import +# import namespace boost { @@ -88,15 +87,15 @@ namespace boost { class high_resolution_timer { public: - high_resolution_timer(void) + high_resolution_timer( void ) { mach_timebase_info_data_t info; - kern_return_t err = mach_timebase_info(&info); - if (err) - throw std::runtime_error("cannot create mach timebase info"); + kern_return_t err = mach_timebase_info( &info ); + if ( err ) + throw std::runtime_error( "cannot create mach timebase info" ); - conversion_factor = (double)info.numer/(double)info.denom; + conversion_factor = (double)info.numer / (double)info.denom; restart(); } @@ -105,36 +104,36 @@ public: start = mach_absolute_time(); } - double elapsed() const // return elapsed time in seconds + double elapsed() const // return elapsed time in seconds { - uint64_t now = mach_absolute_time(); - double duration = double(now - start) * conversion_factor; + uint64_t now = mach_absolute_time(); + double duration = double( now - start ) * conversion_factor; return duration } - double elapsed_max() const // return estimated maximum value for elapsed() + double elapsed_max() const // return estimated maximum value for elapsed() { - return double((std::numeric_limits::max)()); + return double( ( std::numeric_limits< double >::max )() ); } - double elapsed_min() const // return minimum value for elapsed() + double elapsed_min() const // return minimum value for elapsed() { return 0.0; } private: uint64_t start; - double conversion_factor; + double conversion_factor; }; } // namespace boost -#elif defined(BOOST_WINDOWS) +# elif defined( BOOST_WINDOWS ) -#include -#include -#include +# include +# include +# include namespace boost { @@ -151,36 +150,36 @@ public: high_resolution_timer() { start_time.QuadPart = 0; - frequency.QuadPart = 0; + frequency.QuadPart = 0; - if (!QueryPerformanceFrequency(&frequency)) - boost::throw_exception(std::runtime_error("Couldn't acquire frequency")); + if ( !QueryPerformanceFrequency( &frequency ) ) + boost::throw_exception( std::runtime_error( "Couldn't acquire frequency" ) ); restart(); } void restart() { - if (!QueryPerformanceCounter(&start_time)) - boost::throw_exception(std::runtime_error("Couldn't initialize start_time")); + if ( !QueryPerformanceCounter( &start_time ) ) + boost::throw_exception( std::runtime_error( "Couldn't initialize start_time" ) ); } - double elapsed() const // return elapsed time in seconds + double elapsed() const // return elapsed time in seconds { LARGE_INTEGER now; - if (!QueryPerformanceCounter(&now)) - boost::throw_exception(std::runtime_error("Couldn't get current time")); + if ( !QueryPerformanceCounter( &now ) ) + boost::throw_exception( std::runtime_error( "Couldn't get current time" ) ); - return double(now.QuadPart - start_time.QuadPart) / frequency.QuadPart; + return double( now.QuadPart - start_time.QuadPart ) / frequency.QuadPart; } - double elapsed_max() const // return estimated maximum value for elapsed() + double elapsed_max() const // return estimated maximum value for elapsed() { - return (double((std::numeric_limits::max)()) - - double(start_time.QuadPart)) / double(frequency.QuadPart); + return ( double( ( std::numeric_limits< LONGLONG >::max )() ) - double( start_time.QuadPart ) ) + / double( frequency.QuadPart ); } - double elapsed_min() const // return minimum value for elapsed() + double elapsed_min() const // return minimum value for elapsed() { return 1.0 / frequency.QuadPart; } @@ -192,17 +191,16 @@ private: } // namespace boost -#else +# else // For other platforms, simply fall back to boost::timer -#include -#include +# include +# include namespace boost { - typedef boost::timer high_resolution_timer; -} +typedef boost::timer high_resolution_timer; +} // namespace boost -#endif - -#endif // !defined(BOOST_HIGH_RESOLUTION_TIMER_HPP) +# endif +#endif // !defined(BOOST_HIGH_RESOLUTION_TIMER_HPP) diff --git a/tools/throughput_benchmarks.cpp b/tools/throughput_benchmarks.cpp index ea3f5b0..919d940 100644 --- a/tools/throughput_benchmarks.cpp +++ b/tools/throughput_benchmarks.cpp @@ -6,132 +6,124 @@ http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ -#include #include +#include -#include "../../../boost/heap/d_ary_heap.hpp" -#include "../../../boost/heap/pairing_heap.hpp" -#include "../../../boost/heap/fibonacci_heap.hpp" #include "../../../boost/heap/binomial_heap.hpp" +#include "../../../boost/heap/d_ary_heap.hpp" +#include "../../../boost/heap/fibonacci_heap.hpp" +#include "../../../boost/heap/pairing_heap.hpp" #include "../../../boost/heap/skew_heap.hpp" #include "heap_benchmarks.hpp" using namespace std; -template -void run_benchmarks_immutable(void) +template < typename benchmark_selector > +void run_benchmarks_immutable( void ) { - for (int i = 4; i != max_data; ++i) { - for (int j = 0; j != 8; ++j) { - int size = 1<= 4) - size += (1<<(i-1)); + for ( int i = 4; i != max_data; ++i ) { + for ( int j = 0; j != 8; ++j ) { + int size = 1 << i; + if ( j % 4 == 1 ) + size += 1 << ( i - 3 ); + if ( j % 4 == 2 ) + size += 1 << ( i - 2 ); + if ( j % 4 == 3 ) + size += ( 1 << ( i - 3 ) ) + ( 1 << ( i - 2 ) ); + if ( j >= 4 ) + size += ( 1 << ( i - 1 ) ); cout << size << "\t"; { - typedef typename benchmark_selector:: - template rebind > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 2 > > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 2 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 4 > > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 4 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 8 > > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 8 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::binomial_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::fibonacci_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::pairing_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef + typename benchmark_selector::template rebind< boost::heap::skew_heap< long > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef + typename benchmark_selector::template rebind< boost::heap::skew_heap< long > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } cout << endl; @@ -139,84 +131,78 @@ void run_benchmarks_immutable(void) } } -template -void run_benchmarks_mutable(void) +template < typename benchmark_selector > +void run_benchmarks_mutable( void ) { - for (int i = 4; i != max_data; ++i) - { - for (int j = 0; j != 8; ++j) - { - int size = 1<= 4) - size += (1<<(i-1)); + for ( int i = 4; i != max_data; ++i ) { + for ( int j = 0; j != 8; ++j ) { + int size = 1 << i; + if ( j % 4 == 1 ) + size += 1 << ( i - 3 ); + if ( j % 4 == 2 ) + size += 1 << ( i - 2 ); + if ( j % 4 == 3 ) + size += ( 1 << ( i - 3 ) ) + ( 1 << ( i - 2 ) ); + if ( j >= 4 ) + size += ( 1 << ( i - 1 ) ); cout << size << "\t"; { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 2 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 4 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind, boost::heap::mutable_ > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::d_ary_heap< long, boost::heap::arity< 8 >, boost::heap::mutable_< true > > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::binomial_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::fibonacci_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< boost::heap::pairing_heap< long > >::type + benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } { - typedef typename benchmark_selector:: - template rebind > > - ::type benchmark_functor; - benchmark_functor benchmark(size); - double result = run_benchmark(benchmark); + typedef typename benchmark_selector::template rebind< + boost::heap::skew_heap< long, boost::heap::mutable_< true > > >::type benchmark_functor; + benchmark_functor benchmark( size ); + double result = run_benchmark( benchmark ); cout << result << '\t'; } cout << endl; @@ -226,23 +212,23 @@ void run_benchmarks_mutable(void) int main() { - cout << fixed << setprecision(12); + cout << fixed << setprecision( 12 ); cout << "sequential push" << endl; - run_benchmarks_immutable(); + run_benchmarks_immutable< make_sequential_push >(); cout << endl << "sequential pop" << endl; - run_benchmarks_immutable(); + run_benchmarks_immutable< make_sequential_pop >(); cout << endl << "sequential increase" << endl; - run_benchmarks_mutable(); + run_benchmarks_mutable< make_sequential_increase >(); cout << endl << "sequential decrease" << endl; - run_benchmarks_mutable(); + run_benchmarks_mutable< make_sequential_decrease >(); cout << endl << "merge_and_clear" << endl; - run_benchmarks_immutable(); + run_benchmarks_immutable< make_merge_and_clear >(); cout << endl << "equivalence" << endl; - run_benchmarks_immutable(); + run_benchmarks_immutable< make_equivalence >(); }