1 //===----------------------------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_MAKE_PROJECTED_H 10 #define _LIBCPP___ALGORITHM_MAKE_PROJECTED_H 11 12 #include <__concepts/same_as.h> 13 #include <__config> 14 #include <__functional/identity.h> 15 #include <__functional/invoke.h> 16 #include <__type_traits/decay.h> 17 #include <__type_traits/enable_if.h> 18 #include <__type_traits/integral_constant.h> 19 #include <__type_traits/is_member_pointer.h> 20 #include <__type_traits/is_same.h> 21 #include <__utility/declval.h> 22 #include <__utility/forward.h> 23 24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 25 # pragma GCC system_header 26 #endif 27 28 _LIBCPP_BEGIN_NAMESPACE_STD 29 30 template <class _Pred, class _Proj> 31 struct _ProjectedPred { 32 _Pred& __pred; // Can be a unary or a binary predicate. 33 _Proj& __proj; 34 35 _LIBCPP_CONSTEXPR _ProjectedPred(_Pred& __pred_arg, _Proj& __proj_arg) : __pred(__pred_arg), __proj(__proj_arg) {} 36 37 template <class _Tp> 38 typename __invoke_of<_Pred&, 39 decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_Tp>())) 40 >::type 41 _LIBCPP_CONSTEXPR operator()(_Tp&& __v) const { 42 return std::__invoke(__pred, std::__invoke(__proj, std::forward<_Tp>(__v))); 43 } 44 45 template <class _T1, class _T2> 46 typename __invoke_of<_Pred&, 47 decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T1>())), 48 decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T2>())) 49 >::type 50 _LIBCPP_CONSTEXPR operator()(_T1&& __lhs, _T2&& __rhs) const { 51 return std::__invoke(__pred, 52 std::__invoke(__proj, std::forward<_T1>(__lhs)), 53 std::__invoke(__proj, std::forward<_T2>(__rhs))); 54 } 55 56 }; 57 58 template <class _Pred, class _Proj, class = void> 59 struct __can_use_pristine_comp : false_type {}; 60 61 template <class _Pred, class _Proj> 62 struct __can_use_pristine_comp<_Pred, _Proj, __enable_if_t< 63 !is_member_pointer<typename decay<_Pred>::type>::value && ( 64 #if _LIBCPP_STD_VER > 17 65 is_same<typename decay<_Proj>::type, identity>::value || 66 #endif 67 is_same<typename decay<_Proj>::type, __identity>::value 68 ) 69 > > : true_type {}; 70 71 template <class _Pred, class _Proj> 72 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static 73 __enable_if_t< 74 !__can_use_pristine_comp<_Pred, _Proj>::value, 75 _ProjectedPred<_Pred, _Proj> 76 > 77 __make_projected(_Pred& __pred, _Proj& __proj) { 78 return _ProjectedPred<_Pred, _Proj>(__pred, __proj); 79 } 80 81 // Avoid creating the functor and just use the pristine comparator -- for certain algorithms, this would enable 82 // optimizations that rely on the type of the comparator. Additionally, this results in less layers of indirection in 83 // the call stack when the comparator is invoked, even in an unoptimized build. 84 template <class _Pred, class _Proj> 85 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static 86 __enable_if_t< 87 __can_use_pristine_comp<_Pred, _Proj>::value, 88 _Pred& 89 > 90 __make_projected(_Pred& __pred, _Proj&) { 91 return __pred; 92 } 93 94 _LIBCPP_END_NAMESPACE_STD 95 96 #if _LIBCPP_STD_VER > 17 97 98 _LIBCPP_BEGIN_NAMESPACE_STD 99 100 namespace ranges { 101 102 template <class _Comp, class _Proj1, class _Proj2> 103 _LIBCPP_HIDE_FROM_ABI constexpr static 104 decltype(auto) __make_projected_comp(_Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) { 105 if constexpr (same_as<decay_t<_Proj1>, identity> && same_as<decay_t<_Proj2>, identity> && 106 !is_member_pointer_v<decay_t<_Comp>>) { 107 // Avoid creating the lambda and just use the pristine comparator -- for certain algorithms, this would enable 108 // optimizations that rely on the type of the comparator. 109 return __comp; 110 111 } else { 112 return [&](auto&& __lhs, auto&& __rhs) { 113 return std::invoke(__comp, 114 std::invoke(__proj1, std::forward<decltype(__lhs)>(__lhs)), 115 std::invoke(__proj2, std::forward<decltype(__rhs)>(__rhs))); 116 }; 117 } 118 } 119 120 } // namespace ranges 121 122 _LIBCPP_END_NAMESPACE_STD 123 124 #endif // _LIBCPP_STD_VER > 17 125 126 #endif // _LIBCPP___ALGORITHM_MAKE_PROJECTED_H 127