1 // Functional extensions -*- C++ -*- 2 3 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 3, or (at your option) 10 // any later version. 11 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // Under Section 7 of GPL version 3, you are granted additional 18 // permissions described in the GCC Runtime Library Exception, version 19 // 3.1, as published by the Free Software Foundation. 20 21 // You should have received a copy of the GNU General Public License and 22 // a copy of the GCC Runtime Library Exception along with this program; 23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 // <http://www.gnu.org/licenses/>. 25 26 /* 27 * 28 * Copyright (c) 1994 29 * Hewlett-Packard Company 30 * 31 * Permission to use, copy, modify, distribute and sell this software 32 * and its documentation for any purpose is hereby granted without fee, 33 * provided that the above copyright notice appear in all copies and 34 * that both that copyright notice and this permission notice appear 35 * in supporting documentation. Hewlett-Packard Company makes no 36 * representations about the suitability of this software for any 37 * purpose. It is provided "as is" without express or implied warranty. 38 * 39 * 40 * Copyright (c) 1996 41 * Silicon Graphics Computer Systems, Inc. 42 * 43 * Permission to use, copy, modify, distribute and sell this software 44 * and its documentation for any purpose is hereby granted without fee, 45 * provided that the above copyright notice appear in all copies and 46 * that both that copyright notice and this permission notice appear 47 * in supporting documentation. Silicon Graphics makes no 48 * representations about the suitability of this software for any 49 * purpose. It is provided "as is" without express or implied warranty. 50 */ 51 52 /** @file ext/functional 53 * This file is a GNU extension to the Standard C++ Library (possibly 54 * containing extensions from the HP/SGI STL subset). 55 */ 56 57 #ifndef _EXT_FUNCTIONAL 58 #define _EXT_FUNCTIONAL 1 59 60 #pragma GCC system_header 61 62 #include <functional> 63 64 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 65 { 66 _GLIBCXX_BEGIN_NAMESPACE_VERSION 67 68 using std::size_t; 69 using std::unary_function; 70 using std::binary_function; 71 using std::mem_fun1_t; 72 using std::const_mem_fun1_t; 73 using std::mem_fun1_ref_t; 74 using std::const_mem_fun1_ref_t; 75 76 /** The @c identity_element functions are not part of the C++ 77 * standard; SGI provided them as an extension. Its argument is an 78 * operation, and its return value is the identity element for that 79 * operation. It is overloaded for addition and multiplication, 80 * and you can overload it for your own nefarious operations. 81 * 82 * @addtogroup SGIextensions 83 * @{ 84 */ 85 /// An \link SGIextensions SGI extension \endlink. 86 template <class _Tp> 87 inline _Tp 88 identity_element(std::plus<_Tp>) 89 { return _Tp(0); } 90 91 /// An \link SGIextensions SGI extension \endlink. 92 template <class _Tp> 93 inline _Tp 94 identity_element(std::multiplies<_Tp>) 95 { return _Tp(1); } 96 /** @} */ 97 98 /** As an extension to the binders, SGI provided composition functors and 99 * wrapper functions to aid in their creation. The @c unary_compose 100 * functor is constructed from two functions/functors, @c f and @c g. 101 * Calling @c operator() with a single argument @c x returns @c f(g(x)). 102 * The function @c compose1 takes the two functions and constructs a 103 * @c unary_compose variable for you. 104 * 105 * @c binary_compose is constructed from three functors, @c f, @c g1, 106 * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function 107 * compose2 takes f, g1, and g2, and constructs the @c binary_compose 108 * instance for you. For example, if @c f returns an int, then 109 * \code 110 * int answer = (compose2(f,g1,g2))(x); 111 * \endcode 112 * is equivalent to 113 * \code 114 * int temp1 = g1(x); 115 * int temp2 = g2(x); 116 * int answer = f(temp1,temp2); 117 * \endcode 118 * But the first form is more compact, and can be passed around as a 119 * functor to other algorithms. 120 * 121 * @addtogroup SGIextensions 122 * @{ 123 */ 124 /// An \link SGIextensions SGI extension \endlink. 125 template <class _Operation1, class _Operation2> 126 class unary_compose 127 : public unary_function<typename _Operation2::argument_type, 128 typename _Operation1::result_type> 129 { 130 protected: 131 _Operation1 _M_fn1; 132 _Operation2 _M_fn2; 133 134 public: 135 unary_compose(const _Operation1& __x, const _Operation2& __y) 136 : _M_fn1(__x), _M_fn2(__y) {} 137 138 typename _Operation1::result_type 139 operator()(const typename _Operation2::argument_type& __x) const 140 { return _M_fn1(_M_fn2(__x)); } 141 }; 142 143 /// An \link SGIextensions SGI extension \endlink. 144 template <class _Operation1, class _Operation2> 145 inline unary_compose<_Operation1, _Operation2> 146 compose1(const _Operation1& __fn1, const _Operation2& __fn2) 147 { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); } 148 149 /// An \link SGIextensions SGI extension \endlink. 150 template <class _Operation1, class _Operation2, class _Operation3> 151 class binary_compose 152 : public unary_function<typename _Operation2::argument_type, 153 typename _Operation1::result_type> 154 { 155 protected: 156 _Operation1 _M_fn1; 157 _Operation2 _M_fn2; 158 _Operation3 _M_fn3; 159 160 public: 161 binary_compose(const _Operation1& __x, const _Operation2& __y, 162 const _Operation3& __z) 163 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { } 164 165 typename _Operation1::result_type 166 operator()(const typename _Operation2::argument_type& __x) const 167 { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); } 168 }; 169 170 /// An \link SGIextensions SGI extension \endlink. 171 template <class _Operation1, class _Operation2, class _Operation3> 172 inline binary_compose<_Operation1, _Operation2, _Operation3> 173 compose2(const _Operation1& __fn1, const _Operation2& __fn2, 174 const _Operation3& __fn3) 175 { return binary_compose<_Operation1, _Operation2, _Operation3> 176 (__fn1, __fn2, __fn3); } 177 /** @} */ 178 179 /** As an extension, SGI provided a functor called @c identity. When a 180 * functor is required but no operations are desired, this can be used as a 181 * pass-through. Its @c operator() returns its argument unchanged. 182 * 183 * @addtogroup SGIextensions 184 */ 185 template <class _Tp> 186 struct identity 187 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 188 : public std::unary_function<_Tp,_Tp>, 189 public std::_Identity<_Tp> {}; 190 #else 191 : public std::_Identity<_Tp> {}; 192 #endif 193 194 /** @c select1st and @c select2nd are extensions provided by SGI. Their 195 * @c operator()s 196 * take a @c std::pair as an argument, and return either the first member 197 * or the second member, respectively. They can be used (especially with 198 * the composition functors) to @a strip data from a sequence before 199 * performing the remainder of an algorithm. 200 * 201 * @addtogroup SGIextensions 202 * @{ 203 */ 204 /// An \link SGIextensions SGI extension \endlink. 205 template <class _Pair> 206 struct select1st 207 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 208 : public std::unary_function<_Pair, typename _Pair::first_type>, 209 public std::_Select1st<_Pair> {}; 210 #else 211 : public std::_Select1st<_Pair> {}; 212 #endif 213 214 /// An \link SGIextensions SGI extension \endlink. 215 template <class _Pair> 216 struct select2nd 217 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 218 : public std::unary_function<_Pair, typename _Pair::second_type>, 219 public std::_Select2nd<_Pair> {}; 220 #else 221 : public std::_Select2nd<_Pair> {}; 222 #endif 223 /** @} */ 224 225 // extension documented next 226 template <class _Arg1, class _Arg2> 227 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> 228 { 229 _Arg1 230 operator()(const _Arg1& __x, const _Arg2&) const 231 { return __x; } 232 }; 233 234 template <class _Arg1, class _Arg2> 235 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> 236 { 237 _Arg2 238 operator()(const _Arg1&, const _Arg2& __y) const 239 { return __y; } 240 }; 241 242 /** The @c operator() of the @c project1st functor takes two arbitrary 243 * arguments and returns the first one, while @c project2nd returns the 244 * second one. They are extensions provided by SGI. 245 * 246 * @addtogroup SGIextensions 247 * @{ 248 */ 249 250 /// An \link SGIextensions SGI extension \endlink. 251 template <class _Arg1, class _Arg2> 252 struct project1st : public _Project1st<_Arg1, _Arg2> {}; 253 254 /// An \link SGIextensions SGI extension \endlink. 255 template <class _Arg1, class _Arg2> 256 struct project2nd : public _Project2nd<_Arg1, _Arg2> {}; 257 /** @} */ 258 259 // extension documented next 260 template <class _Result> 261 struct _Constant_void_fun 262 { 263 typedef _Result result_type; 264 result_type _M_val; 265 266 _Constant_void_fun(const result_type& __v) : _M_val(__v) {} 267 268 const result_type& 269 operator()() const 270 { return _M_val; } 271 }; 272 273 template <class _Result, class _Argument> 274 struct _Constant_unary_fun 275 { 276 typedef _Argument argument_type; 277 typedef _Result result_type; 278 result_type _M_val; 279 280 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {} 281 282 const result_type& 283 operator()(const _Argument&) const 284 { return _M_val; } 285 }; 286 287 template <class _Result, class _Arg1, class _Arg2> 288 struct _Constant_binary_fun 289 { 290 typedef _Arg1 first_argument_type; 291 typedef _Arg2 second_argument_type; 292 typedef _Result result_type; 293 _Result _M_val; 294 295 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {} 296 297 const result_type& 298 operator()(const _Arg1&, const _Arg2&) const 299 { return _M_val; } 300 }; 301 302 /** These three functors are each constructed from a single arbitrary 303 * variable/value. Later, their @c operator()s completely ignore any 304 * arguments passed, and return the stored value. 305 * - @c constant_void_fun's @c operator() takes no arguments 306 * - @c constant_unary_fun's @c operator() takes one argument (ignored) 307 * - @c constant_binary_fun's @c operator() takes two arguments (ignored) 308 * 309 * The helper creator functions @c constant0, @c constant1, and 310 * @c constant2 each take a @a result argument and construct variables of 311 * the appropriate functor type. 312 * 313 * @addtogroup SGIextensions 314 * @{ 315 */ 316 /// An \link SGIextensions SGI extension \endlink. 317 template <class _Result> 318 struct constant_void_fun 319 : public _Constant_void_fun<_Result> 320 { 321 constant_void_fun(const _Result& __v) 322 : _Constant_void_fun<_Result>(__v) {} 323 }; 324 325 /// An \link SGIextensions SGI extension \endlink. 326 template <class _Result, class _Argument = _Result> 327 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument> 328 { 329 constant_unary_fun(const _Result& __v) 330 : _Constant_unary_fun<_Result, _Argument>(__v) {} 331 }; 332 333 /// An \link SGIextensions SGI extension \endlink. 334 template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1> 335 struct constant_binary_fun 336 : public _Constant_binary_fun<_Result, _Arg1, _Arg2> 337 { 338 constant_binary_fun(const _Result& __v) 339 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {} 340 }; 341 342 /// An \link SGIextensions SGI extension \endlink. 343 template <class _Result> 344 inline constant_void_fun<_Result> 345 constant0(const _Result& __val) 346 { return constant_void_fun<_Result>(__val); } 347 348 /// An \link SGIextensions SGI extension \endlink. 349 template <class _Result> 350 inline constant_unary_fun<_Result, _Result> 351 constant1(const _Result& __val) 352 { return constant_unary_fun<_Result, _Result>(__val); } 353 354 /// An \link SGIextensions SGI extension \endlink. 355 template <class _Result> 356 inline constant_binary_fun<_Result,_Result,_Result> 357 constant2(const _Result& __val) 358 { return constant_binary_fun<_Result, _Result, _Result>(__val); } 359 /** @} */ 360 361 /** The @c subtractive_rng class is documented on 362 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>. 363 * Note that this code assumes that @c int is 32 bits. 364 * 365 * @ingroup SGIextensions 366 */ 367 class subtractive_rng 368 : public unary_function<unsigned int, unsigned int> 369 { 370 private: 371 unsigned int _M_table[55]; 372 size_t _M_index1; 373 size_t _M_index2; 374 375 public: 376 /// Returns a number less than the argument. 377 unsigned int 378 operator()(unsigned int __limit) 379 { 380 _M_index1 = (_M_index1 + 1) % 55; 381 _M_index2 = (_M_index2 + 1) % 55; 382 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2]; 383 return _M_table[_M_index1] % __limit; 384 } 385 386 void 387 _M_initialize(unsigned int __seed) 388 { 389 unsigned int __k = 1; 390 _M_table[54] = __seed; 391 size_t __i; 392 for (__i = 0; __i < 54; __i++) 393 { 394 size_t __ii = (21 * (__i + 1) % 55) - 1; 395 _M_table[__ii] = __k; 396 __k = __seed - __k; 397 __seed = _M_table[__ii]; 398 } 399 for (int __loop = 0; __loop < 4; __loop++) 400 { 401 for (__i = 0; __i < 55; __i++) 402 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55]; 403 } 404 _M_index1 = 0; 405 _M_index2 = 31; 406 } 407 408 /// Ctor allowing you to initialize the seed. 409 subtractive_rng(unsigned int __seed) 410 { _M_initialize(__seed); } 411 412 /// Default ctor; initializes its state with some number you don't see. 413 subtractive_rng() 414 { _M_initialize(161803398u); } 415 }; 416 417 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref, 418 // provided for backward compatibility, they are no longer part of 419 // the C++ standard. 420 421 template <class _Ret, class _Tp, class _Arg> 422 inline mem_fun1_t<_Ret, _Tp, _Arg> 423 mem_fun1(_Ret (_Tp::*__f)(_Arg)) 424 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 425 426 template <class _Ret, class _Tp, class _Arg> 427 inline const_mem_fun1_t<_Ret, _Tp, _Arg> 428 mem_fun1(_Ret (_Tp::*__f)(_Arg) const) 429 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 430 431 template <class _Ret, class _Tp, class _Arg> 432 inline mem_fun1_ref_t<_Ret, _Tp, _Arg> 433 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg)) 434 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 435 436 template <class _Ret, class _Tp, class _Arg> 437 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg> 438 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const) 439 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 440 441 _GLIBCXX_END_NAMESPACE_VERSION 442 } // namespace 443 444 #endif 445 446