xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_max.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_MAX_H
1081ad6265SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_MAX_H
1181ad6265SDimitry Andric 
1281ad6265SDimitry Andric #include <__algorithm/ranges_min_element.h>
1381ad6265SDimitry Andric #include <__assert>
1481ad6265SDimitry Andric #include <__concepts/copyable.h>
1581ad6265SDimitry Andric #include <__config>
1681ad6265SDimitry Andric #include <__functional/identity.h>
1781ad6265SDimitry Andric #include <__functional/invoke.h>
1881ad6265SDimitry Andric #include <__functional/ranges_operations.h>
1981ad6265SDimitry Andric #include <__iterator/concepts.h>
2081ad6265SDimitry Andric #include <__iterator/projected.h>
2181ad6265SDimitry Andric #include <__ranges/access.h>
2281ad6265SDimitry Andric #include <__ranges/concepts.h>
2306c3fb27SDimitry Andric #include <__type_traits/is_trivially_copyable.h>
2481ad6265SDimitry Andric #include <__utility/move.h>
2581ad6265SDimitry Andric #include <initializer_list>
2681ad6265SDimitry Andric 
2781ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2881ad6265SDimitry Andric #  pragma GCC system_header
2981ad6265SDimitry Andric #endif
3081ad6265SDimitry Andric 
3106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3281ad6265SDimitry Andric 
3381ad6265SDimitry Andric _LIBCPP_PUSH_MACROS
3481ad6265SDimitry Andric #  include <__undef_macros>
3581ad6265SDimitry Andric 
3681ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3781ad6265SDimitry Andric 
3881ad6265SDimitry Andric namespace ranges {
3981ad6265SDimitry Andric namespace __max {
4081ad6265SDimitry Andric struct __fn {
4106c3fb27SDimitry Andric   template <class _Tp,
4206c3fb27SDimitry Andric             class _Proj                                                    = identity,
4381ad6265SDimitry Andric             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
44*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
4506c3fb27SDimitry Andric   operator()(_LIBCPP_LIFETIMEBOUND const _Tp& __a,
4606c3fb27SDimitry Andric              _LIBCPP_LIFETIMEBOUND const _Tp& __b,
4706c3fb27SDimitry Andric              _Comp __comp = {},
4806c3fb27SDimitry Andric              _Proj __proj = {}) const {
4981ad6265SDimitry Andric     return std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b)) ? __b : __a;
5081ad6265SDimitry Andric   }
5181ad6265SDimitry Andric 
5206c3fb27SDimitry Andric   template <copyable _Tp,
5306c3fb27SDimitry Andric             class _Proj                                                    = identity,
5481ad6265SDimitry Andric             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
55*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp
5606c3fb27SDimitry Andric   operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
57cb14a3feSDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
58cb14a3feSDimitry Andric         __il.begin() != __il.end(), "initializer_list must contain at least one element");
5981ad6265SDimitry Andric 
605f757f3fSDimitry Andric     auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { return std::invoke(__comp, __rhs, __lhs); };
6181ad6265SDimitry Andric     return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
6281ad6265SDimitry Andric   }
6381ad6265SDimitry Andric 
6406c3fb27SDimitry Andric   template <input_range _Rp,
6506c3fb27SDimitry Andric             class _Proj                                                         = identity,
6681ad6265SDimitry Andric             indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
6781ad6265SDimitry Andric     requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
68*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_value_t<_Rp>
6906c3fb27SDimitry Andric   operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
7081ad6265SDimitry Andric     auto __first = ranges::begin(__r);
7181ad6265SDimitry Andric     auto __last  = ranges::end(__r);
7281ad6265SDimitry Andric 
73cb14a3feSDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element");
7481ad6265SDimitry Andric 
7506c3fb27SDimitry Andric     if constexpr (forward_range<_Rp> && !__is_cheap_to_copy<range_value_t<_Rp>>) {
765f757f3fSDimitry Andric       auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool {
775f757f3fSDimitry Andric         return std::invoke(__comp, __rhs, __lhs);
785f757f3fSDimitry Andric       };
7981ad6265SDimitry Andric       return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
8081ad6265SDimitry Andric     } else {
8181ad6265SDimitry Andric       range_value_t<_Rp> __result = *__first;
8281ad6265SDimitry Andric       while (++__first != __last) {
8381ad6265SDimitry Andric         if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
8481ad6265SDimitry Andric           __result = *__first;
8581ad6265SDimitry Andric       }
8681ad6265SDimitry Andric       return __result;
8781ad6265SDimitry Andric     }
8881ad6265SDimitry Andric   }
8981ad6265SDimitry Andric };
9081ad6265SDimitry Andric } // namespace __max
9181ad6265SDimitry Andric 
9281ad6265SDimitry Andric inline namespace __cpo {
9381ad6265SDimitry Andric inline constexpr auto max = __max::__fn{};
9481ad6265SDimitry Andric } // namespace __cpo
9581ad6265SDimitry Andric } // namespace ranges
9681ad6265SDimitry Andric 
9781ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
9881ad6265SDimitry Andric 
9981ad6265SDimitry Andric _LIBCPP_POP_MACROS
10081ad6265SDimitry Andric 
101*0fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
10281ad6265SDimitry Andric 
10381ad6265SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_MAX_H
104