xref: /aosp_15_r20/external/cronet/third_party/libc++/src/include/__pstl/internal/utils.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _PSTL_UTILS_H
11 #define _PSTL_UTILS_H
12 
13 #include <__config>
14 #include <__exception/terminate.h>
15 #include <__utility/forward.h>
16 #include <new>
17 
18 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
19 
20 namespace __pstl {
21 namespace __internal {
22 
23 template <typename _Fp>
24 _LIBCPP_HIDE_FROM_ABI auto __except_handler(_Fp __f) -> decltype(__f()) {
25 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
26   try {
27 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
28     return __f();
29 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(const std::bad_alloc &)30   } catch (const std::bad_alloc&) {
31     throw;            // re-throw bad_alloc according to the standard [algorithms.parallel.exceptions]
32   } catch (...) {
33     std::terminate(); // Good bye according to the standard [algorithms.parallel.exceptions]
34   }
35 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
36 }
37 
38 template <typename _Fp>
__invoke_if(std::true_type,_Fp __f)39 _LIBCPP_HIDE_FROM_ABI void __invoke_if(std::true_type, _Fp __f) {
40   __f();
41 }
42 
43 template <typename _Fp>
__invoke_if(std::false_type,_Fp)44 _LIBCPP_HIDE_FROM_ABI void __invoke_if(std::false_type, _Fp) {}
45 
46 template <typename _Fp>
__invoke_if_not(std::false_type,_Fp __f)47 _LIBCPP_HIDE_FROM_ABI void __invoke_if_not(std::false_type, _Fp __f) {
48   __f();
49 }
50 
51 template <typename _Fp>
__invoke_if_not(std::true_type,_Fp)52 _LIBCPP_HIDE_FROM_ABI void __invoke_if_not(std::true_type, _Fp) {}
53 
54 template <typename _F1, typename _F2>
55 _LIBCPP_HIDE_FROM_ABI auto __invoke_if_else(std::true_type, _F1 __f1, _F2) -> decltype(__f1()) {
56   return __f1();
57 }
58 
59 template <typename _F1, typename _F2>
60 _LIBCPP_HIDE_FROM_ABI auto __invoke_if_else(std::false_type, _F1, _F2 __f2) -> decltype(__f2()) {
61   return __f2();
62 }
63 
64 //! Unary operator that returns reference to its argument.
65 struct __no_op {
66   template <typename _Tp>
operator__no_op67   _LIBCPP_HIDE_FROM_ABI _Tp&& operator()(_Tp&& __a) const {
68     return std::forward<_Tp>(__a);
69   }
70 };
71 
72 template <typename _Pred>
73 class __reorder_pred {
74   _Pred __pred_;
75 
76 public:
__reorder_pred(_Pred __pred)77   _LIBCPP_HIDE_FROM_ABI explicit __reorder_pred(_Pred __pred) : __pred_(__pred) {}
78 
79   template <typename _FTp, typename _STp>
operator()80   _LIBCPP_HIDE_FROM_ABI bool operator()(_FTp&& __a, _STp&& __b) {
81     return __pred_(std::forward<_STp>(__b), std::forward<_FTp>(__a));
82   }
83 };
84 
85 //! Like a polymorphic lambda for pred(...,value)
86 template <typename _Tp, typename _Predicate>
87 class __equal_value_by_pred {
88   const _Tp& __value_;
89   _Predicate __pred_;
90 
91 public:
__equal_value_by_pred(const _Tp & __value,_Predicate __pred)92   _LIBCPP_HIDE_FROM_ABI __equal_value_by_pred(const _Tp& __value, _Predicate __pred)
93       : __value_(__value), __pred_(__pred) {}
94 
95   template <typename _Arg>
operator()96   _LIBCPP_HIDE_FROM_ABI bool operator()(_Arg&& __arg) {
97     return __pred_(std::forward<_Arg>(__arg), __value_);
98   }
99 };
100 
101 //! Like a polymorphic lambda for ==value
102 template <typename _Tp>
103 class __equal_value {
104   const _Tp& __value_;
105 
106 public:
__equal_value(const _Tp & __value)107   _LIBCPP_HIDE_FROM_ABI explicit __equal_value(const _Tp& __value) : __value_(__value) {}
108 
109   template <typename _Arg>
operator()110   _LIBCPP_HIDE_FROM_ABI bool operator()(_Arg&& __arg) const {
111     return std::forward<_Arg>(__arg) == __value_;
112   }
113 };
114 
115 //! Logical negation of ==value
116 template <typename _Tp>
117 class __not_equal_value {
118   const _Tp& __value_;
119 
120 public:
__not_equal_value(const _Tp & __value)121   _LIBCPP_HIDE_FROM_ABI explicit __not_equal_value(const _Tp& __value) : __value_(__value) {}
122 
123   template <typename _Arg>
operator()124   _LIBCPP_HIDE_FROM_ABI bool operator()(_Arg&& __arg) const {
125     return !(std::forward<_Arg>(__arg) == __value_);
126   }
127 };
128 
129 template <typename _ForwardIterator, typename _Compare>
130 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
__cmp_iterators_by_values(_ForwardIterator __a,_ForwardIterator __b,_Compare __comp)131 __cmp_iterators_by_values(_ForwardIterator __a, _ForwardIterator __b, _Compare __comp) {
132   if (__a < __b) { // we should return closer iterator
133     return __comp(*__b, *__a) ? __b : __a;
134   } else {
135     return __comp(*__a, *__b) ? __a : __b;
136   }
137 }
138 
139 } // namespace __internal
140 } // namespace __pstl
141 
142 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
143 
144 #endif /* _PSTL_UTILS_H */
145