# ext/functional

Go to the documentation of this file.

    00001 // Functional extensions -*- C++ -*-
    00002
    00003 // Copyright (C) 2002, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    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).
    00059  */
    00060
    00061 #ifndef _EXT_FUNCTIONAL
    00062 #define _EXT_FUNCTIONAL 1
    00063
    00064 #pragma GCC system_header
    00065
    00066 #include <functional>
    00067
    00068 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00069
    00070   using std::size_t;
    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++
    00079    *  standard; SGI provided them as an extension.  Its argument is an
    00080    *  operation, and its return value is the identity element for that
    00081    *  operation.  It is overloaded for addition and multiplication,
    00082    *  and you can overload it for your own nefarious operations.
    00083    *
    00084    *  @addtogroup SGIextensions
    00085    *  @{
    00086    */
    00087   /// An \link SGIextensions SGI extension \endlink.
    00088   template <class _Tp>
    00089     inline _Tp
    00090     identity_element(std::plus<_Tp>)
    00091     { return _Tp(0); }
    00092
    00093   /// An \link SGIextensions SGI extension \endlink.
    00094   template <class _Tp>
    00095     inline _Tp
    00096     identity_element(std::multiplies<_Tp>)
    00097     { return _Tp(1); }
    00098   /** @}  */
    00099
    00100   /** As an extension to the binders, SGI provided composition functors and
    00101    *  wrapper functions to aid in their creation.  The @c unary_compose
    00102    *  functor is constructed from two functions/functors, @c f and @c g.
    00103    *  Calling @c operator() with a single argument @c x returns @c f(g(x)).
    00104    *  The function @c compose1 takes the two functions and constructs a
    00105    *  @c unary_compose variable for you.
    00106    *
    00107    *  @c binary_compose is constructed from three functors, @c f, @c g1,
    00108    *  and @c g2.  Its @c operator() returns @c f(g1(x),g2(x)).  The function
    00109    *  @compose2 takes f, g1, and g2, and constructs the @c binary_compose
    00110    *  instance for you.  For example, if @c f returns an int, then
    00111    *  \code
    00112    *  int answer = (compose2(f,g1,g2))(x);
    00113    *  \endcode
    00114    *  is equivalent to
    00115    *  \code
    00116    *  int temp1 = g1(x);
    00117    *  int temp2 = g2(x);
    00118    *  int answer = f(temp1,temp2);
    00119    *  \endcode
    00120    *  But the first form is more compact, and can be passed around as a
    00121    *  functor to other algorithms.
    00122    *
    00123    *  @addtogroup SGIextensions
    00124    *  @{
    00125    */
    00126   /// An \link SGIextensions SGI extension \endlink.
    00127   template <class _Operation1, class _Operation2>
    00128     class unary_compose
    00129     : public unary_function<typename _Operation2::argument_type,
    00130                 typename _Operation1::result_type>
    00131     {
    00132     protected:
    00133       _Operation1 _M_fn1;
    00134       _Operation2 _M_fn2;
    00135
    00136     public:
    00137       unary_compose(const _Operation1& __x, const _Operation2& __y)
    00138       : _M_fn1(__x), _M_fn2(__y) {}
    00139
    00140       typename _Operation1::result_type
    00141       operator()(const typename _Operation2::argument_type& __x) const
    00142       { return _M_fn1(_M_fn2(__x)); }
    00143     };
    00144
    00145   /// An \link SGIextensions SGI extension \endlink.
    00146   template <class _Operation1, class _Operation2>
    00147     inline unary_compose<_Operation1, _Operation2>
    00148     compose1(const _Operation1& __fn1, const _Operation2& __fn2)
    00149     { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); }
    00150
    00151   /// An \link SGIextensions SGI extension \endlink.
    00152   template <class _Operation1, class _Operation2, class _Operation3>
    00153     class binary_compose
    00154     : public unary_function<typename _Operation2::argument_type,
    00155                 typename _Operation1::result_type>
    00156     {
    00157     protected:
    00158       _Operation1 _M_fn1;
    00159       _Operation2 _M_fn2;
    00160       _Operation3 _M_fn3;
    00161
    00162     public:
    00163       binary_compose(const _Operation1& __x, const _Operation2& __y,
    00164              const _Operation3& __z)
    00165       : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
    00166
    00167       typename _Operation1::result_type
    00168       operator()(const typename _Operation2::argument_type& __x) const
    00169       { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); }
    00170     };
    00171
    00172   /// An \link SGIextensions SGI extension \endlink.
    00173   template <class _Operation1, class _Operation2, class _Operation3>
    00174     inline binary_compose<_Operation1, _Operation2, _Operation3>
    00175     compose2(const _Operation1& __fn1, const _Operation2& __fn2,
    00176          const _Operation3& __fn3)
    00177     { return binary_compose<_Operation1, _Operation2, _Operation3>
    00178     (__fn1, __fn2, __fn3); }
    00179   /** @}  */
    00180
    00181   /** As an extension, SGI provided a functor called @c identity.  When a
    00182    *  functor is required but no operations are desired, this can be used as a
    00183    *  pass-through.  Its @c operator() returns its argument unchanged.
    00184    *
    00185    *  @addtogroup SGIextensions
    00186    */
    00187   template <class _Tp>
    00188     struct identity : public std::_Identity<_Tp> {};
    00189
    00190   /** @c select1st and @c select2nd are extensions provided by SGI.  Their
    00191    *  @c operator()s
    00192    *  take a @c std::pair as an argument, and return either the first member
    00193    *  or the second member, respectively.  They can be used (especially with
    00194    *  the composition functors) to "strip" data from a sequence before
    00195    *  performing the remainder of an algorithm.
    00196    *
    00197    *  @addtogroup SGIextensions
    00198    *  @{
    00199    */
    00200   /// An \link SGIextensions SGI extension \endlink.
    00201   template <class _Pair>
    00202     struct select1st : public std::_Select1st<_Pair> {};
    00203
    00204   /// An \link SGIextensions SGI extension \endlink.
    00205   template <class _Pair>
    00206     struct select2nd : public std::_Select2nd<_Pair> {};
    00207   /** @}  */
    00208
    00209   // extension documented next
    00210   template <class _Arg1, class _Arg2>
    00211     struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1>
    00212     {
    00213       _Arg1
    00214       operator()(const _Arg1& __x, const _Arg2&) const
    00215       { return __x; }
    00216     };
    00217
    00218   template <class _Arg1, class _Arg2>
    00219     struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2>
    00220     {
    00221       _Arg2
    00222       operator()(const _Arg1&, const _Arg2& __y) const
    00223       { return __y; }
    00224     };
    00225
    00226   /** The @c operator() of the @c project1st functor takes two arbitrary
    00227    *  arguments and returns the first one, while @c project2nd returns the
    00228    *  second one.  They are extensions provided by SGI.
    00229    *
    00230    *  @addtogroup SGIextensions
    00231    *  @{
    00232    */
    00233
    00234   /// An \link SGIextensions SGI extension \endlink.
    00235   template <class _Arg1, class _Arg2>
    00236     struct project1st : public _Project1st<_Arg1, _Arg2> {};
    00237
    00238   /// An \link SGIextensions SGI extension \endlink.
    00239   template <class _Arg1, class _Arg2>
    00240     struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
    00241   /** @}  */
    00242
    00243   // extension documented next
    00244   template <class _Result>
    00245     struct _Constant_void_fun
    00246     {
    00247       typedef _Result result_type;
    00248       result_type _M_val;
    00249
    00250       _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
    00251
    00252       const result_type&
    00253       operator()() const
    00254       { return _M_val; }
    00255     };
    00256
    00257   template <class _Result, class _Argument>
    00258     struct _Constant_unary_fun
    00259     {
    00260       typedef _Argument argument_type;
    00261       typedef  _Result  result_type;
    00262       result_type _M_val;
    00263
    00264       _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
    00265
    00266       const result_type&
    00267       operator()(const _Argument&) const
    00268       { return _M_val; }
    00269     };
    00270
    00271   template <class _Result, class _Arg1, class _Arg2>
    00272     struct _Constant_binary_fun
    00273     {
    00274       typedef  _Arg1   first_argument_type;
    00275       typedef  _Arg2   second_argument_type;
    00276       typedef  _Result result_type;
    00277       _Result _M_val;
    00278
    00279       _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
    00280
    00281       const result_type&
    00282       operator()(const _Arg1&, const _Arg2&) const
    00283       { return _M_val; }
    00284     };
    00285
    00286   /** These three functors are each constructed from a single arbitrary
    00287    *  variable/value.  Later, their @c operator()s completely ignore any
    00288    *  arguments passed, and return the stored value.
    00289    *  - @c constant_void_fun's @c operator() takes no arguments
    00290    *  - @c constant_unary_fun's @c operator() takes one argument (ignored)
    00291    *  - @c constant_binary_fun's @c operator() takes two arguments (ignored)
    00292    *
    00293    *  The helper creator functions @c constant0, @c constant1, and
    00294    *  @c constant2 each take a "result" argument and construct variables of
    00295    *  the appropriate functor type.
    00296    *
    00297    *  @addtogroup SGIextensions
    00298    *  @{
    00299    */
    00300   /// An \link SGIextensions SGI extension \endlink.
    00301   template <class _Result>
    00302     struct constant_void_fun
    00303     : public _Constant_void_fun<_Result>
    00304     {
    00305       constant_void_fun(const _Result& __v)
    00306       : _Constant_void_fun<_Result>(__v) {}
    00307     };
    00308
    00309   /// An \link SGIextensions SGI extension \endlink.
    00310   template <class _Result, class _Argument = _Result>
    00311     struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
    00312     {
    00313       constant_unary_fun(const _Result& __v)
    00314       : _Constant_unary_fun<_Result, _Argument>(__v) {}
    00315     };
    00316
    00317   /// An \link SGIextensions SGI extension \endlink.
    00318   template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1>
    00319     struct constant_binary_fun
    00320     : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
    00321     {
    00322       constant_binary_fun(const _Result& __v)
    00323       : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
    00324     };
    00325
    00326   /// An \link SGIextensions SGI extension \endlink.
    00327   template <class _Result>
    00328     inline constant_void_fun<_Result>
    00329     constant0(const _Result& __val)
    00330     { return constant_void_fun<_Result>(__val); }
    00331
    00332   /// An \link SGIextensions SGI extension \endlink.
    00333   template <class _Result>
    00334     inline constant_unary_fun<_Result, _Result>
    00335     constant1(const _Result& __val)
    00336     { return constant_unary_fun<_Result, _Result>(__val); }
    00337
    00338   /// An \link SGIextensions SGI extension \endlink.
    00339   template <class _Result>
    00340     inline constant_binary_fun<_Result,_Result,_Result>
    00341     constant2(const _Result& __val)
    00342     { return constant_binary_fun<_Result, _Result, _Result>(__val); }
    00343   /** @}  */
    00344
    00345   /** The @c subtractive_rng class is documented on
    00346    *  <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
    00347    *  Note that this code assumes that @c int is 32 bits.
    00348    *
    00349    *  @ingroup SGIextensions
    00350    */
    00351   class subtractive_rng
    00352   : public unary_function<unsigned int, unsigned int>
    00353   {
    00354   private:
    00355     unsigned int _M_table[55];
    00356     size_t _M_index1;
    00357     size_t _M_index2;
    00358
    00359   public:
    00360     /// Returns a number less than the argument.
    00361     unsigned int
    00362     operator()(unsigned int __limit)
    00363     {
    00364       _M_index1 = (_M_index1 + 1) % 55;
    00365       _M_index2 = (_M_index2 + 1) % 55;
    00366       _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
    00367       return _M_table[_M_index1] % __limit;
    00368     }
    00369
    00370     void
    00371     _M_initialize(unsigned int __seed)
    00372     {
    00373       unsigned int __k = 1;
    00374       _M_table[54] = __seed;
    00375       size_t __i;
    00376       for (__i = 0; __i < 54; __i++)
    00377     {
    00378       size_t __ii = (21 * (__i + 1) % 55) - 1;
    00379       _M_table[__ii] = __k;
    00380       __k = __seed - __k;
    00381       __seed = _M_table[__ii];
    00382     }
    00383       for (int __loop = 0; __loop < 4; __loop++)
    00384     {
    00385       for (__i = 0; __i < 55; __i++)
    00386             _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
    00387     }
    00388       _M_index1 = 0;
    00389       _M_index2 = 31;
    00390     }
    00391
    00392     /// Ctor allowing you to initialize the seed.
    00393     subtractive_rng(unsigned int __seed)
    00394     { _M_initialize(__seed); }
    00395
    00396     /// Default ctor; initializes its state with some number you don't see.
    00397     subtractive_rng()
    00398     { _M_initialize(161803398u); }
    00399   };
    00400
    00401   // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
    00402   // provided for backward compatibility, they are no longer part of
    00403   // the C++ standard.
    00404
    00405   template <class _Ret, class _Tp, class _Arg>
    00406     inline mem_fun1_t<_Ret, _Tp, _Arg>
    00407     mem_fun1(_Ret (_Tp::*__f)(_Arg))
    00408     { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
    00409
    00410   template <class _Ret, class _Tp, class _Arg>
    00411     inline const_mem_fun1_t<_Ret, _Tp, _Arg>
    00412     mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
    00413     { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
    00414
    00415   template <class _Ret, class _Tp, class _Arg>
    00416     inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
    00417     mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
    00418     { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
    00419
    00420   template <class _Ret, class _Tp, class _Arg>
    00421     inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
    00422     mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
    00423     { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
    00424
    00425 _GLIBCXX_END_NAMESPACE
    00426
    00427 #endif
    00428