# functional

Go to the documentation of this file.

    00001 // Functional extensions -*- C++ -*-
    00002
    00003 // Copyright (C) 2002 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1996
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00056 /** @file ext/functional
    00057  *  This file is a GNU extension to the Standard C++ Library (possibly
    00058  *  containing extensions from the HP/SGI STL subset).  You should only
    00059  *  include this header if you are using GCC 3 or later.
    00060  */
    00061
    00062 #ifndef _EXT_FUNCTIONAL
    00063 #define _EXT_FUNCTIONAL 1
    00064
    00065 #pragma GCC system_header
    00066
    00067 #include <functional>
    00068
    00069 namespace __gnu_cxx
    00070 {
    00071 using std::unary_function;
    00072 using std::binary_function;
    00073 using std::mem_fun1_t;
    00074 using std::const_mem_fun1_t;
    00075 using std::mem_fun1_ref_t;
    00076 using std::const_mem_fun1_ref_t;
    00077
    00078 /** The @c identity_element functions are not part of the C++ standard; SGI
    00079  *  provided them as an extension.  Its argument is an operation, and its
    00080  *  return value is the identity element for that operation.  It is overloaded
    00081  *  for addition and multiplication, and you can overload it for your own
    00082  *  nefarious operations.
    00083  *
    00084  *  @addtogroup SGIextensions
    00085  *  @{
    00086 */
    00087 /// An \link SGIextensions SGI extension \endlink.
    00088 template <class _Tp> inline _Tp identity_element(std::plus<_Tp>) {
    00089   return _Tp(0);
    00090 }
    00091 /// An \link SGIextensions SGI extension \endlink.
    00092 template <class _Tp> inline _Tp identity_element(std::multiplies<_Tp>) {
    00093   return _Tp(1);
    00094 }
    00095 /** @}  */
    00096
    00097 /** As an extension to the binders, SGI provided composition functors and
    00098  *  wrapper functions to aid in their creation.  The @c unary_compose
    00099  *  functor is constructed from two functions/functors, @c f and @c g.
    00100  *  Calling @c operator() with a single argument @c x returns @c f(g(x)).
    00101  *  The function @c compose1 takes the two functions and constructs a
    00102  *  @c unary_compose variable for you.
    00103  *
    00104  *  @c binary_compose is constructed from three functors, @c f, @c g1,
    00105  *  and @c g2.  Its @c operator() returns @c f(g1(x),g2(x)).  The function
    00106  *  @compose2 takes f, g1, and g2, and constructs the @c binary_compose
    00107  *  instance for you.  For example, if @c f returns an int, then
    00108  *  \code
    00109  *  int answer = (compose2(f,g1,g2))(x);
    00110  *  \endcode
    00111  *  is equivalent to
    00112  *  \code
    00113  *  int temp1 = g1(x);
    00114  *  int temp2 = g2(x);
    00115  *  int answer = f(temp1,temp2);
    00116  *  \endcode
    00117  *  But the first form is more compact, and can be passed around as a
    00118  *  functor to other algorithms.
    00119  *
    00120  *  @addtogroup SGIextensions
    00121  *  @{
    00122 */
    00123 /// An \link SGIextensions SGI extension \endlink.
    00124 template <class _Operation1, class _Operation2>
    00125 class unary_compose
    00126   : public unary_function<typename _Operation2::argument_type,
    00127                typename _Operation1::result_type>
    00128 {
    00129 protected:
    00130   _Operation1 _M_fn1;
    00131   _Operation2 _M_fn2;
    00132 public:
    00133   unary_compose(const _Operation1& __x, const _Operation2& __y)
    00134     : _M_fn1(__x), _M_fn2(__y) {}
    00135   typename _Operation1::result_type
    00136   operator()(const typename _Operation2::argument_type& __x) const {
    00137     return _M_fn1(_M_fn2(__x));
    00138   }
    00139 };
    00140
    00141 /// An \link SGIextensions SGI extension \endlink.
    00142 template <class _Operation1, class _Operation2>
    00143 inline unary_compose<_Operation1,_Operation2>
    00144 compose1(const _Operation1& __fn1, const _Operation2& __fn2)
    00145 {
    00146   return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
    00147 }
    00148
    00149 /// An \link SGIextensions SGI extension \endlink.
    00150 template <class _Operation1, class _Operation2, class _Operation3>
    00151 class binary_compose
    00152   : public unary_function<typename _Operation2::argument_type,
    00153                           typename _Operation1::result_type> {
    00154 protected:
    00155   _Operation1 _M_fn1;
    00156   _Operation2 _M_fn2;
    00157   _Operation3 _M_fn3;
    00158 public:
    00159   binary_compose(const _Operation1& __x, const _Operation2& __y,
    00160                  const _Operation3& __z)
    00161     : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
    00162   typename _Operation1::result_type
    00163   operator()(const typename _Operation2::argument_type& __x) const {
    00164     return _M_fn1(_M_fn2(__x), _M_fn3(__x));
    00165   }
    00166 };
    00167
    00168 /// An \link SGIextensions SGI extension \endlink.
    00169 template <class _Operation1, class _Operation2, class _Operation3>
    00170 inline binary_compose<_Operation1, _Operation2, _Operation3>
    00171 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
    00172          const _Operation3& __fn3)
    00173 {
    00174   return binary_compose<_Operation1,_Operation2,_Operation3>
    00175     (__fn1, __fn2, __fn3);
    00176 }
    00177 /** @}  */
    00178
    00179 /** As an extension, SGI provided a functor called @c identity.  When a
    00180  *  functor is required but no operations are desired, this can be used as a
    00181  *  pass-through.  Its @c operator() returns its argument unchanged.
    00182  *
    00183  *  @addtogroup SGIextensions
    00184 */
    00185 template <class _Tp> struct identity : public std::_Identity<_Tp> {};
    00186
    00187 /** @c select1st and @c select2nd are extensions provided by SGI.  Their
    00188  *  @c operator()s
    00189  *  take a @c std::pair as an argument, and return either the first member
    00190  *  or the second member, respectively.  They can be used (especially with
    00191  *  the composition functors) to "strip" data from a sequence before
    00192  *  performing the remainder of an algorithm.
    00193  *
    00194  *  @addtogroup SGIextensions
    00195  *  @{
    00196 */
    00197 /// An \link SGIextensions SGI extension \endlink.
    00198 template <class _Pair> struct select1st : public std::_Select1st<_Pair> {};
    00199 /// An \link SGIextensions SGI extension \endlink.
    00200 template <class _Pair> struct select2nd : public std::_Select2nd<_Pair> {};
    00201 /** @}  */
    00202
    00203 // extension documented next
    00204 template <class _Arg1, class _Arg2>
    00205 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
    00206   _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
    00207 };
    00208
    00209 template <class _Arg1, class _Arg2>
    00210 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
    00211   _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
    00212 };
    00213
    00214 /** The @c operator() of the @c project1st functor takes two arbitrary
    00215  *  arguments and returns the first one, while @c project2nd returns the
    00216  *  second one.  They are extensions provided by SGI.
    00217  *
    00218  *  @addtogroup SGIextensions
    00219  *  @{
    00220 */
    00221
    00222 /// An \link SGIextensions SGI extension \endlink.
    00223 template <class _Arg1, class _Arg2>
    00224 struct project1st : public _Project1st<_Arg1, _Arg2> {};
    00225
    00226 /// An \link SGIextensions SGI extension \endlink.
    00227 template <class _Arg1, class _Arg2>
    00228 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
    00229 /** @}  */
    00230
    00231 // extension documented next
    00232 template <class _Result>
    00233 struct _Constant_void_fun {
    00234   typedef _Result result_type;
    00235   result_type _M_val;
    00236
    00237   _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
    00238   const result_type& operator()() const { return _M_val; }
    00239 };
    00240
    00241 template <class _Result, class _Argument>
    00242 struct _Constant_unary_fun {
    00243   typedef _Argument argument_type;
    00244   typedef  _Result  result_type;
    00245   result_type _M_val;
    00246
    00247   _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
    00248   const result_type& operator()(const _Argument&) const { return _M_val; }
    00249 };
    00250
    00251 template <class _Result, class _Arg1, class _Arg2>
    00252 struct _Constant_binary_fun {
    00253   typedef  _Arg1   first_argument_type;
    00254   typedef  _Arg2   second_argument_type;
    00255   typedef  _Result result_type;
    00256   _Result _M_val;
    00257
    00258   _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
    00259   const result_type& operator()(const _Arg1&, const _Arg2&) const {
    00260     return _M_val;
    00261   }
    00262 };
    00263
    00264 /** These three functors are each constructed from a single arbitrary
    00265  *  variable/value.  Later, their @c operator()s completely ignore any
    00266  *  arguments passed, and return the stored value.
    00267  *  - @c constant_void_fun's @c operator() takes no arguments
    00268  *  - @c constant_unary_fun's @c operator() takes one argument (ignored)
    00269  *  - @c constant_binary_fun's @c operator() takes two arguments (ignored)
    00270  *
    00271  *  The helper creator functions @c constant0, @c constant1, and
    00272  *  @c constant2 each take a "result" argument and construct variables of
    00273  *  the appropriate functor type.
    00274  *
    00275  *  @addtogroup SGIextensions
    00276  *  @{
    00277 */
    00278 /// An \link SGIextensions SGI extension \endlink.
    00279 template <class _Result>
    00280 struct constant_void_fun : public _Constant_void_fun<_Result> {
    00281   constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {}
    00282 };
    00283
    00284 /// An \link SGIextensions SGI extension \endlink.
    00285 template <class _Result,
    00286           class _Argument = _Result>
    00287 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
    00288 {
    00289   constant_unary_fun(const _Result& __v)
    00290     : _Constant_unary_fun<_Result, _Argument>(__v) {}
    00291 };
    00292
    00293 /// An \link SGIextensions SGI extension \endlink.
    00294 template <class _Result,
    00295           class _Arg1 = _Result,
    00296           class _Arg2 = _Arg1>
    00297 struct constant_binary_fun
    00298   : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
    00299 {
    00300   constant_binary_fun(const _Result& __v)
    00301     : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
    00302 };
    00303
    00304 /// An \link SGIextensions SGI extension \endlink.
    00305 template <class _Result>
    00306 inline constant_void_fun<_Result> constant0(const _Result& __val)
    00307 {
    00308   return constant_void_fun<_Result>(__val);
    00309 }
    00310
    00311 /// An \link SGIextensions SGI extension \endlink.
    00312 template <class _Result>
    00313 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val)
    00314 {
    00315   return constant_unary_fun<_Result,_Result>(__val);
    00316 }
    00317
    00318 /// An \link SGIextensions SGI extension \endlink.
    00319 template <class _Result>
    00320 inline constant_binary_fun<_Result,_Result,_Result>
    00321 constant2(const _Result& __val)
    00322 {
    00323   return constant_binary_fun<_Result,_Result,_Result>(__val);
    00324 }
    00325 /** @}  */
    00326
    00327 /** The @c subtractive_rng class is documented on
    00328  *  <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
    00329  *  Note that this code assumes that @c int is 32 bits.
    00330  *
    00331  *  @ingroup SGIextensions
    00332 */
    00333 class subtractive_rng : public unary_function<unsigned int, unsigned int> {
    00334 private:
    00335   unsigned int _M_table[55];
    00336   size_t _M_index1;
    00337   size_t _M_index2;
    00338 public:
    00339   /// Returns a number less than the argument.
    00340   unsigned int operator()(unsigned int __limit) {
    00341     _M_index1 = (_M_index1 + 1) % 55;
    00342     _M_index2 = (_M_index2 + 1) % 55;
    00343     _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
    00344     return _M_table[_M_index1] % __limit;
    00345   }
    00346
    00347   void _M_initialize(unsigned int __seed)
    00348   {
    00349     unsigned int __k = 1;
    00350     _M_table[54] = __seed;
    00351     size_t __i;
    00352     for (__i = 0; __i < 54; __i++) {
    00353         size_t __ii = (21 * (__i + 1) % 55) - 1;
    00354         _M_table[__ii] = __k;
    00355         __k = __seed - __k;
    00356         __seed = _M_table[__ii];
    00357     }
    00358     for (int __loop = 0; __loop < 4; __loop++) {
    00359         for (__i = 0; __i < 55; __i++)
    00360             _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
    00361     }
    00362     _M_index1 = 0;
    00363     _M_index2 = 31;
    00364   }
    00365
    00366   /// Ctor allowing you to initialize the seed.
    00367   subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
    00368   /// Default ctor; initializes its state with some number you don't see.
    00369   subtractive_rng() { _M_initialize(161803398u); }
    00370 };
    00371
    00372 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
    00373 // provided for backward compatibility, they are no longer part of
    00374 // the C++ standard.
    00375
    00376 template <class _Ret, class _Tp, class _Arg>
    00377 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg))
    00378   { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
    00379
    00380 template <class _Ret, class _Tp, class _Arg>
    00381 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
    00382   { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
    00383
    00384 template <class _Ret, class _Tp, class _Arg>
    00385 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
    00386   { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
    00387
    00388 template <class _Ret, class _Tp, class _Arg>
    00389 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
    00390 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
    00391   { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
    00392 } // namespace __gnu_cxx
    00393
    00394 #endif
    00395