1 // Functional extensions -*- C++ -*-
3 // Copyright (C) 2002-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file ext/functional
52 * This file is a GNU extension to the Standard C++ Library (possibly
53 * containing extensions from the HP/SGI STL subset).
56 #ifndef _EXT_FUNCTIONAL
57 #define _EXT_FUNCTIONAL 1
59 #pragma GCC system_header
63 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 using std::unary_function;
69 using std::binary_function;
70 using std::mem_fun1_t;
71 using std::const_mem_fun1_t;
72 using std::mem_fun1_ref_t;
73 using std::const_mem_fun1_ref_t;
75 /** The @c identity_element functions are not part of the C++
76 * standard; SGI provided them as an extension. Its argument is an
77 * operation, and its return value is the identity element for that
78 * operation. It is overloaded for addition and multiplication,
79 * and you can overload it for your own nefarious operations.
81 * @addtogroup SGIextensions
84 /// An \link SGIextensions SGI extension \endlink.
87 identity_element(std::plus<_Tp>)
90 /// An \link SGIextensions SGI extension \endlink.
93 identity_element(std::multiplies<_Tp>)
97 /** As an extension to the binders, SGI provided composition functors and
98 * wrapper functions to aid in their creation. The @c unary_compose
99 * functor is constructed from two functions/functors, @c f and @c g.
100 * Calling @c operator() with a single argument @c x returns @c f(g(x)).
101 * The function @c compose1 takes the two functions and constructs a
102 * @c unary_compose variable for you.
104 * @c binary_compose is constructed from three functors, @c f, @c g1,
105 * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function
106 * compose2 takes f, g1, and g2, and constructs the @c binary_compose
107 * instance for you. For example, if @c f returns an int, then
109 * int answer = (compose2(f,g1,g2))(x);
115 * int answer = f(temp1,temp2);
117 * But the first form is more compact, and can be passed around as a
118 * functor to other algorithms.
120 * @addtogroup SGIextensions
123 /// An \link SGIextensions SGI extension \endlink.
124 template <class _Operation1, class _Operation2>
126 : public unary_function<typename _Operation2::argument_type,
127 typename _Operation1::result_type>
134 unary_compose(const _Operation1& __x, const _Operation2& __y)
135 : _M_fn1(__x), _M_fn2(__y) {}
137 typename _Operation1::result_type
138 operator()(const typename _Operation2::argument_type& __x) const
139 { return _M_fn1(_M_fn2(__x)); }
142 /// An \link SGIextensions SGI extension \endlink.
143 template <class _Operation1, class _Operation2>
144 inline unary_compose<_Operation1, _Operation2>
145 compose1(const _Operation1& __fn1, const _Operation2& __fn2)
146 { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); }
148 /// An \link SGIextensions SGI extension \endlink.
149 template <class _Operation1, class _Operation2, class _Operation3>
151 : public unary_function<typename _Operation2::argument_type,
152 typename _Operation1::result_type>
160 binary_compose(const _Operation1& __x, const _Operation2& __y,
161 const _Operation3& __z)
162 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
164 typename _Operation1::result_type
165 operator()(const typename _Operation2::argument_type& __x) const
166 { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); }
169 /// An \link SGIextensions SGI extension \endlink.
170 template <class _Operation1, class _Operation2, class _Operation3>
171 inline binary_compose<_Operation1, _Operation2, _Operation3>
172 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
173 const _Operation3& __fn3)
174 { return binary_compose<_Operation1, _Operation2, _Operation3>
175 (__fn1, __fn2, __fn3); }
178 /** As an extension, SGI provided a functor called @c identity. When a
179 * functor is required but no operations are desired, this can be used as a
180 * pass-through. Its @c operator() returns its argument unchanged.
182 * @addtogroup SGIextensions
186 : public std::_Identity<_Tp> {};
188 /** @c select1st and @c select2nd are extensions provided by SGI. Their
190 * take a @c std::pair as an argument, and return either the first member
191 * or the second member, respectively. They can be used (especially with
192 * the composition functors) to @a strip data from a sequence before
193 * performing the remainder of an algorithm.
195 * @addtogroup SGIextensions
198 /// An \link SGIextensions SGI extension \endlink.
199 template <class _Pair>
201 : public std::_Select1st<_Pair> {};
203 /// An \link SGIextensions SGI extension \endlink.
204 template <class _Pair>
206 : public std::_Select2nd<_Pair> {};
210 // extension documented next
211 template <class _Arg1, class _Arg2>
212 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1>
215 operator()(const _Arg1& __x, const _Arg2&) const
219 template <class _Arg1, class _Arg2>
220 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2>
223 operator()(const _Arg1&, const _Arg2& __y) const
227 /** The @c operator() of the @c project1st functor takes two arbitrary
228 * arguments and returns the first one, while @c project2nd returns the
229 * second one. They are extensions provided by SGI.
231 * @addtogroup SGIextensions
235 /// An \link SGIextensions SGI extension \endlink.
236 template <class _Arg1, class _Arg2>
237 struct project1st : public _Project1st<_Arg1, _Arg2> {};
239 /// An \link SGIextensions SGI extension \endlink.
240 template <class _Arg1, class _Arg2>
241 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
244 // extension documented next
245 template <class _Result>
246 struct _Constant_void_fun
248 typedef _Result result_type;
251 _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
258 template <class _Result, class _Argument>
259 struct _Constant_unary_fun
261 typedef _Argument argument_type;
262 typedef _Result result_type;
265 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
268 operator()(const _Argument&) const
272 template <class _Result, class _Arg1, class _Arg2>
273 struct _Constant_binary_fun
275 typedef _Arg1 first_argument_type;
276 typedef _Arg2 second_argument_type;
277 typedef _Result result_type;
280 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
283 operator()(const _Arg1&, const _Arg2&) const
287 /** These three functors are each constructed from a single arbitrary
288 * variable/value. Later, their @c operator()s completely ignore any
289 * arguments passed, and return the stored value.
290 * - @c constant_void_fun's @c operator() takes no arguments
291 * - @c constant_unary_fun's @c operator() takes one argument (ignored)
292 * - @c constant_binary_fun's @c operator() takes two arguments (ignored)
294 * The helper creator functions @c constant0, @c constant1, and
295 * @c constant2 each take a @a result argument and construct variables of
296 * the appropriate functor type.
298 * @addtogroup SGIextensions
301 /// An \link SGIextensions SGI extension \endlink.
302 template <class _Result>
303 struct constant_void_fun
304 : public _Constant_void_fun<_Result>
306 constant_void_fun(const _Result& __v)
307 : _Constant_void_fun<_Result>(__v) {}
310 /// An \link SGIextensions SGI extension \endlink.
311 template <class _Result, class _Argument = _Result>
312 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
314 constant_unary_fun(const _Result& __v)
315 : _Constant_unary_fun<_Result, _Argument>(__v) {}
318 /// An \link SGIextensions SGI extension \endlink.
319 template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1>
320 struct constant_binary_fun
321 : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
323 constant_binary_fun(const _Result& __v)
324 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
327 /// An \link SGIextensions SGI extension \endlink.
328 template <class _Result>
329 inline constant_void_fun<_Result>
330 constant0(const _Result& __val)
331 { return constant_void_fun<_Result>(__val); }
333 /// An \link SGIextensions SGI extension \endlink.
334 template <class _Result>
335 inline constant_unary_fun<_Result, _Result>
336 constant1(const _Result& __val)
337 { return constant_unary_fun<_Result, _Result>(__val); }
339 /// An \link SGIextensions SGI extension \endlink.
340 template <class _Result>
341 inline constant_binary_fun<_Result,_Result,_Result>
342 constant2(const _Result& __val)
343 { return constant_binary_fun<_Result, _Result, _Result>(__val); }
346 /** The @c subtractive_rng class is documented on
347 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
348 * Note that this code assumes that @c int is 32 bits.
350 * @ingroup SGIextensions
352 class subtractive_rng
353 : public unary_function<unsigned int, unsigned int>
356 unsigned int _M_table[55];
361 /// Returns a number less than the argument.
363 operator()(unsigned int __limit)
365 _M_index1 = (_M_index1 + 1) % 55;
366 _M_index2 = (_M_index2 + 1) % 55;
367 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
368 return _M_table[_M_index1] % __limit;
372 _M_initialize(unsigned int __seed)
374 unsigned int __k = 1;
375 _M_table[54] = __seed;
377 for (__i = 0; __i < 54; __i++)
379 size_t __ii = (21 * (__i + 1) % 55) - 1;
380 _M_table[__ii] = __k;
382 __seed = _M_table[__ii];
384 for (int __loop = 0; __loop < 4; __loop++)
386 for (__i = 0; __i < 55; __i++)
387 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
393 /// Ctor allowing you to initialize the seed.
394 subtractive_rng(unsigned int __seed)
395 { _M_initialize(__seed); }
397 /// Default ctor; initializes its state with some number you don't see.
399 { _M_initialize(161803398u); }
402 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
403 // provided for backward compatibility, they are no longer part of
406 template <class _Ret, class _Tp, class _Arg>
407 inline mem_fun1_t<_Ret, _Tp, _Arg>
408 mem_fun1(_Ret (_Tp::*__f)(_Arg))
409 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
411 template <class _Ret, class _Tp, class _Arg>
412 inline const_mem_fun1_t<_Ret, _Tp, _Arg>
413 mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
414 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
416 template <class _Ret, class _Tp, class _Arg>
417 inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
418 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
419 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
421 template <class _Ret, class _Tp, class _Arg>
422 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
423 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
424 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
426 _GLIBCXX_END_NAMESPACE_VERSION