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