File indexing completed on 2025-12-16 10:27:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef RANGES_V3_ALGORITHM_MINMAX_HPP
0015 #define RANGES_V3_ALGORITHM_MINMAX_HPP
0016
0017 #include <initializer_list>
0018
0019 #include <range/v3/range_fwd.hpp>
0020
0021 #include <range/v3/algorithm/result_types.hpp>
0022 #include <range/v3/functional/comparisons.hpp>
0023 #include <range/v3/functional/identity.hpp>
0024 #include <range/v3/functional/invoke.hpp>
0025 #include <range/v3/iterator/concepts.hpp>
0026 #include <range/v3/iterator/traits.hpp>
0027 #include <range/v3/range/access.hpp>
0028 #include <range/v3/range/concepts.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
0037
0038 template<typename T>
0039 using minmax_result = detail::min_max_result<T, T>;
0040
0041 RANGES_FUNC_BEGIN(minmax)
0042
0043
0044 template(typename T, typename C = less, typename P = identity)(
0045 requires indirect_strict_weak_order<C, projected<T const *, P>>)
0046 constexpr minmax_result<T const &> RANGES_FUNC(minmax)(
0047 T const & a, T const & b, C pred = C{}, P proj = P{})
0048 {
0049 using R = minmax_result<T const &>;
0050 return invoke(pred, invoke(proj, b), invoke(proj, a)) ? R{b, a} : R{a, b};
0051 }
0052
0053
0054 template(typename Rng, typename C = less, typename P = identity)(
0055 requires input_range<Rng> AND
0056 indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> AND
0057 indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
0058 constexpr minmax_result<range_value_t<Rng>>
0059 RANGES_FUNC(minmax)(Rng && rng, C pred = C{}, P proj = P{})
0060 {
0061 using R = minmax_result<range_value_t<Rng>>;
0062 auto first = ranges::begin(rng);
0063 auto last = ranges::end(rng);
0064 RANGES_EXPECT(first != last);
0065 auto result = R{*first, *first};
0066 if(++first != last)
0067 {
0068 {
0069 auto && tmp = *first;
0070 if(invoke(pred, invoke(proj, tmp), invoke(proj, result.min)))
0071 result.min = (decltype(tmp) &&)tmp;
0072 else
0073 result.max = (decltype(tmp) &&)tmp;
0074 }
0075 while(++first != last)
0076 {
0077 range_value_t<Rng> tmp1 = *first;
0078 if(++first == last)
0079 {
0080 if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
0081 result.min = std::move(tmp1);
0082 else if(!invoke(
0083 pred, invoke(proj, tmp1), invoke(proj, result.max)))
0084 result.max = std::move(tmp1);
0085 break;
0086 }
0087
0088 auto && tmp2 = *first;
0089 if(invoke(pred, invoke(proj, tmp2), invoke(proj, tmp1)))
0090 {
0091 if(invoke(pred, invoke(proj, tmp2), invoke(proj, result.min)))
0092 result.min = (decltype(tmp2) &&)tmp2;
0093 if(!invoke(pred, invoke(proj, tmp1), invoke(proj, result.max)))
0094 result.max = std::move(tmp1);
0095 }
0096 else
0097 {
0098 if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
0099 result.min = std::move(tmp1);
0100 if(!invoke(pred, invoke(proj, tmp2), invoke(proj, result.max)))
0101 result.max = (decltype(tmp2) &&)tmp2;
0102 }
0103 }
0104 }
0105 return result;
0106 }
0107
0108
0109 template(typename T, typename C = less, typename P = identity)(
0110 requires copyable<T> AND
0111 indirect_strict_weak_order<C, projected<T const *, P>>)
0112 constexpr minmax_result<T> RANGES_FUNC(minmax)(
0113 std::initializer_list<T> const && rng, C pred = C{}, P proj = P{})
0114 {
0115 return (*this)(rng, std::move(pred), std::move(proj));
0116 }
0117
0118 RANGES_FUNC_END(minmax)
0119
0120 namespace cpp20
0121 {
0122 using ranges::minmax;
0123 using ranges::minmax_result;
0124 }
0125
0126 }
0127
0128 #include <range/v3/detail/epilogue.hpp>
0129
0130 #endif