Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:27:54

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2013-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 #ifndef RANGES_V3_ALGORITHM_PARTIAL_SORT_HPP
0014 #define RANGES_V3_ALGORITHM_PARTIAL_SORT_HPP
0015 
0016 #include <functional>
0017 
0018 #include <range/v3/range_fwd.hpp>
0019 
0020 #include <range/v3/algorithm/heap_algorithm.hpp>
0021 #include <range/v3/functional/comparisons.hpp>
0022 #include <range/v3/functional/identity.hpp>
0023 #include <range/v3/functional/invoke.hpp>
0024 #include <range/v3/iterator/concepts.hpp>
0025 #include <range/v3/iterator/traits.hpp>
0026 #include <range/v3/range/access.hpp>
0027 #include <range/v3/range/concepts.hpp>
0028 #include <range/v3/range/dangling.hpp>
0029 #include <range/v3/range/traits.hpp>
0030 #include <range/v3/utility/static_const.hpp>
0031 
0032 #include <range/v3/detail/prologue.hpp>
0033 
0034 namespace ranges
0035 {
0036     /// \addtogroup group-algorithms
0037     /// @{
0038     RANGES_FUNC_BEGIN(partial_sort)
0039 
0040         /// \brief function template \c partial_sort
0041         template(typename I, typename S, typename C = less, typename P = identity)(
0042             requires sortable<I, C, P> AND random_access_iterator<I> AND
0043                 sentinel_for<S, I>)
0044         constexpr I RANGES_FUNC(partial_sort)(
0045             I first, I middle, S last, C pred = C{}, P proj = P{}) //
0046         {
0047             make_heap(first, middle, ranges::ref(pred), ranges::ref(proj));
0048             auto const len = middle - first;
0049             I i = middle;
0050             for(; i != last; ++i)
0051             {
0052                 if(invoke(pred, invoke(proj, *i), invoke(proj, *first)))
0053                 {
0054                     iter_swap(i, first);
0055                     detail::sift_down_n(
0056                         first, len, first, ranges::ref(pred), ranges::ref(proj));
0057                 }
0058             }
0059             sort_heap(first, middle, ranges::ref(pred), ranges::ref(proj));
0060             return i;
0061         }
0062 
0063         /// \overload
0064         template(typename Rng, typename C = less, typename P = identity)(
0065             requires sortable<iterator_t<Rng>, C, P> AND random_access_range<Rng>)
0066         constexpr borrowed_iterator_t<Rng> RANGES_FUNC(partial_sort)(
0067             Rng && rng, iterator_t<Rng> middle, C pred = C{}, P proj = P{}) //
0068         {
0069             return (*this)(begin(rng),
0070                            std::move(middle),
0071                            end(rng),
0072                            std::move(pred),
0073                            std::move(proj));
0074         }
0075 
0076     RANGES_FUNC_END(partial_sort)
0077 
0078     namespace cpp20
0079     {
0080         using ranges::partial_sort;
0081     }
0082     /// @}
0083 } // namespace ranges
0084 
0085 #include <range/v3/detail/epilogue.hpp>
0086 
0087 #endif