75 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_MAKE_PROJECTED_H10#define _LIBCPP___CXX03___ALGORITHM_MAKE_PROJECTED_H11 12#include <__cxx03/__config>13#include <__cxx03/__functional/identity.h>14#include <__cxx03/__type_traits/decay.h>15#include <__cxx03/__type_traits/enable_if.h>16#include <__cxx03/__type_traits/integral_constant.h>17#include <__cxx03/__type_traits/invoke.h>18#include <__cxx03/__type_traits/is_member_pointer.h>19#include <__cxx03/__type_traits/is_same.h>20#include <__cxx03/__utility/declval.h>21#include <__cxx03/__utility/forward.h>22 23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29template <class _Pred, class _Proj>30struct _ProjectedPred {31 _Pred& __pred; // Can be a unary or a binary predicate.32 _Proj& __proj;33 34 _LIBCPP_HIDE_FROM_ABI _ProjectedPred(_Pred& __pred_arg, _Proj& __proj_arg) : __pred(__pred_arg), __proj(__proj_arg) {}35 36 template <class _Tp>37 typename __invoke_of<_Pred&, decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_Tp>()))>::type38 _LIBCPP_HIDE_FROM_ABI39 operator()(_Tp&& __v) const {40 return std::__invoke(__pred, std::__invoke(__proj, std::forward<_Tp>(__v)));41 }42 43 template <class _T1, class _T2>44 typename __invoke_of<_Pred&,45 decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T1>())),46 decltype(std::__invoke(std::declval<_Proj&>(), std::declval<_T2>()))>::type _LIBCPP_HIDE_FROM_ABI47 operator()(_T1&& __lhs, _T2&& __rhs) const {48 return std::__invoke(49 __pred, std::__invoke(__proj, std::forward<_T1>(__lhs)), std::__invoke(__proj, std::forward<_T2>(__rhs)));50 }51};52 53template <54 class _Pred,55 class _Proj,56 __enable_if_t<!(!is_member_pointer<__decay_t<_Pred> >::value && __is_identity<__decay_t<_Proj> >::value), int> = 0>57_LIBCPP_HIDE_FROM_ABI _ProjectedPred<_Pred, _Proj> __make_projected(_Pred& __pred, _Proj& __proj) {58 return _ProjectedPred<_Pred, _Proj>(__pred, __proj);59}60 61// Avoid creating the functor and just use the pristine comparator -- for certain algorithms, this would enable62// optimizations that rely on the type of the comparator. Additionally, this results in less layers of indirection in63// the call stack when the comparator is invoked, even in an unoptimized build.64template <65 class _Pred,66 class _Proj,67 __enable_if_t<!is_member_pointer<__decay_t<_Pred> >::value && __is_identity<__decay_t<_Proj> >::value, int> = 0>68_LIBCPP_HIDE_FROM_ABI _Pred& __make_projected(_Pred& __pred, _Proj&) {69 return __pred;70}71 72_LIBCPP_END_NAMESPACE_STD73 74#endif // _LIBCPP___CXX03___ALGORITHM_MAKE_PROJECTED_H75