Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:20

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_MAX_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_MAX_H
0011 
0012 #include <__cxx03/__algorithm/ranges_min_element.h>
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__concepts/copyable.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__functional/identity.h>
0017 #include <__cxx03/__functional/invoke.h>
0018 #include <__cxx03/__functional/ranges_operations.h>
0019 #include <__cxx03/__iterator/concepts.h>
0020 #include <__cxx03/__iterator/projected.h>
0021 #include <__cxx03/__ranges/access.h>
0022 #include <__cxx03/__ranges/concepts.h>
0023 #include <__cxx03/__type_traits/is_trivially_copyable.h>
0024 #include <__cxx03/__utility/move.h>
0025 #include <__cxx03/initializer_list>
0026 
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 #  pragma GCC system_header
0029 #endif
0030 
0031 #if _LIBCPP_STD_VER >= 20
0032 
0033 _LIBCPP_PUSH_MACROS
0034 #  include <__cxx03/__undef_macros>
0035 
0036 _LIBCPP_BEGIN_NAMESPACE_STD
0037 
0038 namespace ranges {
0039 namespace __max {
0040 struct __fn {
0041   template <class _Tp,
0042             class _Proj                                                    = identity,
0043             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
0044   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
0045   operator()(_LIBCPP_LIFETIMEBOUND const _Tp& __a,
0046              _LIBCPP_LIFETIMEBOUND const _Tp& __b,
0047              _Comp __comp = {},
0048              _Proj __proj = {}) const {
0049     return std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b)) ? __b : __a;
0050   }
0051 
0052   template <copyable _Tp,
0053             class _Proj                                                    = identity,
0054             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
0055   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp
0056   operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
0057     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0058         __il.begin() != __il.end(), "initializer_list must contain at least one element");
0059 
0060     auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { return std::invoke(__comp, __rhs, __lhs); };
0061     return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
0062   }
0063 
0064   template <input_range _Rp,
0065             class _Proj                                                         = identity,
0066             indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
0067     requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
0068   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_value_t<_Rp>
0069   operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
0070     auto __first = ranges::begin(__r);
0071     auto __last  = ranges::end(__r);
0072 
0073     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element");
0074 
0075     if constexpr (forward_range<_Rp> && !__is_cheap_to_copy<range_value_t<_Rp>>) {
0076       auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool {
0077         return std::invoke(__comp, __rhs, __lhs);
0078       };
0079       return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
0080     } else {
0081       range_value_t<_Rp> __result = *__first;
0082       while (++__first != __last) {
0083         if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
0084           __result = *__first;
0085       }
0086       return __result;
0087     }
0088   }
0089 };
0090 } // namespace __max
0091 
0092 inline namespace __cpo {
0093 inline constexpr auto max = __max::__fn{};
0094 } // namespace __cpo
0095 } // namespace ranges
0096 
0097 _LIBCPP_END_NAMESPACE_STD
0098 
0099 _LIBCPP_POP_MACROS
0100 
0101 #endif // _LIBCPP_STD_VER >= 20
0102 
0103 #endif // _LIBCPP___CXX03___ALGORITHM_RANGES_MAX_H