File indexing completed on 2024-11-15 09:54:39
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef RANGES_V3_ALGORITHM_MINMAX_ELEMENT_HPP
0017 #define RANGES_V3_ALGORITHM_MINMAX_ELEMENT_HPP
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/dangling.hpp>
0030 #include <range/v3/range/traits.hpp>
0031 #include <range/v3/utility/static_const.hpp>
0032
0033 #include <range/v3/detail/prologue.hpp>
0034
0035 namespace ranges
0036 {
0037
0038
0039 template<typename I>
0040 using minmax_element_result = detail::min_max_result<I, I>;
0041
0042 RANGES_FUNC_BEGIN(minmax_element)
0043
0044
0045 template(typename I, typename S, typename C = less, typename P = identity)(
0046 requires forward_iterator<I> AND sentinel_for<S, I> AND
0047 indirect_strict_weak_order<C, projected<I, P>>)
0048 constexpr minmax_element_result<I>
0049 RANGES_FUNC(minmax_element)(I first, S last, C pred = C{}, P proj = P{})
0050 {
0051 minmax_element_result<I> result{first, first};
0052 if(first == last || ++first == last)
0053 return result;
0054 if(invoke(pred, invoke(proj, *first), invoke(proj, *result.min)))
0055 result.min = first;
0056 else
0057 result.max = first;
0058 while(++first != last)
0059 {
0060 I tmp = first;
0061 if(++first == last)
0062 {
0063 if(invoke(pred, invoke(proj, *tmp), invoke(proj, *result.min)))
0064 result.min = tmp;
0065 else if(!invoke(pred, invoke(proj, *tmp), invoke(proj, *result.max)))
0066 result.max = tmp;
0067 break;
0068 }
0069 else
0070 {
0071 if(invoke(pred, invoke(proj, *first), invoke(proj, *tmp)))
0072 {
0073 if(invoke(pred, invoke(proj, *first), invoke(proj, *result.min)))
0074 result.min = first;
0075 if(!invoke(pred, invoke(proj, *tmp), invoke(proj, *result.max)))
0076 result.max = tmp;
0077 }
0078 else
0079 {
0080 if(invoke(pred, invoke(proj, *tmp), invoke(proj, *result.min)))
0081 result.min = tmp;
0082 if(!invoke(pred, invoke(proj, *first), invoke(proj, *result.max)))
0083 result.max = first;
0084 }
0085 }
0086 }
0087 return result;
0088 }
0089
0090
0091 template(typename Rng, typename C = less, typename P = identity)(
0092 requires forward_range<Rng> AND
0093 indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>>)
0094 constexpr minmax_element_result<borrowed_iterator_t<Rng>>
0095 RANGES_FUNC(minmax_element)(Rng && rng, C pred = C{}, P proj = P{})
0096 {
0097 return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
0098 }
0099
0100 RANGES_FUNC_END(minmax_element)
0101
0102 namespace cpp20
0103 {
0104 using ranges::minmax_element;
0105 using ranges::minmax_element_result;
0106 }
0107
0108 }
0109
0110 #include <range/v3/detail/epilogue.hpp>
0111
0112 #endif