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