1 _function.h |
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_H |
|
31 #define _STLP_INTERNAL_FUNCTION_H |
|
32 |
|
33 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H |
|
34 #include <stl/_function_base.h> |
|
35 #endif |
|
36 |
|
37 _STLP_BEGIN_NAMESPACE |
|
38 |
|
39 # ifndef _STLP_NO_EXTENSIONS |
|
40 // identity_element (not part of the C++ standard). |
|
41 template <class _Tp> inline _Tp identity_element(plus<_Tp>) { return _Tp(0); } |
|
42 template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) { return _Tp(1); } |
|
43 # endif |
|
44 |
|
45 # if defined (_STLP_BASE_TYPEDEF_BUG) |
|
46 // this workaround is needed for SunPro 4.0.1 |
|
47 // suggested by "Martin Abernethy" <gma@paston.co.uk>: |
|
48 |
|
49 // We have to introduce the XXary_predicate_aux structures in order to |
|
50 // access the argument and return types of predicate functions supplied |
|
51 // as type parameters. SUN C++ 4.0.1 compiler gives errors for template type parameters |
|
52 // of the form 'name1::name2', where name1 is itself a type parameter. |
|
53 template <class _Pair> |
|
54 struct __pair_aux : private _Pair |
|
55 { |
|
56 typedef typename _Pair::first_type first_type; |
|
57 typedef typename _Pair::second_type second_type; |
|
58 }; |
|
59 |
|
60 template <class _Operation> |
|
61 struct __unary_fun_aux : private _Operation |
|
62 { |
|
63 typedef typename _Operation::argument_type argument_type; |
|
64 typedef typename _Operation::result_type result_type; |
|
65 }; |
|
66 |
|
67 template <class _Operation> |
|
68 struct __binary_fun_aux : private _Operation |
|
69 { |
|
70 typedef typename _Operation::first_argument_type first_argument_type; |
|
71 typedef typename _Operation::second_argument_type second_argument_type; |
|
72 typedef typename _Operation::result_type result_type; |
|
73 }; |
|
74 |
|
75 # define __UNARY_ARG(__Operation,__type) __unary_fun_aux<__Operation>::__type |
|
76 # define __BINARY_ARG(__Operation,__type) __binary_fun_aux<__Operation>::__type |
|
77 # define __PAIR_ARG(__Pair,__type) __pair_aux<__Pair>::__type |
|
78 # else |
|
79 # define __UNARY_ARG(__Operation,__type) __Operation::__type |
|
80 # define __BINARY_ARG(__Operation,__type) __Operation::__type |
|
81 # define __PAIR_ARG(__Pair,__type) __Pair::__type |
|
82 # endif |
|
83 |
|
84 template <class _Predicate> |
|
85 class unary_negate : |
|
86 public unary_function<typename __UNARY_ARG(_Predicate,argument_type), bool> { |
|
87 protected: |
|
88 _Predicate _M_pred; |
|
89 public: |
|
90 explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {} |
|
91 bool operator()(const typename _Predicate::argument_type& __x) const { |
|
92 return !_M_pred(__x); |
|
93 } |
|
94 }; |
|
95 |
|
96 template <class _Predicate> |
|
97 inline unary_negate<_Predicate> |
|
98 not1(const _Predicate& __pred) |
|
99 { |
|
100 return unary_negate<_Predicate>(__pred); |
|
101 } |
|
102 |
|
103 template <class _Predicate> |
|
104 class binary_negate |
|
105 : public binary_function<typename __BINARY_ARG(_Predicate,first_argument_type), |
|
106 typename __BINARY_ARG(_Predicate,second_argument_type), |
|
107 bool> { |
|
108 protected: |
|
109 _Predicate _M_pred; |
|
110 public: |
|
111 explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {} |
|
112 bool operator()(const typename _Predicate::first_argument_type& __x, |
|
113 const typename _Predicate::second_argument_type& __y) const |
|
114 { |
|
115 return !_M_pred(__x, __y); |
|
116 } |
|
117 }; |
|
118 |
|
119 template <class _Predicate> |
|
120 inline binary_negate<_Predicate> |
|
121 not2(const _Predicate& __pred) |
|
122 { |
|
123 return binary_negate<_Predicate>(__pred); |
|
124 } |
|
125 |
|
126 template <class _Operation> |
|
127 class binder1st : |
|
128 public unary_function<typename __BINARY_ARG(_Operation,second_argument_type), |
|
129 typename __BINARY_ARG(_Operation,result_type) > { |
|
130 protected: |
|
131 _Operation op; |
|
132 typename _Operation::first_argument_type value; |
|
133 public: |
|
134 binder1st(const _Operation& __x, |
|
135 const typename _Operation::first_argument_type& __y) |
|
136 : op(__x), value(__y) {} |
|
137 |
|
138 typename _Operation::result_type |
|
139 operator()(const typename _Operation::second_argument_type& __x) const { |
|
140 return op(value, __x); |
|
141 } |
|
142 |
|
143 typename _Operation::result_type |
|
144 operator()(typename _Operation::second_argument_type& __x) const { |
|
145 return op(value, __x); |
|
146 } |
|
147 }; |
|
148 |
|
149 template <class _Operation, class _Tp> |
|
150 inline binder1st<_Operation> |
|
151 bind1st(const _Operation& __fn, const _Tp& __x) |
|
152 { |
|
153 typedef typename _Operation::first_argument_type _Arg1_type; |
|
154 return binder1st<_Operation>(__fn, _Arg1_type(__x)); |
|
155 } |
|
156 |
|
157 template <class _Operation> |
|
158 class binder2nd |
|
159 : public unary_function<typename __BINARY_ARG(_Operation,first_argument_type), |
|
160 typename __BINARY_ARG(_Operation,result_type)> { |
|
161 protected: |
|
162 _Operation op; |
|
163 typename _Operation::second_argument_type value; |
|
164 public: |
|
165 binder2nd(const _Operation& __x, |
|
166 const typename _Operation::second_argument_type& __y) |
|
167 : op(__x), value(__y) {} |
|
168 |
|
169 typename _Operation::result_type |
|
170 operator()(const typename _Operation::first_argument_type& __x) const { |
|
171 return op(__x, value); |
|
172 } |
|
173 |
|
174 typename _Operation::result_type |
|
175 operator()(typename _Operation::first_argument_type& __x) const { |
|
176 return op(__x, value); |
|
177 } |
|
178 }; |
|
179 |
|
180 template <class _Operation, class _Tp> |
|
181 inline binder2nd<_Operation> |
|
182 bind2nd(const _Operation& __fn, const _Tp& __x) |
|
183 { |
|
184 typedef typename _Operation::second_argument_type _Arg2_type; |
|
185 return binder2nd<_Operation>(__fn, _Arg2_type(__x)); |
|
186 } |
|
187 |
|
188 # ifndef _STLP_NO_EXTENSIONS |
|
189 // unary_compose and binary_compose (extensions, not part of the standard). |
|
190 |
|
191 template <class _Operation1, class _Operation2> |
|
192 class unary_compose : |
|
193 public unary_function<typename __UNARY_ARG(_Operation2,argument_type), |
|
194 typename __UNARY_ARG(_Operation1,result_type)> { |
|
195 protected: |
|
196 _Operation1 _M_fn1; |
|
197 _Operation2 _M_fn2; |
|
198 public: |
|
199 unary_compose(const _Operation1& __x, const _Operation2& __y) |
|
200 : _M_fn1(__x), _M_fn2(__y) {} |
|
201 |
|
202 typename _Operation1::result_type |
|
203 operator()(const typename _Operation2::argument_type& __x) const { |
|
204 return _M_fn1(_M_fn2(__x)); |
|
205 } |
|
206 |
|
207 typename _Operation1::result_type |
|
208 operator()(typename _Operation2::argument_type& __x) const { |
|
209 return _M_fn1(_M_fn2(__x)); |
|
210 } |
|
211 }; |
|
212 |
|
213 template <class _Operation1, class _Operation2> |
|
214 inline unary_compose<_Operation1,_Operation2> |
|
215 compose1(const _Operation1& __fn1, const _Operation2& __fn2) |
|
216 { |
|
217 return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); |
|
218 } |
|
219 |
|
220 template <class _Operation1, class _Operation2, class _Operation3> |
|
221 class binary_compose : |
|
222 public unary_function<typename __UNARY_ARG(_Operation2,argument_type), |
|
223 typename __BINARY_ARG(_Operation1,result_type)> { |
|
224 protected: |
|
225 _Operation1 _M_fn1; |
|
226 _Operation2 _M_fn2; |
|
227 _Operation3 _M_fn3; |
|
228 public: |
|
229 binary_compose(const _Operation1& __x, const _Operation2& __y, |
|
230 const _Operation3& __z) |
|
231 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { } |
|
232 |
|
233 typename _Operation1::result_type |
|
234 operator()(const typename _Operation2::argument_type& __x) const { |
|
235 return _M_fn1(_M_fn2(__x), _M_fn3(__x)); |
|
236 } |
|
237 |
|
238 typename _Operation1::result_type |
|
239 operator()(typename _Operation2::argument_type& __x) const { |
|
240 return _M_fn1(_M_fn2(__x), _M_fn3(__x)); |
|
241 } |
|
242 }; |
|
243 |
|
244 template <class _Operation1, class _Operation2, class _Operation3> |
|
245 inline binary_compose<_Operation1, _Operation2, _Operation3> |
|
246 compose2(const _Operation1& __fn1, const _Operation2& __fn2, |
|
247 const _Operation3& __fn3) |
|
248 { |
|
249 return binary_compose<_Operation1,_Operation2,_Operation3> |
|
250 (__fn1, __fn2, __fn3); |
|
251 } |
|
252 |
|
253 # endif /* _STLP_NO_EXTENSIONS */ |
|
254 |
|
255 # ifndef _STLP_NO_EXTENSIONS |
|
256 |
|
257 // identity is an extension: it is not part of the standard. |
|
258 template <class _Tp> struct identity : public _Identity<_Tp> {}; |
|
259 // select1st and select2nd are extensions: they are not part of the standard. |
|
260 template <class _Pair> struct select1st : public _Select1st<_Pair> {}; |
|
261 template <class _Pair> struct select2nd : public _Select2nd<_Pair> {}; |
|
262 |
|
263 template <class _Arg1, class _Arg2> |
|
264 struct project1st : public _Project1st<_Arg1, _Arg2> {}; |
|
265 |
|
266 template <class _Arg1, class _Arg2> |
|
267 struct project2nd : public _Project2nd<_Arg1, _Arg2> {}; |
|
268 |
|
269 |
|
270 // constant_void_fun, constant_unary_fun, and constant_binary_fun are |
|
271 // extensions: they are not part of the standard. (The same, of course, |
|
272 // is true of the helper functions constant0, constant1, and constant2.) |
|
273 |
|
274 template <class _Result> |
|
275 struct _Constant_void_fun { |
|
276 typedef _Result result_type; |
|
277 result_type _M_val; |
|
278 |
|
279 _Constant_void_fun(const result_type& __v) : _M_val(__v) {} |
|
280 const result_type& operator()() const { return _M_val; } |
|
281 }; |
|
282 |
|
283 |
|
284 template <class _Result> |
|
285 struct constant_void_fun : public _Constant_void_fun<_Result> { |
|
286 constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {} |
|
287 }; |
|
288 |
|
289 template <class _Result, __DFL_TMPL_PARAM( _Argument , _Result) > |
|
290 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument> |
|
291 { |
|
292 constant_unary_fun(const _Result& __v) |
|
293 : _Constant_unary_fun<_Result, _Argument>(__v) {} |
|
294 }; |
|
295 |
|
296 template <class _Result, __DFL_TMPL_PARAM( _Arg1 , _Result), __DFL_TMPL_PARAM( _Arg2 , _Arg1) > |
|
297 struct constant_binary_fun |
|
298 : public _Constant_binary_fun<_Result, _Arg1, _Arg2> |
|
299 { |
|
300 constant_binary_fun(const _Result& __v) |
|
301 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {} |
|
302 }; |
|
303 |
|
304 template <class _Result> |
|
305 inline constant_void_fun<_Result> constant0(const _Result& __val) |
|
306 { |
|
307 return constant_void_fun<_Result>(__val); |
|
308 } |
|
309 |
|
310 template <class _Result> |
|
311 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val) |
|
312 { |
|
313 return constant_unary_fun<_Result,_Result>(__val); |
|
314 } |
|
315 |
|
316 template <class _Result> |
|
317 inline constant_binary_fun<_Result,_Result,_Result> |
|
318 constant2(const _Result& __val) |
|
319 { |
|
320 return constant_binary_fun<_Result,_Result,_Result>(__val); |
|
321 } |
|
322 |
|
323 // subtractive_rng is an extension: it is not part of the standard. |
|
324 // Note: this code assumes that int is 32 bits. |
|
325 class subtractive_rng : public unary_function<_STLP_UINT32_T, _STLP_UINT32_T> { |
|
326 private: |
|
327 _STLP_UINT32_T _M_table[55]; |
|
328 _STLP_UINT32_T _M_index1; |
|
329 _STLP_UINT32_T _M_index2; |
|
330 public: |
|
331 _STLP_UINT32_T operator()(_STLP_UINT32_T __limit) { |
|
332 _M_index1 = (_M_index1 + 1) % 55; |
|
333 _M_index2 = (_M_index2 + 1) % 55; |
|
334 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2]; |
|
335 return _M_table[_M_index1] % __limit; |
|
336 } |
|
337 |
|
338 void _M_initialize(_STLP_UINT32_T __seed) |
|
339 { |
|
340 _STLP_UINT32_T __k = 1; |
|
341 _M_table[54] = __seed; |
|
342 _STLP_UINT32_T __i; |
|
343 for (__i = 0; __i < 54; __i++) { |
|
344 _STLP_UINT32_T __ii = (21 * (__i + 1) % 55) - 1; |
|
345 _M_table[__ii] = __k; |
|
346 __k = __seed - __k; |
|
347 __seed = _M_table[__ii]; |
|
348 } |
|
349 for (int __loop = 0; __loop < 4; __loop++) { |
|
350 for (__i = 0; __i < 55; __i++) |
|
351 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55]; |
|
352 } |
|
353 _M_index1 = 0; |
|
354 _M_index2 = 31; |
|
355 } |
|
356 |
|
357 subtractive_rng(unsigned int __seed) { _M_initialize(__seed); } |
|
358 subtractive_rng() { _M_initialize(161803398ul); } |
|
359 }; |
|
360 |
|
361 # endif /* _STLP_NO_EXTENSIONS */ |
|
362 |
|
363 _STLP_END_NAMESPACE |
|
364 |
|
365 #include <stl/_function_adaptors.h> |
|
366 |
|
367 #endif /* _STLP_INTERNAL_FUNCTION_H */ |
|
368 |
|
369 // Local Variables: |
|
370 // mode:C++ |
|
371 // End: |