1 // Functional extensions -*- C++ -*-
3 // Copyright (C) 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 #ifndef _EXT_FUNCTIONAL
57 #define _EXT_FUNCTIONAL
59 #pragma GCC system_header
64 using std::unary_function;
65 using std::binary_function;
66 using std::mem_fun1_t;
67 using std::const_mem_fun1_t;
68 using std::mem_fun1_ref_t;
69 using std::const_mem_fun1_ref_t;
71 /** The @c identity_element functions are not part of the C++ standard; SGI
72 * provided them as an extension. Its argument is an operation, and its
73 * return value is the identity element for that operation. It is overloaded
74 * for addition and multiplication, and you can overload it for your own
75 * nefarious operations.
77 * @addtogroup SGIextensions
80 /// An \link SGIextensions SGI extension \endlink.
81 template <class _Tp> inline _Tp identity_element(std::plus<_Tp>) {
84 /// An \link SGIextensions SGI extension \endlink.
85 template <class _Tp> inline _Tp identity_element(std::multiplies<_Tp>) {
90 /** As an extension to the binders, SGI provided composition functors and
91 * wrapper functions to aid in their creation. The @c unary_compose
92 * functor is constructed from two functions/functors, @c f and @c g.
93 * Calling @c operator() with a single argument @c x returns @c f(g(x)).
94 * The function @c compose1 takes the two functions and constructs a
95 * @c unary_compose variable for you.
97 * @c binary_compose is constructed from three functors, @c f, @c g1,
98 * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function
99 * @compose2 takes f, g1, and g2, and constructs the @c binary_compose
100 * instance for you. For example, if @c f returns an int, then
102 * int answer = (compose2(f,g1,g2))(x);
108 * int answer = f(temp1,temp2);
110 * But the first form is more compact, and can be passed around as a
111 * functor to other algorithms.
113 * @addtogroup SGIextensions
116 /// An \link SGIextensions SGI extension \endlink.
117 template <class _Operation1, class _Operation2>
119 : public unary_function<typename _Operation2::argument_type,
120 typename _Operation1::result_type>
126 unary_compose(const _Operation1& __x, const _Operation2& __y)
127 : _M_fn1(__x), _M_fn2(__y) {}
128 typename _Operation1::result_type
129 operator()(const typename _Operation2::argument_type& __x) const {
130 return _M_fn1(_M_fn2(__x));
134 /// An \link SGIextensions SGI extension \endlink.
135 template <class _Operation1, class _Operation2>
136 inline unary_compose<_Operation1,_Operation2>
137 compose1(const _Operation1& __fn1, const _Operation2& __fn2)
139 return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
142 /// An \link SGIextensions SGI extension \endlink.
143 template <class _Operation1, class _Operation2, class _Operation3>
145 : public unary_function<typename _Operation2::argument_type,
146 typename _Operation1::result_type> {
152 binary_compose(const _Operation1& __x, const _Operation2& __y,
153 const _Operation3& __z)
154 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
155 typename _Operation1::result_type
156 operator()(const typename _Operation2::argument_type& __x) const {
157 return _M_fn1(_M_fn2(__x), _M_fn3(__x));
161 /// An \link SGIextensions SGI extension \endlink.
162 template <class _Operation1, class _Operation2, class _Operation3>
163 inline binary_compose<_Operation1, _Operation2, _Operation3>
164 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
165 const _Operation3& __fn3)
167 return binary_compose<_Operation1,_Operation2,_Operation3>
168 (__fn1, __fn2, __fn3);
172 /** As an extension, SGI provided a functor called @c identity. When a
173 * functor is required but no operations are desired, this can be used as a
174 * pass-through. Its @c operator() returns its argument unchanged.
176 * @addtogroup SGIextensions
178 template <class _Tp> struct identity : public std::_Identity<_Tp> {};
180 /** @c select1st and @c select2nd are extensions provided by SGI. Their
182 * take a @c std::pair as an argument, and return either the first member
183 * or the second member, respectively. They can be used (especially with
184 * the composition functors) to "strip" data from a sequence before
185 * performing the remainder of an algorithm.
187 * @addtogroup SGIextensions
190 /// An \link SGIextensions SGI extension \endlink.
191 template <class _Pair> struct select1st : public std::_Select1st<_Pair> {};
192 /// An \link SGIextensions SGI extension \endlink.
193 template <class _Pair> struct select2nd : public std::_Select2nd<_Pair> {};
196 // extension documented next
197 template <class _Arg1, class _Arg2>
198 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
199 _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
202 template <class _Arg1, class _Arg2>
203 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
204 _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
207 /** The @c operator() of the @c project1st functor takes two arbitrary
208 * arguments and returns the first one, while @c project2nd returns the
209 * second one. They are extensions provided by SGI.
211 * @addtogroup SGIextensions
215 /// An \link SGIextensions SGI extension \endlink.
216 template <class _Arg1, class _Arg2>
217 struct project1st : public _Project1st<_Arg1, _Arg2> {};
219 /// An \link SGIextensions SGI extension \endlink.
220 template <class _Arg1, class _Arg2>
221 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
224 // extension documented next
225 template <class _Result>
226 struct _Constant_void_fun {
227 typedef _Result result_type;
230 _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
231 const result_type& operator()() const { return _M_val; }
234 template <class _Result, class _Argument>
235 struct _Constant_unary_fun {
236 typedef _Argument argument_type;
237 typedef _Result result_type;
240 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
241 const result_type& operator()(const _Argument&) const { return _M_val; }
244 template <class _Result, class _Arg1, class _Arg2>
245 struct _Constant_binary_fun {
246 typedef _Arg1 first_argument_type;
247 typedef _Arg2 second_argument_type;
248 typedef _Result result_type;
251 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
252 const result_type& operator()(const _Arg1&, const _Arg2&) const {
257 /** These three functors are each constructed from a single arbitrary
258 * variable/value. Later, their @c operator()s completely ignore any
259 * arguments passed, and return the stored value.
260 * - @c constant_void_fun's @c operator() takes no arguments
261 * - @c constant_unary_fun's @c operator() takes one argument (ignored)
262 * - @c constant_binary_fun's @c operator() takes two arguments (ignored)
264 * The helper creator functions @c constant0, @c constant1, and
265 * @c constant2 each take a "result" argument and construct variables of
266 * the appropriate functor type.
268 * @addtogroup SGIextensions
271 /// An \link SGIextensions SGI extension \endlink.
272 template <class _Result>
273 struct constant_void_fun : public _Constant_void_fun<_Result> {
274 constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {}
277 /// An \link SGIextensions SGI extension \endlink.
278 template <class _Result,
279 class _Argument = _Result>
280 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
282 constant_unary_fun(const _Result& __v)
283 : _Constant_unary_fun<_Result, _Argument>(__v) {}
286 /// An \link SGIextensions SGI extension \endlink.
287 template <class _Result,
288 class _Arg1 = _Result,
290 struct constant_binary_fun
291 : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
293 constant_binary_fun(const _Result& __v)
294 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
297 /// An \link SGIextensions SGI extension \endlink.
298 template <class _Result>
299 inline constant_void_fun<_Result> constant0(const _Result& __val)
301 return constant_void_fun<_Result>(__val);
304 /// An \link SGIextensions SGI extension \endlink.
305 template <class _Result>
306 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val)
308 return constant_unary_fun<_Result,_Result>(__val);
311 /// An \link SGIextensions SGI extension \endlink.
312 template <class _Result>
313 inline constant_binary_fun<_Result,_Result,_Result>
314 constant2(const _Result& __val)
316 return constant_binary_fun<_Result,_Result,_Result>(__val);
320 /** The @c subtractive_rng class is documented on
321 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
322 * Note that this code assumes that @c int is 32 bits.
324 * @ingroup SGIextensions
326 class subtractive_rng : public unary_function<unsigned int, unsigned int> {
328 unsigned int _M_table[55];
332 /// Returns a number less than the argument.
333 unsigned int operator()(unsigned int __limit) {
334 _M_index1 = (_M_index1 + 1) % 55;
335 _M_index2 = (_M_index2 + 1) % 55;
336 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
337 return _M_table[_M_index1] % __limit;
340 void _M_initialize(unsigned int __seed)
342 unsigned int __k = 1;
343 _M_table[54] = __seed;
345 for (__i = 0; __i < 54; __i++) {
346 size_t __ii = (21 * (__i + 1) % 55) - 1;
347 _M_table[__ii] = __k;
349 __seed = _M_table[__ii];
351 for (int __loop = 0; __loop < 4; __loop++) {
352 for (__i = 0; __i < 55; __i++)
353 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
359 /// Ctor allowing you to initialize the seed.
360 subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
361 /// Default ctor; initializes its state with some number you don't see.
362 subtractive_rng() { _M_initialize(161803398u); }
365 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
366 // provided for backward compatibility, they are no longer part of
369 template <class _Ret, class _Tp, class _Arg>
370 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg))
371 { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
373 template <class _Ret, class _Tp, class _Arg>
374 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
375 { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
377 template <class _Ret, class _Tp, class _Arg>
378 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
379 { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
381 template <class _Ret, class _Tp, class _Arg>
382 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
383 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
384 { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
386 } // namespace __gnu_cxx
388 #endif /* _EXT_FUNCTIONAL */