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