xref: /llvm-project/libcxx/include/__algorithm/ranges_minmax.h (revision 0a0d58f5465a2524ec9ed10ed0bb277cab5a180c)
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 <__concepts/same_as.h>
17 #include <__config>
18 #include <__functional/identity.h>
19 #include <__functional/invoke.h>
20 #include <__functional/ranges_operations.h>
21 #include <__iterator/concepts.h>
22 #include <__iterator/next.h>
23 #include <__iterator/projected.h>
24 #include <__ranges/access.h>
25 #include <__ranges/concepts.h>
26 #include <__utility/forward.h>
27 #include <__utility/move.h>
28 #include <__utility/pair.h>
29 #include <initializer_list>
30 
31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32 #  pragma GCC system_header
33 #endif
34 
35 #if _LIBCPP_STD_VER > 17
36 
37 _LIBCPP_PUSH_MACROS
38 #include <__undef_macros>
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 namespace ranges {
43 template <class _T1>
44 using minmax_result = min_max_result<_T1>;
45 
46 namespace __minmax {
47 struct __fn {
48   template <class _Type, class _Proj = identity,
49             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
50   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&>
51   operator()(_LIBCPP_LIFETIMEBOUND const _Type& __a,
52              _LIBCPP_LIFETIMEBOUND const _Type& __b,
53              _Comp __comp = {},
54              _Proj __proj = {}) const {
55     if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)))
56       return {__b, __a};
57     return {__a, __b};
58   }
59 
60   template <copyable _Type, class _Proj = identity,
61             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
62   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
63   ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const {
64     _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list has to contain at least one element");
65     auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
66     return ranges::minmax_result<_Type> { *__iters.first, *__iters.second };
67   }
68 
69   template <input_range _Range, class _Proj = identity,
70             indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
71     requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
72   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
73   ranges::minmax_result<range_value_t<_Range>> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
74     auto __first = ranges::begin(__r);
75     auto __last = ranges::end(__r);
76     using _ValueT = range_value_t<_Range>;
77 
78     _LIBCPP_ASSERT(__first != __last, "range has to contain at least one element");
79 
80     if constexpr (forward_range<_Range>) {
81       // Special-case the one element case. Avoid repeatedly initializing objects from the result of an iterator
82       // dereference when doing so might not be idempotent. The `if constexpr` avoids the extra branch in cases where
83       // it's not needed.
84       if constexpr (!same_as<remove_cvref_t<range_reference_t<_Range>>, _ValueT> ||
85                     is_rvalue_reference_v<range_reference_t<_Range>>) {
86         if (ranges::next(__first) == __last) {
87           // During initialization, members are allowed to refer to already initialized members
88           // (see http://eel.is/c++draft/dcl.init.aggr#6)
89           minmax_result<_ValueT> __result = {*__first, __result.min};
90           return __result;
91         }
92       }
93       auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
94       return {*__result.first, *__result.second};
95     } else {
96       // input_iterators can't be copied, so the implementation for input_iterators has to store
97       // the values instead of a pointer to the correct values
98       auto __less = [&](auto&& __a, auto&& __b) -> bool {
99         return std::invoke(__comp, std::invoke(__proj, std::forward<decltype(__a)>(__a)),
100                                    std::invoke(__proj, std::forward<decltype(__b)>(__b)));
101       };
102 
103       // During initialization, members are allowed to refer to already initialized members
104       // (see http://eel.is/c++draft/dcl.init.aggr#6)
105       ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
106       if (__first == __last || ++__first == __last)
107         return __result;
108 
109       if (__less(*__first, __result.min))
110         __result.min = *__first;
111       else
112         __result.max = *__first;
113 
114       while (++__first != __last) {
115         _ValueT __i = *__first;
116         if (++__first == __last) {
117           if (__less(__i, __result.min))
118             __result.min = __i;
119           else if (!__less(__i, __result.max))
120             __result.max = __i;
121           return __result;
122         }
123 
124         if (__less(*__first, __i)) {
125           if (__less(*__first, __result.min))
126             __result.min = *__first;
127           if (!__less(__i, __result.max))
128             __result.max = std::move(__i);
129         } else {
130           if (__less(__i, __result.min))
131             __result.min = std::move(__i);
132           if (!__less(*__first, __result.max))
133             __result.max = *__first;
134         }
135       }
136       return __result;
137     }
138   }
139 };
140 } // namespace __minmax
141 
142 inline namespace __cpo {
143   inline constexpr auto minmax = __minmax::__fn{};
144 } // namespace __cpo
145 } // namespace ranges
146 
147 _LIBCPP_END_NAMESPACE_STD
148 
149 _LIBCPP_POP_MACROS
150 
151 #endif // _LIBCPP_STD_VER > 17
152 
153 #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H
154