ext/functional

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