1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_MINMAX_H
10 #define _LIBCPP___ALGORITHM_RANGES_MINMAX_H
12 #include <__algorithm/min_max_result.h>
13 #include <__algorithm/minmax_element.h>
15 #include <__concepts/copyable.h>
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>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
32 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
35 #include <__undef_macros>
37 _LIBCPP_BEGIN_NAMESPACE_STD
41 using minmax_result = min_max_result<_T1>;
45 template <class _Type, class _Proj = identity,
46 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
47 _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&>
48 operator()(const _Type& __a, const _Type& __b, _Comp __comp = {}, _Proj __proj = {}) const {
49 if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)))
54 template <copyable _Type, class _Proj = identity,
55 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
56 _LIBCPP_HIDE_FROM_ABI constexpr
57 ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const {
58 _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list has to contain at least one element");
59 auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
60 return ranges::minmax_result<_Type> { *__iters.first, *__iters.second };
63 template <input_range _Range, class _Proj = identity,
64 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
65 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
66 _LIBCPP_HIDE_FROM_ABI constexpr
67 ranges::minmax_result<range_value_t<_Range>> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
68 auto __first = ranges::begin(__r);
69 auto __last = ranges::end(__r);
70 using _ValueT = range_value_t<_Range>;
72 _LIBCPP_ASSERT(__first != __last, "range has to contain at least one element");
74 if constexpr (forward_range<_Range>) {
75 auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
76 return {*__result.first, *__result.second};
78 // input_iterators can't be copied, so the implementation for input_iterators has to store
79 // the values instead of a pointer to the correct values
80 auto __less = [&](auto&& __a, auto&& __b) -> bool {
81 return std::invoke(__comp, std::invoke(__proj, std::forward<decltype(__a)>(__a)),
82 std::invoke(__proj, std::forward<decltype(__b)>(__b)));
85 ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
86 if (__first == __last || ++__first == __last)
89 if (__less(*__first, __result.min))
90 __result.min = *__first;
92 __result.max = *__first;
94 while (++__first != __last) {
95 _ValueT __i = *__first;
96 if (++__first == __last) {
97 if (__less(__i, __result.min))
99 else if (!__less(__i, __result.max))
104 if (__less(*__first, __i)) {
105 if (__less(*__first, __result.min))
106 __result.min = *__first;
107 if (!__less(__i, __result.max))
108 __result.max = std::move(__i);
110 if (__less(__i, __result.min))
111 __result.min = std::move(__i);
112 if (!__less(*__first, __result.max))
113 __result.max = *__first;
120 } // namespace __minmax
122 inline namespace __cpo {
123 inline constexpr auto minmax = __minmax::__fn{};
125 } // namespace ranges
127 _LIBCPP_END_NAMESPACE_STD
131 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
133 #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H