Debian Codegraph @

libstdc++-v3/include/pstl/utils.h

raw
1 // -*- C++ -*-
2 //===-- utils.h -----------------------------------------------------------===//
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 <new>
14 #include <type_traits>
15
16 namespace __pstl
17 {
18 namespace __internal
19 {
20
21 template <typename _Fp>
22 auto
23 __except_handler(_Fp __f) -> decltype(__f())
24 {
25 try
26 {
27 return __f();
28 }
29 catch (const std::bad_alloc&)
30 {
31 throw; // re-throw bad_alloc according to the standard [algorithms.parallel.exceptions]
32 }
33 catch (...)
34 {
35 std::__terminate(); // Good bye according to the standard [algorithms.parallel.exceptions]
36 }
37 }
38
39 template <typename _Fp>
40 void
41 __invoke_if(std::true_type, _Fp __f)
42 {
43 __f();
44 }
45
46 template <typename _Fp>
47 void __invoke_if(std::false_type, _Fp)
48 {
49 }
50
51 template <typename _Fp>
52 void
53 __invoke_if_not(std::false_type, _Fp __f)
54 {
55 __f();
56 }
57
58 template <typename _Fp>
59 void __invoke_if_not(std::true_type, _Fp)
60 {
61 }
62
63 template <typename _F1, typename _F2>
64 auto
65 __invoke_if_else(std::true_type, _F1 __f1, _F2) -> decltype(__f1())
66 {
67 return __f1();
68 }
69
70 template <typename _F1, typename _F2>
71 auto
72 __invoke_if_else(std::false_type, _F1, _F2 __f2) -> decltype(__f2())
73 {
74 return __f2();
75 }
76
77 //! Unary operator that returns reference to its argument.
78 struct __no_op
79 {
80 template <typename _Tp>
81 _Tp&&
82 operator()(_Tp&& __a) const
83 {
84 return std::forward<_Tp>(__a);
85 }
86 };
87
88 template <typename _Pred>
89 class __reorder_pred
90 {
91 _Pred _M_pred;
92
93 public:
94 explicit __reorder_pred(_Pred __pred) : _M_pred(__pred) {}
95
96 template <typename _FTp, typename _STp>
97 bool
98 operator()(_FTp&& __a, _STp&& __b)
99 {
100 return _M_pred(std::forward<_STp>(__b), std::forward<_FTp>(__a));
101 }
102 };
103
104 //! Like a polymorphic lambda for pred(...,value)
105 template <typename _Tp, typename _Predicate>
106 class __equal_value_by_pred
107 {
108 const _Tp& _M_value;
109 _Predicate _M_pred;
110
111 public:
112 __equal_value_by_pred(const _Tp& __value, _Predicate __pred) : _M_value(__value), _M_pred(__pred) {}
113
114 template <typename _Arg>
115 bool
116 operator()(_Arg&& __arg)
117 {
118 return _M_pred(std::forward<_Arg>(__arg), _M_value);
119 }
120 };
121
122 //! Like a polymorphic lambda for ==value
123 template <typename _Tp>
124 class __equal_value
125 {
126 const _Tp& _M_value;
127
128 public:
129 explicit __equal_value(const _Tp& __value) : _M_value(__value) {}
130
131 template <typename _Arg>
132 bool
133 operator()(_Arg&& __arg) const
134 {
135 return std::forward<_Arg>(__arg) == _M_value;
136 }
137 };
138
139 //! Logical negation of ==value
140 template <typename _Tp>
141 class __not_equal_value
142 {
143 const _Tp& _M_value;
144
145 public:
146 explicit __not_equal_value(const _Tp& __value) : _M_value(__value) {}
147
148 template <typename _Arg>
149 bool
150 operator()(_Arg&& __arg) const
151 {
152 return !(std::forward<_Arg>(__arg) == _M_value);
153 }
154 };
155
156 template <typename _ForwardIterator, typename _Compare>
157 _ForwardIterator
158 __cmp_iterators_by_values(_ForwardIterator __a, _ForwardIterator __b, _Compare __comp)
159 {
160 if (__a < __b)
161 { // we should return closer iterator
162 return __comp(*__b, *__a) ? __b : __a;
163 }
164 else
165 {
166 return __comp(*__a, *__b) ? __a : __b;
167 }
168 }
169
170 } // namespace __internal
171 } // namespace __pstl
172
173 #endif /* _PSTL_UTILS_H */