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 2014-present
0005 //  Copyright Casey Carter 2016
0006 //
0007 //  Use, modification and distribution is subject to the
0008 //  Boost Software License, Version 1.0. (See accompanying
0009 //  file LICENSE_1_0.txt or copy at
0010 //  http://www.boost.org/LICENSE_1_0.txt)
0011 //
0012 // Project home: https://github.com/ericniebler/range-v3
0013 //
0014 //===-------------------------- algorithm ---------------------------------===//
0015 //
0016 //                     The LLVM Compiler Infrastructure
0017 //
0018 // This file is dual licensed under the MIT and the University of Illinois Open
0019 // Source Licenses. See LICENSE.TXT for details.
0020 //
0021 //===----------------------------------------------------------------------===//
0022 #ifndef RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
0023 #define RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
0024 
0025 #include <meta/meta.hpp>
0026 
0027 #include <range/v3/range_fwd.hpp>
0028 
0029 #include <range/v3/algorithm/aux_/partition_point_n.hpp>
0030 #include <range/v3/functional/identity.hpp>
0031 #include <range/v3/functional/invoke.hpp>
0032 #include <range/v3/iterator/concepts.hpp>
0033 #include <range/v3/iterator/operations.hpp>
0034 #include <range/v3/iterator/traits.hpp>
0035 #include <range/v3/range/access.hpp>
0036 #include <range/v3/range/concepts.hpp>
0037 #include <range/v3/range/dangling.hpp>
0038 #include <range/v3/range/traits.hpp>
0039 #include <range/v3/utility/static_const.hpp>
0040 
0041 #include <range/v3/detail/prologue.hpp>
0042 
0043 namespace ranges
0044 {
0045     /// \addtogroup group-algorithms
0046     /// @{
0047 
0048     RANGES_FUNC_BEGIN(partition_point)
0049 
0050         /// \brief function template \c partition_point
0051         template(typename I, typename S, typename C, typename P = identity)(
0052             requires forward_iterator<I> AND sentinel_for<S, I> AND
0053             indirect_unary_predicate<C, projected<I, P>>)
0054         constexpr I RANGES_FUNC(partition_point)(I first, S last, C pred, P proj = P{})
0055         {
0056             if(RANGES_CONSTEXPR_IF(sized_sentinel_for<S, I>))
0057             {
0058                 auto len = distance(first, std::move(last));
0059                 return aux::partition_point_n(
0060                     std::move(first), len, std::move(pred), std::move(proj));
0061             }
0062 
0063             // Probe exponentially for either last-of-range or an iterator
0064             // that is past the partition point (i.e., does not satisfy pred).
0065             auto len = iter_difference_t<I>{1};
0066             while(true)
0067             {
0068                 auto mid = first;
0069                 auto d = advance(mid, len, last);
0070                 if(mid == last || !invoke(pred, invoke(proj, *mid)))
0071                 {
0072                     len -= d;
0073                     return aux::partition_point_n(
0074                         std::move(first), len, ranges::ref(pred), ranges::ref(proj));
0075                 }
0076                 first = std::move(mid);
0077                 len *= 2;
0078             }
0079         }
0080 
0081         /// \overload
0082         template(typename Rng, typename C, typename P = identity)(
0083             requires forward_range<Rng> AND
0084             indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
0085         constexpr borrowed_iterator_t<Rng> //
0086         RANGES_FUNC(partition_point)(Rng && rng, C pred, P proj = P{}) //
0087         {
0088             if(RANGES_CONSTEXPR_IF(sized_range<Rng>))
0089             {
0090                 auto len = distance(rng);
0091                 return aux::partition_point_n(
0092                     begin(rng), len, std::move(pred), std::move(proj));
0093             }
0094             return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
0095         }
0096 
0097     RANGES_FUNC_END(partition_point)
0098 
0099     namespace cpp20
0100     {
0101         using ranges::partition_point;
0102     }
0103     /// @}
0104 } // namespace ranges
0105 
0106 #include <range/v3/detail/epilogue.hpp>
0107 
0108 #endif