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 _LIBCPP___FUNCTIONAL_OPERATIONS_H
11#define _LIBCPP___FUNCTIONAL_OPERATIONS_H
12
13#include <__config>
14#include <__functional/binary_function.h>
15#include <__functional/unary_function.h>
16#include <__fwd/functional.h>
17#include <__type_traits/desugars_to.h>
18#include <__type_traits/is_generic_transparent_comparator.h>
19#include <__type_traits/is_integral.h>
20#include <__type_traits/make_transparent.h>
21#include <__utility/forward.h>
22
23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24# pragma GCC system_header
25#endif
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29// Arithmetic operations
30
31#if _LIBCPP_STD_VER >= 14
32template <class _Tp = void>
33#else
34template <class _Tp>
35#endif
36struct plus : __binary_function<_Tp, _Tp, _Tp> {
37 typedef _Tp __result_type; // used by valarray
38 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
39};
40_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(plus);
41
42// The non-transparent std::plus specialization is only equivalent to a raw plus
43// operator when we don't perform an implicit conversion when calling it.
44template <class _Tp>
45inline const bool __desugars_to_v<__plus_tag, plus<_Tp>, _Tp, _Tp> = true;
46
47template <class _Tp, class _Up>
48inline const bool __desugars_to_v<__plus_tag, plus<void>, _Tp, _Up> = true;
49
50#if _LIBCPP_STD_VER >= 14
51template <>
52struct plus<void> {
53 template <class _T1, class _T2>
54 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
55 noexcept(noexcept(std::forward<_T1>(__t) + std::forward<_T2>(__u))) //
56 -> decltype(std::forward<_T1>(__t) + std::forward<_T2>(__u)) {
57 return std::forward<_T1>(__t) + std::forward<_T2>(__u);
58 }
59 typedef void is_transparent;
60};
61#endif
62
63#if _LIBCPP_STD_VER >= 14
64template <class _Tp = void>
65#else
66template <class _Tp>
67#endif
68struct minus : __binary_function<_Tp, _Tp, _Tp> {
69 typedef _Tp __result_type; // used by valarray
70 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
71};
72_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(minus);
73
74#if _LIBCPP_STD_VER >= 14
75template <>
76struct minus<void> {
77 template <class _T1, class _T2>
78 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
79 noexcept(noexcept(std::forward<_T1>(__t) - std::forward<_T2>(__u))) //
80 -> decltype(std::forward<_T1>(__t) - std::forward<_T2>(__u)) {
81 return std::forward<_T1>(__t) - std::forward<_T2>(__u);
82 }
83 typedef void is_transparent;
84};
85#endif
86
87#if _LIBCPP_STD_VER >= 14
88template <class _Tp = void>
89#else
90template <class _Tp>
91#endif
92struct multiplies : __binary_function<_Tp, _Tp, _Tp> {
93 typedef _Tp __result_type; // used by valarray
94 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
95};
96_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(multiplies);
97
98#if _LIBCPP_STD_VER >= 14
99template <>
100struct multiplies<void> {
101 template <class _T1, class _T2>
102 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
103 noexcept(noexcept(std::forward<_T1>(__t) * std::forward<_T2>(__u))) //
104 -> decltype(std::forward<_T1>(__t) * std::forward<_T2>(__u)) {
105 return std::forward<_T1>(__t) * std::forward<_T2>(__u);
106 }
107 typedef void is_transparent;
108};
109#endif
110
111#if _LIBCPP_STD_VER >= 14
112template <class _Tp = void>
113#else
114template <class _Tp>
115#endif
116struct divides : __binary_function<_Tp, _Tp, _Tp> {
117 typedef _Tp __result_type; // used by valarray
118 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
119};
120_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(divides);
121
122#if _LIBCPP_STD_VER >= 14
123template <>
124struct divides<void> {
125 template <class _T1, class _T2>
126 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
127 noexcept(noexcept(std::forward<_T1>(__t) / std::forward<_T2>(__u))) //
128 -> decltype(std::forward<_T1>(__t) / std::forward<_T2>(__u)) {
129 return std::forward<_T1>(__t) / std::forward<_T2>(__u);
130 }
131 typedef void is_transparent;
132};
133#endif
134
135#if _LIBCPP_STD_VER >= 14
136template <class _Tp = void>
137#else
138template <class _Tp>
139#endif
140struct modulus : __binary_function<_Tp, _Tp, _Tp> {
141 typedef _Tp __result_type; // used by valarray
142 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
143};
144_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(modulus);
145
146#if _LIBCPP_STD_VER >= 14
147template <>
148struct modulus<void> {
149 template <class _T1, class _T2>
150 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
151 noexcept(noexcept(std::forward<_T1>(__t) % std::forward<_T2>(__u))) //
152 -> decltype(std::forward<_T1>(__t) % std::forward<_T2>(__u)) {
153 return std::forward<_T1>(__t) % std::forward<_T2>(__u);
154 }
155 typedef void is_transparent;
156};
157#endif
158
159#if _LIBCPP_STD_VER >= 14
160template <class _Tp = void>
161#else
162template <class _Tp>
163#endif
164struct negate : __unary_function<_Tp, _Tp> {
165 typedef _Tp __result_type; // used by valarray
166 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x) const { return -__x; }
167};
168_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(negate);
169
170#if _LIBCPP_STD_VER >= 14
171template <>
172struct negate<void> {
173 template <class _Tp>
174 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_Tp&& __x) const noexcept(noexcept(-std::forward<_Tp>(__x))) //
175 -> decltype(-std::forward<_Tp>(__x)) {
176 return -std::forward<_Tp>(__x);
177 }
178 typedef void is_transparent;
179};
180#endif
181
182// Bitwise operations
183
184#if _LIBCPP_STD_VER >= 14
185template <class _Tp = void>
186#else
187template <class _Tp>
188#endif
189struct bit_and : __binary_function<_Tp, _Tp, _Tp> {
190 typedef _Tp __result_type; // used by valarray
191 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x & __y; }
192};
193_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_and);
194
195#if _LIBCPP_STD_VER >= 14
196template <>
197struct bit_and<void> {
198 template <class _T1, class _T2>
199 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
200 noexcept(noexcept(std::forward<_T1>(__t) & std::forward<_T2>(__u)))
201 -> decltype(std::forward<_T1>(__t) & std::forward<_T2>(__u)) {
202 return std::forward<_T1>(__t) & std::forward<_T2>(__u);
203 }
204 typedef void is_transparent;
205};
206#endif
207
208#if _LIBCPP_STD_VER >= 14
209template <class _Tp = void>
210struct bit_not : __unary_function<_Tp, _Tp> {
211 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x) const { return ~__x; }
212};
213_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_not);
214
215template <>
216struct bit_not<void> {
217 template <class _Tp>
218 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_Tp&& __x) const noexcept(noexcept(~std::forward<_Tp>(__x))) //
219 -> decltype(~std::forward<_Tp>(__x)) {
220 return ~std::forward<_Tp>(__x);
221 }
222 typedef void is_transparent;
223};
224#endif
225
226#if _LIBCPP_STD_VER >= 14
227template <class _Tp = void>
228#else
229template <class _Tp>
230#endif
231struct bit_or : __binary_function<_Tp, _Tp, _Tp> {
232 typedef _Tp __result_type; // used by valarray
233 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x | __y; }
234};
235_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_or);
236
237#if _LIBCPP_STD_VER >= 14
238template <>
239struct bit_or<void> {
240 template <class _T1, class _T2>
241 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
242 noexcept(noexcept(std::forward<_T1>(__t) | std::forward<_T2>(__u))) //
243 -> decltype(std::forward<_T1>(__t) | std::forward<_T2>(__u)) {
244 return std::forward<_T1>(__t) | std::forward<_T2>(__u);
245 }
246 typedef void is_transparent;
247};
248#endif
249
250#if _LIBCPP_STD_VER >= 14
251template <class _Tp = void>
252#else
253template <class _Tp>
254#endif
255struct bit_xor : __binary_function<_Tp, _Tp, _Tp> {
256 typedef _Tp __result_type; // used by valarray
257 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x ^ __y; }
258};
259_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_xor);
260
261#if _LIBCPP_STD_VER >= 14
262template <>
263struct bit_xor<void> {
264 template <class _T1, class _T2>
265 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
266 noexcept(noexcept(std::forward<_T1>(__t) ^ std::forward<_T2>(__u))) //
267 -> decltype(std::forward<_T1>(__t) ^ std::forward<_T2>(__u)) {
268 return std::forward<_T1>(__t) ^ std::forward<_T2>(__u);
269 }
270 typedef void is_transparent;
271};
272#endif
273
274// Comparison operations
275
276#if _LIBCPP_STD_VER >= 14
277template <class _Tp = void>
278#else
279template <class _Tp>
280#endif
281struct equal_to : __binary_function<_Tp, _Tp, bool> {
282 typedef bool __result_type; // used by valarray
283 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
284};
285_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(equal_to);
286
287#if _LIBCPP_STD_VER >= 14
288template <>
289struct equal_to<void> {
290 template <class _T1, class _T2>
291 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
292 noexcept(noexcept(std::forward<_T1>(__t) == std::forward<_T2>(__u))) //
293 -> decltype(std::forward<_T1>(__t) == std::forward<_T2>(__u)) {
294 return std::forward<_T1>(__t) == std::forward<_T2>(__u);
295 }
296 typedef void is_transparent;
297};
298#endif
299
300// The non-transparent std::equal_to specialization is only equivalent to a raw equality
301// comparison when we don't perform an implicit conversion when calling it.
302template <class _Tp>
303inline const bool __desugars_to_v<__equal_tag, equal_to<_Tp>, _Tp, _Tp> = true;
304
305// In the transparent case, we do not enforce that
306template <class _Tp, class _Up>
307inline const bool __desugars_to_v<__equal_tag, equal_to<void>, _Tp, _Up> = true;
308
309#if _LIBCPP_STD_VER >= 14
310template <class _Tp = void>
311#else
312template <class _Tp>
313#endif
314struct not_equal_to : __binary_function<_Tp, _Tp, bool> {
315 typedef bool __result_type; // used by valarray
316 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
317};
318_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(not_equal_to);
319
320#if _LIBCPP_STD_VER >= 14
321template <>
322struct not_equal_to<void> {
323 template <class _T1, class _T2>
324 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
325 noexcept(noexcept(std::forward<_T1>(__t) != std::forward<_T2>(__u))) //
326 -> decltype(std::forward<_T1>(__t) != std::forward<_T2>(__u)) {
327 return std::forward<_T1>(__t) != std::forward<_T2>(__u);
328 }
329 typedef void is_transparent;
330};
331#endif
332
333template <class _Tp>
334struct less : __binary_function<_Tp, _Tp, bool> {
335 typedef bool __result_type; // used by valarray
336 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
337};
338_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less);
339
340template <class _Tp>
341inline const bool __desugars_to_v<__less_tag, less<_Tp>, _Tp, _Tp> = true;
342
343template <class _Tp>
344inline const bool __desugars_to_v<__totally_ordered_less_tag, less<_Tp>, _Tp, _Tp> = is_integral<_Tp>::value;
345
346#if _LIBCPP_STD_VER >= 14
347template <>
348struct less<void> {
349 template <class _T1, class _T2>
350 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
351 noexcept(noexcept(std::forward<_T1>(__t) < std::forward<_T2>(__u))) //
352 -> decltype(std::forward<_T1>(__t) < std::forward<_T2>(__u)) {
353 return std::forward<_T1>(__t) < std::forward<_T2>(__u);
354 }
355 typedef void is_transparent;
356};
357
358template <class _ArgumentType>
359struct __make_transparent<_ArgumentType, less<_ArgumentType> > {
360 using type _LIBCPP_NODEBUG = less<>;
361};
362
363template <>
364inline const bool __is_generic_transparent_comparator_v<less<>> = true;
365
366template <class _Tp, class _Up>
367inline const bool __desugars_to_v<__less_tag, less<>, _Tp, _Up> = true;
368
369template <class _Tp>
370inline const bool __desugars_to_v<__totally_ordered_less_tag, less<>, _Tp, _Tp> = is_integral<_Tp>::value;
371#endif
372
373#if _LIBCPP_STD_VER >= 14
374template <class _Tp = void>
375#else
376template <class _Tp>
377#endif
378struct less_equal : __binary_function<_Tp, _Tp, bool> {
379 typedef bool __result_type; // used by valarray
380 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
381};
382_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less_equal);
383
384#if _LIBCPP_STD_VER >= 14
385template <>
386struct less_equal<void> {
387 template <class _T1, class _T2>
388 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
389 noexcept(noexcept(std::forward<_T1>(__t) <= std::forward<_T2>(__u))) //
390 -> decltype(std::forward<_T1>(__t) <= std::forward<_T2>(__u)) {
391 return std::forward<_T1>(__t) <= std::forward<_T2>(__u);
392 }
393 typedef void is_transparent;
394};
395#endif
396
397#if _LIBCPP_STD_VER >= 14
398template <class _Tp = void>
399#else
400template <class _Tp>
401#endif
402struct greater_equal : __binary_function<_Tp, _Tp, bool> {
403 typedef bool __result_type; // used by valarray
404 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
405};
406_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater_equal);
407
408#if _LIBCPP_STD_VER >= 14
409template <>
410struct greater_equal<void> {
411 template <class _T1, class _T2>
412 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
413 noexcept(noexcept(std::forward<_T1>(__t) >= std::forward<_T2>(__u)))
414 -> decltype(std::forward<_T1>(__t) >= std::forward<_T2>(__u)) {
415 return std::forward<_T1>(__t) >= std::forward<_T2>(__u);
416 }
417 typedef void is_transparent;
418};
419#endif
420
421#if _LIBCPP_STD_VER >= 14
422template <class _Tp = void>
423#else
424template <class _Tp>
425#endif
426struct greater : __binary_function<_Tp, _Tp, bool> {
427 typedef bool __result_type; // used by valarray
428 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
429};
430_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater);
431
432template <class _Tp>
433inline const bool __desugars_to_v<__greater_tag, greater<_Tp>, _Tp, _Tp> = true;
434
435#if _LIBCPP_STD_VER >= 14
436template <>
437struct greater<void> {
438 template <class _T1, class _T2>
439 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
440 noexcept(noexcept(std::forward<_T1>(__t) > std::forward<_T2>(__u))) //
441 -> decltype(std::forward<_T1>(__t) > std::forward<_T2>(__u)) {
442 return std::forward<_T1>(__t) > std::forward<_T2>(__u);
443 }
444 typedef void is_transparent;
445};
446
447template <class _Tp, class _Up>
448inline const bool __desugars_to_v<__greater_tag, greater<>, _Tp, _Up> = true;
449
450template <class _ArgumentType>
451struct __make_transparent<_ArgumentType, greater<_ArgumentType>> {
452 using type _LIBCPP_NODEBUG = greater<>;
453};
454
455template <>
456inline const bool __is_generic_transparent_comparator_v<greater<>> = true;
457#endif
458
459// Logical operations
460
461#if _LIBCPP_STD_VER >= 14
462template <class _Tp = void>
463#else
464template <class _Tp>
465#endif
466struct logical_and : __binary_function<_Tp, _Tp, bool> {
467 typedef bool __result_type; // used by valarray
468 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
469};
470_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_and);
471
472#if _LIBCPP_STD_VER >= 14
473template <>
474struct logical_and<void> {
475 template <class _T1, class _T2>
476 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
477 noexcept(noexcept(std::forward<_T1>(__t) && std::forward<_T2>(__u))) //
478 -> decltype(std::forward<_T1>(__t) && std::forward<_T2>(__u)) {
479 return std::forward<_T1>(__t) && std::forward<_T2>(__u);
480 }
481 typedef void is_transparent;
482};
483#endif
484
485#if _LIBCPP_STD_VER >= 14
486template <class _Tp = void>
487#else
488template <class _Tp>
489#endif
490struct logical_not : __unary_function<_Tp, bool> {
491 typedef bool __result_type; // used by valarray
492 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x) const { return !__x; }
493};
494_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_not);
495
496#if _LIBCPP_STD_VER >= 14
497template <>
498struct logical_not<void> {
499 template <class _Tp>
500 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_Tp&& __x) const noexcept(noexcept(!std::forward<_Tp>(__x))) //
501 -> decltype(!std::forward<_Tp>(__x)) {
502 return !std::forward<_Tp>(__x);
503 }
504 typedef void is_transparent;
505};
506#endif
507
508#if _LIBCPP_STD_VER >= 14
509template <class _Tp = void>
510#else
511template <class _Tp>
512#endif
513struct logical_or : __binary_function<_Tp, _Tp, bool> {
514 typedef bool __result_type; // used by valarray
515 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
516};
517_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_or);
518
519#if _LIBCPP_STD_VER >= 14
520template <>
521struct logical_or<void> {
522 template <class _T1, class _T2>
523 _LIBCPP_CONSTEXPR_SINCE_CXX14 auto operator()(_T1&& __t, _T2&& __u) const
524 noexcept(noexcept(std::forward<_T1>(__t) || std::forward<_T2>(__u))) //
525 -> decltype(std::forward<_T1>(__t) || std::forward<_T2>(__u)) {
526 return std::forward<_T1>(__t) || std::forward<_T2>(__u);
527 }
528 typedef void is_transparent;
529};
530#endif
531
532_LIBCPP_END_NAMESPACE_STD
533
534#endif // _LIBCPP___FUNCTIONAL_OPERATIONS_H
535