|
1 /* |
|
2 * |
|
3 * Copyright (c) 1994 |
|
4 * Hewlett-Packard Company |
|
5 * |
|
6 * Copyright (c) 1996-1998 |
|
7 * Silicon Graphics Computer Systems, Inc. |
|
8 * |
|
9 * Copyright (c) 1997 |
|
10 * Moscow Center for SPARC Technology |
|
11 * |
|
12 * Copyright (c) 1999 |
|
13 * Boris Fomitchev |
|
14 * |
|
15 * This material is provided "as is", with absolutely no warranty expressed |
|
16 * or implied. Any use is at your own risk. |
|
17 * |
|
18 * Permission to use or copy this software for any purpose is hereby granted |
|
19 * without fee, provided the above notices are retained on all copies. |
|
20 * Permission to modify the code and to distribute modified code is granted, |
|
21 * provided the above notices are retained, and a notice that the code was |
|
22 * modified is included with the above copyright notice. |
|
23 * |
|
24 */ |
|
25 |
|
26 /* NOTE: This is an internal header file, included by other STL headers. |
|
27 * You should not attempt to use it directly. |
|
28 */ |
|
29 |
|
30 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H |
|
31 #define _STLP_INTERNAL_FUNCTION_BASE_H |
|
32 |
|
33 #ifndef _STLP_CONFIG_H |
|
34 #include <stl/_config.h> |
|
35 #endif |
|
36 |
|
37 _STLP_BEGIN_NAMESPACE |
|
38 |
|
39 template <class _Arg, class _Result> |
|
40 struct unary_function { |
|
41 typedef _Arg argument_type; |
|
42 typedef _Result result_type; |
|
43 }; |
|
44 |
|
45 template <class _Arg1, class _Arg2, class _Result> |
|
46 struct binary_function { |
|
47 typedef _Arg1 first_argument_type; |
|
48 typedef _Arg2 second_argument_type; |
|
49 typedef _Result result_type; |
|
50 }; |
|
51 |
|
52 template <class _Tp> |
|
53 struct equal_to : public binary_function<_Tp,_Tp,bool> |
|
54 { |
|
55 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; } |
|
56 }; |
|
57 |
|
58 template <class _Tp> |
|
59 struct not_equal_to : public binary_function<_Tp,_Tp,bool> |
|
60 { |
|
61 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; } |
|
62 }; |
|
63 |
|
64 template <class _Tp> |
|
65 struct greater : public binary_function<_Tp,_Tp,bool> |
|
66 { |
|
67 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; } |
|
68 }; |
|
69 |
|
70 template <class _Tp> |
|
71 struct less : public binary_function<_Tp,_Tp,bool> |
|
72 { |
|
73 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; } |
|
74 }; |
|
75 |
|
76 template <class _Tp> |
|
77 struct greater_equal : public binary_function<_Tp,_Tp,bool> |
|
78 { |
|
79 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; } |
|
80 }; |
|
81 |
|
82 template <class _Tp> |
|
83 struct less_equal : public binary_function<_Tp,_Tp,bool> |
|
84 { |
|
85 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; } |
|
86 }; |
|
87 |
|
88 template <class _Tp> |
|
89 less<_Tp> __less(_Tp* ) { return less<_Tp>(); } |
|
90 |
|
91 template <class _Tp> |
|
92 equal_to<_Tp> __equal_to(_Tp* ) { return equal_to<_Tp>(); } |
|
93 |
|
94 template <class _Tp> |
|
95 struct plus : public binary_function<_Tp,_Tp,_Tp> { |
|
96 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; } |
|
97 }; |
|
98 |
|
99 template <class _Tp> |
|
100 struct minus : public binary_function<_Tp,_Tp,_Tp> { |
|
101 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; } |
|
102 }; |
|
103 |
|
104 template <class _Tp> |
|
105 plus<_Tp> __plus(_Tp* ) { return plus<_Tp>(); } |
|
106 |
|
107 template <class _Tp> |
|
108 minus<_Tp> __minus(_Tp* ) { return minus<_Tp>(); } |
|
109 |
|
110 template <class _Tp> |
|
111 struct multiplies : public binary_function<_Tp,_Tp,_Tp> { |
|
112 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; } |
|
113 }; |
|
114 |
|
115 template <class _Tp> |
|
116 struct divides : public binary_function<_Tp,_Tp,_Tp> { |
|
117 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; } |
|
118 }; |
|
119 |
|
120 template <class _Tp> |
|
121 struct modulus : public binary_function<_Tp,_Tp,_Tp> |
|
122 { |
|
123 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; } |
|
124 }; |
|
125 |
|
126 template <class _Tp> |
|
127 struct negate : public unary_function<_Tp,_Tp> |
|
128 { |
|
129 _Tp operator()(const _Tp& __x) const { return -__x; } |
|
130 }; |
|
131 |
|
132 template <class _Tp> |
|
133 struct logical_and : public binary_function<_Tp,_Tp,bool> |
|
134 { |
|
135 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; } |
|
136 }; |
|
137 |
|
138 template <class _Tp> |
|
139 struct logical_or : public binary_function<_Tp,_Tp,bool> |
|
140 { |
|
141 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; } |
|
142 }; |
|
143 |
|
144 template <class _Tp> |
|
145 struct logical_not : public unary_function<_Tp,bool> |
|
146 { |
|
147 bool operator()(const _Tp& __x) const { return !__x; } |
|
148 }; |
|
149 |
|
150 template <class _Pair> |
|
151 struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> { |
|
152 const typename _Pair::first_type& operator()(const _Pair& __x) const { |
|
153 return __x.first; |
|
154 } |
|
155 }; |
|
156 |
|
157 template <class _Pair> |
|
158 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type> |
|
159 { |
|
160 const typename _Pair::second_type& operator()(const _Pair& __x) const { |
|
161 return __x.second; |
|
162 } |
|
163 }; |
|
164 |
|
165 // project1st and project2nd are extensions: they are not part of the standard |
|
166 template <class _Arg1, class _Arg2> |
|
167 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> { |
|
168 _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; } |
|
169 }; |
|
170 |
|
171 template <class _Arg1, class _Arg2> |
|
172 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> { |
|
173 _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; } |
|
174 }; |
|
175 |
|
176 #ifdef _STLP_MULTI_CONST_TEMPLATE_ARG_BUG |
|
177 // fbp : sort of select1st just for maps |
|
178 template <class _Pair, class _Whatever> |
|
179 // JDJ (CW Pro1 doesn't like const when first_type is also const) |
|
180 struct __Select1st_hint : public unary_function<_Pair, _Whatever> { |
|
181 const _Whatever& operator () (const _Pair& __x) const { return __x.first; } |
|
182 }; |
|
183 # define _STLP_SELECT1ST(__x,__y) __Select1st_hint< __x, __y > |
|
184 # else |
|
185 # define _STLP_SELECT1ST(__x, __y) _Select1st< __x > |
|
186 # endif |
|
187 |
|
188 template <class _Tp> |
|
189 struct _Identity : public unary_function<_Tp,_Tp> { |
|
190 const _Tp& operator()(const _Tp& __x) const { return __x; } |
|
191 }; |
|
192 |
|
193 template <class _Result, class _Argument> |
|
194 struct _Constant_unary_fun { |
|
195 typedef _Argument argument_type; |
|
196 typedef _Result result_type; |
|
197 result_type _M_val; |
|
198 |
|
199 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {} |
|
200 const result_type& operator()(const _Argument&) const { return _M_val; } |
|
201 }; |
|
202 |
|
203 template <class _Result, class _Arg1, class _Arg2> |
|
204 struct _Constant_binary_fun { |
|
205 typedef _Arg1 first_argument_type; |
|
206 typedef _Arg2 second_argument_type; |
|
207 typedef _Result result_type; |
|
208 _Result _M_val; |
|
209 |
|
210 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {} |
|
211 const result_type& operator()(const _Arg1&, const _Arg2&) const { |
|
212 return _M_val; |
|
213 } |
|
214 }; |
|
215 |
|
216 // identity_element (not part of the C++ standard). |
|
217 template <class _Tp> inline _Tp __identity_element(plus<_Tp>) { return _Tp(0); } |
|
218 template <class _Tp> inline _Tp __identity_element(multiplies<_Tp>) { return _Tp(1); } |
|
219 |
|
220 _STLP_END_NAMESPACE |
|
221 |
|
222 #endif /* _STLP_INTERNAL_FUNCTION_BASE_H */ |
|
223 |
|
224 // Local Variables: |
|
225 // mode:C++ |
|
226 // End: |