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 <__utility/pair.h> 27 #include <initializer_list> 28 29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30 # pragma GCC system_header 31 #endif 32 33 #if _LIBCPP_STD_VER > 17 34 35 _LIBCPP_PUSH_MACROS 36 #include <__undef_macros> 37 38 _LIBCPP_BEGIN_NAMESPACE_STD 39 40 namespace ranges { 41 template <class _T1> 42 using minmax_result = min_max_result<_T1>; 43 44 namespace __minmax { 45 struct __fn { 46 template <class _Type, class _Proj = identity, 47 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less> 48 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&> 49 operator()(const _Type& __a, const _Type& __b, _Comp __comp = {}, _Proj __proj = {}) const { 50 if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a))) 51 return {__b, __a}; 52 return {__a, __b}; 53 } 54 55 template <copyable _Type, class _Proj = identity, 56 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less> 57 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr 58 ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const { 59 _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list has to contain at least one element"); 60 auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj); 61 return ranges::minmax_result<_Type> { *__iters.first, *__iters.second }; 62 } 63 64 template <input_range _Range, class _Proj = identity, 65 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less> 66 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*> 67 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr 68 ranges::minmax_result<range_value_t<_Range>> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const { 69 auto __first = ranges::begin(__r); 70 auto __last = ranges::end(__r); 71 using _ValueT = range_value_t<_Range>; 72 73 _LIBCPP_ASSERT(__first != __last, "range has to contain at least one element"); 74 75 if constexpr (forward_range<_Range>) { 76 auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj); 77 return {*__result.first, *__result.second}; 78 } else { 79 // input_iterators can't be copied, so the implementation for input_iterators has to store 80 // the values instead of a pointer to the correct values 81 auto __less = [&](auto&& __a, auto&& __b) -> bool { 82 return std::invoke(__comp, std::invoke(__proj, std::forward<decltype(__a)>(__a)), 83 std::invoke(__proj, std::forward<decltype(__b)>(__b))); 84 }; 85 86 ranges::minmax_result<_ValueT> __result = {*__first, __result.min}; 87 if (__first == __last || ++__first == __last) 88 return __result; 89 90 if (__less(*__first, __result.min)) 91 __result.min = *__first; 92 else 93 __result.max = *__first; 94 95 while (++__first != __last) { 96 _ValueT __i = *__first; 97 if (++__first == __last) { 98 if (__less(__i, __result.min)) 99 __result.min = __i; 100 else if (!__less(__i, __result.max)) 101 __result.max = __i; 102 return __result; 103 } 104 105 if (__less(*__first, __i)) { 106 if (__less(*__first, __result.min)) 107 __result.min = *__first; 108 if (!__less(__i, __result.max)) 109 __result.max = std::move(__i); 110 } else { 111 if (__less(__i, __result.min)) 112 __result.min = std::move(__i); 113 if (!__less(*__first, __result.max)) 114 __result.max = *__first; 115 } 116 } 117 return __result; 118 } 119 } 120 }; 121 } // namespace __minmax 122 123 inline namespace __cpo { 124 inline constexpr auto minmax = __minmax::__fn{}; 125 } // namespace __cpo 126 } // namespace ranges 127 128 _LIBCPP_END_NAMESPACE_STD 129 130 _LIBCPP_POP_MACROS 131 132 #endif // _LIBCPP_STD_VER > 17 133 134 #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H 135