1 1.1 mrg // Functional extensions -*- C++ -*- 2 1.1 mrg 3 1.1.1.13 mrg // Copyright (C) 2002-2024 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.1.13 mrg #include <bits/requires_hosted.h> // GNU extensions are currently omitted 62 1.1.1.13 mrg 63 1.1 mrg #include <functional> 64 1.1 mrg 65 1.1.1.2 mrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 66 1.1.1.2 mrg { 67 1.1.1.2 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 68 1.1 mrg 69 1.1.1.11 mrg #pragma GCC diagnostic push 70 1.1.1.11 mrg #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 71 1.1.1.11 mrg 72 1.1 mrg /** The @c identity_element functions are not part of the C++ 73 1.1 mrg * standard; SGI provided them as an extension. Its argument is an 74 1.1 mrg * operation, and its return value is the identity element for that 75 1.1 mrg * operation. It is overloaded for addition and multiplication, 76 1.1 mrg * and you can overload it for your own nefarious operations. 77 1.1 mrg * 78 1.1 mrg * @addtogroup SGIextensions 79 1.1 mrg * @{ 80 1.1 mrg */ 81 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 82 1.1 mrg template <class _Tp> 83 1.1 mrg inline _Tp 84 1.1 mrg identity_element(std::plus<_Tp>) 85 1.1 mrg { return _Tp(0); } 86 1.1 mrg 87 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 88 1.1 mrg template <class _Tp> 89 1.1 mrg inline _Tp 90 1.1 mrg identity_element(std::multiplies<_Tp>) 91 1.1 mrg { return _Tp(1); } 92 1.1 mrg /** @} */ 93 1.1 mrg 94 1.1 mrg /** As an extension to the binders, SGI provided composition functors and 95 1.1 mrg * wrapper functions to aid in their creation. The @c unary_compose 96 1.1 mrg * functor is constructed from two functions/functors, @c f and @c g. 97 1.1 mrg * Calling @c operator() with a single argument @c x returns @c f(g(x)). 98 1.1 mrg * The function @c compose1 takes the two functions and constructs a 99 1.1 mrg * @c unary_compose variable for you. 100 1.1 mrg * 101 1.1 mrg * @c binary_compose is constructed from three functors, @c f, @c g1, 102 1.1 mrg * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function 103 1.1.1.2 mrg * compose2 takes f, g1, and g2, and constructs the @c binary_compose 104 1.1 mrg * instance for you. For example, if @c f returns an int, then 105 1.1 mrg * \code 106 1.1 mrg * int answer = (compose2(f,g1,g2))(x); 107 1.1 mrg * \endcode 108 1.1 mrg * is equivalent to 109 1.1 mrg * \code 110 1.1 mrg * int temp1 = g1(x); 111 1.1 mrg * int temp2 = g2(x); 112 1.1 mrg * int answer = f(temp1,temp2); 113 1.1 mrg * \endcode 114 1.1 mrg * But the first form is more compact, and can be passed around as a 115 1.1 mrg * functor to other algorithms. 116 1.1 mrg * 117 1.1 mrg * @addtogroup SGIextensions 118 1.1 mrg * @{ 119 1.1 mrg */ 120 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 121 1.1 mrg template <class _Operation1, class _Operation2> 122 1.1 mrg class unary_compose 123 1.1.1.10 mrg : public std::unary_function<typename _Operation2::argument_type, 124 1.1.1.10 mrg typename _Operation1::result_type> 125 1.1 mrg { 126 1.1 mrg protected: 127 1.1 mrg _Operation1 _M_fn1; 128 1.1 mrg _Operation2 _M_fn2; 129 1.1 mrg 130 1.1 mrg public: 131 1.1 mrg unary_compose(const _Operation1& __x, const _Operation2& __y) 132 1.1 mrg : _M_fn1(__x), _M_fn2(__y) {} 133 1.1 mrg 134 1.1 mrg typename _Operation1::result_type 135 1.1 mrg operator()(const typename _Operation2::argument_type& __x) const 136 1.1 mrg { return _M_fn1(_M_fn2(__x)); } 137 1.1 mrg }; 138 1.1 mrg 139 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 140 1.1 mrg template <class _Operation1, class _Operation2> 141 1.1 mrg inline unary_compose<_Operation1, _Operation2> 142 1.1 mrg compose1(const _Operation1& __fn1, const _Operation2& __fn2) 143 1.1 mrg { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); } 144 1.1 mrg 145 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 146 1.1 mrg template <class _Operation1, class _Operation2, class _Operation3> 147 1.1 mrg class binary_compose 148 1.1.1.10 mrg : public std::unary_function<typename _Operation2::argument_type, 149 1.1.1.10 mrg typename _Operation1::result_type> 150 1.1 mrg { 151 1.1 mrg protected: 152 1.1 mrg _Operation1 _M_fn1; 153 1.1 mrg _Operation2 _M_fn2; 154 1.1 mrg _Operation3 _M_fn3; 155 1.1 mrg 156 1.1 mrg public: 157 1.1 mrg binary_compose(const _Operation1& __x, const _Operation2& __y, 158 1.1 mrg const _Operation3& __z) 159 1.1 mrg : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { } 160 1.1 mrg 161 1.1 mrg typename _Operation1::result_type 162 1.1 mrg operator()(const typename _Operation2::argument_type& __x) const 163 1.1 mrg { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); } 164 1.1 mrg }; 165 1.1 mrg 166 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 167 1.1 mrg template <class _Operation1, class _Operation2, class _Operation3> 168 1.1 mrg inline binary_compose<_Operation1, _Operation2, _Operation3> 169 1.1 mrg compose2(const _Operation1& __fn1, const _Operation2& __fn2, 170 1.1 mrg const _Operation3& __fn3) 171 1.1 mrg { return binary_compose<_Operation1, _Operation2, _Operation3> 172 1.1 mrg (__fn1, __fn2, __fn3); } 173 1.1 mrg /** @} */ 174 1.1 mrg 175 1.1 mrg /** As an extension, SGI provided a functor called @c identity. When a 176 1.1 mrg * functor is required but no operations are desired, this can be used as a 177 1.1 mrg * pass-through. Its @c operator() returns its argument unchanged. 178 1.1 mrg * 179 1.1 mrg * @addtogroup SGIextensions 180 1.1 mrg */ 181 1.1 mrg template <class _Tp> 182 1.1.1.2 mrg struct identity 183 1.1.1.2 mrg : public std::_Identity<_Tp> {}; 184 1.1 mrg 185 1.1 mrg /** @c select1st and @c select2nd are extensions provided by SGI. Their 186 1.1 mrg * @c operator()s 187 1.1 mrg * take a @c std::pair as an argument, and return either the first member 188 1.1 mrg * or the second member, respectively. They can be used (especially with 189 1.1 mrg * the composition functors) to @a strip data from a sequence before 190 1.1 mrg * performing the remainder of an algorithm. 191 1.1 mrg * 192 1.1 mrg * @addtogroup SGIextensions 193 1.1 mrg * @{ 194 1.1 mrg */ 195 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 196 1.1 mrg template <class _Pair> 197 1.1.1.2 mrg struct select1st 198 1.1.1.2 mrg : public std::_Select1st<_Pair> {}; 199 1.1 mrg 200 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 201 1.1 mrg template <class _Pair> 202 1.1.1.2 mrg struct select2nd 203 1.1.1.2 mrg : public std::_Select2nd<_Pair> {}; 204 1.1.1.2 mrg 205 1.1 mrg /** @} */ 206 1.1 mrg 207 1.1 mrg // extension documented next 208 1.1 mrg template <class _Arg1, class _Arg2> 209 1.1.1.10 mrg struct _Project1st : public std::binary_function<_Arg1, _Arg2, _Arg1> 210 1.1 mrg { 211 1.1 mrg _Arg1 212 1.1 mrg operator()(const _Arg1& __x, const _Arg2&) const 213 1.1 mrg { return __x; } 214 1.1 mrg }; 215 1.1 mrg 216 1.1 mrg template <class _Arg1, class _Arg2> 217 1.1.1.10 mrg struct _Project2nd : public std::binary_function<_Arg1, _Arg2, _Arg2> 218 1.1 mrg { 219 1.1 mrg _Arg2 220 1.1 mrg operator()(const _Arg1&, const _Arg2& __y) const 221 1.1 mrg { return __y; } 222 1.1 mrg }; 223 1.1 mrg 224 1.1 mrg /** The @c operator() of the @c project1st functor takes two arbitrary 225 1.1 mrg * arguments and returns the first one, while @c project2nd returns the 226 1.1 mrg * second one. They are extensions provided by SGI. 227 1.1 mrg * 228 1.1 mrg * @addtogroup SGIextensions 229 1.1 mrg * @{ 230 1.1 mrg */ 231 1.1 mrg 232 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 233 1.1 mrg template <class _Arg1, class _Arg2> 234 1.1 mrg struct project1st : public _Project1st<_Arg1, _Arg2> {}; 235 1.1 mrg 236 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 237 1.1 mrg template <class _Arg1, class _Arg2> 238 1.1 mrg struct project2nd : public _Project2nd<_Arg1, _Arg2> {}; 239 1.1 mrg /** @} */ 240 1.1 mrg 241 1.1 mrg // extension documented next 242 1.1 mrg template <class _Result> 243 1.1 mrg struct _Constant_void_fun 244 1.1 mrg { 245 1.1 mrg typedef _Result result_type; 246 1.1 mrg result_type _M_val; 247 1.1 mrg 248 1.1 mrg _Constant_void_fun(const result_type& __v) : _M_val(__v) {} 249 1.1 mrg 250 1.1 mrg const result_type& 251 1.1 mrg operator()() const 252 1.1 mrg { return _M_val; } 253 1.1 mrg }; 254 1.1 mrg 255 1.1 mrg template <class _Result, class _Argument> 256 1.1 mrg struct _Constant_unary_fun 257 1.1 mrg { 258 1.1 mrg typedef _Argument argument_type; 259 1.1 mrg typedef _Result result_type; 260 1.1 mrg result_type _M_val; 261 1.1 mrg 262 1.1 mrg _Constant_unary_fun(const result_type& __v) : _M_val(__v) {} 263 1.1 mrg 264 1.1 mrg const result_type& 265 1.1 mrg operator()(const _Argument&) const 266 1.1 mrg { return _M_val; } 267 1.1 mrg }; 268 1.1 mrg 269 1.1 mrg template <class _Result, class _Arg1, class _Arg2> 270 1.1 mrg struct _Constant_binary_fun 271 1.1 mrg { 272 1.1 mrg typedef _Arg1 first_argument_type; 273 1.1 mrg typedef _Arg2 second_argument_type; 274 1.1 mrg typedef _Result result_type; 275 1.1 mrg _Result _M_val; 276 1.1 mrg 277 1.1 mrg _Constant_binary_fun(const _Result& __v) : _M_val(__v) {} 278 1.1 mrg 279 1.1 mrg const result_type& 280 1.1 mrg operator()(const _Arg1&, const _Arg2&) const 281 1.1 mrg { return _M_val; } 282 1.1 mrg }; 283 1.1 mrg 284 1.1 mrg /** These three functors are each constructed from a single arbitrary 285 1.1 mrg * variable/value. Later, their @c operator()s completely ignore any 286 1.1 mrg * arguments passed, and return the stored value. 287 1.1 mrg * - @c constant_void_fun's @c operator() takes no arguments 288 1.1 mrg * - @c constant_unary_fun's @c operator() takes one argument (ignored) 289 1.1 mrg * - @c constant_binary_fun's @c operator() takes two arguments (ignored) 290 1.1 mrg * 291 1.1 mrg * The helper creator functions @c constant0, @c constant1, and 292 1.1 mrg * @c constant2 each take a @a result argument and construct variables of 293 1.1 mrg * the appropriate functor type. 294 1.1 mrg * 295 1.1 mrg * @addtogroup SGIextensions 296 1.1 mrg * @{ 297 1.1 mrg */ 298 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 299 1.1 mrg template <class _Result> 300 1.1 mrg struct constant_void_fun 301 1.1 mrg : public _Constant_void_fun<_Result> 302 1.1 mrg { 303 1.1 mrg constant_void_fun(const _Result& __v) 304 1.1 mrg : _Constant_void_fun<_Result>(__v) {} 305 1.1 mrg }; 306 1.1 mrg 307 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 308 1.1 mrg template <class _Result, class _Argument = _Result> 309 1.1 mrg struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument> 310 1.1 mrg { 311 1.1 mrg constant_unary_fun(const _Result& __v) 312 1.1 mrg : _Constant_unary_fun<_Result, _Argument>(__v) {} 313 1.1 mrg }; 314 1.1 mrg 315 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 316 1.1 mrg template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1> 317 1.1 mrg struct constant_binary_fun 318 1.1 mrg : public _Constant_binary_fun<_Result, _Arg1, _Arg2> 319 1.1 mrg { 320 1.1 mrg constant_binary_fun(const _Result& __v) 321 1.1 mrg : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {} 322 1.1 mrg }; 323 1.1 mrg 324 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 325 1.1 mrg template <class _Result> 326 1.1 mrg inline constant_void_fun<_Result> 327 1.1 mrg constant0(const _Result& __val) 328 1.1 mrg { return constant_void_fun<_Result>(__val); } 329 1.1 mrg 330 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 331 1.1 mrg template <class _Result> 332 1.1 mrg inline constant_unary_fun<_Result, _Result> 333 1.1 mrg constant1(const _Result& __val) 334 1.1 mrg { return constant_unary_fun<_Result, _Result>(__val); } 335 1.1 mrg 336 1.1 mrg /// An \link SGIextensions SGI extension \endlink. 337 1.1 mrg template <class _Result> 338 1.1 mrg inline constant_binary_fun<_Result,_Result,_Result> 339 1.1 mrg constant2(const _Result& __val) 340 1.1 mrg { return constant_binary_fun<_Result, _Result, _Result>(__val); } 341 1.1 mrg /** @} */ 342 1.1 mrg 343 1.1 mrg /** The @c subtractive_rng class is documented on 344 1.1 mrg * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>. 345 1.1 mrg * Note that this code assumes that @c int is 32 bits. 346 1.1 mrg * 347 1.1 mrg * @ingroup SGIextensions 348 1.1 mrg */ 349 1.1 mrg class subtractive_rng 350 1.1.1.10 mrg : public std::unary_function<unsigned int, unsigned int> 351 1.1 mrg { 352 1.1 mrg private: 353 1.1 mrg unsigned int _M_table[55]; 354 1.1.1.10 mrg std::size_t _M_index1; 355 1.1.1.10 mrg std::size_t _M_index2; 356 1.1 mrg 357 1.1 mrg public: 358 1.1 mrg /// Returns a number less than the argument. 359 1.1 mrg unsigned int 360 1.1 mrg operator()(unsigned int __limit) 361 1.1 mrg { 362 1.1 mrg _M_index1 = (_M_index1 + 1) % 55; 363 1.1 mrg _M_index2 = (_M_index2 + 1) % 55; 364 1.1 mrg _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2]; 365 1.1 mrg return _M_table[_M_index1] % __limit; 366 1.1 mrg } 367 1.1 mrg 368 1.1 mrg void 369 1.1 mrg _M_initialize(unsigned int __seed) 370 1.1 mrg { 371 1.1 mrg unsigned int __k = 1; 372 1.1 mrg _M_table[54] = __seed; 373 1.1.1.10 mrg std::size_t __i; 374 1.1 mrg for (__i = 0; __i < 54; __i++) 375 1.1 mrg { 376 1.1.1.10 mrg std::size_t __ii = (21 * (__i + 1) % 55) - 1; 377 1.1 mrg _M_table[__ii] = __k; 378 1.1 mrg __k = __seed - __k; 379 1.1 mrg __seed = _M_table[__ii]; 380 1.1 mrg } 381 1.1 mrg for (int __loop = 0; __loop < 4; __loop++) 382 1.1 mrg { 383 1.1 mrg for (__i = 0; __i < 55; __i++) 384 1.1 mrg _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55]; 385 1.1 mrg } 386 1.1 mrg _M_index1 = 0; 387 1.1 mrg _M_index2 = 31; 388 1.1 mrg } 389 1.1 mrg 390 1.1 mrg /// Ctor allowing you to initialize the seed. 391 1.1 mrg subtractive_rng(unsigned int __seed) 392 1.1 mrg { _M_initialize(__seed); } 393 1.1 mrg 394 1.1 mrg /// Default ctor; initializes its state with some number you don't see. 395 1.1 mrg subtractive_rng() 396 1.1 mrg { _M_initialize(161803398u); } 397 1.1 mrg }; 398 1.1 mrg 399 1.1 mrg // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref, 400 1.1 mrg // provided for backward compatibility, they are no longer part of 401 1.1 mrg // the C++ standard. 402 1.1 mrg 403 1.1 mrg template <class _Ret, class _Tp, class _Arg> 404 1.1.1.10 mrg inline std::mem_fun1_t<_Ret, _Tp, _Arg> 405 1.1 mrg mem_fun1(_Ret (_Tp::*__f)(_Arg)) 406 1.1.1.10 mrg { return std::mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 407 1.1 mrg 408 1.1 mrg template <class _Ret, class _Tp, class _Arg> 409 1.1.1.10 mrg inline std::const_mem_fun1_t<_Ret, _Tp, _Arg> 410 1.1 mrg mem_fun1(_Ret (_Tp::*__f)(_Arg) const) 411 1.1.1.10 mrg { return std::const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 412 1.1 mrg 413 1.1 mrg template <class _Ret, class _Tp, class _Arg> 414 1.1.1.10 mrg inline std::mem_fun1_ref_t<_Ret, _Tp, _Arg> 415 1.1 mrg mem_fun1_ref(_Ret (_Tp::*__f)(_Arg)) 416 1.1.1.10 mrg { return std::mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 417 1.1 mrg 418 1.1 mrg template <class _Ret, class _Tp, class _Arg> 419 1.1.1.10 mrg inline std::const_mem_fun1_ref_t<_Ret, _Tp, _Arg> 420 1.1 mrg mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const) 421 1.1.1.10 mrg { return std::const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 422 1.1 mrg 423 1.1.1.12 mrg #pragma GCC diagnostic pop 424 1.1.1.12 mrg 425 1.1.1.2 mrg _GLIBCXX_END_NAMESPACE_VERSION 426 1.1.1.2 mrg } // namespace 427 1.1 mrg 428 1.1 mrg #endif 429 1.1 mrg 430