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