xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__functional/operations.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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___CXX03___FUNCTIONAL_OPERATIONS_H
11 #define _LIBCPP___CXX03___FUNCTIONAL_OPERATIONS_H
12 
13 #include <__cxx03/__config>
14 #include <__cxx03/__functional/binary_function.h>
15 #include <__cxx03/__functional/unary_function.h>
16 #include <__cxx03/__type_traits/desugars_to.h>
17 #include <__cxx03/__utility/forward.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 // Arithmetic operations
26 
27 template <class _Tp>
28 struct _LIBCPP_TEMPLATE_VIS plus : __binary_function<_Tp, _Tp, _Tp> {
29   typedef _Tp __result_type; // used by valarray
operatorplus30   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
31 };
32 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(plus);
33 
34 // The non-transparent std::plus specialization is only equivalent to a raw plus
35 // operator when we don't perform an implicit conversion when calling it.
36 template <class _Tp>
37 inline const bool __desugars_to_v<__plus_tag, plus<_Tp>, _Tp, _Tp> = true;
38 
39 template <class _Tp, class _Up>
40 inline const bool __desugars_to_v<__plus_tag, plus<void>, _Tp, _Up> = true;
41 
42 template <class _Tp>
43 struct _LIBCPP_TEMPLATE_VIS minus : __binary_function<_Tp, _Tp, _Tp> {
44   typedef _Tp __result_type; // used by valarray
operatorminus45   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
46 };
47 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(minus);
48 
49 template <class _Tp>
50 struct _LIBCPP_TEMPLATE_VIS multiplies : __binary_function<_Tp, _Tp, _Tp> {
51   typedef _Tp __result_type; // used by valarray
operatormultiplies52   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
53 };
54 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(multiplies);
55 
56 template <class _Tp>
57 struct _LIBCPP_TEMPLATE_VIS divides : __binary_function<_Tp, _Tp, _Tp> {
58   typedef _Tp __result_type; // used by valarray
operatordivides59   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
60 };
61 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(divides);
62 
63 template <class _Tp>
64 struct _LIBCPP_TEMPLATE_VIS modulus : __binary_function<_Tp, _Tp, _Tp> {
65   typedef _Tp __result_type; // used by valarray
operatormodulus66   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
67 };
68 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(modulus);
69 
70 template <class _Tp = void>
71 struct _LIBCPP_TEMPLATE_VIS negate : __unary_function<_Tp, _Tp> {
72   typedef _Tp __result_type; // used by valarray
operatornegate73   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x) const { return -__x; }
74 };
75 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(negate);
76 
77 // Bitwise operations
78 
79 template <class _Tp>
80 struct _LIBCPP_TEMPLATE_VIS bit_and : __binary_function<_Tp, _Tp, _Tp> {
81   typedef _Tp __result_type; // used by valarray
operatorbit_and82   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x & __y; }
83 };
84 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_and);
85 
86 template <class _Tp>
87 struct _LIBCPP_TEMPLATE_VIS bit_or : __binary_function<_Tp, _Tp, _Tp> {
88   typedef _Tp __result_type; // used by valarray
operatorbit_or89   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x | __y; }
90 };
91 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_or);
92 
93 template <class _Tp = void>
94 struct _LIBCPP_TEMPLATE_VIS bit_xor : __binary_function<_Tp, _Tp, _Tp> {
95   typedef _Tp __result_type; // used by valarray
operatorbit_xor96   _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x ^ __y; }
97 };
98 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_xor);
99 
100 // Comparison operations
101 
102 template <class _Tp>
103 struct _LIBCPP_TEMPLATE_VIS equal_to : __binary_function<_Tp, _Tp, bool> {
104   typedef bool __result_type; // used by valarray
operatorequal_to105   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
106 };
107 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(equal_to);
108 
109 // The non-transparent std::equal_to specialization is only equivalent to a raw equality
110 // comparison when we don't perform an implicit conversion when calling it.
111 template <class _Tp>
112 inline const bool __desugars_to_v<__equal_tag, equal_to<_Tp>, _Tp, _Tp> = true;
113 
114 // In the transparent case, we do not enforce that
115 template <class _Tp, class _Up>
116 inline const bool __desugars_to_v<__equal_tag, equal_to<void>, _Tp, _Up> = true;
117 
118 template <class _Tp>
119 struct _LIBCPP_TEMPLATE_VIS not_equal_to : __binary_function<_Tp, _Tp, bool> {
120   typedef bool __result_type; // used by valarray
operatornot_equal_to121   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
122 };
123 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(not_equal_to);
124 
125 template <class _Tp>
126 struct _LIBCPP_TEMPLATE_VIS less : __binary_function<_Tp, _Tp, bool> {
127   typedef bool __result_type; // used by valarray
operatorless128   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
129 };
130 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less);
131 
132 template <class _Tp>
133 inline const bool __desugars_to_v<__less_tag, less<_Tp>, _Tp, _Tp> = true;
134 
135 template <class _Tp>
136 struct _LIBCPP_TEMPLATE_VIS less_equal : __binary_function<_Tp, _Tp, bool> {
137   typedef bool __result_type; // used by valarray
operatorless_equal138   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
139 };
140 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less_equal);
141 
142 template <class _Tp>
143 struct _LIBCPP_TEMPLATE_VIS greater_equal : __binary_function<_Tp, _Tp, bool> {
144   typedef bool __result_type; // used by valarray
operatorgreater_equal145   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
146 };
147 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater_equal);
148 
149 template <class _Tp>
150 struct _LIBCPP_TEMPLATE_VIS greater : __binary_function<_Tp, _Tp, bool> {
151   typedef bool __result_type; // used by valarray
operatorgreater152   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
153 };
154 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater);
155 
156 // Logical operations
157 
158 template <class _Tp>
159 struct _LIBCPP_TEMPLATE_VIS logical_and : __binary_function<_Tp, _Tp, bool> {
160   typedef bool __result_type; // used by valarray
operatorlogical_and161   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
162 };
163 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_and);
164 
165 template <class _Tp>
166 struct _LIBCPP_TEMPLATE_VIS logical_not : __unary_function<_Tp, bool> {
167   typedef bool __result_type; // used by valarray
operatorlogical_not168   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x) const { return !__x; }
169 };
170 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_not);
171 
172 template <class _Tp>
173 struct _LIBCPP_TEMPLATE_VIS logical_or : __binary_function<_Tp, _Tp, bool> {
174   typedef bool __result_type; // used by valarray
operatorlogical_or175   _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
176 };
177 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_or);
178 
179 _LIBCPP_END_NAMESPACE_STD
180 
181 #endif // _LIBCPP___CXX03___FUNCTIONAL_OPERATIONS_H
182