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