Back to home page

EIC code displayed by LXR

 
 

    


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

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___ALGORITHM_RANGES_MAX_H
0010 #define _LIBCPP___ALGORITHM_RANGES_MAX_H
0011 
0012 #include <__algorithm/ranges_min_element.h>
0013 #include <__assert>
0014 #include <__concepts/copyable.h>
0015 #include <__config>
0016 #include <__functional/identity.h>
0017 #include <__functional/invoke.h>
0018 #include <__functional/ranges_operations.h>
0019 #include <__iterator/concepts.h>
0020 #include <__iterator/projected.h>
0021 #include <__ranges/access.h>
0022 #include <__ranges/concepts.h>
0023 #include <__type_traits/is_trivially_copyable.h>
0024 #include <__utility/move.h>
0025 #include <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 <__undef_macros>
0035 
0036 _LIBCPP_BEGIN_NAMESPACE_STD
0037 
0038 namespace ranges {
0039 struct __max {
0040   template <class _Tp,
0041             class _Proj                                                    = identity,
0042             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
0043   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
0044   operator()(_LIBCPP_LIFETIMEBOUND const _Tp& __a,
0045              _LIBCPP_LIFETIMEBOUND const _Tp& __b,
0046              _Comp __comp = {},
0047              _Proj __proj = {}) const {
0048     return std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b)) ? __b : __a;
0049   }
0050 
0051   template <copyable _Tp,
0052             class _Proj                                                    = identity,
0053             indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
0054   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp
0055   operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
0056     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0057         __il.begin() != __il.end(), "initializer_list must contain at least one element");
0058 
0059     auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { return std::invoke(__comp, __rhs, __lhs); };
0060     return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
0061   }
0062 
0063   template <input_range _Rp,
0064             class _Proj                                                         = identity,
0065             indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
0066     requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
0067   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_value_t<_Rp>
0068   operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
0069     auto __first = ranges::begin(__r);
0070     auto __last  = ranges::end(__r);
0071 
0072     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element");
0073 
0074     if constexpr (forward_range<_Rp> && !__is_cheap_to_copy<range_value_t<_Rp>>) {
0075       auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool {
0076         return std::invoke(__comp, __rhs, __lhs);
0077       };
0078       return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
0079     } else {
0080       range_value_t<_Rp> __result = *__first;
0081       while (++__first != __last) {
0082         if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
0083           __result = *__first;
0084       }
0085       return __result;
0086     }
0087   }
0088 };
0089 
0090 inline namespace __cpo {
0091 inline constexpr auto max = __max{};
0092 } // namespace __cpo
0093 } // namespace ranges
0094 
0095 _LIBCPP_END_NAMESPACE_STD
0096 
0097 _LIBCPP_POP_MACROS
0098 
0099 #endif // _LIBCPP_STD_VER >= 20
0100 
0101 #endif // _LIBCPP___ALGORITHM_RANGES_MAX_H