make_projected.h 4.0 KB

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