diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 52d187f..8e06101 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v6.0.0 hooks: - id: check-yaml - id: end-of-file-fixer @@ -11,7 +11,7 @@ repos: - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v17.0.6 + rev: v22.1.3 hooks: - id: clang-format types_or: [c++, c, cuda] diff --git a/include/boost/heap/binomial_heap.hpp b/include/boost/heap/binomial_heap.hpp index 873007a..87798e4 100644 --- a/include/boost/heap/binomial_heap.hpp +++ b/include/boost/heap/binomial_heap.hpp @@ -242,6 +242,8 @@ class binomial_heap : /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) binomial_heap& operator=( binomial_heap const& rhs ) { + if ( this == &rhs ) + return *this; clear(); size_holder::set_size( rhs.get_size() ); static_cast< super_t& >( *this ) = rhs; @@ -400,7 +402,7 @@ class binomial_heap : size_holder::decrement(); if ( element->child_count() ) { - size_type sz = ( 1 << element->child_count() ) - 1; + size_type sz = ( size_type( 1 ) << element->child_count() ) - 1; binomial_heap children( value_comp(), element->children, sz ); if ( trees.empty() ) { @@ -543,7 +545,7 @@ class binomial_heap : rhs.set_size( 0 ); rhs.top_element = nullptr; - super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + super_t::set_stability_count( (std::max)( super_t::get_stability_count(), rhs.get_stability_count() ) ); rhs.set_stability_count( 0 ); } diff --git a/include/boost/heap/d_ary_heap.hpp b/include/boost/heap/d_ary_heap.hpp index dff1f9c..470e91e 100644 --- a/include/boost/heap/d_ary_heap.hpp +++ b/include/boost/heap/d_ary_heap.hpp @@ -345,7 +345,7 @@ class d_ary_heap : private make_heap_base< T, BoundArgs, false >::type 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_type last_index = (std::min)( first_index + D - 1, size() - 1 ); return last_index; } diff --git a/include/boost/heap/detail/ilog2.hpp b/include/boost/heap/detail/ilog2.hpp index 3dc4f1e..2870d04 100644 --- a/include/boost/heap/detail/ilog2.hpp +++ b/include/boost/heap/detail/ilog2.hpp @@ -56,6 +56,6 @@ IntType log2( IntType value ) return fn( value ); } -}} // namespace boost::heap +}} // 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 cccfb47..c5d309c 100644 --- a/include/boost/heap/detail/mutable_heap.hpp +++ b/include/boost/heap/detail/mutable_heap.hpp @@ -354,7 +354,9 @@ class priority_queue_mutable_wrapper template < class... Args > handle_type emplace( Args&&... args ) { - objects.push_front( std::make_pair( std::forward< Args >( args )..., 0 ) ); + objects.emplace_front( std::piecewise_construct, + std::forward_as_tuple( std::forward< Args >( args )... ), + std::forward_as_tuple( 0 ) ); list_iterator ret = objects.begin(); q_.push( ret ); return handle_type( ret ); @@ -520,6 +522,6 @@ class priority_queue_mutable_wrapper }; -}}} // namespace boost::heap::detail +}}} // 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 4e88bc0..d84b127 100644 --- a/include/boost/heap/detail/ordered_adaptor_iterator.hpp +++ b/include/boost/heap/detail/ordered_adaptor_iterator.hpp @@ -131,6 +131,6 @@ class ordered_adaptor_iterator : }; -}}} // namespace boost::heap::detail +}}} // 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 25ff88e..2272068 100644 --- a/include/boost/heap/detail/stable_heap.hpp +++ b/include/boost/heap/detail/stable_heap.hpp @@ -380,18 +380,18 @@ struct heap_base< T, Cmp, constant_time_size, StabilityCounterType, true > : internal_type make_node( T const& val ) { - stability_counter_type count = ++counter_; - if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() ) + if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() - 1 ) BOOST_THROW_EXCEPTION( std::runtime_error( "boost::heap counter overflow" ) ); + stability_counter_type count = ++counter_; return internal_type( count, val ); } template < class... Args > internal_type make_node( Args&&... args ) { - stability_counter_type count = ++counter_; - if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() ) + if ( counter_ == ( std::numeric_limits< stability_counter_type >::max )() - 1 ) BOOST_THROW_EXCEPTION( std::runtime_error( "boost::heap counter overflow" ) ); + stability_counter_type count = ++counter_; return internal_type( count, std::forward< Args >( args )... ); } @@ -563,6 +563,6 @@ struct extract_allocator_types }; -}}} // namespace boost::heap::detail +}}} // 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 2988161..aadbdba 100644 --- a/include/boost/heap/detail/tree_iterator.hpp +++ b/include/boost/heap/detail/tree_iterator.hpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -343,6 +344,6 @@ class recursive_tree_iterator : }; -}}} // namespace boost::heap::detail +}}} // 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 7ca9ae6..7b37790 100644 --- a/include/boost/heap/fibonacci_heap.hpp +++ b/include/boost/heap/fibonacci_heap.hpp @@ -260,6 +260,8 @@ class fibonacci_heap : /// \copydoc boost::heap::priority_queue::operator=(priority_queue const &) fibonacci_heap& operator=( fibonacci_heap const& rhs ) { + if ( this == &rhs ) + return *this; clear(); size_holder::set_size( rhs.size() ); static_cast< super_t& >( *this ) = rhs; @@ -591,7 +593,7 @@ class fibonacci_heap : rhs.top_element = nullptr; rhs.set_size( 0 ); - super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + super_t::set_stability_count( (std::max)( super_t::get_stability_count(), rhs.get_stability_count() ) ); rhs.set_stability_count( 0 ); } @@ -699,8 +701,13 @@ class fibonacci_heap : if ( roots.empty() ) return; - static const size_type max_log2 = sizeof( size_type ) * 8; - std::array< node_pointer, max_log2 > aux {}; + // The maximum degree of any node in a Fibonacci heap with N elements is + // floor(log_phi(N)) where phi = (1+sqrt(5))/2 ~ 1.618. Since + // log_phi(N) < 1.4405 * log2(N), a safe upper bound on the degree for a + // size_type of B bits is ceil(1.4405 * B) + 2. We compute a conservative + // compile-time constant that is always large enough. + constexpr size_type max_degree = sizeof( size_type ) * 12 + 4; + std::array< node_pointer, max_degree > aux {}; node_list_iterator it = roots.begin(); top_element = static_cast< node_pointer >( &*it ); @@ -709,6 +716,7 @@ class fibonacci_heap : node_pointer n = static_cast< node_pointer >( &*it ); ++it; size_type node_rank = n->child_count(); + BOOST_ASSERT( node_rank < max_degree ); if ( aux[ node_rank ] == nullptr ) aux[ node_rank ] = n; @@ -729,6 +737,7 @@ class fibonacci_heap : aux[ node_rank ] = nullptr; node_rank = n->child_count(); + BOOST_ASSERT( node_rank < max_degree ); } while ( aux[ node_rank ] != nullptr ); aux[ node_rank ] = n; } diff --git a/include/boost/heap/heap_concepts.hpp b/include/boost/heap/heap_concepts.hpp index d088074..502ee26 100644 --- a/include/boost/heap/heap_concepts.hpp +++ b/include/boost/heap/heap_concepts.hpp @@ -101,6 +101,6 @@ struct MutablePriorityQueue : PriorityQueue< C > bool equal, not_equal; }; -}} // namespace boost::heap +}} // 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 0c8c7d0..e38626d 100644 --- a/include/boost/heap/heap_merge.hpp +++ b/include/boost/heap/heap_merge.hpp @@ -61,7 +61,7 @@ struct heap_merge_emulate rhs.pop(); } - lhs.set_stability_count( ( std::max )( lhs.get_stability_count(), rhs.get_stability_count() ) ); + lhs.set_stability_count( (std::max)( lhs.get_stability_count(), rhs.get_stability_count() ) ); rhs.set_stability_count( 0 ); } }; @@ -118,6 +118,6 @@ void heap_merge( Heap1& lhs, Heap2& rhs ) } -}} // namespace boost::heap +}} // 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 96319c8..e52db84 100644 --- a/include/boost/heap/pairing_heap.hpp +++ b/include/boost/heap/pairing_heap.hpp @@ -267,6 +267,8 @@ class pairing_heap : /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs) pairing_heap& operator=( pairing_heap const& rhs ) { + if ( this == &rhs ) + return *this; clear(); size_holder::set_size( rhs.get_size() ); static_cast< super_t& >( *this ) = rhs; @@ -585,7 +587,7 @@ class pairing_heap : rhs.set_size( 0 ); rhs.root = nullptr; - super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + super_t::set_stability_count( (std::max)( super_t::get_stability_count(), rhs.get_stability_count() ) ); rhs.set_stability_count( 0 ); } diff --git a/include/boost/heap/policies.hpp b/include/boost/heap/policies.hpp index a92edfe..5779276 100644 --- a/include/boost/heap/policies.hpp +++ b/include/boost/heap/policies.hpp @@ -180,6 +180,6 @@ struct arity {}; #endif -}} // namespace boost::heap +}} // 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 25be7da..b27577d 100644 --- a/include/boost/heap/priority_queue.hpp +++ b/include/boost/heap/priority_queue.hpp @@ -409,6 +409,6 @@ class priority_queue : } }; -}} // namespace boost::heap +}} // 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 1b67834..1ca5a2d 100644 --- a/include/boost/heap/skew_heap.hpp +++ b/include/boost/heap/skew_heap.hpp @@ -371,6 +371,8 @@ class skew_heap : /// \copydoc boost::heap::priority_queue::operator=(priority_queue const & rhs) skew_heap& operator=( skew_heap const& rhs ) { + if ( this == &rhs ) + return *this; clear(); size_holder::set_size( rhs.get_size() ); static_cast< super_t& >( *this ) = rhs; @@ -554,7 +556,7 @@ class skew_heap : rhs.root = nullptr; sanity_check(); - super_t::set_stability_count( ( std::max )( super_t::get_stability_count(), rhs.get_stability_count() ) ); + super_t::set_stability_count( (std::max)( super_t::get_stability_count(), rhs.get_stability_count() ) ); rhs.set_stability_count( 0 ); } diff --git a/test/common_heap_tests.hpp b/test/common_heap_tests.hpp index ff9ed33..f17f782 100644 --- a/test/common_heap_tests.hpp +++ b/test/common_heap_tests.hpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include