Back to home page

EIC code displayed by LXR

 
 

    


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

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_MAKE_PROJECTED_H
0010 #define _LIBCPP___ALGORITHM_MAKE_PROJECTED_H
0011 
0012 #include <__config>
0013 #include <__functional/identity.h>
0014 #include <__functional/invoke.h>
0015 #include <__type_traits/decay.h>
0016 #include <__type_traits/enable_if.h>
0017 #include <__type_traits/invoke.h>
0018 #include <__type_traits/is_member_pointer.h>
0019 #include <__utility/declval.h>
0020 #include <__utility/forward.h>
0021 
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 #  pragma GCC system_header
0024 #endif
0025 
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027 
0028 template <class _Pred, class _Proj>
0029 struct _ProjectedPred {
0030   _Pred& __pred; // Can be a unary or a binary predicate.
0031   _Proj& __proj;
0032 
0033   _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI _ProjectedPred(_Pred& __pred_arg, _Proj& __proj_arg)
0034       : __pred(__pred_arg), __proj(__proj_arg) {}
0035 
0036   template <class _Tp>
0037   __invoke_result_t<_Pred&, decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_Tp>()))> _LIBCPP_CONSTEXPR
0038   _LIBCPP_HIDE_FROM_ABI
0039   operator()(_Tp&& __v) const {
0040     return std::__invoke(__pred, std::__invoke(__proj, std::forward<_Tp>(__v)));
0041   }
0042 
0043   template <class _T1, class _T2>
0044   __invoke_result_t<_Pred&,
0045                     decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T1>())),
0046                     decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T2>()))> _LIBCPP_CONSTEXPR
0047   _LIBCPP_HIDE_FROM_ABI
0048   operator()(_T1&& __lhs, _T2&& __rhs) const {
0049     return std::__invoke(
0050         __pred, std::__invoke(__proj, std::forward<_T1>(__lhs)), std::__invoke(__proj, std::forward<_T2>(__rhs)));
0051   }
0052 };
0053 
0054 template <
0055     class _Pred,
0056     class _Proj,
0057     __enable_if_t<!(!is_member_pointer<__decay_t<_Pred> >::value && __is_identity<__decay_t<_Proj> >::value), int> = 0>
0058 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ProjectedPred<_Pred, _Proj> __make_projected(_Pred& __pred, _Proj& __proj) {
0059   return _ProjectedPred<_Pred, _Proj>(__pred, __proj);
0060 }
0061 
0062 // Avoid creating the functor and just use the pristine comparator -- for certain algorithms, this would enable
0063 // optimizations that rely on the type of the comparator. Additionally, this results in less layers of indirection in
0064 // the call stack when the comparator is invoked, even in an unoptimized build.
0065 template <
0066     class _Pred,
0067     class _Proj,
0068     __enable_if_t<!is_member_pointer<__decay_t<_Pred> >::value && __is_identity<__decay_t<_Proj> >::value, int> = 0>
0069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Pred& __make_projected(_Pred& __pred, _Proj&) {
0070   return __pred;
0071 }
0072 
0073 _LIBCPP_END_NAMESPACE_STD
0074 
0075 #if _LIBCPP_STD_VER >= 20
0076 
0077 _LIBCPP_BEGIN_NAMESPACE_STD
0078 
0079 namespace ranges {
0080 
0081 template <class _Comp, class _Proj1, class _Proj2>
0082 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) __make_projected_comp(_Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
0083   if constexpr (__is_identity<decay_t<_Proj1>>::value && __is_identity<decay_t<_Proj2>>::value &&
0084                 !is_member_pointer_v<decay_t<_Comp>>) {
0085     // Avoid creating the lambda and just use the pristine comparator -- for certain algorithms, this would enable
0086     // optimizations that rely on the type of the comparator.
0087     return __comp;
0088 
0089   } else {
0090     return [&](auto&& __lhs, auto&& __rhs) -> bool {
0091       return std::invoke(__comp,
0092                          std::invoke(__proj1, std::forward<decltype(__lhs)>(__lhs)),
0093                          std::invoke(__proj2, std::forward<decltype(__rhs)>(__rhs)));
0094     };
0095   }
0096 }
0097 
0098 } // namespace ranges
0099 
0100 _LIBCPP_END_NAMESPACE_STD
0101 
0102 #endif // _LIBCPP_STD_VER >= 20
0103 
0104 #endif // _LIBCPP___ALGORITHM_MAKE_PROJECTED_H