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