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