ext/functional

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

Generated on Wed Mar 26 00:42:58 2008 for libstdc++ by  doxygen 1.5.1