1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_RANGES_MINMAX_H 10 #define _LIBCPP___ALGORITHM_RANGES_MINMAX_H 11 12 #include <__algorithm/min_max_result.h> 13 #include <__algorithm/minmax_element.h> 14 #include <__assert> 15 #include <__concepts/copyable.h> 16 #include <__config> 17 #include <__functional/identity.h> 18 #include <__functional/invoke.h> 19 #include <__functional/ranges_operations.h> 20 #include <__iterator/concepts.h> 21 #include <__iterator/projected.h> 22 #include <__ranges/access.h> 23 #include <__ranges/concepts.h> 24 #include <__utility/forward.h> 25 #include <__utility/move.h> 26 #include <initializer_list> 27 28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 29 # pragma GCC system_header 30 #endif 31 32 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 33 34 _LIBCPP_PUSH_MACROS 35 #include <__undef_macros> 36 37 _LIBCPP_BEGIN_NAMESPACE_STD 38 39 namespace ranges { 40 template <class _T1> 41 using minmax_result = min_max_result<_T1>; 42 43 namespace __minmax { 44 struct __fn { 45 template <class _Type, class _Proj = identity, 46 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less> 47 _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&> 48 operator()(const _Type& __a, const _Type& __b, _Comp __comp = {}, _Proj __proj = {}) const { 49 if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a))) 50 return {__b, __a}; 51 return {__a, __b}; 52 } 53 54 template <copyable _Type, class _Proj = identity, 55 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less> 56 _LIBCPP_HIDE_FROM_ABI constexpr 57 ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const { 58 _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list has to contain at least one element"); 59 auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj); 60 return ranges::minmax_result<_Type> { *__iters.first, *__iters.second }; 61 } 62 63 template <input_range _Range, class _Proj = identity, 64 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less> 65 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*> 66 _LIBCPP_HIDE_FROM_ABI constexpr 67 ranges::minmax_result<range_value_t<_Range>> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const { 68 auto __first = ranges::begin(__r); 69 auto __last = ranges::end(__r); 70 using _ValueT = range_value_t<_Range>; 71 72 _LIBCPP_ASSERT(__first != __last, "range has to contain at least one element"); 73 74 if constexpr (forward_range<_Range>) { 75 auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj); 76 return {*__result.first, *__result.second}; 77 } else { 78 // input_iterators can't be copied, so the implementation for input_iterators has to store 79 // the values instead of a pointer to the correct values 80 auto __less = [&](auto&& __a, auto&& __b) -> bool { 81 return std::invoke(__comp, std::invoke(__proj, std::forward<decltype(__a)>(__a)), 82 std::invoke(__proj, std::forward<decltype(__b)>(__b))); 83 }; 84 85 ranges::minmax_result<_ValueT> __result = {*__first, __result.min}; 86 if (__first == __last || ++__first == __last) 87 return __result; 88 89 if (__less(*__first, __result.min)) 90 __result.min = *__first; 91 else 92 __result.max = *__first; 93 94 while (++__first != __last) { 95 _ValueT __i = *__first; 96 if (++__first == __last) { 97 if (__less(__i, __result.min)) 98 __result.min = __i; 99 else if (!__less(__i, __result.max)) 100 __result.max = __i; 101 return __result; 102 } 103 104 if (__less(*__first, __i)) { 105 if (__less(*__first, __result.min)) 106 __result.min = *__first; 107 if (!__less(__i, __result.max)) 108 __result.max = std::move(__i); 109 } else { 110 if (__less(__i, __result.min)) 111 __result.min = std::move(__i); 112 if (!__less(*__first, __result.max)) 113 __result.max = *__first; 114 } 115 } 116 return __result; 117 } 118 } 119 }; 120 } // namespace __minmax 121 122 inline namespace __cpo { 123 inline constexpr auto minmax = __minmax::__fn{}; 124 } // namespace __cpo 125 } // namespace ranges 126 127 _LIBCPP_END_NAMESPACE_STD 128 129 _LIBCPP_POP_MACROS 130 131 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 132 133 #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H 134