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