functional

Go to the documentation of this file.
00001 // Functional extensions -*- C++ -*-
00002 
00003 // Copyright (C) 2002, 2004 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 
00061 #ifndef _EXT_FUNCTIONAL
00062 #define _EXT_FUNCTIONAL 1
00063 
00064 #pragma GCC system_header
00065 
00066 #include <functional>
00067 
00068 namespace __gnu_cxx
00069 {
00070   using std::unary_function;
00071   using std::binary_function;
00072   using std::mem_fun1_t;
00073   using std::const_mem_fun1_t;
00074   using std::mem_fun1_ref_t;
00075   using std::const_mem_fun1_ref_t;
00076 
00086 
00087   template <class _Tp>
00088     inline _Tp
00089     identity_element(std::plus<_Tp>)
00090     { return _Tp(0); }
00091 
00093   template <class _Tp>
00094     inline _Tp
00095     identity_element(std::multiplies<_Tp>)
00096     { return _Tp(1); }
00125 
00126   template <class _Operation1, class _Operation2>
00127     class unary_compose
00128     : public unary_function<typename _Operation2::argument_type,
00129                 typename _Operation1::result_type>
00130     {
00131     protected:
00132       _Operation1 _M_fn1;
00133       _Operation2 _M_fn2;
00134 
00135     public:
00136       unary_compose(const _Operation1& __x, const _Operation2& __y)
00137       : _M_fn1(__x), _M_fn2(__y) {}
00138 
00139       typename _Operation1::result_type
00140       operator()(const typename _Operation2::argument_type& __x) const
00141       { return _M_fn1(_M_fn2(__x)); }
00142     };
00143 
00145   template <class _Operation1, class _Operation2>
00146     inline unary_compose<_Operation1, _Operation2>
00147     compose1(const _Operation1& __fn1, const _Operation2& __fn2)
00148     { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); }
00149 
00151   template <class _Operation1, class _Operation2, class _Operation3>
00152     class binary_compose
00153     : public unary_function<typename _Operation2::argument_type,
00154                 typename _Operation1::result_type>
00155     {
00156     protected:
00157       _Operation1 _M_fn1;
00158       _Operation2 _M_fn2;
00159       _Operation3 _M_fn3;
00160       
00161     public:
00162       binary_compose(const _Operation1& __x, const _Operation2& __y,
00163              const _Operation3& __z)
00164       : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
00165 
00166       typename _Operation1::result_type
00167       operator()(const typename _Operation2::argument_type& __x) const
00168       { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); }
00169     };
00170 
00172   template <class _Operation1, class _Operation2, class _Operation3>
00173     inline binary_compose<_Operation1, _Operation2, _Operation3>
00174     compose2(const _Operation1& __fn1, const _Operation2& __fn2,
00175          const _Operation3& __fn3)
00176     { return binary_compose<_Operation1, _Operation2, _Operation3>
00177     (__fn1, __fn2, __fn3); }
00186   template <class _Tp>
00187     struct identity : public std::_Identity<_Tp> {};
00188 
00199 
00200   template <class _Pair>
00201     struct select1st : public std::_Select1st<_Pair> {};
00202 
00204   template <class _Pair>
00205     struct select2nd : public std::_Select2nd<_Pair> {};
00208   // extension documented next
00209   template <class _Arg1, class _Arg2>
00210     struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1>
00211     {
00212       _Arg1
00213       operator()(const _Arg1& __x, const _Arg2&) const
00214       { return __x; }
00215     };
00216 
00217   template <class _Arg1, class _Arg2>
00218     struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2>
00219     {
00220       _Arg2
00221       operator()(const _Arg1&, const _Arg2& __y) const
00222       { return __y; }
00223     };
00224 
00233 
00234   template <class _Arg1, class _Arg2>
00235     struct project1st : public _Project1st<_Arg1, _Arg2> {};
00236 
00238   template <class _Arg1, class _Arg2>
00239     struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
00242   // extension documented next
00243   template <class _Result>
00244     struct _Constant_void_fun
00245     {
00246       typedef _Result result_type;
00247       result_type _M_val;
00248 
00249       _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
00250 
00251       const result_type&
00252       operator()() const
00253       { return _M_val; }
00254     };
00255 
00256   template <class _Result, class _Argument>
00257     struct _Constant_unary_fun
00258     {
00259       typedef _Argument argument_type;
00260       typedef  _Result  result_type;
00261       result_type _M_val;
00262       
00263       _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
00264 
00265       const result_type&
00266       operator()(const _Argument&) const
00267       { return _M_val; }
00268     };
00269 
00270   template <class _Result, class _Arg1, class _Arg2>
00271     struct _Constant_binary_fun
00272     {
00273       typedef  _Arg1   first_argument_type;
00274       typedef  _Arg2   second_argument_type;
00275       typedef  _Result result_type;
00276       _Result _M_val;
00277 
00278       _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
00279       
00280       const result_type&
00281       operator()(const _Arg1&, const _Arg2&) const
00282       { return _M_val; }
00283     };
00284 
00299 
00300   template <class _Result>
00301     struct constant_void_fun
00302     : public _Constant_void_fun<_Result>
00303     {
00304       constant_void_fun(const _Result& __v)
00305       : _Constant_void_fun<_Result>(__v) {}
00306     };
00307 
00309   template <class _Result, class _Argument = _Result>
00310     struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
00311     {
00312       constant_unary_fun(const _Result& __v)
00313       : _Constant_unary_fun<_Result, _Argument>(__v) {}
00314     };
00315 
00317   template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1>
00318     struct constant_binary_fun
00319     : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
00320     {
00321       constant_binary_fun(const _Result& __v)
00322       : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
00323     };
00324 
00326   template <class _Result>
00327     inline constant_void_fun<_Result>
00328     constant0(const _Result& __val)
00329     { return constant_void_fun<_Result>(__val); }
00330 
00332   template <class _Result>
00333     inline constant_unary_fun<_Result, _Result>
00334     constant1(const _Result& __val)
00335     { return constant_unary_fun<_Result, _Result>(__val); }
00336 
00338   template <class _Result>
00339     inline constant_binary_fun<_Result,_Result,_Result>
00340     constant2(const _Result& __val)
00341     { return constant_binary_fun<_Result, _Result, _Result>(__val); }
00350   class subtractive_rng
00351   : public unary_function<unsigned int, unsigned int>
00352   {
00353   private:
00354     unsigned int _M_table[55];
00355     size_t _M_index1;
00356     size_t _M_index2;
00357 
00358   public:
00360     unsigned int
00361     operator()(unsigned int __limit)
00362     {
00363       _M_index1 = (_M_index1 + 1) % 55;
00364       _M_index2 = (_M_index2 + 1) % 55;
00365       _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
00366       return _M_table[_M_index1] % __limit;
00367     }
00368 
00369     void
00370     _M_initialize(unsigned int __seed)
00371     {
00372       unsigned int __k = 1;
00373       _M_table[54] = __seed;
00374       size_t __i;
00375       for (__i = 0; __i < 54; __i++)
00376     {
00377       size_t __ii = (21 * (__i + 1) % 55) - 1;
00378       _M_table[__ii] = __k;
00379       __k = __seed - __k;
00380       __seed = _M_table[__ii];
00381     }
00382       for (int __loop = 0; __loop < 4; __loop++)
00383     {
00384       for (__i = 0; __i < 55; __i++)
00385             _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
00386     }
00387       _M_index1 = 0;
00388       _M_index2 = 31;
00389     }
00390 
00392     subtractive_rng(unsigned int __seed)
00393     { _M_initialize(__seed); }
00394 
00396     subtractive_rng()
00397     { _M_initialize(161803398u); }
00398   };
00399 
00400   // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
00401   // provided for backward compatibility, they are no longer part of
00402   // the C++ standard.
00403   
00404   template <class _Ret, class _Tp, class _Arg>
00405     inline mem_fun1_t<_Ret, _Tp, _Arg>
00406     mem_fun1(_Ret (_Tp::*__f)(_Arg))
00407     { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
00408 
00409   template <class _Ret, class _Tp, class _Arg>
00410     inline const_mem_fun1_t<_Ret, _Tp, _Arg>
00411     mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
00412     { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
00413 
00414   template <class _Ret, class _Tp, class _Arg>
00415     inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
00416     mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
00417     { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
00418 
00419   template <class _Ret, class _Tp, class _Arg>
00420     inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
00421     mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
00422     { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
00423 } // namespace __gnu_cxx
00424 #endif
00425 

Generated on Tue Feb 2 16:55:57 2010 for GNU C++ STL by  doxygen 1.4.7