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 00064 00065 #pragma GCC system_header 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 00077 /** The @c identity_element functions are not part of the C++ standard; SGI 00078 * provided them as an extension. Its argument is an operation, and its 00079 * return value is the identity element for that operation. It is overloaded 00080 * for addition and multiplication, and you can overload it for your own 00081 * nefarious operations. 00082 * 00083 * @addtogroup SGIextensions 00084 * @{ 00085 */ 00086 /// An \link SGIextensions SGI extension \endlink. 00087 template <class _Tp> inline _Tp identity_element(std::plus<_Tp>) { 00088 return _Tp(0); 00089 } 00090 /// An \link SGIextensions SGI extension \endlink. 00091 template <class _Tp> inline _Tp identity_element(std::multiplies<_Tp>) { 00092 return _Tp(1); 00093 } 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 public: 00132 unary_compose(const _Operation1& __x, const _Operation2& __y) 00133 : _M_fn1(__x), _M_fn2(__y) {} 00134 typename _Operation1::result_type 00135 operator()(const typename _Operation2::argument_type& __x) const { 00136 return _M_fn1(_M_fn2(__x)); 00137 } 00138 }; 00139 00140 /// An \link SGIextensions SGI extension \endlink. 00141 template <class _Operation1, class _Operation2> 00142 inline unary_compose<_Operation1,_Operation2> 00143 compose1(const _Operation1& __fn1, const _Operation2& __fn2) 00144 { 00145 return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); 00146 } 00147 00148 /// An \link SGIextensions SGI extension \endlink. 00149 template <class _Operation1, class _Operation2, class _Operation3> 00150 class binary_compose 00151 : public unary_function<typename _Operation2::argument_type, 00152 typename _Operation1::result_type> { 00153 protected: 00154 _Operation1 _M_fn1; 00155 _Operation2 _M_fn2; 00156 _Operation3 _M_fn3; 00157 public: 00158 binary_compose(const _Operation1& __x, const _Operation2& __y, 00159 const _Operation3& __z) 00160 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { } 00161 typename _Operation1::result_type 00162 operator()(const typename _Operation2::argument_type& __x) const { 00163 return _M_fn1(_M_fn2(__x), _M_fn3(__x)); 00164 } 00165 }; 00166 00167 /// An \link SGIextensions SGI extension \endlink. 00168 template <class _Operation1, class _Operation2, class _Operation3> 00169 inline binary_compose<_Operation1, _Operation2, _Operation3> 00170 compose2(const _Operation1& __fn1, const _Operation2& __fn2, 00171 const _Operation3& __fn3) 00172 { 00173 return binary_compose<_Operation1,_Operation2,_Operation3> 00174 (__fn1, __fn2, __fn3); 00175 } 00176 /** @} */ 00177 00178 /** As an extension, SGI provided a functor called @c identity. When a 00179 * functor is required but no operations are desired, this can be used as a 00180 * pass-through. Its @c operator() returns its argument unchanged. 00181 * 00182 * @addtogroup SGIextensions 00183 */ 00184 template <class _Tp> 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 "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> struct select1st : public std::_Select1st<_Pair> {}; 00198 /// An \link SGIextensions SGI extension \endlink. 00199 template <class _Pair> struct select2nd : public std::_Select2nd<_Pair> {}; 00200 /** @} */ 00201 00202 // extension documented next 00203 template <class _Arg1, class _Arg2> 00204 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> { 00205 _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; } 00206 }; 00207 00208 template <class _Arg1, class _Arg2> 00209 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> { 00210 _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; } 00211 }; 00212 00213 /** The @c operator() of the @c project1st functor takes two arbitrary 00214 * arguments and returns the first one, while @c project2nd returns the 00215 * second one. They are extensions provided by SGI. 00216 * 00217 * @addtogroup SGIextensions 00218 * @{ 00219 */ 00220 00221 /// An \link SGIextensions SGI extension \endlink. 00222 template <class _Arg1, class _Arg2> 00223 struct project1st : public _Project1st<_Arg1, _Arg2> {}; 00224 00225 /// An \link SGIextensions SGI extension \endlink. 00226 template <class _Arg1, class _Arg2> 00227 struct project2nd : public _Project2nd<_Arg1, _Arg2> {}; 00228 /** @} */ 00229 00230 // extension documented next 00231 template <class _Result> 00232 struct _Constant_void_fun { 00233 typedef _Result result_type; 00234 result_type _M_val; 00235 00236 _Constant_void_fun(const result_type& __v) : _M_val(__v) {} 00237 const result_type& operator()() const { return _M_val; } 00238 }; 00239 00240 template <class _Result, class _Argument> 00241 struct _Constant_unary_fun { 00242 typedef _Argument argument_type; 00243 typedef _Result result_type; 00244 result_type _M_val; 00245 00246 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {} 00247 const result_type& operator()(const _Argument&) const { return _M_val; } 00248 }; 00249 00250 template <class _Result, class _Arg1, class _Arg2> 00251 struct _Constant_binary_fun { 00252 typedef _Arg1 first_argument_type; 00253 typedef _Arg2 second_argument_type; 00254 typedef _Result result_type; 00255 _Result _M_val; 00256 00257 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {} 00258 const result_type& operator()(const _Arg1&, const _Arg2&) const { 00259 return _M_val; 00260 } 00261 }; 00262 00263 /** These three functors are each constructed from a single arbitrary 00264 * variable/value. Later, their @c operator()s completely ignore any 00265 * arguments passed, and return the stored value. 00266 * - @c constant_void_fun's @c operator() takes no arguments 00267 * - @c constant_unary_fun's @c operator() takes one argument (ignored) 00268 * - @c constant_binary_fun's @c operator() takes two arguments (ignored) 00269 * 00270 * The helper creator functions @c constant0, @c constant1, and 00271 * @c constant2 each take a "result" argument and construct variables of 00272 * the appropriate functor type. 00273 * 00274 * @addtogroup SGIextensions 00275 * @{ 00276 */ 00277 /// An \link SGIextensions SGI extension \endlink. 00278 template <class _Result> 00279 struct constant_void_fun : public _Constant_void_fun<_Result> { 00280 constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {} 00281 }; 00282 00283 /// An \link SGIextensions SGI extension \endlink. 00284 template <class _Result, 00285 class _Argument = _Result> 00286 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument> 00287 { 00288 constant_unary_fun(const _Result& __v) 00289 : _Constant_unary_fun<_Result, _Argument>(__v) {} 00290 }; 00291 00292 /// An \link SGIextensions SGI extension \endlink. 00293 template <class _Result, 00294 class _Arg1 = _Result, 00295 class _Arg2 = _Arg1> 00296 struct constant_binary_fun 00297 : public _Constant_binary_fun<_Result, _Arg1, _Arg2> 00298 { 00299 constant_binary_fun(const _Result& __v) 00300 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {} 00301 }; 00302 00303 /// An \link SGIextensions SGI extension \endlink. 00304 template <class _Result> 00305 inline constant_void_fun<_Result> constant0(const _Result& __val) 00306 { 00307 return constant_void_fun<_Result>(__val); 00308 } 00309 00310 /// An \link SGIextensions SGI extension \endlink. 00311 template <class _Result> 00312 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val) 00313 { 00314 return constant_unary_fun<_Result,_Result>(__val); 00315 } 00316 00317 /// An \link SGIextensions SGI extension \endlink. 00318 template <class _Result> 00319 inline constant_binary_fun<_Result,_Result,_Result> 00320 constant2(const _Result& __val) 00321 { 00322 return constant_binary_fun<_Result,_Result,_Result>(__val); 00323 } 00324 /** @} */ 00325 00326 /** The @c subtractive_rng class is documented on 00327 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>. 00328 * Note that this code assumes that @c int is 32 bits. 00329 * 00330 * @ingroup SGIextensions 00331 */ 00332 class subtractive_rng : public unary_function<unsigned int, unsigned int> { 00333 private: 00334 unsigned int _M_table[55]; 00335 size_t _M_index1; 00336 size_t _M_index2; 00337 public: 00338 /// Returns a number less than the argument. 00339 unsigned int operator()(unsigned int __limit) { 00340 _M_index1 = (_M_index1 + 1) % 55; 00341 _M_index2 = (_M_index2 + 1) % 55; 00342 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2]; 00343 return _M_table[_M_index1] % __limit; 00344 } 00345 00346 void _M_initialize(unsigned int __seed) 00347 { 00348 unsigned int __k = 1; 00349 _M_table[54] = __seed; 00350 size_t __i; 00351 for (__i = 0; __i < 54; __i++) { 00352 size_t __ii = (21 * (__i + 1) % 55) - 1; 00353 _M_table[__ii] = __k; 00354 __k = __seed - __k; 00355 __seed = _M_table[__ii]; 00356 } 00357 for (int __loop = 0; __loop < 4; __loop++) { 00358 for (__i = 0; __i < 55; __i++) 00359 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55]; 00360 } 00361 _M_index1 = 0; 00362 _M_index2 = 31; 00363 } 00364 00365 /// Ctor allowing you to initialize the seed. 00366 subtractive_rng(unsigned int __seed) { _M_initialize(__seed); } 00367 /// Default ctor; initializes its state with some number you don't see. 00368 subtractive_rng() { _M_initialize(161803398u); } 00369 }; 00370 00371 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref, 00372 // provided for backward compatibility, they are no longer part of 00373 // the C++ standard. 00374 00375 template <class _Ret, class _Tp, class _Arg> 00376 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg)) 00377 { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); } 00378 00379 template <class _Ret, class _Tp, class _Arg> 00380 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const) 00381 { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); } 00382 00383 template <class _Ret, class _Tp, class _Arg> 00384 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg)) 00385 { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); } 00386 00387 template <class _Ret, class _Tp, class _Arg> 00388 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg> 00389 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const) 00390 { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); } 00391 00392 } // namespace __gnu_cxx 00393 00394 #endif /* _EXT_FUNCTIONAL */ 00395

Generated on Wed Aug 4 21:43:11 2004 for libstdc++-v3 Source by doxygen 1.3.8